KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > ConnectorRegistry


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.connectors;
25
26 import com.sun.enterprise.config.serverbeans.*;
27 import com.sun.enterprise.deployment.ConnectorDescriptor;
28 import com.sun.logging.LogDomains;
29 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.logging.*;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import com.sun.enterprise.connectors.authentication.RuntimeSecurityMap;
36
37
38 /**
39   * This is an registry class which holds various objects in hashMaps,
40   * hash tables, and vectors. These objects are updated and queried
41   * during various funtionalities of rar deployment/undeployment, resource
42   * creation/destruction
43   * Ex. of these objects are ResourcesAdapter instances, security maps for
44   * pool managed connection factories.
45   * It follows singleton patter. i.e only one instance at any point of time.
46   * @author Binod P.G and Srikanth P
47   * @version
48  */

49
50 public class ConnectorRegistry {
51
52     protected static ConnectorRegistry connectorRegistryInstance;
53
54     /**
55      * <code>resourceAdapters</code> keeps track of all active resource
56      * adapters in the connector runtime.
57      * String:resourceadapterName Vs ActiveResourceAdapter
58      */

59     protected HashMap JavaDoc resourceAdapters;
60     protected HashMap JavaDoc factories;
61     protected HashMap JavaDoc resourceAdapterConfig;
62     static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
63
64     /** Return the ConnectorRegistry instance
65      * @return ConnectorRegistry instance which is a singleton
66      */

67     
68     public static ConnectorRegistry getInstance() {
69         if (connectorRegistryInstance == null) {
70             connectorRegistryInstance = new ConnectorRegistry();
71             _logger.log(Level.FINE,"creating new connector registry");
72         }
73         logFine("returning the connector registry");
74         return connectorRegistryInstance;
75     }
76    
77     /**
78      * Protected constructor.
79      * It is protected as it follows singleton pattern.
80      */

81
82     protected ConnectorRegistry() {
83         resourceAdapters = new HashMap JavaDoc();
84         factories = new HashMap JavaDoc();
85         resourceAdapterConfig = new HashMap JavaDoc();
86         _logger.log(Level.FINE,"initialized the connector registry");
87     }
88
89     /** Adds the object implementing ActiveResourceAdapter
90      * interface to the registry.
91      * @param rarModuleName RarName which is the key
92      * @param rar ActiveResourceAdapter instance which is the value.
93      */

94
95     public void addActiveResourceAdapter( String JavaDoc rarModuleName,
96                  ActiveResourceAdapter rar) {
97     synchronized( resourceAdapters ) {
98             resourceAdapters.put(rarModuleName,rar);
99     }
100         _logger.log(Level.FINE,
101                     "Added the active resource adapter to connector registry",
102                     rarModuleName);
103     }
104
105     /** Removes the object implementing ActiveResourceAdapter
106      * interface from the registry.
107      * This method is called whenever an active connector module
108      * is removed from the Connector runtime. [eg. undeploy/recreate etc]
109      * @param rarModuleName RarName which is the key
110      * @return true if successfully removed
111      * false if deletion fails.
112      */

113
114     public boolean removeActiveResourceAdapter(String JavaDoc rarModuleName) {
115         Object JavaDoc o = null;
116     
117     synchronized( resourceAdapters ) {
118         o = resourceAdapters.remove( rarModuleName );
119     }
120     
121         if( o == null) {
122             logFine("Failed to remove the resource adapter from connector registry" +
123                rarModuleName);
124             return false;
125         } else {
126             _logger.log(Level.FINE,
127                  "removed the active resource adapter from connector registry",
128                  rarModuleName);
129             return true;
130         }
131     }
132
133     /** Retrieves the object implementing ActiveResourceAdapter interface
134      * from the registry. Key is the rarName.
135      * @param rarModuleName Rar name. It is the key
136      * @return object implementing ActiveResourceAdapter interface
137      */

138
139     public ActiveResourceAdapter getActiveResourceAdapter(
140                      String JavaDoc rarModuleName) {
141         if(rarModuleName != null) {
142             _logger.log(Level.FINE,
143                "returning/found the resource adapter from connector registry",
144                rarModuleName);
145             return (ActiveResourceAdapter) resourceAdapters.get(rarModuleName);
146         } else {
147             _logger.log(Level.FINE,
148                "Resourceadapter not found in connector registry.Returning null",
149                rarModuleName);
150             return null;
151         }
152     }
153
154     /** Checks if the MCF pertaining to the pool is instantiated and present
155      * in the registry. Each pool has its own MCF instance.
156      * @param poolName Name of the pool
157      * @return true if the MCF is found.
158      * false if MCF is not found
159      */

160
161     public boolean isMCFCreated (String JavaDoc poolName) {
162         boolean created = factories.containsKey( poolName );
163         logFine("isMCFCreated " + poolName + " - " + created);
164         return created;
165     }
166
167     /** Remove MCF instance pertaining to the poolName from the registry.
168      * @param poolName Name of the pool
169      * @return true if successfully removed.
170      * false if removal fails.
171      */

172
173     public boolean removeManagedConnectionFactory(String JavaDoc poolName) {
174         if(factories.remove(poolName) == null) {
175             _logger.log(Level.FINE,
176                "Failed to remove the MCF from connector registry.", poolName);
177             return false;
178         } else {
179             logFine("removeMCF " + poolName + " - " + !factories.containsKey(poolName));
180             return true;
181         }
182     }
183
184     /** Add MCF instance pertaining to the poolName to the registry.
185      * @param poolName Name of the pool
186      * @param factory MCF instance to be added.
187      */

188
189     public void addManagedConnectionFactory(String JavaDoc poolName,
190                                     PoolMetaData pmd ) {
191         factories.put(poolName, pmd);
192         logFine("Added MCF to connector registry for: "+ poolName);
193     }
194
195     /** Retrieve MCF instance pertaining to the poolName from the registry.
196      * @param poolName Name of the pool
197      * @return factory MCF instance retrieved.
198      */

199     
200     public ManagedConnectionFactory JavaDoc getManagedConnectionFactory(
201                      String JavaDoc poolName) {
202         if(poolName != null) {
203             _logger.log(Level.FINE,
204                  "Returning the MCF from connector registry.", poolName);
205             
206             PoolMetaData pmd = (PoolMetaData) factories.get( poolName );
207         if (pmd != null) {
208                 return pmd.getMCF();
209             }
210
211         }
212         return null;
213     }
214    
215
216     /** Checks whether the rar is already deployed i.e registered with
217      * connector registry
218      * @param rarModuleName rar Name.
219      * @return true if rar is registered
220      * false if rar is not registered.
221      */

222
223     public boolean isRegistered(String JavaDoc rarModuleName) {
224         _logger.log(Level.FINE,
225             "Checking for MCF presence in connector registry.",rarModuleName);
226         synchronized(resourceAdapters ) {
227             return resourceAdapters.containsKey(rarModuleName);
228     }
229     }
230
231     /** Gets the connector descriptor pertaining the rar
232      * @param rarModuleName rarName
233      * @return ConnectorDescriptor which represents the ra.xml
234      */

235
236     public ConnectorDescriptor getDescriptor(String JavaDoc rarModuleName) {
237         ActiveResourceAdapter ar = null;
238         if(rarModuleName != null) {
239             ar = (ActiveResourceAdapter) resourceAdapters.get(rarModuleName);
240         }
241         if (ar != null) {
242             _logger.log(Level.FINE,
243                  "Found/returing Connector descriptor in connector registry.",
244                  rarModuleName);
245             return ar.getDescriptor();
246         } else {
247             _logger.log(Level.FINE,
248                  "Couldnot find Connector descriptor in connector registry.",
249                  rarModuleName);
250             return null;
251         }
252     }
253
254     /** Gets the runtime equivalent of policies enforced by the Security Maps
255      * pertaining to a pool from the Pool's Meta Data.
256      * @param poolName Name of the pool
257      * @return runtimeSecurityMap in the form of HashMap of
258      * HashMaps (user and groups).
259      * @see SecurityMapUtils.processSecurityMaps(SecurityMap[])
260      */

261
262     public RuntimeSecurityMap getRuntimeSecurityMap(String JavaDoc poolName) {
263         if(poolName != null) {
264             _logger.log(Level.FINE,
265               "Returing the security map from connector registry.", poolName);
266             PoolMetaData pmd = (PoolMetaData)factories.get(poolName);
267             return pmd.getRuntimeSecurityMap();
268         } else {
269             return null;
270         }
271     }
272
273     /** Get the resource adapter config properties object registered with
274      * registry against the rarName.
275      * @param rarName Name of the rar
276      * @return ResourceAdapter configuration object
277      */

278
279     public ResourceAdapterConfig getResourceAdapterConfig(String JavaDoc rarName) {
280         if(rarName != null) {
281             _logger.log(Level.FINE,
282               "Returing the resourceadapter Config from registry.",rarName);
283             return (ResourceAdapterConfig)resourceAdapterConfig.get(rarName);
284         } else {
285             return null;
286         }
287     }
288
289     /** Add the resource adapter config properties object to registry
290      * against the rarName.
291      * @param rarName Name of the rar
292      * @param raConfig ResourceAdapter configuration object
293      */

294
295     public void addResourceAdapterConfig(String JavaDoc rarName,
296                  ResourceAdapterConfig raConfig) {
297         if( rarName != null) {
298             _logger.log(Level.FINE,
299                  "Adding the resourceAdapter Config to connector registry.",
300                  rarName);
301             resourceAdapterConfig.put(rarName,raConfig);
302         }
303     }
304
305     /** Remove the resource adapter config properties object from registry
306      * @param rarName Name of the rar
307      * @return true if successfully deleted
308      * false if deletion fails
309      */

310
311     public boolean removeResourceAdapterConfig(String JavaDoc rarName) {
312         if(resourceAdapterConfig.remove(rarName) == null) {
313             _logger.log(Level.FINE,
314                  "failed to remove the resourceAdapter config from registry.",
315                  rarName);
316             return false;
317         } else {
318             _logger.log(Level.FINE,
319                  "Removed the resourceAdapter config map from registry.",
320                  rarName);
321             return true;
322         }
323     }
324
325     /**
326      * Returns all Active Resource Adapters in the connector runtime.
327      * @return All active resource adapters in the connector runtime
328      */

329     public ActiveResourceAdapter[] getAllActiveResourceAdapters() {
330         return (ActiveResourceAdapter[])
331                 this.resourceAdapters.values().
332                         toArray(new ActiveResourceAdapter[]{});
333     }
334
335
336     static private void logFine( String JavaDoc msg ) {
337         if ( msg != null && _logger.isLoggable(Level.FINE) ) {
338         _logger.fine( msg );
339     }
340     }
341
342     public PoolMetaData getPoolMetaData( String JavaDoc poolName ) {
343         return (PoolMetaData) factories.get( poolName );
344     }
345 }
346
Popular Tags