KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ServerContextImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31
32 package com.sun.enterprise.server;
33
34 import java.io.File JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36
37 import com.sun.enterprise.Switch;
38 import com.sun.enterprise.NamingManager;
39 import com.sun.enterprise.InvocationManager;
40
41 import com.sun.enterprise.config.ConfigContext;
42 import com.sun.enterprise.config.ConfigException;
43 import com.sun.enterprise.config.serverbeans.Server;
44 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
45 import com.sun.enterprise.instance.InstanceEnvironment;
46 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory;
47 import com.sun.enterprise.util.i18n.StringManager;
48 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistry;
49
50 /**
51  * ServerContext interface: the server-wide runtime environment created by
52  * ApplicationServer and shared by its subsystems such as the web container
53  * or EJB container.
54  */

55 public class ServerContextImpl implements ServerContext {
56
57     /** XXX: should move these to Config API */
58
59     public final String JavaDoc SERVER_XML = "domain.xml";
60
61     public final String JavaDoc DEFAULT_DOMAIN_NAME = "com.sun.appserv";
62
63     public final String JavaDoc CONFIG_DIR = "config";
64
65     /** server command line arguments */
66     private String JavaDoc[] cmdLineArgs;
67
68     /** Pluggable features factory */
69     private PluggableFeatureFactory featureFactory;
70
71     /** install root of this server instance */
72     private String JavaDoc installRoot;
73
74     /** name of this server instance */
75     private String JavaDoc instanceName;
76
77     /** local string manager for i18n */
78     private static StringManager localStrings =
79                             StringManager.getManager("com.sun.enterprise.server");
80
81     /** common class loader i.e., $instance/lib classloader */
82     private static ClassLoader JavaDoc commonClassLoader;
83
84     /** shared class loader i.e., connector class loader */
85     private static ClassLoader JavaDoc sharedClassLoader;
86
87     /** parent class loader for the life cycle modules */
88     private ClassLoader JavaDoc lifeCycleClassLoader;
89
90     /** server config context */
91     private ConfigContext configContext;
92
93     /** server config bean */
94     private Server server;
95
96     // path to the server-wide configuration
97
private String JavaDoc serverConfigPath = null;
98
99     // server xml file URL
100
private String JavaDoc serverConfigURL = null;
101
102     /** environment object for this server instance */
103     private InstanceEnvironment instanceEnvironment = null;
104
105     /**
106      * public constructor
107      */

108     public ServerContextImpl() {
109       installRoot = System.getProperty(Constants.INSTALL_ROOT);
110       instanceName = System.getProperty("com.sun.aas.instanceName");
111       getInstanceEnvironment();
112     }
113
114     /**
115      * Set the server command-line arguments
116      *
117      * @param cmdLineArgs array of command-line arguments
118      */

119     protected void setCmdLineArgs(String JavaDoc[] cmdLineArgs) {
120         this.cmdLineArgs = cmdLineArgs;
121     }
122
123     /**
124      * Get the server command-line arguments
125      *
126      * @return the server command-line arguments
127      */

128     public String JavaDoc[] getCmdLineArgs() {
129         return cmdLineArgs;
130     }
131
132     /**
133      * Set pluggable features factory. Initialization of server context
134      * must be followed by a call to this method. An easy way to obtain
135      * a PluggableFeatureFactory is to call the static method getInstance()
136      * on com.sun.enterprise.server.pluggable PluggableFeatureFactoryImpl
137      * class.
138      */

139     public void setPluggableFeatureFactory(PluggableFeatureFactory obj) {
140         featureFactory = obj;
141     }
142
143     /**
144      * Get a factory for obtaining implementation of pluggable features.
145      */

146     public PluggableFeatureFactory getPluggableFeatureFactory() {
147         return featureFactory;
148     }
149
150     /**
151      * Install root of the server
152      *
153      * @param installRoot server installation root
154      */

155     protected void setInstallRoot(String JavaDoc installRoot) {
156         this.installRoot = installRoot;
157     }
158
159     /**
160      * Get server installation root
161      *
162      * @return the server installation root
163      */

164     public String JavaDoc getInstallRoot() {
165         return this.installRoot;
166     }
167
168     /**
169      * Instace name
170      *
171      * @param instanceName name of this application server instance
172      */

173     protected void setInstanceName(String JavaDoc instanceName) {
174         this.instanceName = instanceName;
175     }
176
177     /**
178      * Get the server instance name
179      *
180      * @return the server instance name
181      */

182     public String JavaDoc getInstanceName() {
183         return instanceName;
184     }
185
186     /**
187      * Get the path to the server configuration directory
188      */

189     public String JavaDoc getServerConfigPath() {
190         return instanceEnvironment.getConfigDirPath();
191     }
192
193     /**
194      * Get a URL representation of any configuration file in server config path
195      */

196     public String JavaDoc getConfigURL(String JavaDoc configName) {
197         String JavaDoc configURI = instanceEnvironment.getConfigFilePath();
198
199         // XXX: Config API Doesn't expect URLs
200
return configURI;
201         // if (isWindows())
202
// return "file:/" + configURI;
203
// else
204
// return "file:" + configURI;
205
}
206
207     /**
208      * Get a URL representation of server configuration
209      *
210      * @return the URL to the server configuration
211      */

212     public String JavaDoc getServerConfigURL() {
213             return instanceEnvironment.getConfigFilePath();
214     }
215
216     /**
217      * Get the initial naming context.
218      */

219     public InitialContext JavaDoc getInitialContext() {
220         // XXX: cleanup static instance variables
221
NamingManager mgr = Switch.getSwitch().getNamingManager();
222         if (mgr == null) {
223             return null;
224         }
225         return (InitialContext JavaDoc)(mgr.getInitialContext());
226     }
227
228     /**
229      * Are we running on Windows platform?
230      */

231     private boolean isWindows() {
232         return (File.separatorChar == '\\');
233     }
234
235     /**
236      * Get the server configuration bean.
237      *
238      * @return the server config bean
239      */

240     public Server getConfigBean() throws ConfigException {
241         if (server != null) {
242             return server;
243         }
244
245         // create the Server config bean
246
if (configContext != null) {
247             server = ServerBeansFactory.getServerBean(configContext);
248         } else {
249             // perhaps uninitialized?
250
String JavaDoc msg = localStrings.getString(
251                             "serverContext.config_context_is_null");
252             throw new ConfigException(msg);
253         }
254         return this.server;
255     }
256
257     /**
258      * Returns the config context for this server.
259      *
260      * @return the config context
261      */

262     public ConfigContext getConfigContext() {
263         return this.configContext;
264     }
265
266     /**
267      * Sets the given config context.
268      *
269      * This method gets called from the AdminEventMulticaster to set the
270      * config context after event processing is complete.
271      *
272      * @param config config context
273      */

274     public void setConfigContext(ConfigContext config) {
275         this.configContext = config;
276     }
277
278     /**
279      * Get the classloader that loads .jars in $instance/lib and classes
280      * in $instance/lib/classes.
281      *
282      * @return the common class loader for this server instance
283      */

284     public ClassLoader JavaDoc getCommonClassLoader() {
285         return this.commonClassLoader;
286     }
287
288     /**
289      * Set the classloader that loads .jars in $instance/lib and classes
290      * in $instance/lib/classes.
291      *
292      * Only allow classes in this package to set this value.
293      */

294     protected void setCommonClassLoader(ClassLoader JavaDoc cl) {
295         this.commonClassLoader = cl;
296     }
297
298     /**
299      * Returns the shared class loader of the server instance.
300      *
301      * @return the shared class loader
302      */

303     public ClassLoader JavaDoc getSharedClassLoader() {
304         return this.sharedClassLoader;
305     }
306
307     /**
308      * Sets the shared class loader for the instance.
309      * Only allow classes in this package to set this value.
310      *
311      * @param cl shared class loader
312      */

313     protected void setSharedClassLoader(ClassLoader JavaDoc cl) {
314         this.sharedClassLoader = cl;
315     }
316
317     /**
318      * Returns the parent class loader for the life cycle modules.
319      *
320      * @return the parent class loader for the life cycle modules
321      */

322     public ClassLoader JavaDoc getLifecycleParentClassLoader() {
323         return this.lifeCycleClassLoader;
324     }
325
326     /**
327      * Sets the parent class loader for the life cycle module
328      *
329      * @param cl the parent class loader for the life cycle module
330      */

331     protected void setLifecycleParentClassLoader(ClassLoader JavaDoc cl) {
332         this.lifeCycleClassLoader = cl;
333     }
334
335     /**
336      * Returns the environment object for this server instance.
337      *
338      * @return the environment object for this server instance
339      */

340     public InstanceEnvironment getInstanceEnvironment() {
341         if (this.instanceEnvironment == null) {
342             this.instanceEnvironment =
343                 new InstanceEnvironment(this.instanceName);
344         }
345         return this.instanceEnvironment;
346     }
347
348     /**
349      * Sets the instance environment object for this server instance.
350      *
351      * @param instanceEnv environment object for this server instance
352      */

353     void setInstanceEnvironment(InstanceEnvironment instanceEnv) {
354         this.instanceEnvironment = instanceEnv;
355     }
356
357     /**
358      * Get the J2EE Server invocation manager
359      *
360      * @return InvocationManager
361      */

362     public InvocationManager getInvocationManager() {
363         return Switch.getSwitch().getInvocationManager();
364     }
365
366     /**
367      * get the default domain name
368      *
369      * @return String default domain name
370      */

371     public String JavaDoc getDefaultDomainName() {
372         return DEFAULT_DOMAIN_NAME;
373     }
374     /**
375      * Returns the MonitoringRegistry implementation used for registration of
376      * monitoring stats.
377      * @return instance of MonitoringRegistry
378      */

379     public MonitoringRegistry getMonitoringRegistry() {
380         /* Get the class from appserv-rt.jar for now through
381          * reflection */

382         final String JavaDoc REG_IMPL_CLASS =
383         "com.sun.enterprise.admin.monitor.registry.spi.MonitoringRegistrationHelper";
384         final String JavaDoc METHOD = "getInstance";
385         try {
386             final Class JavaDoc c = Class.forName(REG_IMPL_CLASS);
387             final java.lang.reflect.Method JavaDoc m = c.getMethod(METHOD, (Class JavaDoc[]) null);
388             final MonitoringRegistry r = (MonitoringRegistry) m.invoke(c, (Object JavaDoc[]) null);
389             return r;
390         }
391         catch(Throwable JavaDoc t) {
392             throw new RuntimeException JavaDoc(t);
393         }
394     }
395 }
396
Popular Tags