KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > instance > AppsManager


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 package com.sun.enterprise.instance;
25
26 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
27
28 import com.sun.appserv.server.util.ASClassLoaderUtil;
29 import com.sun.enterprise.config.ConfigBean;
30 import com.sun.enterprise.config.ConfigContext;
31 import com.sun.enterprise.config.ConfigException;
32 import com.sun.enterprise.config.serverbeans.Applications;
33 import com.sun.enterprise.config.serverbeans.J2eeApplication;
34 import com.sun.enterprise.config.serverbeans.PropertyResolver;
35 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
36 import com.sun.enterprise.config.serverbeans.ServerTags;
37 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
38 import com.sun.enterprise.deployment.Application;
39 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
40 import com.sun.enterprise.deployment.archivist.EjbArchivist;
41 import com.sun.enterprise.deployment.archivist.WebArchivist;
42 import com.sun.enterprise.deployment.backend.DeployableObjectInfo;
43 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
44 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
45 import com.sun.enterprise.deployment.EjbBundleDescriptor;
46 import com.sun.enterprise.deployment.BundleDescriptor;
47 import com.sun.enterprise.deployment.util.ModuleContentLinker;
48 import com.sun.enterprise.deployment.util.ModuleDescriptor;
49 import com.sun.enterprise.loader.EJBClassLoader;
50 import com.sun.enterprise.loader.EJBClassPathUtils;
51 import com.sun.enterprise.loader.InstrumentableClassLoader;
52 import com.sun.enterprise.util.RelativePathResolver;
53 import com.sun.enterprise.util.SystemPropertyConstants;
54
55 import java.io.File JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.net.MalformedURLException JavaDoc;
58 import java.net.URL JavaDoc;
59 import java.util.logging.Level JavaDoc;
60 import java.util.*;
61
62 import com.sun.enterprise.util.io.FileUtils;
63 import org.xml.sax.SAXParseException JavaDoc;
64
65 /**
66  * Provides access to application information per server instance.
67  */

68 public class AppsManager extends BaseManager {
69     
70     public AppsManager(InstanceEnvironment env) throws ConfigException {
71         super(env, true);
72     }
73     
74     public AppsManager(InstanceEnvironment env, boolean useBackupServerXml)
75             throws ConfigException {
76         super(env, useBackupServerXml);
77         
78         //FIXME: HACK START
79
J2eeApplication[] jArray = ((Applications)configBean).getJ2eeApplication();
80     if(jArray!=null) {
81             for(int i=0;i<jArray.length;i++) {
82                 jArray[i].setConfigContext(configContext);
83         jArray[i].setXPath(ServerXPathHelper.getAppIdXpathExpression(jArray[i].getName()));
84             }
85     }
86     //FIXME: HACK END
87
}
88     
89     public ModuleType JavaDoc getModuleType() {
90         return ModuleType.EAR;
91     }
92     
93     public String JavaDoc getStubLocation(String JavaDoc appId) {
94
95         ApplicationEnvironment env =
96             instanceEnvironment.getApplicationEnvironment(appId);
97         return env.getAppStubPath();
98     }
99
100     public String JavaDoc getGeneratedXMLLocation(String JavaDoc name){
101         ApplicationEnvironment env = instanceEnvironment.getApplicationEnvironment(name);
102         return env.getAppGeneratedXMLPath();
103     }
104     
105     public String JavaDoc getJSPLocation(String JavaDoc appId) {
106
107         ApplicationEnvironment env =
108             instanceEnvironment.getApplicationEnvironment(appId);
109         return env.getAppJSPPath();
110     }
111     
112     /*
113     private String getDefaultVirtualServerId() throws ConfigException {
114         ConnectionGroup cg = (ConnectionGroup)
115         ConfigBean.getConfigBeanByXPath(this.configContext,
116                             ServerXPathHelper.XPATH_CONNECTION_GROUP);
117         
118         if(cg == null) {
119             throw new ConfigException("Cannot find Default Virtual Server");
120         }
121         
122         return cg.getDefaultVirtualServer();
123         
124     }
125      */

126     
127     /**
128      * Returns an array of all applications deployed with the server.
129      */

130     public J2eeApplication[] getAllApps() {
131         J2eeApplication[] apps = ((Applications)this.configBean).getJ2eeApplication();
132         if(apps == null) return new J2eeApplication[0];
133
134         ArrayList list = new ArrayList();
135         for (int i=0; i<apps.length; i++) {
136             // add the application to the list if it is referenced
137
// by this server
138
if ( isReferenced(apps[i].getName()) ) {
139                 list.add(apps[i]);
140             }
141         }
142         // returns an array of applications referenced by this server
143
J2eeApplication[] refList = new J2eeApplication[list.size()];
144         return ( (J2eeApplication[]) list.toArray(refList) );
145     }
146     
147     /**
148      * Returns a list of all applications deployed with the server.
149      */

150     public List listIds() {
151         ArrayList arr = new ArrayList();
152         J2eeApplication[] apps = ((Applications)this.configBean).getJ2eeApplication();
153         if(apps == null) return arr;
154             
155         for (int i=0;i<apps.length;i++) {
156             String JavaDoc name = apps[i].getName();
157             // adds the application to the list if
158
// it is referenced by this server
159
if ( isReferenced(name) ) {
160                 arr.add(name);
161             }
162         }
163         return arr;
164     }
165     
166     public String JavaDoc getLocation(String JavaDoc appId) throws ConfigException {
167         J2eeApplication app = (J2eeApplication)
168         ((Applications)this.configBean).getJ2eeApplicationByName(appId);
169         String JavaDoc resolvedPath = new PropertyResolver(super.configContext,
170                 getInstanceEnvironment().getName()).
171                 resolve(app.getLocation());
172         return resolvedPath;
173         
174     }
175     
176     public boolean isEnabled(String JavaDoc appId) throws ConfigException {
177         return getJ2eeApplication(appId).isEnabled();
178     }
179
180     /**
181      *Reports whether Java Web Start access is enabled for the app clients in
182      *the specified application.
183      *@param appId the module ID of the app to check
184      *@return boolean indicating whether access is permitted
185      */

186     public boolean isJavaWebStartEnabled(String JavaDoc appId) throws ConfigException {
187         J2eeApplication app = (J2eeApplication) ((Applications)this.configBean).getJ2eeApplicationByName(appId);
188         return app.isJavaWebStartEnabled();
189     }
190     
191     /**
192      * Checks whether this application is a system app
193      * ResourceType in domain.xml should start with "system-"
194      * @return true if resourceType starts with "system-"
195      */

196     public boolean isSystem(String JavaDoc appId) throws ConfigException {
197        J2eeApplication ja = getJ2eeApplication(appId);
198        String JavaDoc resourceType = ja.getObjectType();
199        if(resourceType.startsWith(SYSTEM_PREFIX))
200            return true;
201        else
202            return false;
203     }
204     
205     /**
206      * Checks whether this application is a system admin app
207          * ResourceType in domain.xml should start with "system-admin"
208      * @return true if resourceType starts with "system-admin"
209      */

210         public boolean isSystemAdmin(String JavaDoc appId) throws ConfigException {
211            J2eeApplication ja = getJ2eeApplication(appId);
212            String JavaDoc resourceType = ja.getObjectType();
213            if(resourceType.startsWith(SYSTEM_ADMIN_PREFIX))
214                return true;
215            else
216                return false;
217         }
218     
219     protected boolean isRegistered(String JavaDoc appId, ConfigBean bean) {
220         ConfigBean cb = null;
221         try {
222             cb = ((Applications)bean).getJ2eeApplicationByName(appId);
223         } catch(Exception JavaDoc cn) {
224         }
225         
226         if(cb != null) return true;
227         return false;
228     }
229     
230     /**
231      * Removes the application information from the configuration file.
232      *
233      * @param appId a unique identifier for the application
234      */

235     public void remove(String JavaDoc appId) throws ConfigException {
236         J2eeApplication backJa = (J2eeApplication)
237             ((Applications)configBean).getJ2eeApplicationByName(appId);
238         ((Applications)configBean).removeJ2eeApplication(backJa);
239     }
240     
241     /**
242      * Identifies whether an application is enabled or disabled.
243      *
244      * @param appId unique idenitifier for the application
245      * @param isEnabled flag for enabling or disabling the application
246      */

247     public void setEnable(String JavaDoc appId, boolean enabled)
248             throws ConfigException {
249         getJ2eeApplication(appId).setEnabled(enabled);
250     }
251
252     /**
253      * Set the location for an App
254      *
255      * @param appId unique idenitifier for the application
256      * @param location
257      */

258     public void setLocation(String JavaDoc appId, String JavaDoc location)
259             throws ConfigException {
260         getJ2eeApplication(appId).setLocation(location);
261     }
262     
263     
264     /**
265      * Set the optional attributes for an App
266      *
267      * @param appId unique idenitifier for the application
268      * @param optionalAttributes - pairs tag/value to set
269      */

270     public void setOptionalAttributes(String JavaDoc appId, Properties optionalAttributes)
271             throws ConfigException {
272         if(optionalAttributes!=null) {
273             J2eeApplication ja = getJ2eeApplication(appId);
274             Enumeration tags = optionalAttributes.keys();
275             while(tags.hasMoreElements())
276             {
277                 String JavaDoc tag = (String JavaDoc)tags.nextElement();
278                 String JavaDoc value = optionalAttributes.getProperty(tag);
279                 ja.setAttributeValue(tag, value);
280             }
281         }
282     }
283
284     /**
285      * This method only returns from cache. To force the creation of
286      * an application object, see getAppDescriptor method.
287      * Only parameter appID is used. The rest of the params are kept for
288      * backward compatibility purpose until we clean up the instance
289      * managers completely.
290      */

291     public Application getDescriptor(
292         String JavaDoc appID, ClassLoader JavaDoc cl, String JavaDoc loc, boolean validateXML)
293             throws ConfigException {
294         return getRegisteredDescriptor(appID);
295     }
296
297     /**
298      * This method is called by ResourcesUtil during server start up
299      * and by ApplicationLoader when the dd for this app is not registered.
300      * We need to construct the top level application and its classloader
301      * before calling getAppDescriptor(Application) to fully populate the
302      * application object.
303      */

304     public Application getAppDescriptor(String JavaDoc appID, ClassLoader JavaDoc parentClassLoader)
305             throws ConfigException {
306
307         Application application = getRegisteredDescriptor(appID);
308         if (application != null) {
309             return application;
310         }
311
312         try {
313             // partially load the deployment descriptor...
314
ApplicationArchivist archivist = new ApplicationArchivist();
315             FileArchive appArchive = new FileArchive();
316             appArchive.open(getLocation(appID));
317
318             //for upgrade senario, we still load from the original
319
//application repository for application.xml first
320
if (!archivist.hasStandardDeploymentDescriptor(appArchive)) {
321                 //read from generated/xml location
322
appArchive.open(getGeneratedXMLLocation(appID));
323             }
324
325             application = Application.createApplication(appArchive,false);
326             application.setRegistrationName(appID);
327             String JavaDoc moduleRoot = getLocation(application.getRegistrationName());
328             String JavaDoc[] classPaths = (String JavaDoc[]) EJBClassPathUtils.getAppClasspath(
329                                                             application, this).toArray(new String JavaDoc[0]);
330             if (_logger.isLoggable(Level.FINE)) {
331                 _logger.log(Level.FINE, "[AppsManager] :: appID " + appID + " classpaths "
332                                                 + classPaths + " moduleRoot " + moduleRoot
333                                                 + " parentClassLoader " + parentClassLoader);
334             }
335             ClassLoader JavaDoc cl = EJBClassPathUtils.createEJBClassLoader(classPaths,
336                                                             moduleRoot , appID, parentClassLoader,
337                                                             application.getModuleType());
338             application.setClassLoader(cl);
339         } catch (Exception JavaDoc confEx) {
340             _logger.log(Level.SEVERE,"loader.error_while_loading_app_desc", confEx);
341             throw new ConfigException(confEx);
342         }
343
344         return getAppDescriptor(application);
345     }
346     
347     /**
348      * Returns the deployment descriptor object for the application DD.
349      * Given the top level application object, this method populates
350      * it with all the submodule information by loading from disc.
351      *
352      * @param application the top level application
353      *
354      * @return the deployment descriptor object for the given application id
355      *
356      * @throws ConfigException if unable to load the deployment descriptor
357      */

358     public Application getAppDescriptor(Application application)
359             throws ConfigException {
360
361         if (application == null) {
362             throw new ConfigException("Application object should not be null");
363         }
364
365         ClassLoader JavaDoc cl = application.getClassLoader();
366
367         // We need to use a temp CL until we are done with validate().
368
// See https://glassfish.dev.java.net/issues/show_bug.cgi?id=223
369
// for details.
370
if (cl instanceof InstrumentableClassLoader) {
371             ClassLoader JavaDoc tcl = InstrumentableClassLoader.class.cast(cl).copy();
372             application.setClassLoader(tcl);
373             // set it in all the bundles as well,
374
for (BundleDescriptor bd : (Collection<BundleDescriptor>)
375                     application.getBundleDescriptors()) {
376                 bd.setClassLoader(tcl);
377             }
378         }
379         String JavaDoc appId = application.getRegistrationName();
380         
381         // we need to load this puppy, save it in the cache...
382
try {
383             String JavaDoc appDir = getLocation(appId);
384             FileArchive in = new FileArchive();
385             // if is system predeployed app, load from original app dir
386
// else load from generated/xml dir
387
// print a warning if generated/xml dir is not there
388
// and load from original dir (upgrade scenario)
389
if (isSystemAdmin(appId)) {
390                 in.open(appDir);
391             } else {
392                 String JavaDoc xmlDir = getGeneratedXMLLocation(appId);
393                 if (FileUtils.safeIsDirectory(xmlDir)) {
394                     in.open(xmlDir);
395                 } else {
396                     // log a warning message in the server log
397
_logger.log(Level.WARNING, "core.no_xmldir",
398                         new Object JavaDoc[]{xmlDir, appDir});
399                     in.open(appDir);
400                 }
401             }
402
403             ApplicationArchivist archivist = new ApplicationArchivist();
404             archivist.readModulesDescriptors(application, in);
405             archivist.readRuntimeDeploymentDescriptor(in, application);
406             if(!isSystemAdmin(appId) && !isSystem(appId)) {
407                 // we need to read persistence descriptors separately
408
// because they are read from appDir as oppsed to xmlDir.
409
readPersistenceDeploymentDescriptors(appDir, application);
410             }
411             archivist.setDescriptor(application);
412
413             // use temp CL that is set in the application. For details,
414
// see https://glassfish.dev.java.net/issues/show_bug.cgi?id=223
415
archivist.validate(application.getClassLoader());
416             
417             application.setGeneratedXMLDirectory(getGeneratedXMLLocation(appId));
418             
419             if (!application.getWebServiceDescriptors().isEmpty()) {
420                 ModuleContentLinker visitor = new ModuleContentLinker(in);
421                 application.visit((com.sun.enterprise.deployment.util.ApplicationVisitor) visitor);
422             }
423
424             // Now that validate() is called, we can set the actual CL.
425
// See https://glassfish.dev.java.net/issues/show_bug.cgi?id=223
426
// for details.
427
application.setClassLoader(cl);
428             // set it in all the bundles as well,
429
for (BundleDescriptor bd : (Collection<BundleDescriptor>)
430                     application.getBundleDescriptors()) {
431                 bd.setClassLoader(cl);
432             }
433             registerDescriptor(appId, application);
434
435             return application;
436         } catch (ConfigException ce) {
437             throw ce;
438         } catch (Throwable JavaDoc t) {
439             throw new ConfigException(
440                 Localizer.getValue(ExceptionType.FAIL_DD_LOAD, appId), t);
441         }
442     }
443
444     public String JavaDoc getDescription(String JavaDoc id) throws ConfigException {
445         return getJ2eeApplication(id).getDescription();
446     }
447     
448     private J2eeApplication getJ2eeApplication(String JavaDoc appId)
449             throws ConfigException {
450
451         J2eeApplication app = (J2eeApplication)
452             ((Applications)this.configBean).getJ2eeApplicationByName(appId);
453
454         if(app == null)
455             throw new ConfigException(
456                 Localizer.getValue(ExceptionType.APP_NOT_EXIST));
457         return app;
458         
459     }
460     
461     public void setDescription(String JavaDoc id, String JavaDoc desc) throws ConfigException {
462         getJ2eeApplication(id).setDescription(desc);
463     }
464     
465     public String JavaDoc getVirtualServersByAppName(String JavaDoc appName) throws ConfigException {
466             return ServerBeansFactory.getVirtualServersByAppName(configContext, appName);
467     }
468     
469     /**
470      * @return the registered descriptors map
471      */

472     public Map getRegisteredDescriptors() {
473                 
474         if (apps==null) {
475             synchronized (AppsManager.class) {
476                 if (apps==null) {
477                     apps = new HashMap();
478                 }
479             }
480         }
481         return apps;
482     }
483     
484     private static Map apps=null;
485 }
486
Popular Tags