KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > DASResourcesUtil


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.server;
25 /**
26  * This class overrides the isReferenced method in the ResourcesUtil class.
27  * An instance of this class is always returned when ResourcesUtil.getInstance()
28  * happens in the DAS
29  *
30  * @author Aditya Gore
31  * @version
32  */

33
34 import com.sun.enterprise.config.*;
35 import com.sun.enterprise.config.serverbeans.*;
36 import com.sun.enterprise.util.RelativePathResolver;
37 import com.sun.enterprise.connectors.DeferredResourceConfig;
38
39 import java.util.logging.Level JavaDoc;
40
41 public class DASResourcesUtil extends ResourcesUtil {
42     
43     /**
44      * Flags if a getConnection call is coming from the connector runtime.
45      * Only for such a call, we should not be checking isReferenced
46      * The reason for this is that the getConnection call might be happening
47      * in the DAS for a jdbc resource which does not have a resource-ref in
48      * the DAS. However, we still need to give a connection from that
49      * resource, so we should not check for referencing info.
50      */

51     public boolean getConnectionFromConnectorRuntime_ = false;
52     
53     protected DASResourcesUtil() {
54         super();
55     }
56     
57     /**
58      * Returns true if the given resource is referenced by this server.
59      *
60      * @param resourceName the name of the resource
61      *
62      * @return true if the named resource is used/referred by this server
63      *
64      * @throws ConfigException if an error while parsing domain.xml
65      */

66     protected boolean isReferenced(String JavaDoc resourceName) throws ConfigException {
67         if (_logger.isLoggable(Level.FINE)) {
68         _logger.fine("isReferenced in DASResourcesUtil:: " + resourceName);
69     }
70
71     if (isGetConnectionFromConnectorRuntime()) {
72             if (_logger.isLoggable(Level.FINE)) {
73             _logger.fine("getConnectionFromConnectorRuntime :: true");
74         }
75         return true;
76     }
77
78     return super.isReferenced( resourceName );
79     }
80
81     public boolean isGetConnectionFromConnectorRuntime() {
82         return getConnectionFromConnectorRuntime_;
83     }
84
85     public void setGetConnectionFromConnectorRuntime(boolean flag) {
86         getConnectionFromConnectorRuntime_ = flag;
87     }
88     
89     public String JavaDoc getLocation(String JavaDoc moduleName) {
90         if(moduleName == null) {
91             return null;
92         }
93
94         //Use admin config context, as in DAS, the server config context would
95
//not have the latest config context changes until the end of the deploy
96
//event
97
ConfigContext adminConfigCtx = com.sun.enterprise.admin.server.core.
98             AdminService.getAdminService().getAdminContext().getAdminConfigContext();
99
100         try {
101             ConnectorModule connectorModule = ServerBeansFactory.getDomainBean(adminConfigCtx).
102                             getApplications().getConnectorModuleByName(moduleName);
103             
104             if (connectorModule == null) {
105                 return null;
106             }
107             
108             String JavaDoc connectorModuleLocation = connectorModule.getLocation();
109             String JavaDoc connectorModuleLocationResolved = RelativePathResolver.
110                                 resolvePath(connectorModuleLocation);
111             return connectorModuleLocationResolved;
112         } catch (ConfigException e) {
113             _logger.warning(e.getMessage());
114             _logger.log(Level.FINE, "Exception while tryign to find location" +
115                     "of connector module " + moduleName, new Object JavaDoc[]{e});
116         }
117         return null;
118     }
119     
120     /**
121      * Gets the deployment location for a J2EE application.
122      * @param rarName
123      * @return
124      */

125     public String JavaDoc getApplicationDeployLocation(String JavaDoc appName) {
126         ConfigContext adminConfigCtx = com.sun.enterprise.admin.server.core.
127         AdminService.getAdminService().getAdminContext().getAdminConfigContext();
128         
129         try {
130             J2eeApplication app = ServerBeansFactory.getDomainBean(adminConfigCtx).
131             getApplications().getJ2eeApplicationByName(appName);
132             return RelativePathResolver.resolvePath(app.getLocation());
133         } catch (ConfigException e) {
134             e.printStackTrace();
135         }
136         return null;
137     }
138  
139     /**
140      * Gets a PMF resource on the basis of its jndi name
141      *
142      * @param jndiName the jndi name of the PMF resource to lookup
143      * @param checkReference if true, returns this PMF resource only if it is referenced in
144      * this server. If false, returns the PMF resource irrespective of
145      * whether it is referenced or not.
146      */

147     public PersistenceManagerFactoryResource getPMFResourceByJndiName(
148             String JavaDoc jndiName ) {
149         reinitialize();
150
151     Resources localRes = getResourcesServerBean();
152     if ( localRes == null) {
153         return null;
154     }
155
156     if (_logger.isLoggable(Level.FINE)) {
157         _logger.fine("ResourceUtil :: looking up pmf resource, jndiName is :"
158             + jndiName );
159     }
160     PersistenceManagerFactoryResource pmf =
161             localRes.getPersistenceManagerFactoryResourceByJndiName( jndiName );
162         
163     if (_logger.isLoggable(Level.FINE)) {
164         _logger.fine("getPMFResourceByJndiName:: looked up pmf resource : "
165             + pmf);
166     }
167     //does the isReferenced method throw NPE for null value? Better be safe
168
if (pmf == null) {
169         return null;
170     }
171         
172         try {
173            return isReferenced( jndiName ) ? pmf : null;
174         }catch( ConfigException ce ) {
175            return null;
176         }
177     }
178
179     /**
180      * Gets a JDBC resource on the basis of its jndi name
181      * @param jndiName the jndi name of the JDBC resource to lookup
182      * @param checkReference if true, returns this JDBC resource only if it is referenced in
183      * this server. If false, returns the JDBC resource irrespective of
184      * whether it is referenced or not.
185      */

186     public JdbcResource getJdbcResourceByJndiName( String JavaDoc jndiName) {
187         reinitialize();
188
189     Resources localRes = getResourcesServerBean();
190     if ( localRes == null ) {
191         return null;
192     }
193
194     if (_logger.isLoggable(Level.FINE)) {
195         _logger.fine("ResourcesUtil :: looking up jdbc resource, jndiName is :"
196             + jndiName );
197     }
198
199     JdbcResource jdbcRes =
200             localRes.getJdbcResourceByJndiName( jndiName );
201         
202     if (_logger.isLoggable(Level.FINE)) {
203         _logger.fine("ResourcesUtil :: looked up jdbc resource:" + jdbcRes.getJndiName() );
204     }
205     //does the isReferenced method throw NPE for null value? Better be safe
206
if (jdbcRes == null) {
207         return null;
208     }
209     
210         try {
211             return isReferenced( jndiName ) ? jdbcRes : null;
212         }catch( ConfigException ce ) {
213             return null;
214         }
215     }
216
217     /**
218      * Checks if this is a PMF resource
219      * @param jndiName the jndi name of the PMF resource to lookup
220      * @param checkReference if true, returns this PMF resource only if it is referenced in
221      * this server
222      */

223     public boolean isPMFResource( String JavaDoc jndiName) {
224
225     if (_logger.isLoggable(Level.FINE)) {
226         _logger.fine("ResourcesUtil :: checking if this is a pmf resource:"
227             + jndiName );
228     }
229         return (getPMFResourceByJndiName(jndiName) != null);
230     }
231
232
233     protected DeferredResourceConfig getDeferredJdbcResourceConfigs(
234                   String JavaDoc resourceName)
235     {
236
237         if(resourceName == null) {
238             return null;
239         }
240         reinitialize();
241         try {
242             //__pm does not have a domain.xml entry and hence will not
243
//be referenced
244
if(!(resourceName.endsWith("__pm"))){
245                 if(!isReferenced(resourceName)){
246                     return null;
247                 }
248             }
249         } catch (ConfigException e) {
250             _logger.log(Level.WARNING, e.getMessage());
251             _logger.log(Level.FINE, "Error while finding resource references " , e);
252         }
253
254     Resources localRes = getResourcesServerBean();
255     if ( localRes == null ) {
256         return null;
257     }
258         
259         ConfigBean[] resourcesToload = new ConfigBean[2];
260         JdbcResource jdbcResource = localRes.getJdbcResourceByJndiName(resourceName);
261         if(jdbcResource == null || !jdbcResource.isEnabled()) {
262             String JavaDoc cmpResourceName =
263                         getCorrespondingCmpResourceName(resourceName);
264             jdbcResource = localRes.getJdbcResourceByJndiName(cmpResourceName);
265             if(jdbcResource == null) {
266                 return null;
267             }
268         }
269         JdbcConnectionPool jdbcPool =
270                  localRes.getJdbcConnectionPoolByName(jdbcResource.getPoolName());
271         if(jdbcPool == null) {
272             return null;
273         }
274         String JavaDoc rarName = getRAForJdbcConnectionPool(jdbcPool);
275         if(rarName != null && belongToSystemRar(rarName)) {
276             resourcesToload[0] = jdbcPool;
277             resourcesToload[1] = jdbcResource;
278             DeferredResourceConfig resourceConfig =
279                             new DeferredResourceConfig(rarName,null,null,
280                             null,jdbcPool,jdbcResource,null);
281             resourceConfig.setResourcesToLoad(resourcesToload);
282             return resourceConfig;
283         }
284         return null;
285     }
286
287     protected DeferredResourceConfig getDeferredJdbcPoolConfigs(
288                   String JavaDoc poolName)
289     {
290         if(poolName == null) {
291             return null;
292         }
293         reinitialize();
294
295         Resources localRes = getResourcesServerBean();
296         JdbcConnectionPool jdbcPool =
297                 localRes.getJdbcConnectionPoolByName(poolName);
298         if(jdbcPool == null) {
299             return null;
300         }
301         String JavaDoc rarName = getRAForJdbcConnectionPool(jdbcPool);
302         
303         ConfigBean[] resourcesToload = new ConfigBean[1];
304         if(rarName != null && belongToSystemRar(rarName)) {
305                     resourcesToload[0] = jdbcPool;
306                     DeferredResourceConfig resourceConfig =
307                             new DeferredResourceConfig(rarName,null,null,
308                             null,jdbcPool,null,null);
309                     resourceConfig.setResourcesToLoad(resourcesToload);
310                     return resourceConfig;
311         }
312         return null;
313     }
314
315     protected Resources getResourcesServerBean() {
316     //IF and only IF we are executing in the context of a
317
//ConnectorRuntime.getConnection call, we should use the
318
//admin config context to get to the Resources MBean
319
//so check and return the correct Resources MBean
320

321     if (! isGetConnectionFromConnectorRuntime() ) {
322         //return the cached Resources MBean from ResourcesUtil
323
//reinitialize has been called before this, so we are ok
324
return super.getResourcesServerBean();
325     }
326
327         ConfigContext adminConfigCtx = com.sun.enterprise.admin.server.core.
328             AdminService.getAdminService().getAdminContext().getAdminConfigContext();
329
330     Domain localdom = null;
331     Resources localres = null;
332     try {
333             localdom = ServerBeansFactory.getDomainBean( adminConfigCtx );
334             localres = localdom.getResources();
335         } catch( ConfigException ce ) {
336         _logger.log( Level.WARNING, ce.getMessage() );
337         return null;
338     }
339
340     return localres;
341     }
342
343     protected DeferredResourceConfig getDeferredConnectorResourceConfigs(
344                   String JavaDoc resourceName)
345     {
346
347         if(resourceName == null) {
348             return null;
349         }
350         
351         reinitialize();
352
353         try {
354             if(!isReferenced(resourceName)){
355                 return null;
356             }
357         } catch (ConfigException e) {
358             _logger.log(Level.WARNING, e.getMessage());
359             _logger.log(Level.FINE, "Error while finding resource references " , e);
360         }
361         
362         Resources localRes = getResourcesServerBean();
363     if ( localRes == null ) {
364         return null;
365     }
366         ConfigBean[] resourcesToload = new ConfigBean[2];
367         ConnectorResource connectorResource =
368                      localRes.getConnectorResourceByJndiName(resourceName);
369         if(connectorResource == null || !connectorResource.isEnabled()) {
370             return null;
371         }
372         String JavaDoc poolName = connectorResource.getPoolName();
373         ConnectorConnectionPool ccPool =
374                        localRes.getConnectorConnectionPoolByName(poolName);
375         if(ccPool == null) {
376             return null;
377         }
378         String JavaDoc rarName = ccPool.getResourceAdapterName();
379         if(rarName != null && belongToSystemRar(rarName)) {
380             resourcesToload[0] = ccPool;
381             resourcesToload[1] = connectorResource;
382             ResourceAdapterConfig[] resourceAdapterConfig =
383                                         new ResourceAdapterConfig[1];
384             resourceAdapterConfig[0] =
385                          localRes.getResourceAdapterConfigByResourceAdapterName(
386                           rarName);
387             DeferredResourceConfig resourceConfig =
388                             new DeferredResourceConfig(rarName,null,ccPool,
389                             connectorResource,null,null,
390                             resourceAdapterConfig);
391             resourceConfig.setResourcesToLoad(resourcesToload);
392             return resourceConfig;
393         }
394         return null;
395     }
396
397     protected DeferredResourceConfig getDeferredConnectorPoolConfigs(
398                   String JavaDoc poolName)
399     {
400
401         if(poolName == null) {
402             return null;
403         }
404         
405         reinitialize();
406         Resources localRes = getResourcesServerBean();
407
408         ConfigBean[] resourcesToload = new ConfigBean[1];
409         ConnectorConnectionPool ccPool =
410                 localRes.getConnectorConnectionPoolByName(poolName);
411         if(ccPool == null) {
412             return null;
413         }
414         
415         String JavaDoc rarName = ccPool.getResourceAdapterName();
416         
417     //Commenting out the belongToSystemRar call since the method
418
//originally was introduced to check if a pool belonged to
419
//a system rar while lazily loading already created pools.
420
//Now, because of lazy deployment of pools on resource creation,
421
//this check is no longer relevant.
422
//TODO: revisit later
423
if(rarName != null) { //&& belongToSystemRar(rarName)) {
424
resourcesToload[0] = ccPool;
425             ResourceAdapterConfig[] resourceAdapterConfig =
426                       new ResourceAdapterConfig[1];
427             resourceAdapterConfig[0] =
428                    localRes.getResourceAdapterConfigByResourceAdapterName(
429                    rarName);
430             DeferredResourceConfig resourceConfig =
431                  new DeferredResourceConfig(rarName,null,ccPool,
432                  null,null,null,resourceAdapterConfig);
433             resourceConfig.setResourcesToLoad(resourcesToload);
434             return resourceConfig;
435         }
436         return null;
437     }
438
439
440     protected DeferredResourceConfig getDeferredAdminObjectConfigs(
441                   String JavaDoc resourceName)
442     {
443
444         if(resourceName == null) {
445             return null;
446         }
447         reinitialize();
448         
449         try {
450             if(!isReferenced(resourceName)){
451                 return null;
452             }
453         } catch (ConfigException e) {
454             _logger.log(Level.WARNING, e.getMessage());
455             _logger.log(Level.FINE, "Error while finding resource references " , e);
456         }
457
458     Resources localRes = getResourcesServerBean();
459     if ( localRes == null) {
460         return null;
461     }
462         
463         ConfigBean[] resourcesToload = new ConfigBean[1];
464         AdminObjectResource adminObjectResource =
465                       localRes.getAdminObjectResourceByJndiName(resourceName);
466         if(adminObjectResource == null || !adminObjectResource.isEnabled()) {
467             return null;
468         }
469         String JavaDoc rarName = adminObjectResource.getResAdapter();
470         if(rarName != null && belongToSystemRar(rarName)) {
471             resourcesToload[0] = adminObjectResource;
472             ResourceAdapterConfig[] resourceAdapterConfig =
473                                         new ResourceAdapterConfig[1];
474             resourceAdapterConfig[0] =
475                    localRes.getResourceAdapterConfigByResourceAdapterName(rarName);
476             DeferredResourceConfig resourceConfig =
477                          new DeferredResourceConfig(rarName,adminObjectResource,
478                          null,null,null,null,resourceAdapterConfig);
479             resourceConfig.setResourcesToLoad(resourcesToload);
480             return resourceConfig;
481         }
482         return null;
483     }
484
485     public JdbcConnectionPool[] getJdbcConnectionPools() {
486         //the method below is actually a method to indicate that the
487
//ConfigContext to be used is the AdminConfigContext and not
488
//the normal ConfigContext
489
//TODO: Rename the flag
490
//This change is needed since 9.0 onwards we create the pool
491
//object eagerly at deployment time and hence the pool is not
492
//available in the ConfigContext. Note this method is accessed
493
//in the isJdbcPool call. There we need to get *all* pools present
494
//at that time in the environment
495
Resources localRes = null;
496
497         setGetConnectionFromConnectorRuntime( true );
498         try {
499             localRes = getResourcesServerBean();
500         } finally {
501             setGetConnectionFromConnectorRuntime( false );
502         }
503
504         JdbcConnectionPool[] jdbcConnectionPool = localRes.getJdbcConnectionPool();
505         if(jdbcConnectionPool == null) return null;
506         if (jdbcConnectionPool.length == 0) {
507             return null;
508         } else {
509             return jdbcConnectionPool;
510         }
511     }
512
513     public ConnectorConnectionPool[] getConnectorConnectionPools() {
514         //the method below is actually a method to indicate that the
515
//ConfigContext to be used is the AdminConfigContext and not
516
//the normal ConfigContext
517
//TODO: Rename the flag
518
Resources localRes = null;
519         setGetConnectionFromConnectorRuntime( true );
520         try {
521             localRes = getResourcesServerBean();
522         } finally {
523             setGetConnectionFromConnectorRuntime( false );
524         }
525
526         ConnectorConnectionPool[] connectorConnectionPools =
527             localRes.getConnectorConnectionPool();
528         if(connectorConnectionPools == null) return null;
529         if (connectorConnectionPools.length == 0) {
530             return null;
531         } else {
532             return connectorConnectionPools;
533         }
534     }
535
536 }
537
Popular Tags