KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > ApplicationHelper


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 package com.sun.enterprise.config.serverbeans;
24
25 import com.sun.enterprise.config.ConfigContext;
26 import com.sun.enterprise.config.ConfigException;
27 import com.sun.enterprise.config.ConfigBean;
28 import com.sun.enterprise.config.serverbeans.Server;
29 import com.sun.enterprise.config.serverbeans.Cluster;
30 import com.sun.enterprise.config.serverbeans.Applications;
31 import com.sun.enterprise.config.serverbeans.J2eeApplication;
32 import com.sun.enterprise.config.serverbeans.EjbModule;
33 import com.sun.enterprise.config.serverbeans.WebModule;
34 import com.sun.enterprise.config.serverbeans.AppclientModule;
35 import com.sun.enterprise.config.serverbeans.ConnectorModule;
36 import com.sun.enterprise.config.serverbeans.LifecycleModule;
37 import com.sun.enterprise.config.serverbeans.Mbean;
38 import com.sun.enterprise.admin.util.IAdminConstants;
39
40 import com.sun.enterprise.util.i18n.StringManager;
41
42 import java.util.ArrayList JavaDoc;
43
44 public class ApplicationHelper extends ReferenceHelperBase {
45     
46     protected static final StringManager _strMgr=StringManager.getManager(ApplicationHelper.class);
47     private static ApplicationHelper _theInstance;
48     
49     public ApplicationHelper() {
50         super();
51     }
52     
53     protected Server[] getReferencingServers(ConfigContext configContext, String JavaDoc name)
54         throws ConfigException
55     {
56         return ServerHelper.getServersReferencingApplication(configContext, name);
57     }
58     
59     protected Cluster[] getReferencingClusters(ConfigContext configContext, String JavaDoc name)
60         throws ConfigException
61     {
62         return ClusterHelper.getClustersReferencingApplication(configContext, name);
63     }
64         
65     private synchronized static ApplicationHelper getInstance()
66     {
67         if (_theInstance == null) {
68             _theInstance = new ApplicationHelper();
69         }
70         return _theInstance;
71     }
72     
73     /**
74      * Is the configuration referenced by anyone (i.e. any server instance or cluster
75      */

76     public static boolean isApplicationReferenced(ConfigContext configContext, String JavaDoc appName)
77         throws ConfigException
78     {
79         return getInstance().isReferenced(configContext, appName);
80     }
81     
82     /**
83      * Return true if the configuration is referenced by no-one other than the given
84      * server instance.
85      */

86     public static boolean isApplicationReferencedByServerOnly(ConfigContext configContext,
87         String JavaDoc appName, String JavaDoc serverName) throws ConfigException
88     {
89         return getInstance().isReferencedByServerOnly(configContext, appName, serverName);
90     }
91     
92     /**
93      * Return true if the configuration is referenced by no-one other than the given
94      * cluster.
95      */

96     public static boolean isApplicationReferencedByClusterOnly(ConfigContext configContext,
97         String JavaDoc appName, String JavaDoc clusterName) throws ConfigException
98     {
99         return getInstance().isReferencedByClusterOnly(configContext, appName, clusterName);
100     }
101     
102     /**
103      * Find all the servers or clusters associated with the given configuration and return them
104      * as a comma separated list.
105      */

106     public static String JavaDoc getApplicationReferenceesAsString(ConfigContext configContext, String JavaDoc appName)
107         throws ConfigException
108     {
109         return getInstance().getReferenceesAsString(configContext, appName);
110     }
111
112     /**
113      * Returns the type of a given application
114      */

115     public static String JavaDoc getApplicationType(ConfigContext ctx, String JavaDoc id)
116             throws ConfigException {
117         ConfigBean appBean = findApplication(ctx, id);
118         if (appBean instanceof J2eeApplication)
119             return Applications.J2EE_APPLICATION;
120         if (appBean instanceof EjbModule)
121             return Applications.EJB_MODULE;
122         if (appBean instanceof WebModule)
123             return Applications.WEB_MODULE;
124         if (appBean instanceof LifecycleModule)
125             return Applications.LIFECYCLE_MODULE;
126         if (appBean instanceof AppclientModule)
127             return Applications.APPCLIENT_MODULE;
128         if (appBean instanceof ConnectorModule)
129             return Applications.CONNECTOR_MODULE;
130         if (appBean instanceof Mbean)
131             return Applications.MBEAN;
132         return null;
133     }
134
135     public static ConfigBean findApplication(ConfigContext configContext,
136                    String JavaDoc appName) throws ConfigException
137     {
138         Applications root = ((Domain)configContext.getRootConfigBean()).
139                                     getApplications();
140
141         ConfigBean app = root.getJ2eeApplicationByName(appName);
142         if ( app != null)
143             return app;
144
145         app = root.getEjbModuleByName(appName);
146         if ( app != null)
147             return app;
148
149         app = root.getWebModuleByName(appName);
150         if ( app != null)
151             return app;
152
153         app = root.getConnectorModuleByName(appName);
154         if ( app != null)
155             return app;
156         
157         app = root.getAppclientModuleByName(appName);
158         if ( app != null)
159             return app;
160
161         app = root.getLifecycleModuleByName(appName);
162         if ( app != null)
163             return app;
164
165         app = root.getMbeanByName(appName);
166         if ( app != null)
167             return app;
168
169         return null;
170     }
171
172     public static String JavaDoc[] getApplicationsInDomain(ConfigContext configContext)
173         throws ConfigException
174     {
175         ArrayList JavaDoc result = new ArrayList JavaDoc();
176         final Domain domain = ConfigAPIHelper.getDomainConfigBean(configContext);
177         final Applications applications = domain.getApplications();
178         AppclientModule[] appclients = applications.getAppclientModule();
179         for (int i = 0; i < appclients.length; i++) {
180             result.add(appclients[i].getName());
181         }
182         ConnectorModule[] connectors = applications.getConnectorModule();
183         for (int i = 0; i < connectors.length; i++) {
184             result.add(connectors[i].getName());
185         }
186         EjbModule[] ebjs = applications.getEjbModule();
187         for (int i = 0; i < ebjs.length; i++) {
188             result.add(ebjs[i].getName());
189         }
190         J2eeApplication[] apps = applications.getJ2eeApplication();
191         for (int i = 0; i < apps.length; i++) {
192             result.add(apps[i].getName());
193         }
194         LifecycleModule[] lifecycles = applications.getLifecycleModule();
195         for (int i = 0; i < lifecycles.length; i++) {
196             result.add(lifecycles[i].getName());
197         }
198         WebModule[] webs = applications.getWebModule();
199         for (int i = 0; i < webs.length; i++) {
200             result.add(webs[i].getName());
201         }
202         Mbean[] mbeans = applications.getMbean();
203         for (int i = 0; i < mbeans.length; i++) {
204             result.add(mbeans[i].getName());
205         }
206         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
207     }
208
209     public static boolean isSystemApp(ConfigContext ctx, String JavaDoc appId)
210         throws ConfigException
211     {
212         ConfigBean bean = findApplication(ctx, appId);
213         if (bean == null) {
214             throw new ConfigException(_strMgr.getString("noSuchApplication",
215                 appId));
216         }
217         String JavaDoc objectType = null;
218         try {
219             objectType = bean.getAttributeValue(ServerTags.OBJECT_TYPE);
220         } catch (Exception JavaDoc ex) {
221             //if the object-type attribute does not exist, then assume that
222
//the app is not a system app.
223
return false;
224         }
225         if (objectType.equals(IAdminConstants.SYSTEM_ALL) ||
226             objectType.equals(IAdminConstants.SYSTEM_ADMIN) ||
227             objectType.equals(IAdminConstants.SYSTEM_INSTANCE)) {
228             return true;
229         } else {
230             return false;
231         }
232     }
233
234     /**
235      * Given an application ID, end point name and transformation rule name
236      * this method returns the matching TransformationRule ConfigBean.
237      *
238      * @param cfgContext Config context to be used
239      * @param appId Application or module name
240      * @param epName Endpoint's name
241      * @param ruleName Transformation rule's name
242      */

243     public static TransformationRule findTransformationRule(
244         ConfigContext cfgContext, String JavaDoc appId, String JavaDoc epName, String JavaDoc
245         ruleName) throws ConfigException {
246
247         ConfigBean appBean = findApplication(cfgContext, appId);
248         if (appBean == null) {
249             throw new ConfigException(_strMgr.getString("noSuchApplication",
250                 appId));
251         }
252         WebServiceEndpoint wsep = null;
253         if (appBean instanceof J2eeApplication) {
254             wsep = ((J2eeApplication) appBean).getWebServiceEndpointByName(
255                     epName);
256         } else if (appBean instanceof EjbModule) {
257             wsep = ((EjbModule) appBean).getWebServiceEndpointByName( epName);
258         } else if (appBean instanceof WebModule) {
259             wsep = ((WebModule) appBean).getWebServiceEndpointByName( epName);
260         }
261         if (wsep == null) {
262             throw new ConfigException(_strMgr.getString("noSuchWSEP",
263                 epName));
264         }
265         TransformationRule tRule = wsep.getTransformationRuleByName(ruleName);
266         if (tRule == null) {
267             throw new ConfigException(
268                 _strMgr.getString("noSuchTransformationRule", ruleName));
269         } else {
270             return tRule;
271         }
272
273     }
274
275
276 }
277
Popular Tags