KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > J2EEModule


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.admin.mbeans;
25
26 import com.sun.enterprise.admin.common.exception.ServerInstanceException;
27 import com.sun.enterprise.config.serverbeans.Applications;
28 import com.sun.enterprise.config.ConfigBeansFactory;
29 import com.sun.enterprise.config.ConfigContext;
30 import com.sun.enterprise.config.serverbeans.Applications;
31 import com.sun.enterprise.config.serverbeans.J2eeApplication;
32 import com.sun.enterprise.config.serverbeans.WebModule;
33 import com.sun.enterprise.config.serverbeans.EjbModule;
34 import com.sun.enterprise.config.serverbeans.ConnectorModule;
35 import com.sun.enterprise.config.serverbeans.AppclientModule;
36 import com.sun.enterprise.config.serverbeans.LifecycleModule;
37 import com.sun.enterprise.config.serverbeans.Mbean;
38 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
39 import com.sun.enterprise.deployment.backend.DeploymentUtils;
40 import com.sun.enterprise.deployment.util.XModuleType;
41 import com.sun.enterprise.instance.AppsManager;
42 import com.sun.enterprise.deployment.Application;
43 import com.sun.enterprise.deployment.BundleDescriptor;
44 import com.sun.enterprise.server.ApplicationServer;
45 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
46 import com.sun.enterprise.deployment.io.DescriptorConstants;
47 import com.sun.enterprise.admin.server.core.AdminService;
48 import java.util.Iterator JavaDoc;
49 import java.io.File JavaDoc;
50 import java.io.FileReader JavaDoc;
51 import java.io.StringWriter JavaDoc;
52 import java.io.EOFException JavaDoc;
53 import java.io.FileNotFoundException JavaDoc;
54 import java.io.IOException JavaDoc;
55 import com.sun.enterprise.instance.InstanceFactory;
56 import com.sun.enterprise.util.io.FileUtils;
57 import java.util.logging.Level JavaDoc;
58 import java.util.logging.Logger JavaDoc;
59 import com.sun.enterprise.util.i18n.StringManager;
60 import com.sun.logging.LogDomains;
61 import com.sun.enterprise.util.RelativePathResolver;
62
63 /**
64  * Provides API for getting attribute values like
65  * moduleType, location, etc. for j2ee stand-alone modules
66  * and the sub-modules within.
67  *
68  * @author Sreenivas Munnangi
69  */

70
71 public class J2EEModule {
72
73     // logger and local strings initialization
74
private static final StringManager localStrings =
75     StringManager.getManager(J2EEModule.class);
76     private static final Logger JavaDoc sLogger =
77     Logger.getLogger(LogDomains.ADMIN_LOGGER);
78
79     // local variables
80
private String JavaDoc standaloneModuleName = null;
81     private String JavaDoc subModuleName = null;
82     private ModuleType JavaDoc moduleType = null;
83     private String JavaDoc ddLocation = null;
84
85     /**
86      * null constructor
87      */

88     public J2EEModule () {
89     }
90
91     /**
92      * constructor for stand-alone module
93      */

94     public J2EEModule (String JavaDoc standaloneModuleName)
95     throws ServerInstanceException {
96
97     if ((standaloneModuleName == null) ||
98             (standaloneModuleName.length() < 1)) {
99         sLogger.log(Level.WARNING, getLogMsg("invalid standAloneModuleName"));
100         throw new ServerInstanceException ( localStrings.getString(
101             "admin.mbeans.J2EEModule.invalidStandaloneModuleName"));
102     }
103     this.standaloneModuleName = standaloneModuleName;
104     }
105
106     /**
107      * constructor for sub-module
108      */

109     public J2EEModule (String JavaDoc standaloneModuleName, String JavaDoc subModuleName)
110     throws ServerInstanceException {
111
112     this(standaloneModuleName);
113
114     if ((subModuleName == null) ||
115             (subModuleName.length() < 1)) {
116         sLogger.log(Level.WARNING,
117             getLogMsg("invalid sub module for the given stand-alone module"));
118         throw new ServerInstanceException (localStrings.getString(
119             "admin.mbeans.J2EEModule.invalidSubModuleName"));
120     }
121     this.subModuleName = subModuleName;
122     }
123
124     /**
125      * Returns module type
126      *
127      * If both standaloneModuleName and subModuleName are valid strings
128      * then the assumption is that standaloneModuleName represents a valid
129      * j2ee application and subModuleName represents a module within
130      * the application.
131      *
132      * If subModuleName is null then the assumption is that the
133      * standaloneModuleName is a valid j2ee stand-alone module.
134      */

135      public ModuleType JavaDoc getModuleType() throws ServerInstanceException {
136
137     if (moduleType != null) return moduleType;
138
139     if (subModuleName == null) {
140         moduleType = getModuleType(standaloneModuleName);
141     } else {
142         moduleType = getModuleType(standaloneModuleName, subModuleName);
143     }
144
145     return moduleType;
146      }
147
148     /**
149      * Returns the deployment descriptors location for this module, i.e.
150      * where the deployment descriptors are stored.
151      */

152     public String JavaDoc getDeploymentDescriptorsLocation()
153     throws ServerInstanceException {
154
155     if (ddLocation != null) {
156         ddLocation = RelativePathResolver.resolvePath(ddLocation);
157         return ddLocation;
158     }
159
160     if (subModuleName == null) {
161         ddLocation = getDDLocation(standaloneModuleName);
162     } else {
163         ddLocation = getDDLocation(standaloneModuleName, subModuleName);
164     }
165
166     if (ddLocation != null) {
167         ddLocation = RelativePathResolver.resolvePath(ddLocation);
168     }
169
170     return ddLocation;
171     }
172
173     /**
174      * Returns moduleType for the given stand-alone module
175      */

176     ModuleType JavaDoc getModuleType(String JavaDoc standaloneModuleName)
177     throws ServerInstanceException {
178
179     sLogger.log(Level.FINE,
180         getLogMsg("getModuleType for standaloneModuleName" +
181         standaloneModuleName));
182
183         // iterate through each of module types
184

185         ModuleType JavaDoc moduleType = null;
186
187         try {
188
189             // Application
190
Applications appsConfigBean =
191                     (Applications) ConfigBeansFactory.getConfigBeanByXPath(
192             AdminService.getAdminService().getAdminContext().getAdminConfigContext(),
193             ServerXPathHelper.XPATH_APPLICATIONS);
194
195             // J2EEApplication
196
J2eeApplication[] j2eeApps = appsConfigBean.getJ2eeApplication();
197             if (j2eeApps != null) {
198                 for(int i=0; i<j2eeApps.length; i++) {
199                     if ((j2eeApps[i].getName()).equals(standaloneModuleName)) {
200             ddLocation = j2eeApps[i].getLocation();
201             return ModuleType.EAR;
202                     }
203                 }
204             }
205
206             // EJBModule
207
EjbModule[] eModules = appsConfigBean.getEjbModule();
208             if (eModules != null) {
209                 for(int i=0; i<eModules.length; i++) {
210                     if ((eModules[i].getName()).equals(standaloneModuleName)) {
211             ddLocation = eModules[i].getLocation();
212             return ModuleType.EJB;
213                     }
214                 }
215             }
216
217             // WebModule
218
WebModule[] wModules = appsConfigBean.getWebModule();
219             if (wModules != null) {
220                 for(int i=0; i<wModules.length; i++) {
221                     if ((wModules[i].getName()).equals(standaloneModuleName)) {
222             ddLocation = wModules[i].getLocation();
223             return ModuleType.WAR;
224                     }
225                 }
226             }
227
228             // ResourceAdapterModule
229
ConnectorModule[] connectorConfigBeans = appsConfigBean.getConnectorModule();
230             if (connectorConfigBeans != null) {
231                 for(int i = 0; i < connectorConfigBeans.length; i++) {
232                     if ((connectorConfigBeans[i].getName()).equals(standaloneModuleName)) {
233             ddLocation = connectorConfigBeans[i].getLocation();
234             return ModuleType.RAR;
235                     }
236                 }
237             }
238
239             // AppClient Module
240
AppclientModule[] acModules = appsConfigBean.getAppclientModule();
241             if (acModules != null) {
242                 for(int i = 0; i < acModules.length; i++) {
243                     if ((acModules[i].getName()).equals(standaloneModuleName)) {
244             ddLocation = acModules[i].getLocation();
245             return ModuleType.CAR;
246                     }
247                 }
248             }
249
250             // Lifecycle Module
251
LifecycleModule[] lcModules = appsConfigBean.getLifecycleModule();
252             if (lcModules != null) {
253                 for(int i = 0; i < lcModules.length; i++) {
254                     if ((lcModules[i].getName()).equals(standaloneModuleName)) {
255             return XModuleType.LCM;
256                     }
257                 }
258             }
259
260             // Custom MBean Module
261
Mbean[] mbModules = appsConfigBean.getMbean();
262             if (mbModules != null) {
263                 for(int i = 0; i < mbModules.length; i++) {
264                     if ((mbModules[i].getName()).equals(standaloneModuleName)) {
265             return XModuleType.CMB;
266                     }
267                 }
268             }
269
270         } catch (Exception JavaDoc e) {
271                 throw new ServerInstanceException(e.getLocalizedMessage());
272         }
273         return moduleType;
274     }
275
276     /**
277      * Returns moduleType for the given combination of
278      * stand-alone module and sub-module
279      */

280     ModuleType JavaDoc getModuleType(String JavaDoc standaloneModuleName, String JavaDoc subModuleName)
281     throws ServerInstanceException {
282
283     sLogger.log(Level.FINE, getLogMsg("getModuleType " +
284         "standaloneModuleName = " + standaloneModuleName + " " +
285         "subModuleName = " + subModuleName));
286
287         ModuleType JavaDoc moduleType = null;
288
289         try {
290             // Get application descriptor
291

292             AppsManager am = InstanceFactory.createAppsManager(
293             ApplicationServer.getServerContext().getInstanceName());
294
295             Application appD = (Application)
296                 DeploymentUtils.getDescriptor(standaloneModuleName, am);
297
298             // Get the bundle descriptor for the given module
299
// and determine its' type
300

301             BundleDescriptor bd = null;
302             java.util.Set JavaDoc bds = appD.getBundleDescriptors();
303             for(Iterator JavaDoc it=bds.iterator(); it.hasNext(); ) {
304                 bd = (BundleDescriptor)it.next();
305                 if ((bd.getModuleDescriptor().getArchiveUri()).equals(subModuleName) ||
306                      bd.getModuleID().equals(subModuleName) ||
307              bd.getName().equals(subModuleName)) {
308                         moduleType = bd.getModuleType();
309             // set dd location
310
ddLocation = am.getLocation(standaloneModuleName) +
311                 File.separator +
312                 FileUtils.makeFriendlyFileName(
313                     bd.getModuleDescriptor().getArchiveUri());
314             break;
315                 }
316             }
317
318         } catch (Exception JavaDoc e) {
319             throw new ServerInstanceException(e.getLocalizedMessage());
320         }
321
322         return moduleType;
323
324     }
325
326
327     /**
328      * Returns deployment descriptors location for the
329      * given stand-alone module
330      *
331      * Now the ddLocation is set as part of determininig moduleType
332      * In future if rewuired this method can be used to provide enhanced
333      * functionality.
334      */

335     String JavaDoc getDDLocation(String JavaDoc standaloneModuleName)
336     throws ServerInstanceException {
337     sLogger.log(Level.FINE, getLogMsg(
338         "getDDLocation for standaloneModuleName " + standaloneModuleName));
339     return ddLocation;
340     }
341
342     /**
343      * Returns deployment descriptors location for the
344      * given combination of stand-alone module and sub-module
345      *
346      * Now the ddLocation is set as part of determininig moduleType
347      * In future if rewuired this method can be used to provide enhanced
348      * functionality.
349      */

350     String JavaDoc getDDLocation(String JavaDoc standaloneModuleName, String JavaDoc subModuleName)
351     throws ServerInstanceException {
352     sLogger.log(Level.FINE, getLogMsg(
353         "getDDLocation for standaloneModuleName " +
354         standaloneModuleName + " " +
355         "subModuleName = " + subModuleName));
356     return ddLocation;
357     }
358
359
360
361     /**
362      * Method to read deployment descriptor xml
363      * and return it as String
364      */

365     public String JavaDoc getStringForDDxml(String JavaDoc fileName)
366             throws ServerInstanceException {
367
368         try {
369             return FileUtils.getFileContents(fileName);
370         } catch (FileNotFoundException JavaDoc fnfe) {
371             sLogger.log(Level.WARNING, getLogMsg(
372             "getStringForDDxml FileNotFoundException " + fileName));
373             throw new ServerInstanceException(fnfe.getLocalizedMessage());
374         } catch (IOException JavaDoc ioe) {
375             sLogger.log(Level.WARNING, getLogMsg(
376             "getStringForDDxml IOException " + fileName));
377             throw new ServerInstanceException(ioe.getLocalizedMessage());
378         }
379     }
380     
381     /**
382      * private method for decorating log messages with class name
383      * and other appropriate info
384      */

385      private String JavaDoc getLogMsg(String JavaDoc str) {
386     return (this.getClass().getName() + ":" +
387         str);
388      }
389
390 }
391
Popular Tags