KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > ClientDataSourceFactory


1 /*
2
3    Derby - Class org.apache.derby.client.ClientDataSourceFactory
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.client;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Enumeration JavaDoc;
26
27 import javax.naming.RefAddr JavaDoc;
28 import javax.naming.Reference JavaDoc;
29 import org.apache.derby.jdbc.ClientBaseDataSource;
30
31 import org.apache.derby.jdbc.ClientConnectionPoolDataSource;
32 import org.apache.derby.jdbc.ClientDataSource;
33 import org.apache.derby.jdbc.ClientXADataSource;
34
35 /**
36  * The data source factory currrently for ClientDataSource only. This factory will support XA and pooling-enabled data
37  * sources in the future.
38  * <p/>
39  * This factory reconstructs a DERBY simple data source object when it is retrieved from JNDI. References are needed
40  * since many naming services don't have the ability to store Java objects in their serialized form. When a data source
41  * object is bound in this type of naming service the Reference for that object is actually stored by the JNDI
42  * implementation, not the data source object itself.
43  * <p/>
44  * A JNDI administrator is responsible for making sure that both the object factory and data source implementation
45  * classes provided by a JDBC driver vendor are accessible to the JNDI service provider at runtime.
46  * <p/>
47  * An object factory implements the javax.naming.spi.ObjectFactory interface. This interface contains a single method,
48  * getObjectInstance, which is called by a JNDI service provider to reconstruct an object when that object is retrieved
49  * from JNDI. A JDBC driver vendor should provide an object factory as part of their JDBC 2.0 product.
50  *
51  * @see ClientDataSource
52  */

53 public class ClientDataSourceFactory implements javax.naming.spi.ObjectFactory JavaDoc {
54
55     public ClientDataSourceFactory() {
56     }
57
58     /**
59      * Reconstructs a ClientDataSource object from a JNDI data source reference.
60      * <p/>
61      * The getObjectInstance() method is passed a reference that corresponds to the object being retrieved as its first
62      * parameter. The other parameters are optional in the case of JDBC data source objects. The object factory should
63      * use the information contained in the reference to reconstruct the data source. If for some reason, a data source
64      * object cannot be reconstructed from the reference, a value of null may be returned. This allows other object
65      * factories that may be registered in JNDI to be tried. If an exception is thrown then no other object factories
66      * are tried.
67      *
68      * @param refObj The possibly null object containing location or reference information that can be used in
69      * creating an object.
70      * @param name The name of this object relative to nameContext, or null if no name is specified.
71      * @param nameContext Context relative to which the name parameter is specified, or null if name is relative to the
72      * default initial context.
73      * @param environment Possibly null environment that is used in creating the object.
74      *
75      * @return object created; null if an object cannot be created
76      */

77     public Object JavaDoc getObjectInstance(Object JavaDoc refObj,
78                                     javax.naming.Name JavaDoc name,
79                                     javax.naming.Context JavaDoc nameContext,
80                                     java.util.Hashtable JavaDoc environment) throws java.lang.Exception JavaDoc {
81         javax.naming.Reference JavaDoc ref = (javax.naming.Reference JavaDoc) refObj;
82
83         // Create the proper data source object shell.
84
ClientBaseDataSource ds = null;
85         if (ref.getClassName().equals(ClientDataSource.className__)) {
86             ds = new ClientDataSource();
87         } else if (ref.getClassName().equals(ClientXADataSource.className__)) {
88             ds = new ClientXADataSource();
89         } else if (ref.getClassName().equals(ClientConnectionPoolDataSource.className__)) {
90             ds = new ClientConnectionPoolDataSource();
91         } else {
92             return null;
93         }
94
95         // Fill in the data source object shell with values from the jndi reference.
96
ClientDataSourceFactory.setBeanProperties(ds, ref);
97
98         return ds;
99     }
100     
101     /** Reflect lookup for Java bean method taking a single String arg */
102     private static final Class JavaDoc[] STRING_ARG = { "".getClass() };
103     /** Reflect lookup for Java bean method taking a single int arg */
104     private static final Class JavaDoc[] INT_ARG = { Integer.TYPE };
105     /** Reflect lookup for Java bean method taking a single boolean arg */
106     private static final Class JavaDoc[] BOOLEAN_ARG = { Boolean.TYPE };
107     /** Reflect lookup for Java bean method taking a single short arg */
108     private static final Class JavaDoc[] SHORT_ARG = { Short.TYPE };
109     
110     /*
111      * Set the Java bean properties for an object from its Reference. The
112      * Reference contains a set of StringRefAddr values with the key being the
113      * bean name and the value a String representation of the bean's value. This
114      * code looks for setXXX() method where the set method corresponds to the
115      * standard bean naming scheme and has a single parameter of type String,
116      * int, boolean or short.
117      */

118     private static void setBeanProperties(Object JavaDoc ds, Reference JavaDoc ref)
119             throws Exception JavaDoc {
120
121         for (Enumeration JavaDoc e = ref.getAll(); e.hasMoreElements();) {
122
123             RefAddr JavaDoc attribute = (RefAddr JavaDoc) e.nextElement();
124
125             String JavaDoc propertyName = attribute.getType();
126
127             String JavaDoc value = (String JavaDoc) attribute.getContent();
128
129             String JavaDoc methodName = "set"
130                     + propertyName.substring(0, 1).toUpperCase(
131                             java.util.Locale.ENGLISH)
132                     + propertyName.substring(1);
133
134             Method JavaDoc m;
135
136             Object JavaDoc argValue;
137             try {
138                 m = ds.getClass().getMethod(methodName, STRING_ARG);
139                 argValue = value;
140             } catch (NoSuchMethodException JavaDoc nsme) {
141                 try {
142                     m = ds.getClass().getMethod(methodName, INT_ARG);
143                     argValue = Integer.valueOf(value);
144                 } catch (NoSuchMethodException JavaDoc nsme2) {
145                     try {
146                         m = ds.getClass().getMethod(methodName, BOOLEAN_ARG);
147                         argValue = Boolean.valueOf(value);
148                     } catch (NoSuchMethodException JavaDoc nsme3) {
149                         m = ds.getClass().getMethod(methodName, SHORT_ARG);
150                         argValue = Short.valueOf(value);
151                     }
152                 }
153             }
154             m.invoke(ds, new Object JavaDoc[] { argValue });
155         }
156     }
157 }
158
Popular Tags