Skip to content

Commit 5b39890

Browse files
Merge pull request #9068 from NicolaIsotta/tomcat-edit-context
Add "Edit context.xml" action to Tomcat menu
2 parents 3c74da1 + 2746d5b commit 5b39890

File tree

4 files changed

+120
-5
lines changed

4 files changed

+120
-5
lines changed

enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/TomcatInstanceNode.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openide.util.Lookup;
3434
import org.openide.util.actions.SystemAction;
3535
import org.netbeans.modules.tomcat5.customizer.Customizer;
36+
import org.netbeans.modules.tomcat5.ui.nodes.actions.EditContextXmlAction;
3637
import org.netbeans.modules.tomcat5.ui.nodes.actions.SharedContextLogAction;
3738
import org.netbeans.modules.tomcat5.ui.nodes.actions.EditServerXmlAction;
3839
import org.netbeans.modules.tomcat5.ui.nodes.actions.OpenServerOutputAction;
@@ -94,14 +95,15 @@ public TomcatManager getTomcatManager() {
9495

9596
@Override
9697
public javax.swing.Action[] getActions(boolean context) {
97-
java.util.List actions = new LinkedList();
98+
java.util.List<SystemAction> actions = new LinkedList<>();
9899
// terminate does not work on Windows, see issue #63157
99100
if (!Utilities.isWindows()) {
100101
actions.add(null);
101102
actions.add(SystemAction.get(TerminateAction.class));
102103
}
103104
actions.add(null);
104105
actions.add(SystemAction.get(EditServerXmlAction.class));
106+
actions.add(SystemAction.get(EditContextXmlAction.class));
105107
if (tm.isTomcat50() || tm.isTomcat55()) {
106108
actions.add(SystemAction.get(AdminConsoleAction.class));
107109
}
@@ -112,15 +114,15 @@ public javax.swing.Action[] getActions(boolean context) {
112114
actions.add(SystemAction.get(ServerLogAction.class));
113115
}
114116
actions.add(SystemAction.get(OpenServerOutputAction.class));
115-
return (SystemAction[])actions.toArray(new SystemAction[0]);
117+
return actions.toArray(new SystemAction[0]);
116118
}
117-
119+
118120
private FileObject getTomcatConf() {
119121
tm.ensureCatalinaBaseReady(); // generated the catalina base folder if empty
120122
TomcatProperties tp = tm.getTomcatProperties();
121123
return FileUtil.toFileObject(tp.getServerXml());
122124
}
123-
125+
124126
/**
125127
* Open server.xml file in editor.
126128
*/
@@ -144,6 +146,35 @@ public void editServerXml() {
144146
}
145147
}
146148

149+
private FileObject getTomcatContextXml() {
150+
tm.ensureCatalinaBaseReady(); // generated the catalina base folder if empty
151+
TomcatProperties tp = tm.getTomcatProperties();
152+
return FileUtil.toFileObject(tp.getContextXml());
153+
}
154+
155+
/**
156+
* Open context.xml file in editor.
157+
*/
158+
public void editContextXml() {
159+
FileObject fileObject = getTomcatContextXml();
160+
if (fileObject != null) {
161+
DataObject dataObject = null;
162+
try {
163+
dataObject = DataObject.find(fileObject);
164+
} catch (DataObjectNotFoundException ex) {
165+
Logger.getLogger(TomcatInstanceNode.class.getName()).log(Level.INFO, null, ex);
166+
}
167+
if (dataObject != null) {
168+
EditorCookie editorCookie = dataObject.getLookup().lookup(EditorCookie.class);
169+
if (editorCookie != null) {
170+
editorCookie.open();
171+
} else {
172+
Logger.getLogger(TomcatInstanceNode.class.getName()).log(Level.INFO, "Cannot find EditorCookie."); // NOI18N
173+
}
174+
}
175+
}
176+
}
177+
147178
/**
148179
* Open the server log (output).
149180
*/

enterprise/tomcat5/src/org/netbeans/modules/tomcat5/ui/nodes/actions/Bundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ LBL_TerminateAction=&Terminate
3131
MSG_terminate=Do you really want to terminate the running {0} process?
3232
LBL_AdminConsoleAction=View &Admin Console
3333
LBL_ServerLogAction=View Server &Log
34+
LBL_EditContextXmlAction=Edit context.xml
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.netbeans.modules.tomcat5.ui.nodes.actions;
20+
21+
import org.openide.nodes.Node;
22+
import org.openide.util.HelpCtx;
23+
import org.openide.util.NbBundle;
24+
import org.openide.util.actions.NodeAction;
25+
import org.netbeans.modules.tomcat5.ui.nodes.TomcatInstanceNode;
26+
27+
/**
28+
* Opens context.xml file in editor.
29+
*/
30+
public class EditContextXmlAction extends NodeAction {
31+
32+
@Override
33+
protected boolean enable(Node[] nodes) {
34+
return true;
35+
}
36+
37+
@Override
38+
public HelpCtx getHelpCtx() {
39+
return HelpCtx.DEFAULT_HELP;
40+
}
41+
42+
@Override
43+
public String getName() {
44+
return NbBundle.getMessage(SharedContextLogAction.class, "LBL_EditContextXmlAction"); // NOI18N
45+
}
46+
47+
@Override
48+
protected boolean asynchronous() {
49+
return false;
50+
}
51+
52+
@Override
53+
protected void performAction(Node[] nodes) {
54+
for (int i = 0; i < nodes.length; i++) {
55+
TomcatInstanceNode cookie = nodes[i].getLookup().lookup(TomcatInstanceNode.class);
56+
if (cookie != null) {
57+
cookie.editContextXml();
58+
}
59+
}
60+
}
61+
62+
}

enterprise/tomcat5/src/org/netbeans/modules/tomcat5/util/TomcatProperties.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,28 @@ public File getTomeeXml() {
852852
}
853853
return null;
854854
}
855-
855+
856+
/**
857+
* Return context.xml file from the catalina base folder if the base folder
858+
* is used or from the catalina home folder otherwise.
859+
* <p>
860+
* <b>BEWARE</b>: If the catalina base folder is used but has not bee
861+
* generated yet, the context.xml file from the catalina home folder will be
862+
* returned.
863+
* </p>
864+
*/
865+
public File getContextXml() {
866+
String confContextXml = "conf/context.xml"; // NIO18N
867+
File contextXml = null;
868+
if (baseDir != null) {
869+
contextXml = new File(baseDir, confContextXml);
870+
}
871+
if (contextXml == null || !contextXml.exists()) {
872+
contextXml = new File(getCatalinaHome(), confContextXml);
873+
}
874+
return contextXml;
875+
}
876+
856877
public String getHost () {
857878
String val = ip.getProperty(PROP_HOST);
858879
return val != null ? val : DEF_VALUE_HOST;

0 commit comments

Comments
 (0)