KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
36
37 import com.sun.enterprise.config.ConfigBean;
38 import com.sun.enterprise.config.ConfigContext;
39 import com.sun.enterprise.config.ConfigException;
40 import com.sun.enterprise.config.serverbeans.ApplicationRef;
41 import com.sun.enterprise.config.serverbeans.Applications;
42 import com.sun.enterprise.config.serverbeans.Server;
43 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
44 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
45 import com.sun.enterprise.config.serverbeans.WebModule;
46 import com.sun.enterprise.deployment.Application;
47 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
48 import com.sun.enterprise.deployment.archivist.WebArchivist;
49 import com.sun.enterprise.deployment.archivist.WebArchivist;
50 import com.sun.enterprise.deployment.backend.DeployableObjectInfo;
51 import com.sun.enterprise.deployment.backend.DeployableObjectType;
52 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
53 import com.sun.enterprise.deployment.Descriptor;
54 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
55 import com.sun.enterprise.deployment.util.ModuleContentLinker;
56 import com.sun.enterprise.deployment.util.ModuleDescriptor;
57 import com.sun.enterprise.deployment.WebBundleDescriptor;
58 import com.sun.enterprise.util.io.FileUtils;
59 import com.sun.enterprise.util.RelativePathResolver;
60 import com.sun.enterprise.util.SystemPropertyConstants;
61
62 public class WebModulesManager extends ModulesManager {
63     public WebModulesManager(InstanceEnvironment env) throws ConfigException {
64         super(env, true);
65     }
66     
67     public WebModulesManager(InstanceEnvironment env,
68             boolean useBackupServerXml) throws ConfigException {
69         super(env, useBackupServerXml);
70         //FIXME: HACK START
71
WebModule[] jArray = ((Applications)configBean).getWebModule();
72     if(jArray!=null) {
73             for(int i=0;i<jArray.length;i++) {
74                 jArray[i].setConfigContext(configContext);
75         jArray[i].setXPath(ServerXPathHelper.getWebModuleIdXpathExpression(jArray[i].getName()));
76             }
77     }
78     //FIXME: HACK END
79

80     }
81     
82     /**
83      * @return the module type this class is managing
84      */

85     public ModuleType JavaDoc getModuleType() {
86         return ModuleType.WAR;
87     }
88
89     private WebModule getWebModule(String JavaDoc modId) throws ConfigException {
90         WebModule mod = (WebModule)
91             ((Applications)this.configBean).getWebModuleByName(modId);
92         
93         if(mod == null)
94             throw new ConfigException(Localizer.getValue(ExceptionType.NO_SUCH_WEB_MOD));
95         
96         return mod;
97         
98     }
99     
100     /**
101      * Returns a list of all WebModule beans deployed on the server.
102      */

103     public WebModule[] listWebModules() throws ConfigException {
104         WebModule[] modules = ((Applications)this.configBean).getWebModule();
105
106         if (modules == null) {
107             return new WebModule[0];
108         }
109         
110         ArrayList JavaDoc list = new ArrayList JavaDoc();
111         for (int i=0; i<modules.length; i++) {
112             // add the modules to the list if it is referenced
113
// by this server
114
if ( isReferenced(modules[i].getName()) ) {
115                 list.add(modules[i]);
116             }
117         }
118         // returns an array of modules referenced by this server
119
WebModule[] refList = new WebModule[list.size()];
120         return ( (WebModule[]) list.toArray(refList) );
121     }
122
123     /**
124      * Returns a list of (type java.lang.String) all web modules
125      * deployed to the server.
126      */

127     public List JavaDoc listIds() {
128
129         ArrayList JavaDoc arr = new ArrayList JavaDoc();
130         WebModule[] mods = ((Applications)this.configBean).getWebModule();
131
132         // returns an empty list if there are no web modules in the domain
133
if(mods == null) return arr;
134
135
136         for (int i=0;i<mods.length;i++) {
137             String JavaDoc name = mods[i].getName();
138             // adds the web module to the list if
139
// it is referenced by this server
140
if ( isReferenced(name) ) {
141                 arr.add(name);
142             }
143         }
144         return arr;
145     }
146     
147     public void remove(String JavaDoc modId) throws ConfigException {
148         WebModule backEm = (WebModule)
149             ((Applications)configBean).getWebModuleByName(modId);
150         ((Applications)configBean).removeWebModule(backEm);
151     }
152     
153     protected boolean isRegistered(String JavaDoc modId, ConfigBean bean) {
154         ConfigBean cb = null;
155         try {
156             cb = ((Applications)bean).getWebModuleByName(modId);
157         } catch(Exception JavaDoc cn) {
158         }
159         
160         if(cb != null) return true;
161         return false;
162     }
163         
164     public boolean isEnabled(String JavaDoc modId) throws ConfigException{
165         return getWebModule(modId).isEnabled();
166     }
167
168     /**
169      * Checks whether this module is a systemmodule
170      * ResourceType in domain.xml should start with "system-"
171      * @return true if resourceType starts with "system-"
172      */

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

187     public boolean isSystemAdmin(String JavaDoc modId) throws ConfigException{
188         WebModule wm = getWebModule(modId);
189         String JavaDoc resourceType = wm.getObjectType();
190         if(resourceType.startsWith(SYSTEM_ADMIN_PREFIX))
191             return true;
192         else
193             return false;
194     }
195     
196     public void setEnable(String JavaDoc modId, boolean enable) throws ConfigException{
197         getWebModule(modId).setEnabled(enable);
198     }
199     
200     /**
201      * Set the optional attributes for an module
202      *
203      * @param modId unique idenitifier for the module
204      * @param optionalAttributes - pairs tag/value to set
205      */

206     public void setOptionalAttributes(String JavaDoc modId, Properties JavaDoc optionalAttributes)
207             throws ConfigException {
208         if(optionalAttributes!=null) {
209             WebModule wm = getWebModule(modId);
210             Enumeration JavaDoc tags = optionalAttributes.keys();
211             while(tags.hasMoreElements())
212             {
213                 String JavaDoc tag = (String JavaDoc)tags.nextElement();
214                 String JavaDoc value = optionalAttributes.getProperty(tag);
215                 wm.setAttributeValue(tag, value);
216             }
217         }
218     }
219     
220     public String JavaDoc getLocation(String JavaDoc name) throws ConfigException {
221         WebModule webModule = (WebModule)
222             ((Applications)this.configBean).getWebModuleByName(name);
223         String JavaDoc location = null;
224         if (webModule != null)
225             location = webModule.getLocation();
226         return resolvePath(location);
227     }
228
229     public void setLocation(String JavaDoc name, String JavaDoc location) throws ConfigException {
230         WebModule webModule = (WebModule)
231             ((Applications)this.configBean).getWebModuleByName(name);
232         if (webModule != null)
233             webModule.setLocation(location);
234     }
235
236     // WBN 4-18-2002
237
public String JavaDoc getJSPLocation(String JavaDoc name){
238         ModuleEnvironment menv = instanceEnvironment.getModuleEnvironment(name,
239                                                      DeployableObjectType.WEB);
240         return menv.getModuleJSPPath();
241     }
242
243     public String JavaDoc getGeneratedXMLLocation(String JavaDoc name){
244         ModuleEnvironment menv = instanceEnvironment.getModuleEnvironment(name,
245                                                      DeployableObjectType.WEB);
246         return menv.getModuleGeneratedXMLPath();
247     }
248
249     public String JavaDoc getStubLocation(String JavaDoc name){
250         ModuleEnvironment menv = instanceEnvironment.getModuleEnvironment(name,
251                                                      DeployableObjectType.WEB);
252         return menv.getModuleStubPath();
253     }
254
255     public String JavaDoc getDescription(String JavaDoc modId) throws ConfigException {
256         return getWebModule(modId).getDescription();
257     }
258     
259     public void setDescription(String JavaDoc modId, String JavaDoc desc)
260             throws ConfigException {
261         getWebModule(modId).setDescription(desc);
262     }
263     
264     public String JavaDoc getVirtualServers(String JavaDoc modId) throws ConfigException {
265     Server server = ServerBeansFactory.getServerBean(configContext);
266     ApplicationRef ar = server.getApplicationRefByRef(modId);
267     return ar.getVirtualServers();
268     }
269    /*
270     public void setVirtualServers(String modId, String value)
271             throws ConfigException {
272     Server server = ServerBeansFactory.getServerBean(configContext);
273     ApplicationRef ar = server.getApplicationRefByRef(modId);
274         ar.setVirtualServers(value);
275     }
276     */

277     
278     public String JavaDoc getContextRoot(String JavaDoc modId) throws ConfigException {
279         return getWebModule(modId).getContextRoot();
280     }
281     
282     public void setContextRoot(String JavaDoc modId, String JavaDoc value)
283             throws ConfigException {
284         getWebModule(modId).setContextRoot(value);
285     }
286
287     /**
288      * Returns the deployment descriptor object for the specified web module.
289      *
290      * @param modId web module id
291      * @param cl class loader to associate with the descriptors
292      * @param validateXml use validating parser when true
293      
294      *
295      * @return the deployment descriptor object for this web module
296      *
297      * @throws ConfigException if unable to load the deployment descriptor
298      */

299     public Application getDescriptor(String JavaDoc modId, ClassLoader JavaDoc cl,
300             String JavaDoc moduleLoc, boolean validateXML) throws ConfigException {
301         Application app = getDescriptor(modId, moduleLoc, validateXML);
302     app.setClassLoader(cl);
303     return app;
304     }
305
306     /**
307      * Returns the deployment descriptor object for the web module in the
308      * specified directory.
309      *
310      * @param modId web module id
311      * @param modDir The directory containing the web module
312      *
313      * @return the deployment descriptor object for this web module
314      *
315      * @throws ConfigException if unable to load the deployment descriptor
316      */

317     public Application getDescriptor(String JavaDoc modId, String JavaDoc modDir)
318             throws ConfigException {
319
320         return getDescriptor(modId, modDir, false);
321     }
322
323     /**
324      * Returns the deployment descriptor object for the web module in the
325      * specified directory.
326      *
327      * @param modId web module id
328      * @param modDir The directory containing the web module
329      * @param validateXml use validating parser when true
330      *
331      * @return the deployment descriptor object for this web module
332      *
333      * @throws ConfigException if unable to load the deployment descriptor
334      */

335     public Application getDescriptor(String JavaDoc modId, String JavaDoc modDir,
336             boolean validateXml) throws ConfigException {
337             
338         Application application = getRegisteredDescriptor(modId);
339         if (application!=null) {
340             return application;
341         }
342         try {
343         WebArchivist webArchivist = new WebArchivist();
344         webArchivist.setXMLValidation(validateXml);
345         
346         FileArchive archive = new FileArchive();
347             // if is system predeployed app, load from original mod dir
348
// else load from generated/xml dir
349
// print a warning if generated/xml dir is not there
350
// and load from original dir (upgrade scenario)
351
if (isSystemAdmin(modId)) {
352                 archive.open(modDir);
353             } else {
354                 String JavaDoc xmlDir = getGeneratedXMLLocation(modId);
355                 if (FileUtils.safeIsDirectory(xmlDir)) {
356                     archive.open(xmlDir);
357                 } else {
358                     // log a warning message in the server log
359
_logger.log(Level.WARNING, "core.no_xmldir",
360                         new Object JavaDoc[]{xmlDir, modDir});
361                     archive.open(modDir);
362                 }
363         }
364         application = ApplicationArchivist.openArchive(modId, webArchivist, archive, true);
365             if(!isSystemAdmin(modId) && !isSystem(modId)) {
366                 // we need to read persistence descriptors separately
367
// because they are read from appDir as oppsed to xmlDir.
368
readPersistenceDeploymentDescriptors(modDir, application);
369             }
370             application.setGeneratedXMLDirectory(getGeneratedXMLLocation(modId));
371
372             if (!application.getWebServiceDescriptors().isEmpty()) {
373                 ModuleContentLinker visitor = new ModuleContentLinker(archive);
374                 application.visit((com.sun.enterprise.deployment.util.ApplicationVisitor) visitor);
375             }
376             
377             registerDescriptor(modId, application);
378
379             return application;
380         } catch (IOException JavaDoc ioe) {
381             throw new ConfigException(Localizer.getValue(
382                 ExceptionType.IO_ERROR_LOADING_DD, modId), ioe);
383         } catch (Throwable JavaDoc t) {
384             throw new ConfigException(Localizer.getValue(
385                 ExceptionType.FAIL_DD_LOAD, modId), t);
386         }
387     }
388 }
389
Popular Tags