KickJava   Java API By Example, From Geeks To Geeks.

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


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.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.naming.Reference JavaDoc;
27 import javax.naming.RefAddr JavaDoc;
28 import javax.naming.spi.ObjectFactory JavaDoc;
29 import org.apache.naming.EjbRef;
30
31 /**
32  * Object factory for EJBs.
33  *
34  * @author Remy Maucherat
35  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
36  */

37
38 public class EjbFactory
39     implements ObjectFactory JavaDoc {
40
41
42     // ----------------------------------------------------------- Constructors
43

44
45     // -------------------------------------------------------------- Constants
46

47
48     // ----------------------------------------------------- Instance Variables
49

50
51     // --------------------------------------------------------- Public Methods
52

53
54     // -------------------------------------------------- ObjectFactory Methods
55

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

62     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
63                                     Hashtable JavaDoc environment)
64         throws Exception JavaDoc {
65         
66         if (obj instanceof EjbRef) {
67             Reference JavaDoc ref = (Reference JavaDoc) obj;
68
69             // If ejb-link has been specified, resolving the link using JNDI
70
RefAddr JavaDoc linkRefAddr = ref.get(EjbRef.LINK);
71             if (linkRefAddr != null) {
72                 // Retrieving the EJB link
73
String JavaDoc ejbLink = linkRefAddr.getContent().toString();
74                 Object JavaDoc beanObj = (new InitialContext JavaDoc()).lookup(ejbLink);
75                 // Load home interface and checking if bean correctly
76
// implements specified home interface
77
/*
78                 String homeClassName = ref.getClassName();
79                 try {
80                     Class home = Class.forName(homeClassName);
81                     if (home.isInstance(beanObj)) {
82                         System.out.println("Bean of type "
83                                            + beanObj.getClass().getName()
84                                            + " implements home interface "
85                                            + home.getName());
86                     } else {
87                         System.out.println("Bean of type "
88                                            + beanObj.getClass().getName()
89                                            + " doesn't implement home interface "
90                                            + home.getName());
91                         throw new NamingException
92                             ("Bean of type " + beanObj.getClass().getName()
93                              + " doesn't implement home interface "
94                              + home.getName());
95                     }
96                 } catch (ClassNotFoundException e) {
97                     System.out.println("Couldn't load home interface "
98                                        + homeClassName);
99                 }
100                 */

101                 return beanObj;
102             }
103             
104             ObjectFactory JavaDoc factory = null;
105             RefAddr JavaDoc factoryRefAddr = ref.get(Constants.FACTORY);
106             if (factoryRefAddr != null) {
107                 // Using the specified factory
108
String JavaDoc factoryClassName =
109                     factoryRefAddr.getContent().toString();
110                 // Loading factory
111
ClassLoader JavaDoc tcl =
112                     Thread.currentThread().getContextClassLoader();
113                 Class JavaDoc factoryClass = null;
114                 if (tcl != null) {
115                     try {
116                         factoryClass = tcl.loadClass(factoryClassName);
117                     } catch(ClassNotFoundException JavaDoc e) {
118                         NamingException JavaDoc ex = new NamingException JavaDoc
119                             ("Could not load resource factory class");
120                         ex.initCause(e);
121                         throw ex;
122                     }
123                 } else {
124                     try {
125                         factoryClass = Class.forName(factoryClassName);
126                     } catch(ClassNotFoundException JavaDoc e) {
127                         NamingException JavaDoc ex = new NamingException JavaDoc
128                             ("Could not load resource factory class");
129                         ex.initCause(e);
130                         throw ex;
131                     }
132                 }
133                 if (factoryClass != null) {
134                     try {
135                         factory = (ObjectFactory JavaDoc) factoryClass.newInstance();
136                     } catch(Throwable JavaDoc t) {
137                     }
138                 }
139             } else {
140                 String JavaDoc javaxEjbFactoryClassName =
141                     System.getProperty("javax.ejb.Factory",
142                                        Constants.OPENEJB_EJB_FACTORY);
143                 try {
144                     factory = (ObjectFactory JavaDoc)
145                         Class.forName(javaxEjbFactoryClassName).newInstance();
146                 } catch(Throwable JavaDoc t) {
147                     if (t instanceof NamingException JavaDoc)
148                         throw (NamingException JavaDoc) t;
149                     NamingException JavaDoc ex = new NamingException JavaDoc
150                         ("Could not create resource factory instance");
151                     ex.initCause(t);
152                     throw ex;
153                 }
154             }
155
156             if (factory != null) {
157                 return factory.getObjectInstance
158                     (obj, name, nameCtx, environment);
159             } else {
160                 throw new NamingException JavaDoc
161                     ("Cannot create resource instance");
162             }
163
164         }
165
166         return null;
167
168     }
169
170
171 }
172
173
Popular Tags