KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > factory > BeanFactory


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

17
18 package org.apache.naming.factory;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import javax.naming.Name JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.naming.Reference JavaDoc;
26 import javax.naming.RefAddr JavaDoc;
27 import javax.naming.spi.ObjectFactory JavaDoc;
28 import org.apache.naming.ResourceRef;
29
30 import java.beans.Introspector JavaDoc;
31 import java.beans.BeanInfo JavaDoc;
32 import java.beans.PropertyDescriptor JavaDoc;
33
34 import java.lang.reflect.Method JavaDoc;
35
36 /**
37  * Object factory for any Resource conforming to the JavaBean spec.
38  *
39  * <p>This factory can be configured in a <code>&lt;DefaultContext&gt;</code>
40  * or <code>&lt;Context&gt;</code> element in your <code>conf/server.xml</code>
41  * configuration file. An example of factory configuration is:</p>
42  * <pre>
43  * &lt;Resource name="jdbc/myDataSource" auth="SERVLET"
44  * type="oracle.jdbc.pool.OracleConnectionCacheImpl"/&gt;
45  * &lt;ResourceParams name="jdbc/myDataSource"&gt;
46  * &lt;parameter&gt;
47  * &lt;name&gt;factory&lt;/name&gt;
48  * &lt;value&gt;org.apache.naming.factory.BeanFactory&lt;/value&gt;
49  * &lt;/parameter&gt;
50  * &lt;parameter&gt;
51  * &lt;name&gt;driverType&lt;/name&gt;
52  * &lt;value&gt;thin&lt;/value&gt;
53  * &lt;/parameter&gt;
54  * &lt;parameter&gt;
55  * &lt;name&gt;serverName&lt;/name&gt;
56  * &lt;value&gt;hue&lt;/value&gt;
57  * &lt;/parameter&gt;
58  * &lt;parameter&gt;
59  * &lt;name&gt;networkProtocol&lt;/name&gt;
60  * &lt;value&gt;tcp&lt;/value&gt;
61  * &lt;/parameter&gt;
62  * &lt;parameter&gt;
63  * &lt;name&gt;databaseName&lt;/name&gt;
64  * &lt;value&gt;XXXX&lt;/value&gt;
65  * &lt;/parameter&gt;
66  * &lt;parameter&gt;
67  * &lt;name&gt;portNumber&lt;/name&gt;
68  * &lt;value&gt;NNNN&lt;/value&gt;
69  * &lt;/parameter&gt;
70  * &lt;parameter&gt;
71  * &lt;name&gt;user&lt;/name&gt;
72  * &lt;value&gt;XXXX&lt;/value&gt;
73  * &lt;/parameter&gt;
74  * &lt;parameter&gt;
75  * &lt;name&gt;password&lt;/name&gt;
76  * &lt;value&gt;XXXX&lt;/value&gt;
77  * &lt;/parameter&gt;
78  * &lt;parameter&gt;
79  * &lt;name&gt;maxLimit&lt;/name&gt;
80  * &lt;value&gt;5&lt;/value&gt;
81  * &lt;/parameter&gt;
82  * &lt;/ResourceParams&gt;
83  * </pre>
84  *
85  * @author <a HREF="mailto:aner at ncstech.com">Aner Perez</a>
86  */

87 public class BeanFactory
88     implements ObjectFactory JavaDoc {
89
90     // ----------------------------------------------------------- Constructors
91

92
93     // -------------------------------------------------------------- Constants
94

95
96     // ----------------------------------------------------- Instance Variables
97

98
99     // --------------------------------------------------------- Public Methods
100

101
102     // -------------------------------------------------- ObjectFactory Methods
103

104
105     /**
106      * Create a new Bean instance.
107      *
108      * @param obj The reference object describing the Bean
109      */

110     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
111                                     Hashtable JavaDoc environment)
112         throws NamingException JavaDoc {
113
114         if (obj instanceof ResourceRef) {
115
116             try {
117                 
118                 Reference JavaDoc ref = (Reference JavaDoc) obj;
119                 String JavaDoc beanClassName = ref.getClassName();
120                 Class JavaDoc beanClass = null;
121                 ClassLoader JavaDoc tcl =
122                     Thread.currentThread().getContextClassLoader();
123                 if (tcl != null) {
124                     try {
125                         beanClass = tcl.loadClass(beanClassName);
126                     } catch(ClassNotFoundException JavaDoc e) {
127                     }
128                 } else {
129                     try {
130                         beanClass = Class.forName(beanClassName);
131                     } catch(ClassNotFoundException JavaDoc e) {
132                         e.printStackTrace();
133                     }
134                 }
135                 if (beanClass == null) {
136                     throw new NamingException JavaDoc
137                         ("Class not found: " + beanClassName);
138                 }
139                 
140                 BeanInfo JavaDoc bi = Introspector.getBeanInfo(beanClass);
141                 PropertyDescriptor JavaDoc[] pda = bi.getPropertyDescriptors();
142                 
143                 Object JavaDoc bean = beanClass.newInstance();
144                 
145                 Enumeration JavaDoc e = ref.getAll();
146                 while (e.hasMoreElements()) {
147                     
148                     RefAddr JavaDoc ra = (RefAddr JavaDoc) e.nextElement();
149                     String JavaDoc propName = ra.getType();
150                     
151                     if (propName.equals(Constants.FACTORY) ||
152                         propName.equals("scope") || propName.equals("auth")) {
153                         continue;
154                     }
155                     
156                     String JavaDoc value = (String JavaDoc)ra.getContent();
157                     
158                     Object JavaDoc[] valueArray = new Object JavaDoc[1];
159                     
160                     int i = 0;
161                     for (i = 0; i<pda.length; i++) {
162
163                         if (pda[i].getName().equals(propName)) {
164
165                             Class JavaDoc propType = pda[i].getPropertyType();
166
167                             if (propType.equals(String JavaDoc.class)) {
168                                 valueArray[0] = value;
169                             } else if (propType.equals(Character JavaDoc.class)
170                                        || propType.equals(char.class)) {
171                                 valueArray[0] = new Character JavaDoc(value.charAt(0));
172                             } else if (propType.equals(Byte JavaDoc.class)
173                                        || propType.equals(byte.class)) {
174                                 valueArray[0] = new Byte JavaDoc(value);
175                             } else if (propType.equals(Short JavaDoc.class)
176                                        || propType.equals(short.class)) {
177                                 valueArray[0] = new Short JavaDoc(value);
178                             } else if (propType.equals(Integer JavaDoc.class)
179                                        || propType.equals(int.class)) {
180                                 valueArray[0] = new Integer JavaDoc(value);
181                             } else if (propType.equals(Long JavaDoc.class)
182                                        || propType.equals(long.class)) {
183                                 valueArray[0] = new Long JavaDoc(value);
184                             } else if (propType.equals(Float JavaDoc.class)
185                                        || propType.equals(float.class)) {
186                                 valueArray[0] = new Float JavaDoc(value);
187                             } else if (propType.equals(Double JavaDoc.class)
188                                        || propType.equals(double.class)) {
189                                 valueArray[0] = new Double JavaDoc(value);
190                             } else if (propType.equals(Boolean JavaDoc.class)
191                                        || propType.equals(boolean.class)) {
192                                 valueArray[0] = new Boolean JavaDoc(value);
193                             } else {
194                                 throw new NamingException JavaDoc
195                                     ("String conversion for property type '"
196                                      + propType.getName() + "' not available");
197                             }
198                             
199                             Method JavaDoc setProp = pda[i].getWriteMethod();
200                             if (setProp != null) {
201                                 setProp.invoke(bean, valueArray);
202                             } else {
203                                 throw new NamingException JavaDoc
204                                     ("Write not allowed for property: "
205                                      + propName);
206                             }
207
208                             break;
209
210                         }
211
212                     }
213
214                     if (i == pda.length) {
215                         throw new NamingException JavaDoc
216                             ("No set method found for property: " + propName);
217                     }
218
219                 }
220
221                 return bean;
222
223             } catch (java.beans.IntrospectionException JavaDoc ie) {
224                 NamingException JavaDoc ne = new NamingException JavaDoc(ie.getMessage());
225                 ne.setRootCause(ie);
226                 throw ne;
227             } catch (java.lang.IllegalAccessException JavaDoc iae) {
228                 NamingException JavaDoc ne = new NamingException JavaDoc(iae.getMessage());
229                 ne.setRootCause(iae);
230                 throw ne;
231             } catch (java.lang.InstantiationException JavaDoc ie2) {
232                 NamingException JavaDoc ne = new NamingException JavaDoc(ie2.getMessage());
233                 ne.setRootCause(ie2);
234                 throw ne;
235             } catch (java.lang.reflect.InvocationTargetException JavaDoc ite) {
236                 NamingException JavaDoc ne = new NamingException JavaDoc(ite.getMessage());
237                 ne.setRootCause(ite);
238                 throw ne;
239             }
240
241         } else {
242             return null;
243         }
244
245     }
246 }
247
Popular Tags