From 2746d5b4b9d55f5413864c01a194f5473974c22e Mon Sep 17 00:00:00 2001 From: Nicola Isotta Date: Fri, 5 Dec 2025 17:27:39 +0100 Subject: [PATCH] Add "Edit context.xml" action to Tomcat menu --- .../tomcat5/ui/nodes/TomcatInstanceNode.java | 39 ++++++++++-- .../ui/nodes/actions/Bundle.properties | 1 + .../nodes/actions/EditContextXmlAction.java | 62 +++++++++++++++++++ .../tomcat5/util/TomcatProperties.java | 23 ++++++- 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/EditContextXmlAction.java diff --git a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/TomcatInstanceNode.java b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/TomcatInstanceNode.java index 263b3be6f80f..1cc54212967f 100644 --- a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/TomcatInstanceNode.java +++ b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/TomcatInstanceNode.java @@ -33,6 +33,7 @@ import org.openide.util.Lookup; import org.openide.util.actions.SystemAction; import org.netbeans.modules.tomcat5.customizer.Customizer; +import org.netbeans.modules.tomcat5.ui.nodes.actions.EditContextXmlAction; import org.netbeans.modules.tomcat5.ui.nodes.actions.SharedContextLogAction; import org.netbeans.modules.tomcat5.ui.nodes.actions.EditServerXmlAction; import org.netbeans.modules.tomcat5.ui.nodes.actions.OpenServerOutputAction; @@ -94,7 +95,7 @@ public TomcatManager getTomcatManager() { @Override public javax.swing.Action[] getActions(boolean context) { - java.util.List actions = new LinkedList(); + java.util.List actions = new LinkedList<>(); // terminate does not work on Windows, see issue #63157 if (!Utilities.isWindows()) { actions.add(null); @@ -102,6 +103,7 @@ public javax.swing.Action[] getActions(boolean context) { } actions.add(null); actions.add(SystemAction.get(EditServerXmlAction.class)); + actions.add(SystemAction.get(EditContextXmlAction.class)); if (tm.isTomcat50() || tm.isTomcat55()) { actions.add(SystemAction.get(AdminConsoleAction.class)); } @@ -112,15 +114,15 @@ public javax.swing.Action[] getActions(boolean context) { actions.add(SystemAction.get(ServerLogAction.class)); } actions.add(SystemAction.get(OpenServerOutputAction.class)); - return (SystemAction[])actions.toArray(new SystemAction[0]); + return actions.toArray(new SystemAction[0]); } - + private FileObject getTomcatConf() { tm.ensureCatalinaBaseReady(); // generated the catalina base folder if empty TomcatProperties tp = tm.getTomcatProperties(); return FileUtil.toFileObject(tp.getServerXml()); } - + /** * Open server.xml file in editor. */ @@ -144,6 +146,35 @@ public void editServerXml() { } } + private FileObject getTomcatContextXml() { + tm.ensureCatalinaBaseReady(); // generated the catalina base folder if empty + TomcatProperties tp = tm.getTomcatProperties(); + return FileUtil.toFileObject(tp.getContextXml()); + } + + /** + * Open context.xml file in editor. + */ + public void editContextXml() { + FileObject fileObject = getTomcatContextXml(); + if (fileObject != null) { + DataObject dataObject = null; + try { + dataObject = DataObject.find(fileObject); + } catch (DataObjectNotFoundException ex) { + Logger.getLogger(TomcatInstanceNode.class.getName()).log(Level.INFO, null, ex); + } + if (dataObject != null) { + EditorCookie editorCookie = dataObject.getLookup().lookup(EditorCookie.class); + if (editorCookie != null) { + editorCookie.open(); + } else { + Logger.getLogger(TomcatInstanceNode.class.getName()).log(Level.INFO, "Cannot find EditorCookie."); // NOI18N + } + } + } + } + /** * Open the server log (output). */ diff --git a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/Bundle.properties b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/Bundle.properties index c31edf094164..30a10834d262 100644 --- a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/Bundle.properties +++ b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/Bundle.properties @@ -31,3 +31,4 @@ LBL_TerminateAction=&Terminate MSG_terminate=Do you really want to terminate the running {0} process? LBL_AdminConsoleAction=View &Admin Console LBL_ServerLogAction=View Server &Log +LBL_EditContextXmlAction=Edit context.xml diff --git a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/EditContextXmlAction.java b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/EditContextXmlAction.java new file mode 100644 index 000000000000..4d6bc6d9ff42 --- /dev/null +++ b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/EditContextXmlAction.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.tomcat5.ui.nodes.actions; + +import org.openide.nodes.Node; +import org.openide.util.HelpCtx; +import org.openide.util.NbBundle; +import org.openide.util.actions.NodeAction; +import org.netbeans.modules.tomcat5.ui.nodes.TomcatInstanceNode; + +/** + * Opens context.xml file in editor. + */ +public class EditContextXmlAction extends NodeAction { + + @Override + protected boolean enable(Node[] nodes) { + return true; + } + + @Override + public HelpCtx getHelpCtx() { + return HelpCtx.DEFAULT_HELP; + } + + @Override + public String getName() { + return NbBundle.getMessage(SharedContextLogAction.class, "LBL_EditContextXmlAction"); // NOI18N + } + + @Override + protected boolean asynchronous() { + return false; + } + + @Override + protected void performAction(Node[] nodes) { + for (int i = 0; i < nodes.length; i++) { + TomcatInstanceNode cookie = nodes[i].getLookup().lookup(TomcatInstanceNode.class); + if (cookie != null) { + cookie.editContextXml(); + } + } + } + +} diff --git a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/util/TomcatProperties.java b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/util/TomcatProperties.java index d7bc82617110..7c47b14fd520 100644 --- a/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/util/TomcatProperties.java +++ b/enterprise/tomcat5/src/org/netbeans/modules/tomcat5/util/TomcatProperties.java @@ -852,7 +852,28 @@ public File getTomeeXml() { } return null; } - + + /** + * Return context.xml file from the catalina base folder if the base folder + * is used or from the catalina home folder otherwise. + *

+ * BEWARE: If the catalina base folder is used but has not bee + * generated yet, the context.xml file from the catalina home folder will be + * returned. + *

+ */ + public File getContextXml() { + String confContextXml = "conf/context.xml"; // NIO18N + File contextXml = null; + if (baseDir != null) { + contextXml = new File(baseDir, confContextXml); + } + if (contextXml == null || !contextXml.exists()) { + contextXml = new File(getCatalinaHome(), confContextXml); + } + return contextXml; + } + public String getHost () { String val = ip.getProperty(PROP_HOST); return val != null ? val : DEF_VALUE_HOST;