KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > connection > ConnectionManager


1 /*
2  * CoadunationUtil: The coadunation util library.
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ConnectionManager.java
20  */

21
22 // package
23
package com.rift.coad.util.connection;
24
25 // java imports
26
import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.concurrent.ConcurrentHashMap JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 // logging import
33
import org.apache.log4j.Logger;
34
35
36 /**
37  * This object is responsible for managing the connections to various JNDI bound
38  * objects. This object assumes it is using a Coadunation based JNDI name
39  * service.
40  *
41  * @author Brett Chaldecott
42  */

43 public class ConnectionManager {
44     
45     // class singleton methods
46
private static ConnectionManager singleton = null;
47     private static Map JavaDoc singletonMap = new ConcurrentHashMap JavaDoc();
48     
49     // the logger reference
50
protected static Logger log =
51             Logger.getLogger(ConnectionManager.class.getName());
52     
53     
54     // private member variables
55
private Context JavaDoc context = null;
56     private Map JavaDoc lookupCache = new ConcurrentHashMap JavaDoc();
57     private Map JavaDoc synchKeyCache = new HashMap JavaDoc();
58     
59     /**
60      * Creates a new instance of ConnectionManager
61      */

62     private ConnectionManager() throws ConnectionException {
63         try {
64             context = new InitialContext JavaDoc();
65         } catch (Exception JavaDoc ex) {
66             log.error("Failed init the connection manager : " + ex.getMessage(),
67                     ex);
68         }
69     }
70     
71     
72     /**
73      * The contructor of the context manager.
74      *
75      * @param context The context that will be used to lookup names.
76      */

77     private ConnectionManager(Context JavaDoc context) {
78         this.context = context;
79     }
80     
81     
82     /**
83      * This method is responsible for returning a reference to the connection
84      * manager singleton instance.
85      *
86      * @return The reference to the connection manager.
87      * @exception ConnectionException
88      */

89     public synchronized static ConnectionManager getInstance() throws
90             ConnectionException {
91         if (singleton == null) {
92             singleton = new ConnectionManager();
93         }
94         return singleton;
95     }
96     
97     
98     /**
99      * This method returns the connection manager instance for the context.
100      *
101      * @return The reference to the connection manager.
102      * @param context The reference to the context object.
103      * @exception ConnectionException
104      */

105     public static ConnectionManager getInstance(Context JavaDoc context) throws
106             ConnectionException {
107         synchronized (context) {
108             if (!singletonMap.containsKey(context)) {
109                 singletonMap.put(context,new ConnectionManager());
110             }
111             return (ConnectionManager)singletonMap.get(context);
112         }
113     }
114     
115     
116     /**
117      * This method returns a connection to the the requested object.
118      *
119      * @param type The class type for the return result.
120      * @param jndiURL The jndi url of the object.
121      * @exception ConnectionException
122      * @exception java.lang.ClassCastException
123      */

124     public Object JavaDoc getConnection(Class JavaDoc type, String JavaDoc jndiURL) throws
125             ConnectionException, java.lang.ClassCastException JavaDoc {
126         Object JavaDoc syncEntry = getSyncKey(jndiURL);
127         synchronized(syncEntry) {
128             Connection connection = (Connection)lookupCache.get(jndiURL);
129             if (connection == null) {
130                 if (RMIConnection.isRMIConnection(jndiURL)) {
131                     connection = new RMIConnection(context, jndiURL);
132                 } else {
133                     connection = new ProxyConnection(context, jndiURL);
134                 }
135                 lookupCache.put(jndiURL,connection);
136             }
137             return connection.getConnection(type);
138         }
139     }
140     
141     
142     /**
143      * This method returns the object to synchronize the connection on.
144      *
145      * @return The object to synchronize the connection on.
146      * @param name The name of the object to synchronize the object on.
147      */

148     private synchronized Object JavaDoc getSyncKey(String JavaDoc name) {
149         Object JavaDoc entry = synchKeyCache.get(name);
150         if (entry != null) {
151             return entry;
152         }
153         entry = new String JavaDoc(name);
154         synchKeyCache.put(name,entry);
155         return entry;
156     }
157 }
158
Popular Tags