KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.openide.ErrorManager;
25 import org.netbeans.modules.tomcat5.config.gen.Engine;
26 import org.netbeans.modules.tomcat5.config.gen.Host;
27 import org.netbeans.modules.tomcat5.config.gen.SContext;
28 import org.netbeans.modules.tomcat5.config.gen.Server;
29 import org.netbeans.modules.tomcat5.config.gen.Service;
30
31 /**
32  * <code>TomcatManagerConfig</code> offers easy access to some server.xml settings.
33  *
34  * @author Stepan Herold
35  */

36 public class TomcatManagerConfig {
37     private File JavaDoc serverXml;
38     private long timestamp;
39
40     // shared context logger settings
41
private boolean hasLogger;
42     private String JavaDoc loggerClassName;
43     private String JavaDoc loggerDir;
44     private String JavaDoc loggerPrefix;
45     private String JavaDoc loggerSuffix;
46     private boolean loggerTimestamp;
47     
48     /**
49      * Creates a new instance of TomcatManagerConfig.
50      *
51      * @param serverXmlPath path to server.xml file.
52      */

53     public TomcatManagerConfig(File JavaDoc serverXml) {
54         this.serverXml = serverXml;
55         refresh();
56     }
57     
58     /**
59      * Refresh cached values if the server.xml file changed.
60      */

61     public void refresh() {
62         long newTimestamp = serverXml.lastModified();
63         if (newTimestamp > timestamp) {
64             timestamp = newTimestamp;
65             Host host = getHostElement();
66             if (host != null && host.isLogger()) {
67                 hasLogger = true;
68                 loggerClassName = host.getAttributeValue(SContext.LOGGER, "className"); // NOI18N
69
loggerDir = host.getAttributeValue(SContext.LOGGER, "directory"); // NOI18N
70
loggerPrefix = host.getAttributeValue(SContext.LOGGER, "prefix"); // NOI18N
71
loggerSuffix = host.getAttributeValue(SContext.LOGGER, "suffix"); // NOI18N
72
String JavaDoc timestamp = host.getAttributeValue(SContext.LOGGER, "timestamp"); // NOI18N
73
loggerTimestamp = Boolean.valueOf(timestamp).booleanValue();
74             } else {
75                 Engine engine = getEngineElement();
76                 if (engine != null && engine.isLogger()) {
77                     hasLogger = true;
78                     loggerClassName = engine.getAttributeValue(SContext.LOGGER, "className"); // NOI18N
79
loggerDir = engine.getAttributeValue(SContext.LOGGER, "directory"); // NOI18N
80
loggerPrefix = engine.getAttributeValue(SContext.LOGGER, "prefix"); // NOI18N
81
loggerSuffix = engine.getAttributeValue(SContext.LOGGER, "suffix"); // NOI18N
82
String JavaDoc timestamp = engine.getAttributeValue(SContext.LOGGER, "timestamp"); // NOI18N
83
loggerTimestamp = Boolean.valueOf(timestamp).booleanValue();
84                 } else {
85                     hasLogger = false;
86                 }
87             }
88         }
89     }
90     
91     /**
92      * Return path to server.xml file.
93      *
94      * @return path to server.xml file.
95      */

96     public String JavaDoc serverXmlPath() {
97         return serverXml.getAbsolutePath();
98     }
99     
100     /**
101      * Return bean representation of the Server element of the server.xml file.
102      *
103      * @return bean representation of the Server element of the server.xml file,
104      * <code>null</code> if error occurs.
105      */

106     public Server getServerElement() {
107         try {
108             return Server.createGraph(serverXml);
109         } catch (IOException JavaDoc ioe) {
110             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ioe);
111         } catch (RuntimeException JavaDoc e) {
112             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
113         }
114         return null;
115     }
116     
117     /**
118      * Return engine element from server.xml if defined.
119      * Looks only for the first appearance of the service element.
120      * (ide currently does not support multiple service and host elements).
121      */

122     public Engine getEngineElement() {
123         Server server = getServerElement();
124         if (server == null) return null;
125         Service[] service = server.getService();
126         if (service.length > 0) {
127             return service[0].getEngine();
128         }
129         return null;
130     }
131     
132     /**
133      * Return host element from server.xml if defined.
134      * Looks only for the first appearance of the service and host element.
135      * (ide currently does not support multiple service and host elements).
136      */

137     public Host getHostElement() {
138         Engine engine = getEngineElement();
139         if (engine != null) {
140             Host[] host = engine.getHost();
141             if (host.length > 0) {
142                 return host[0];
143             }
144         }
145         return null;
146     }
147     
148     /**
149      * Return <code>true</code> if there is a logger defined in the first host
150      * or engine element in server.xml, <code>false</code> otherwise.
151      *
152      * @return <code>true</code> if there is a logger defined in the first host
153      * or engine element in server.xml, <code>false</code> otherwise.
154      */

155     public boolean hasLogger() {
156         return hasLogger;
157     }
158     
159     /**
160      * Return logger class name.
161      *
162      * @return logger class name.
163      */

164     public String JavaDoc loggerClassName() {
165         return loggerClassName;
166     }
167     
168     /**
169      * Return logger directory.
170      *
171      * @return logger directory.
172      */

173     public String JavaDoc loggerDir() {
174         return loggerDir;
175     }
176
177     /**
178      * Return logger prefix.
179      *
180      * @return logger prefix.
181      */

182     public String JavaDoc loggerPrefix() {
183         return loggerPrefix;
184     }
185     
186     /**
187      * Return logger suffix.
188      *
189      * @return logger suffix.
190      */

191     public String JavaDoc loggerSuffix() {
192         return loggerSuffix;
193     }
194
195     /**
196      * Return <code>true</code> whether logger timestamps messages, <code>false</code>
197      * otherwise.
198      *
199      * @return <code>true</code> whether logger timestamps messages, <code>false</code>
200      * otherwise.
201      */

202     public boolean loggerTimestamp() {
203         return loggerTimestamp;
204     }
205 }
206
Popular Tags