KickJava   Java API By Example, From Geeks To Geeks.

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


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.enterprise.config.ConfigBean;
29 import com.sun.enterprise.config.ConfigContext;
30 import com.sun.enterprise.config.ConfigException;
31 import com.sun.enterprise.config.serverbeans.*;
32 import com.sun.enterprise.deployment.Application;
33 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
34 import com.sun.enterprise.deployment.archivist.EjbArchivist;
35 import com.sun.enterprise.deployment.backend.DeployableObjectInfo;
36 import com.sun.enterprise.deployment.backend.DeployableObjectType;
37 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
38 import com.sun.enterprise.deployment.Descriptor;
39 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
40 import com.sun.enterprise.deployment.BundleDescriptor;
41 import com.sun.enterprise.deployment.util.ModuleContentLinker;
42 import com.sun.enterprise.deployment.util.ModuleDescriptor;
43 import com.sun.enterprise.loader.EJBClassLoader;
44 import com.sun.enterprise.loader.EJBClassPathUtils;
45 import com.sun.enterprise.loader.InstrumentableClassLoader;
46
47 import java.io.File JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.net.MalformedURLException JavaDoc;
50 import java.net.URL JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.*;
53
54 //The RelativePathResolver is used to translate relative paths containing
55
//embedded system properties (e.g. ${com.sun.aas.instanceRoot}/applications)
56
//into absolute paths
57
import com.sun.enterprise.util.RelativePathResolver;
58 import com.sun.enterprise.util.SystemPropertyConstants;
59
60 import com.sun.enterprise.util.io.FileUtils;
61
62 public class EjbModulesManager extends ModulesManager{
63
64     public EjbModulesManager(InstanceEnvironment env) throws ConfigException {
65         super(env, true);
66     }
67
68     public EjbModulesManager(InstanceEnvironment env,
69             boolean useBackupServerXml) throws ConfigException {
70         super(env, useBackupServerXml);
71         //FIXME: HACK START
72
EjbModule[] jArray = ((Applications)configBean).getEjbModule();
73     if(jArray!=null) {
74             for(int i=0;i<jArray.length;i++) {
75                 jArray[i].setConfigContext(configContext);
76         jArray[i].setXPath(ServerXPathHelper.getEjbModuleIdXpathExpression(jArray[i].getName()));
77             }
78     }
79     //FIXME: HACK END
80
}
81     
82     /**
83      * @return the module type this class is managing
84      */

85     public ModuleType JavaDoc getModuleType() {
86         return ModuleType.EJB;
87     }
88
89     /**
90      * Returns an array of all ejb modules deployed with the server.
91      */

92     public EjbModule[] listEjbModules() {
93         EjbModule[] mods = ((Applications)this.configBean).getEjbModule();
94         if(mods == null) return new EjbModule[0];
95
96         ArrayList list = new ArrayList();
97         for (int i=0; i<mods.length; i++) {
98             // add the modules to the list if it is referenced
99
// by this server
100
if ( isReferenced(mods[i].getName()) ) {
101                 list.add(mods[i]);
102             }
103         }
104         // returns an array of modules referenced by this server
105
EjbModule[] refList = new EjbModule[list.size()];
106         return ( (EjbModule[]) list.toArray(refList) );
107     }
108
109     /**
110      * Returns a list of all ejb modules deployed with the server.
111      */

112     public List listIds() {
113         ArrayList arr = new ArrayList();
114         EjbModule[] mods = ((Applications)this.configBean).getEjbModule();
115         if(mods == null) return arr;
116
117         for (int i=0;i<mods.length;i++) {
118             String JavaDoc name = mods[i].getName();
119             // adds the web module to the list if
120
// it is referenced by this server
121
if ( isReferenced(name) ) {
122                 arr.add(name);
123             }
124         }
125         return arr;
126     }
127     
128     private EjbModule getEjbModule(String JavaDoc modId) throws ConfigException {
129         EjbModule mod = (EjbModule)
130             ((Applications)this.configBean).getEjbModuleByName(modId);
131         
132         if(mod == null)
133             throw new ConfigException(Localizer.getValue(ExceptionType.NO_SUCH_EJB_MOD));
134         
135         return mod;
136         
137     }
138     
139     public void remove(String JavaDoc modID) throws ConfigException {
140         removeEjbModule(modID);
141     }
142     
143      private void removeEjbModule(String JavaDoc modId) throws ConfigException {
144         EjbModule backEm = (EjbModule)
145             ((Applications)configBean).getEjbModuleByName(modId);
146         ((Applications)configBean).removeEjbModule(backEm);
147     }
148     
149     protected boolean isRegistered(String JavaDoc appId, ConfigBean bean) {
150         ConfigBean cb = null;
151         try {
152             cb = ((Applications)bean).getEjbModuleByName(appId);
153         } catch(Exception JavaDoc cn) {
154         }
155         
156         if(cb != null) return true;
157         return false;
158     }
159     
160     public boolean isShared(String JavaDoc modId) throws ConfigException{
161         //FIXME NYI
162
return false; //getEjbModule(modId).isShared();
163
}
164     
165     public boolean isEnabled(String JavaDoc modId) throws ConfigException{
166         return getEjbModule(modId).isEnabled();
167     }
168
169     /**
170      * Checks whether this module is a systemmodule
171      * ResourceType in domain.xml should start with "system-"
172      * @return true if resourceType starts with "system-"
173      */

174     public boolean isSystem(String JavaDoc modId) throws ConfigException{
175         EjbModule em = getEjbModule(modId);
176         String JavaDoc resourceType = em.getObjectType();
177         if(resourceType.startsWith(SYSTEM_PREFIX))
178             return true;
179         else
180             return false;
181     }
182
183     /**
184      * Checks whether this module is a system admin module
185      * ResourceType in domain.xml should start with "system-admin"
186      * @return true if resourceType starts with "system-admin"
187      */

188     public boolean isSystemAdmin(String JavaDoc modId) throws ConfigException{
189         EjbModule em = getEjbModule(modId);
190         String JavaDoc resourceType = em.getObjectType();
191         if(resourceType.startsWith(SYSTEM_ADMIN_PREFIX))
192             return true;
193         else
194             return false;
195     }
196     
197     public void setShared(String JavaDoc modId, boolean shared) throws ConfigException{
198          //FIXME NYI
199
//getEjbModule(modId).setShared(shared);
200
}
201     
202     public void setEnable(String JavaDoc modId, boolean enable) throws ConfigException{
203         getEjbModule(modId).setEnabled(enable);
204     }
205     
206     /**
207      * Set the optional attributes for an module
208      *
209      * @param modId unique idenitifier for the module
210      * @param optionalAttributes - pairs tag/value to set
211      */

212     public void setOptionalAttributes(String JavaDoc modId, Properties optionalAttributes)
213             throws ConfigException {
214         if(optionalAttributes!=null) {
215             EjbModule em = getEjbModule(modId);
216             Enumeration tags = optionalAttributes.keys();
217             while(tags.hasMoreElements())
218             {
219                 String JavaDoc tag = (String JavaDoc)tags.nextElement();
220                 String JavaDoc value = optionalAttributes.getProperty(tag);
221                 em.setAttributeValue(tag, value);
222             }
223         }
224     }
225     
226     public String JavaDoc getLocation(String JavaDoc name) throws ConfigException {
227         EjbModule ejbModule = (EjbModule)
228             ((Applications)this.configBean).getEjbModuleByName(name);
229         String JavaDoc location = ejbModule.getLocation();
230         return resolvePath(location);
231     }
232
233     /**
234      * Set the location for an EJB Module
235      *
236      * @param modId unique idenitifier for the ejb module
237      * @param location full path String
238      */

239     
240     public void setLocation(String JavaDoc modId, String JavaDoc location) throws ConfigException{
241         getEjbModule(modId).setLocation(location);
242     }
243    
244     public String JavaDoc getStubLocation(String JavaDoc name) {
245         ModuleEnvironment menv = instanceEnvironment.getModuleEnvironment(name,
246                                                      DeployableObjectType.EJB);
247         return menv.getModuleStubPath();
248     }
249     
250     public String JavaDoc getGeneratedXMLLocation(String JavaDoc name){
251         ModuleEnvironment menv = instanceEnvironment.getModuleEnvironment(name,
252                                                      DeployableObjectType.EJB);
253         return menv.getModuleGeneratedXMLPath();
254     }
255     
256     public String JavaDoc getDescription(String JavaDoc modId) throws ConfigException {
257         return getEjbModule(modId).getDescription();
258     }
259     
260     public void setDescription(String JavaDoc modId, String JavaDoc desc)
261             throws ConfigException {
262         getEjbModule(modId).setDescription(desc);
263     }
264
265     /**
266      * This method is called only by ResourcesUtil during server start up.
267      * We need to construct the application classloader before calling into
268      * other getDescriptor method to fully populate the application object.
269      */

270     public Application getDescriptor(
271         String JavaDoc moduleID, ClassLoader JavaDoc parentClassLoader)
272             throws ConfigException {
273
274         ClassLoader JavaDoc cl = new EJBClassLoader(parentClassLoader);
275         String JavaDoc[] classPaths = (String JavaDoc[]) EJBClassPathUtils.getModuleClasspath(
276                                 moduleID, null, this).toArray(new String JavaDoc[0]);
277
278         if (classPaths != null) {
279             int classPathSize = classPaths.length;
280             for (int i=0; i<classPathSize; i++) {
281                 try {
282                     ((EJBClassLoader) cl).appendURL(new File JavaDoc(classPaths[i]));
283                 } catch (IOException JavaDoc ioe) {
284                     //@@ i18n
285
_logger.log(Level.WARNING, "Cannot convert path to URL: " + classPaths[i]);
286                 }
287             }
288         }
289
290         return getDescriptor(moduleID, cl, false);
291     }
292
293     /**
294      * Returns the deployment descriptor object for this ejb module.
295      *
296      * @param modId ejb module id
297      * @param cl ejb class loader
298      * @param validateXml use validating parser when true
299      *
300      * @return the deployment descriptor object for this ejb module
301      *
302      * @throws ConfigException if unable to load the deployment descriptor
303      */

304     public Application getDescriptor(String JavaDoc modId, ClassLoader JavaDoc cl,
305             String JavaDoc loc, boolean validateXML) throws ConfigException {
306         
307         return getDescriptor(modId, cl, validateXML);
308     }
309
310     /**
311      * Returns the deployment descriptor object for this ejb module. This
312      * method gets called when deployment backend is running verification
313      * during deployment.
314      *
315      * @param modId ejb module id
316      * @param cl ejb class loader
317      * @param validateXml use validating parser when true
318      * @param verify sets cmp mappings, doctype, etc when true
319      *
320      * @return the deployment descriptor object for this ejb module
321      *
322      * @throws ConfigException if unable to load the deployment descriptor
323      */

324     public Application getDescriptor(String JavaDoc modId, ClassLoader JavaDoc cl,
325             boolean validateXml) throws ConfigException {
326
327         Application application = getRegisteredDescriptor(modId);
328         if (application!=null) {
329             application.setClassLoader(cl);
330             return application;
331         }
332         try {
333             String JavaDoc moduleDir = getLocation(modId);
334         EjbArchivist ejbArchivist = new EjbArchivist();
335         ejbArchivist.setXMLValidation(validateXml);
336
337             // We need to use a temp CL until we are done with validate(),
338
// see https://glassfish.dev.java.net/issues/show_bug.cgi?id=223
339
ClassLoader JavaDoc tcl = (cl instanceof InstrumentableClassLoader) ?
340                     InstrumentableClassLoader.class.cast(cl).copy() : cl;
341             ejbArchivist.setClassLoader(tcl);
342
343         FileArchive archive = new FileArchive();
344             // if is system predeployed app, load from original mod dir
345
// else load from generated/xml dir
346
// print a warning if generated/xml dir is not there
347
// and load from original dir (upgrade scenario)
348
if (isSystemAdmin(modId)) {
349                 archive.open(moduleDir);
350             } else {
351                 String JavaDoc xmlDir = getGeneratedXMLLocation(modId);
352                 if (FileUtils.safeIsDirectory(xmlDir)) {
353                     archive.open(xmlDir);
354                 } else {
355                     // log a warning message in the server log
356
_logger.log(Level.WARNING, "core.no_xmldir",
357                         new Object JavaDoc[]{xmlDir, moduleDir});
358                     archive.open(moduleDir);
359                 }
360             }
361         application = ApplicationArchivist.openArchive(modId, ejbArchivist, archive, true);
362
363             // We need to use a temp CL until the end of this method...
364
// see https://glassfish.dev.java.net/issues/show_bug.cgi?id=223
365
application.setClassLoader(tcl);
366             // set it in all the bundles as well,
367
for (BundleDescriptor bd : (Collection<BundleDescriptor>)
368                     application.getBundleDescriptors()) {
369                 bd.setClassLoader(tcl);
370             }
371             if(!isSystemAdmin(modId) && !isSystem(modId)) {
372                 // we need to read persistence descriptors separately
373
// because they are read from appDir as oppsed to xmlDir.
374
readPersistenceDeploymentDescriptors(moduleDir, application);
375             }
376             application.setGeneratedXMLDirectory(getGeneratedXMLLocation(modId));
377             
378             if (!application.getWebServiceDescriptors().isEmpty()) {
379                 ModuleContentLinker visitor = new ModuleContentLinker(archive);
380                 application.visit((com.sun.enterprise.deployment.util.ApplicationVisitor) visitor);
381             }
382
383             // Now, let's set the actual class loader
384
application.setClassLoader(cl);
385             // set it in all the bundles as well,
386
for (BundleDescriptor bd : (Collection<BundleDescriptor>)
387                     application.getBundleDescriptors()) {
388                 bd.setClassLoader(cl);
389             }
390             registerDescriptor(modId, application);
391             
392             return application;
393         } catch (ConfigException ce) {
394             throw ce;
395         } catch (IOException JavaDoc ioe) {
396             throw new ConfigException(Localizer.getValue(
397                 ExceptionType.IO_ERROR_LOADING_DD, modId), ioe);
398         } catch (Throwable JavaDoc t) {
399             throw new ConfigException(Localizer.getValue(
400                         ExceptionType.FAIL_DD_LOAD, modId), t);
401         }
402     }
403 }
404
Popular Tags