KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConfigBean;
26 import com.sun.enterprise.config.ConfigContext;
27 import com.sun.enterprise.config.ConfigException;
28 import com.sun.enterprise.config.serverbeans.JdbcResource;
29 import com.sun.enterprise.config.serverbeans.ConnectorResource;
30 import com.sun.enterprise.admin.util.IAdminConstants;
31 import com.sun.enterprise.util.i18n.StringManager;
32
33 public class ResourceHelper extends ReferenceHelperBase {
34     
35     protected static final StringManager _strMgr=StringManager.getManager(ResourceHelper.class);
36     private static ResourceHelper _theInstance;
37     
38     public ResourceHelper() {
39         super();
40     }
41     
42     protected Server[] getReferencingServers(ConfigContext configContext, String JavaDoc name)
43         throws ConfigException
44     {
45         return ServerHelper.getServersReferencingResource(configContext, name);
46     }
47     
48     protected Cluster[] getReferencingClusters(ConfigContext configContext, String JavaDoc name)
49         throws ConfigException
50     {
51         return ClusterHelper.getClustersReferencingResource(configContext, name);
52     }
53         
54     private synchronized static ResourceHelper getInstance()
55     {
56         if (_theInstance == null) {
57             _theInstance = new ResourceHelper();
58         }
59         return _theInstance;
60     }
61     
62     /**
63      * Is the configuration referenced by anyone (i.e. any server instance or cluster
64      */

65     public static boolean isResourceReferenced(ConfigContext configContext, String JavaDoc resourceName)
66         throws ConfigException
67     {
68         return getInstance().isReferenced(configContext, resourceName);
69     }
70     
71     /**
72      * Return true if the configuration is referenced by no-one other than the given
73      * server instance.
74      */

75     public static boolean isResourceReferencedByServerOnly(ConfigContext configContext,
76         String JavaDoc resourceName, String JavaDoc serverName) throws ConfigException
77     {
78         return getInstance().isReferencedByServerOnly(configContext, resourceName, serverName);
79     }
80     
81     /**
82      * Return true if the configuration is referenced by no-one other than the given
83      * cluster.
84      */

85     public static boolean isResourceReferencedByClusterOnly(ConfigContext configContext,
86         String JavaDoc resourceName, String JavaDoc clusterName) throws ConfigException
87     {
88         return getInstance().isReferencedByClusterOnly(configContext, resourceName, clusterName);
89     }
90     
91     /**
92      * Find all the servers or clusters associated with the given configuration and return them
93      * as a comma separated list.
94      */

95     public static String JavaDoc getResourceReferenceesAsString(ConfigContext configContext, String JavaDoc resourceName)
96         throws ConfigException
97     {
98         return getInstance().getReferenceesAsString(configContext, resourceName);
99     }
100
101     /**
102      * Returns true if the named pool has a reference from a jdbc resource
103      * that is used by the given server instance.
104      *
105      * @param ctx config context
106      * @param poolName jdbc resource pool name
107      * @param serverName name of the server instance
108      *
109      * @return true if the pool is used by the server instance
110      *
111      * @throw ConfigException if an error while parsing domain.xml
112      */

113     public static boolean isJdbcPoolReferenced(ConfigContext ctx,
114             String JavaDoc poolName, String JavaDoc serverName) throws ConfigException {
115
116         if (ctx == null || poolName == null || serverName == null) {
117             return false;
118         }
119
120         Resources rBean = ServerBeansFactory.getDomainBean(ctx).getResources();
121
122         JdbcResource[] jdbcBeans = rBean.getJdbcResource();
123
124         // no jdbc resource in the domain, so it is not possible
125
// for the jdbc pool to be in use by a server in this domain
126
if (jdbcBeans == null) {
127             return false;
128         }
129
130         for (int i = 0; i <jdbcBeans.length; i++) {
131
132             // jdbc resource is not referenced by the server instance
133
if ( !ServerHelper.serverReferencesResource(
134                     ctx, serverName, jdbcBeans[i].getJndiName()) ) {
135
136                 continue;
137             } else {
138                 String JavaDoc pool = jdbcBeans[i].getPoolName();
139                 if ( (pool != null) && pool.equals(poolName) ) {
140                     // jdbc pool is referenced by server (server->res->pool)
141
return true;
142                 }
143             }
144         }
145
146         // no jdbc resource referred by this server is using this pool
147
return false;
148     }
149
150     /**
151      * Returns true if the named pool has a reference from a connector resource
152      * that is used by the given server instance.
153      *
154      * @param ctx config context
155      * @param poolName connector pool name
156      * @param serverName name of the server instance
157      *
158      * @return true if the pool is used by the server instance
159      *
160      * @throw ConfigException if an error while parsing domain.xml
161      */

162     public static boolean isConnectorPoolReferenced(ConfigContext ctx,
163             String JavaDoc poolName, String JavaDoc serverName) throws ConfigException {
164
165         if (ctx == null || poolName == null || serverName == null) {
166             return false;
167         }
168
169         Resources rBean = ServerBeansFactory.getDomainBean(ctx).getResources();
170
171         ConnectorResource[] conBeans = rBean.getConnectorResource();
172
173         // no connector resource in the domain, so it is not possible
174
// for the connector pool to be in use by a server in this domain
175
if (conBeans == null) {
176             return false;
177         }
178
179         for (int i = 0; i <conBeans.length; i++) {
180
181             // connector resource is not referenced by the server instance
182
if ( !ServerHelper.serverReferencesResource(
183                     ctx, serverName, conBeans[i].getJndiName()) ) {
184
185                 continue;
186             } else {
187                 String JavaDoc pool = conBeans[i].getPoolName();
188                 if ( (pool != null) && pool.equals(poolName) ) {
189                     return true;
190                 }
191             }
192         }
193         return false;
194     }
195
196     /**
197      * Returns the resource type of a given resource
198      */

199     public static String JavaDoc getResourceType(ConfigContext ctx, String JavaDoc id)
200             throws ConfigException {
201
202         Resources root = ((Domain)ctx.getRootConfigBean()).getResources();
203
204         ConfigBean res = root.getJdbcResourceByJndiName(id);
205         if ( res != null )
206             return Resources.JDBC_RESOURCE;
207
208         res = root.getMailResourceByJndiName(id);
209         if( res != null )
210             return Resources.MAIL_RESOURCE;
211
212         res = root.getCustomResourceByJndiName(id);
213         if( res != null )
214             return Resources.CUSTOM_RESOURCE;
215
216         res = root.getExternalJndiResourceByJndiName(id);
217         if ( res != null )
218             return Resources.EXTERNAL_JNDI_RESOURCE;
219
220         res = root.getPersistenceManagerFactoryResourceByJndiName(id);
221         if ( res != null)
222             return Resources.PERSISTENCE_MANAGER_FACTORY_RESOURCE;
223
224         res = root.getAdminObjectResourceByJndiName(id);
225         if ( res != null )
226             return Resources.ADMIN_OBJECT_RESOURCE;
227
228         res = root.getConnectorResourceByJndiName(id);
229         if ( res != null )
230             return Resources.CONNECTOR_RESOURCE;
231
232         res = root.getJdbcConnectionPoolByName(id);
233         if ( res != null )
234             return Resources.JDBC_CONNECTION_POOL;
235
236         res = root.getConnectorConnectionPoolByName(id);
237         if ( res != null )
238             return Resources.CONNECTOR_CONNECTION_POOL;
239
240         res = root.getResourceAdapterConfigByResourceAdapterName(id);
241         if ( res != null )
242             return Resources.RESOURCE_ADAPTER_CONFIG;
243
244         return null;
245     }
246        
247     public static boolean isSystemResource(ConfigContext ctx, String JavaDoc resourceName)
248         throws ConfigException
249     {
250         ConfigBean bean = findResource(ctx, resourceName);
251         if (bean == null) {
252             throw new ConfigException(_strMgr.getString("noSuchResource",
253                 resourceName));
254         }
255         String JavaDoc objectType = null;
256         try {
257             objectType = bean.getAttributeValue(ServerTags.OBJECT_TYPE);
258         } catch (Exception JavaDoc ex) {
259             //if the object-type attribute does not exist, then assume that
260
//the resource is not a system resource.
261
return false;
262         }
263         if (objectType.equals(IAdminConstants.SYSTEM_ALL) ||
264             objectType.equals(IAdminConstants.SYSTEM_ADMIN) ||
265             objectType.equals(IAdminConstants.SYSTEM_INSTANCE)) {
266             return true;
267         } else {
268             return false;
269         }
270     }
271       
272     public static ConfigBean findResource(ConfigContext ctx, String JavaDoc id)
273             throws ConfigException {
274         Resources root = ((Domain)ctx.getRootConfigBean()).getResources();
275         return findResource(root, id);
276     }
277
278     public static ConfigBean findResource(Resources root, String JavaDoc id)
279             throws ConfigException {
280
281         ConfigBean res = root.getJdbcResourceByJndiName(id);
282         if ( res != null )
283             return res;
284
285         res = root.getMailResourceByJndiName(id);
286         if( res != null )
287             return res;
288
289         res = root.getCustomResourceByJndiName(id);
290         if( res != null )
291             return res;
292
293         res = root.getExternalJndiResourceByJndiName(id);
294         if ( res != null )
295             return res;
296
297         res = root.getPersistenceManagerFactoryResourceByJndiName(id);
298         if ( res != null)
299             return res;
300
301         res = root.getAdminObjectResourceByJndiName(id);
302         if ( res != null )
303             return res;
304
305         res = root.getConnectorResourceByJndiName(id);
306         if ( res != null )
307             return res;
308
309         res = root.getJdbcConnectionPoolByName(id);
310         if ( res != null )
311             return res;
312
313         res = root.getConnectorConnectionPoolByName(id);
314         if ( res != null )
315             return res;
316
317         res = root.getResourceAdapterConfigByResourceAdapterName(id);
318         if ( res != null )
319             return res;
320
321         return null;
322     }
323 }
324
Popular Tags