KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package org.apache.naming.factory;
20
21 import java.util.Hashtable 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 /**
31  * Object factory for Resources.
32  *
33  * @author Remy Maucherat
34  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
35  */

36
37 public class ResourceFactory
38     implements ObjectFactory JavaDoc {
39
40
41     // ----------------------------------------------------------- Constructors
42

43
44     // -------------------------------------------------------------- Constants
45

46
47     // ----------------------------------------------------- Instance Variables
48

49
50     // --------------------------------------------------------- Public Methods
51

52
53     // -------------------------------------------------- ObjectFactory Methods
54

55
56     /**
57      * Crete a new DataSource instance.
58      *
59      * @param obj The reference object describing the DataSource
60      */

61     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
62                                     Hashtable JavaDoc environment)
63         throws Exception JavaDoc {
64         
65         if (obj instanceof ResourceRef) {
66             Reference JavaDoc ref = (Reference JavaDoc) obj;
67             ObjectFactory JavaDoc factory = null;
68             RefAddr JavaDoc factoryRefAddr = ref.get(Constants.FACTORY);
69             if (factoryRefAddr != null) {
70                 // Using the specified factory
71
String JavaDoc factoryClassName =
72                     factoryRefAddr.getContent().toString();
73                 // Loading factory
74
ClassLoader JavaDoc tcl =
75                     Thread.currentThread().getContextClassLoader();
76                 Class JavaDoc factoryClass = null;
77                 if (tcl != null) {
78                     try {
79                         factoryClass = tcl.loadClass(factoryClassName);
80                     } catch(ClassNotFoundException JavaDoc e) {
81                         NamingException JavaDoc ex = new NamingException JavaDoc
82                             ("Could not load resource factory class");
83                         ex.initCause(e);
84                         throw ex;
85                     }
86                 } else {
87                     try {
88                         factoryClass = Class.forName(factoryClassName);
89                     } catch(ClassNotFoundException JavaDoc e) {
90                         NamingException JavaDoc ex = new NamingException JavaDoc
91                             ("Could not load resource factory class");
92                         ex.initCause(e);
93                         throw ex;
94                     }
95                 }
96                 if (factoryClass != null) {
97                     try {
98                         factory = (ObjectFactory JavaDoc) factoryClass.newInstance();
99                     } catch (Throwable JavaDoc t) {
100                         if (t instanceof NamingException JavaDoc)
101                             throw (NamingException JavaDoc) t;
102                         NamingException JavaDoc ex = new NamingException JavaDoc
103                             ("Could not create resource factory instance");
104                         ex.initCause(t);
105                         throw ex;
106                     }
107                 }
108             } else {
109                 if (ref.getClassName().equals("javax.sql.DataSource")) {
110                     String JavaDoc javaxSqlDataSourceFactoryClassName =
111                         System.getProperty("javax.sql.DataSource.Factory",
112                                            Constants.DBCP_DATASOURCE_FACTORY);
113                     try {
114                         factory = (ObjectFactory JavaDoc)
115                             Class.forName(javaxSqlDataSourceFactoryClassName)
116                             .newInstance();
117                     } catch (Throwable JavaDoc t) {
118                         NamingException JavaDoc ex = new NamingException JavaDoc
119                             ("Could not create resource factory instance");
120                         ex.initCause(t);
121                         throw ex;
122                     }
123                 } else if (ref.getClassName().equals("javax.mail.Session")) {
124                     String JavaDoc javaxMailSessionFactoryClassName =
125                         System.getProperty("javax.mail.Session.Factory",
126                                            "org.apache.naming.factory.MailSessionFactory");
127                     try {
128                         factory = (ObjectFactory JavaDoc)
129                             Class.forName(javaxMailSessionFactoryClassName)
130                             .newInstance();
131                     } catch(Throwable JavaDoc t) {
132                         NamingException JavaDoc ex = new NamingException JavaDoc
133                             ("Could not create resource factory instance");
134                         ex.initCause(t);
135                         throw ex;
136                     }
137                 }
138             }
139             if (factory != null) {
140                 return factory.getObjectInstance
141                     (obj, name, nameCtx, environment);
142             } else {
143                 throw new NamingException JavaDoc
144                     ("Cannot create resource instance");
145             }
146         }
147         
148         return null;
149
150     }
151
152
153 }
154
155
Popular Tags