KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > TomcatModuleConfig


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tomcat5;
21
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import org.netbeans.modules.tomcat5.config.gen.Context;
26 import org.netbeans.modules.tomcat5.config.gen.Engine;
27 import org.netbeans.modules.tomcat5.config.gen.Host;
28 import org.netbeans.modules.tomcat5.config.gen.SContext;
29 import org.netbeans.modules.tomcat5.config.gen.Server;
30 import org.netbeans.modules.tomcat5.config.gen.Service;
31 import org.openide.ErrorManager;
32
33
34 /**
35  * <code>TomcatModuleConfig</code> offers easy access to some context.xml and
36  * server.xml settings.
37  *
38  * @author Stepan Herold
39  */

40 public class TomcatModuleConfig {
41     private static final String JavaDoc CONTEXT_XML_PATH = "/META-INF/context.xml"; // NOI18N
42

43     private File JavaDoc contextXml;
44     private File JavaDoc serverXml;
45
46     private long timestampContextXML;
47     private long timestampServerXML;
48     private String JavaDoc path;
49     
50     
51     // context logger settings
52
private boolean hasLogger;
53     private String JavaDoc loggerClassName;
54     private String JavaDoc loggerDir;
55     private String JavaDoc loggerPrefix;
56     private String JavaDoc loggerSuffix;
57     private boolean loggerTimestamp;
58     
59     /**
60      * Creates a new instance of TomcatModuleConfig.
61      *
62      * @param docBase document base class.
63      * @param path context path.
64      * @param serverXmlPath path to server.xml file.
65      */

66     public TomcatModuleConfig(String JavaDoc docBase, String JavaDoc path, String JavaDoc serverXmlPath) {
67         if (path.equals("/")) {
68             this.path = ""; // NOI18N
69
} else {
70             this.path = path;
71         }
72         contextXml = new File JavaDoc(docBase + CONTEXT_XML_PATH);
73         serverXml = new File JavaDoc(serverXmlPath);
74         refresh();
75     }
76     
77     /**
78      * Returns context from META-INF/context.xml if exists, <code>null</code> otherwise
79      * @return context from META-INF/context.xml if exists, <code>null</code> otherwise
80      */

81     private Context getContext() {
82         try {
83             timestampContextXML = contextXml.lastModified();
84             Context ctx = Context.createGraph(contextXml);
85             return ctx;
86         } catch (IOException JavaDoc ioe) {
87             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
88             return null;
89         } catch (RuntimeException JavaDoc e) {
90             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
91             return null;
92         }
93     }
94     
95     /**
96      * Returns context element from server.xml if defined, <code>null</code> otherwise
97      * @return context element from server.xml if defined, <code>null</code> otherwise
98      */

99     private SContext getSContext() {
100         try {
101             timestampServerXML = serverXml.lastModified();
102             Server server = Server.createGraph(serverXml);
103             
104             // Looks for the first appearance of the service and host element.
105
// (ide currently does not support multiple service and host elements).
106
Service[] service = server.getService();
107             if (service.length > 0) {
108                 Engine engine = service[0].getEngine();
109                 if (engine != null) {
110                     Host[] host = engine.getHost();
111                     if (host.length > 0) {
112                         SContext[] sContext = host[0].getSContext();
113                         for (int i = 0; i < sContext.length; i++) {
114                             if (sContext[i].getAttributeValue("path").equals(path)) { // NOI18N
115
return sContext[i];
116                             }
117                         }
118                     }
119                 }
120             }
121             
122         } catch (IOException JavaDoc ioe) {
123             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
124         }
125         return null;
126     }
127     
128     /**
129      * Returns <code>true</code> if there is a logger defined for this module,
130      * <code>false</code> otherwise.
131      *
132      * @return <code>true</code> if there is a logger defined for this module,
133      * <code>false</code> otherwise.
134      */

135     public boolean hasLogger() {
136         return hasLogger;
137     }
138     
139     /**
140      * Return logger class name.
141      *
142      * @return logger class name.
143      */

144     public String JavaDoc loggerClassName() {
145         return loggerClassName;
146     }
147     
148     /**
149      * Return logger directory.
150      *
151      * @return logger directory.
152      */

153     public String JavaDoc loggerDir() {
154         return loggerDir;
155     }
156     
157     /**
158      * Return logger prefix.
159      *
160      * @return logger prefix.
161      */

162     public String JavaDoc loggerPrefix() {
163         return loggerPrefix;
164     }
165     
166     /**
167      * Return logger suffix.
168      *
169      * @return logger suffix.
170      */

171     public String JavaDoc loggerSuffix() {
172         return loggerSuffix;
173     }
174
175     /**
176      * Return <code>true</code> whether logger timestamps messages, <code>false</code>
177      * otherwise.
178      *
179      * @return <code>true</code> whether logger timestamps messages, <code>false</code>
180      * otherwise.
181      */

182     public boolean loggerTimestamp() {
183         return loggerTimestamp;
184     }
185     
186     /**
187      * Refresh cached values if the context.xml or server.xml file changed.
188      */

189     public void refresh() {
190         if (contextXml.exists()) {
191             long newTimestamp = contextXml.lastModified();
192             if (newTimestamp > timestampContextXML) {
193                 timestampContextXML = newTimestamp;
194                 Context ctx = getContext();
195                 if (ctx != null) {
196                     hasLogger = ctx.isLogger();
197                     if (hasLogger) {
198                         loggerClassName = ctx.getLoggerClassName();
199                         loggerDir = ctx.getLoggerDirectory();
200                         loggerPrefix = ctx.getLoggerPrefix();
201                         loggerSuffix = ctx.getLoggerSuffix();
202                         loggerTimestamp = Boolean.valueOf(ctx.getLoggerTimestamp()).booleanValue();
203                         return;
204                     }
205                 }
206             }
207         } else if (serverXml.exists()) {
208             long newTimestamp = serverXml.lastModified();
209             if (newTimestamp > timestampServerXML) {
210                 timestampServerXML = newTimestamp;
211                 SContext sCtx = getSContext();
212                 if (sCtx != null) {
213                     hasLogger = sCtx.isLogger();
214                     if (hasLogger) {
215                         loggerClassName = sCtx.getAttributeValue(SContext.LOGGER, "className"); // NOI18N
216
loggerDir = sCtx.getAttributeValue(SContext.LOGGER, "directory"); // NOI18N
217
loggerPrefix = sCtx.getAttributeValue(SContext.LOGGER, "prefix"); // NOI18N
218
loggerSuffix = sCtx.getAttributeValue(SContext.LOGGER, "suffix"); // NOI18N
219
String JavaDoc timestamp = sCtx.getAttributeValue(SContext.LOGGER, "timestamp"); // NOI18N
220
loggerTimestamp = Boolean.valueOf(timestamp).booleanValue();
221                     }
222                 }
223             }
224         } else {
225             hasLogger = false; // this shouldn't happen
226
}
227     }
228 }
229
Popular Tags