KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > datasources > InstanceKeyObjectFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp.datasources;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.naming.Context JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.RefAddr JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.spi.ObjectFactory JavaDoc;
34
35 /**
36  * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>s
37  * or <code>PerUserPoolDataSource</code>s
38  *
39  * @version $Revision: 1.9 $ $Date: 2004/02/28 21:51:59 $
40  */

41 abstract class InstanceKeyObjectFactory
42     implements ObjectFactory JavaDoc
43 {
44     private static Map JavaDoc instanceMap = new HashMap JavaDoc();
45
46     synchronized static String JavaDoc registerNewInstance(InstanceKeyDataSource ds) {
47         int max = 0;
48         Iterator JavaDoc i = instanceMap.keySet().iterator();
49         while (i.hasNext()) {
50             Object JavaDoc obj = i.next();
51             if (obj instanceof String JavaDoc)
52             {
53                 try {
54                     max = Math.max(max, Integer.valueOf((String JavaDoc)obj).intValue());
55                 }
56                 catch (NumberFormatException JavaDoc e) {
57                     // no sweat, ignore those keys
58
}
59             }
60         }
61         String JavaDoc instanceKey = String.valueOf(max + 1);
62         // put a placeholder here for now, so other instances will not
63
// take our key. we will replace with a pool when ready.
64
instanceMap.put(instanceKey, ds);
65         return instanceKey;
66     }
67     
68     static void removeInstance(String JavaDoc key)
69     {
70         instanceMap.remove(key);
71     }
72
73     /**
74      * Close all pools associated with this class.
75      */

76     public static void closeAll() throws Exception JavaDoc {
77         //Get iterator to loop over all instances of this datasource.
78
Iterator JavaDoc instanceIterator = instanceMap.entrySet().iterator();
79         while (instanceIterator.hasNext()) {
80             ((InstanceKeyDataSource)
81                 ((Map.Entry JavaDoc) instanceIterator.next()).getValue()).close();
82         }
83         instanceMap.clear();
84     }
85
86
87     /**
88      * implements ObjectFactory to create an instance of SharedPoolDataSource
89      * or PerUserPoolDataSource
90      */

91     public Object JavaDoc getObjectInstance(Object JavaDoc refObj, Name JavaDoc name,
92                                     Context JavaDoc context, Hashtable JavaDoc env)
93         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
94         // The spec says to return null if we can't create an instance
95
// of the reference
96
Object JavaDoc obj = null;
97         if (refObj instanceof Reference JavaDoc) {
98             Reference JavaDoc ref = (Reference JavaDoc) refObj;
99             if (isCorrectClass(ref.getClassName())) {
100                 RefAddr JavaDoc ra = ref.get("instanceKey");
101                 if (ra != null && ra.getContent() != null) {
102                     // object was bound to jndi via Referenceable api.
103
obj = instanceMap.get(ra.getContent());
104                 }
105                 else
106                 {
107                     // tomcat jndi creates a Reference out of server.xml
108
// <ResourceParam> configuration and passes it to an
109
// instance of the factory given in server.xml.
110
String JavaDoc key = null;
111                     if (name != null)
112                     {
113                         key = name.toString();
114                         obj = instanceMap.get(key);
115                     }
116                     if (obj == null)
117                     {
118                         InstanceKeyDataSource ds = getNewInstance(ref);
119                         setCommonProperties(ref, ds);
120                         obj = ds;
121                         if (key != null)
122                         {
123                             instanceMap.put(key, ds);
124                         }
125                     }
126                 }
127             }
128         }
129         return obj;
130     }
131
132     private void setCommonProperties(Reference JavaDoc ref,
133                                      InstanceKeyDataSource ikds)
134         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
135                     
136         RefAddr JavaDoc ra = ref.get("dataSourceName");
137         if (ra != null && ra.getContent() != null) {
138             ikds.setDataSourceName(ra.getContent().toString());
139         }
140                     
141         ra = ref.get("defaultAutoCommit");
142         if (ra != null && ra.getContent() != null) {
143             ikds.setDefaultAutoCommit(Boolean.valueOf(
144                 ra.getContent().toString()).booleanValue());
145         }
146         
147         ra = ref.get("defaultReadOnly");
148         if (ra != null && ra.getContent() != null) {
149             ikds.setDefaultReadOnly(Boolean.valueOf(
150                 ra.getContent().toString()).booleanValue());
151         }
152         
153         ra = ref.get("description");
154         if (ra != null && ra.getContent() != null) {
155             ikds.setDescription(ra.getContent().toString());
156         }
157         
158         ra = ref.get("jndiEnvironment");
159         if (ra != null && ra.getContent() != null) {
160             byte[] serialized = (byte[]) ra.getContent();
161             ikds.jndiEnvironment =
162                 (Properties JavaDoc) deserialize(serialized);
163         }
164         
165         ra = ref.get("loginTimeout");
166         if (ra != null && ra.getContent() != null) {
167             ikds.setLoginTimeout(
168                 Integer.parseInt(ra.getContent().toString()));
169         }
170         
171         ra = ref.get("testOnBorrow");
172         if (ra != null && ra.getContent() != null) {
173             ikds.setTestOnBorrow(
174                 Boolean.getBoolean(ra.getContent().toString()));
175         }
176         
177         ra = ref.get("testOnReturn");
178         if (ra != null && ra.getContent() != null) {
179             ikds.setTestOnReturn(Boolean.valueOf(
180                 ra.getContent().toString()).booleanValue());
181         }
182         
183         ra = ref.get("timeBetweenEvictionRunsMillis");
184         if (ra != null && ra.getContent() != null) {
185             ikds.setTimeBetweenEvictionRunsMillis(
186                 Integer.parseInt(ra.getContent().toString()));
187         }
188         
189         ra = ref.get("numTestsPerEvictionRun");
190         if (ra != null && ra.getContent() != null) {
191             ikds.setNumTestsPerEvictionRun(
192                 Integer.parseInt(ra.getContent().toString()));
193         }
194         
195         ra = ref.get("minEvictableIdleTimeMillis");
196         if (ra != null && ra.getContent() != null) {
197             ikds.setMinEvictableIdleTimeMillis(
198                 Integer.parseInt(ra.getContent().toString()));
199         }
200         
201         ra = ref.get("testWhileIdle");
202         if (ra != null && ra.getContent() != null) {
203             ikds.setTestWhileIdle(Boolean.valueOf(
204                 ra.getContent().toString()).booleanValue());
205         }
206                 
207         ra = ref.get("validationQuery");
208         if (ra != null && ra.getContent() != null) {
209             ikds.setValidationQuery(ra.getContent().toString());
210         }
211     }
212
213
214     /**
215      * @return true if and only if className is the value returned
216      * from getClass().getName().toString()
217      */

218     protected abstract boolean isCorrectClass(String JavaDoc className);
219
220     /**
221      * Creates an instance of the subclass and sets any properties
222      * contained in the Reference.
223      */

224     protected abstract InstanceKeyDataSource getNewInstance(Reference JavaDoc ref)
225         throws IOException JavaDoc, ClassNotFoundException JavaDoc;
226
227     /**
228      * used to set some properties saved within a Reference
229      */

230     protected static final Object JavaDoc deserialize(byte[] data)
231         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
232         ObjectInputStream JavaDoc in = null;
233         try {
234             in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(data));
235             return in.readObject();
236         } finally {
237             try {
238                 in.close();
239             } catch (IOException JavaDoc ex) {
240             }
241         }
242     }
243 }
244
245
Popular Tags