001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import org.netbeans.modules.tomcat5.config.gen.Context;
049: import org.netbeans.modules.tomcat5.config.gen.Engine;
050: import org.netbeans.modules.tomcat5.config.gen.Host;
051: import org.netbeans.modules.tomcat5.config.gen.SContext;
052: import org.netbeans.modules.tomcat5.config.gen.Server;
053: import org.netbeans.modules.tomcat5.config.gen.Service;
054:
055: /**
056: * <code>TomcatModuleConfig</code> offers easy access to some context.xml and
057: * server.xml settings.
058: *
059: * @author Stepan Herold
060: */
061: public class TomcatModuleConfig {
062: private static final String CONTEXT_XML_PATH = "/META-INF/context.xml"; // NOI18N
063:
064: private File contextXml;
065: private File serverXml;
066:
067: private long timestampContextXML;
068: private long timestampServerXML;
069: private String path;
070:
071: // context logger settings
072: private boolean hasLogger;
073: private String loggerClassName;
074: private String loggerDir;
075: private String loggerPrefix;
076: private String loggerSuffix;
077: private boolean loggerTimestamp;
078:
079: /**
080: * Creates a new instance of TomcatModuleConfig.
081: *
082: * @param docBase document base class.
083: * @param path context path.
084: * @param serverXmlPath path to server.xml file.
085: */
086: public TomcatModuleConfig(String docBase, String path,
087: String serverXmlPath) {
088: if (path.equals("/")) {
089: this .path = ""; // NOI18N
090: } else {
091: this .path = path;
092: }
093: contextXml = new File(docBase + CONTEXT_XML_PATH);
094: serverXml = new File(serverXmlPath);
095: refresh();
096: }
097:
098: /**
099: * Returns context from META-INF/context.xml if exists, <code>null</code> otherwise
100: * @return context from META-INF/context.xml if exists, <code>null</code> otherwise
101: */
102: private Context getContext() {
103: try {
104: timestampContextXML = contextXml.lastModified();
105: Context ctx = Context.createGraph(contextXml);
106: return ctx;
107: } catch (IOException ioe) {
108: Logger.getLogger(TomcatModuleConfig.class.getName()).log(
109: Level.INFO, null, ioe);
110: return null;
111: } catch (RuntimeException e) {
112: Logger.getLogger(TomcatModuleConfig.class.getName()).log(
113: Level.INFO, null, e);
114: return null;
115: }
116: }
117:
118: /**
119: * Returns context element from server.xml if defined, <code>null</code> otherwise
120: * @return context element from server.xml if defined, <code>null</code> otherwise
121: */
122: private SContext getSContext() {
123: try {
124: timestampServerXML = serverXml.lastModified();
125: Server server = Server.createGraph(serverXml);
126:
127: // Looks for the first appearance of the service and host element.
128: // (ide currently does not support multiple service and host elements).
129: Service[] service = server.getService();
130: if (service.length > 0) {
131: Engine engine = service[0].getEngine();
132: if (engine != null) {
133: Host[] host = engine.getHost();
134: if (host.length > 0) {
135: SContext[] sContext = host[0].getSContext();
136: for (int i = 0; i < sContext.length; i++) {
137: if (sContext[i].getAttributeValue("path")
138: .equals(path)) { // NOI18N
139: return sContext[i];
140: }
141: }
142: }
143: }
144: }
145:
146: } catch (IOException ioe) {
147: Logger.getLogger(TomcatModuleConfig.class.getName()).log(
148: Level.INFO, null, ioe);
149: }
150: return null;
151: }
152:
153: /**
154: * Returns <code>true</code> if there is a logger defined for this module,
155: * <code>false</code> otherwise.
156: *
157: * @return <code>true</code> if there is a logger defined for this module,
158: * <code>false</code> otherwise.
159: */
160: public boolean hasLogger() {
161: return hasLogger;
162: }
163:
164: /**
165: * Return logger class name.
166: *
167: * @return logger class name.
168: */
169: public String loggerClassName() {
170: return loggerClassName;
171: }
172:
173: /**
174: * Return logger directory.
175: *
176: * @return logger directory.
177: */
178: public String loggerDir() {
179: return loggerDir;
180: }
181:
182: /**
183: * Return logger prefix.
184: *
185: * @return logger prefix.
186: */
187: public String loggerPrefix() {
188: return loggerPrefix;
189: }
190:
191: /**
192: * Return logger suffix.
193: *
194: * @return logger suffix.
195: */
196: public String loggerSuffix() {
197: return loggerSuffix;
198: }
199:
200: /**
201: * Return <code>true</code> whether logger timestamps messages, <code>false</code>
202: * otherwise.
203: *
204: * @return <code>true</code> whether logger timestamps messages, <code>false</code>
205: * otherwise.
206: */
207: public boolean loggerTimestamp() {
208: return loggerTimestamp;
209: }
210:
211: /**
212: * Refresh cached values if the context.xml or server.xml file changed.
213: */
214: public void refresh() {
215: if (contextXml.exists()) {
216: long newTimestamp = contextXml.lastModified();
217: if (newTimestamp > timestampContextXML) {
218: timestampContextXML = newTimestamp;
219: Context ctx = getContext();
220: if (ctx != null) {
221: hasLogger = ctx.isLogger();
222: if (hasLogger) {
223: loggerClassName = ctx.getLoggerClassName();
224: loggerDir = ctx.getLoggerDirectory();
225: loggerPrefix = ctx.getLoggerPrefix();
226: loggerSuffix = ctx.getLoggerSuffix();
227: loggerTimestamp = Boolean.valueOf(
228: ctx.getLoggerTimestamp())
229: .booleanValue();
230: return;
231: }
232: }
233: }
234: } else if (serverXml.exists()) {
235: long newTimestamp = serverXml.lastModified();
236: if (newTimestamp > timestampServerXML) {
237: timestampServerXML = newTimestamp;
238: SContext sCtx = getSContext();
239: if (sCtx != null) {
240: hasLogger = sCtx.isLogger();
241: if (hasLogger) {
242: loggerClassName = sCtx.getAttributeValue(
243: SContext.LOGGER, "className"); // NOI18N
244: loggerDir = sCtx.getAttributeValue(
245: SContext.LOGGER, "directory"); // NOI18N
246: loggerPrefix = sCtx.getAttributeValue(
247: SContext.LOGGER, "prefix"); // NOI18N
248: loggerSuffix = sCtx.getAttributeValue(
249: SContext.LOGGER, "suffix"); // NOI18N
250: String timestamp = sCtx.getAttributeValue(
251: SContext.LOGGER, "timestamp"); // NOI18N
252: loggerTimestamp = Boolean.valueOf(timestamp)
253: .booleanValue();
254: }
255: }
256: }
257: } else {
258: hasLogger = false; // this shouldn't happen
259: }
260: }
261: }
|