KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > bussinessproxy > target > EJBObjectFactory


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

16 package com.jdon.bussinessproxy.target;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20
21 import javax.ejb.EJBHome JavaDoc;
22 import javax.ejb.EJBLocalHome JavaDoc;
23 import javax.ejb.EJBLocalObject JavaDoc;
24 import javax.ejb.EJBObject JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26
27 import com.jdon.bussinessproxy.TargetMetaDef;
28 import com.jdon.bussinessproxy.meta.DistributedTargetMetaDef;
29 import com.jdon.bussinessproxy.meta.EJBTargetMetaDef;
30 import com.jdon.servicelocator.ServiceLocatorException;
31 import com.jdon.servicelocator.web.ServiceLocator;
32 import com.jdon.util.Debug;
33
34 /**
35  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
36  *
37  */

38 public class EJBObjectFactory {
39     private final static String JavaDoc module = EJBObjectFactory.class.getName();
40     
41     private ServiceLocator sl;
42     
43     
44     /**
45      * @param sl
46      */

47     public EJBObjectFactory(ServiceLocator sl) {
48         super();
49         this.sl = sl;
50     }
51
52     public Object JavaDoc create(TargetMetaDef targetMetaDef) throws Exception JavaDoc{
53         Object JavaDoc obj = null;
54         Debug.logVerbose("[JdonFramework] enter createObject in EJBTargetService " + targetMetaDef.getClassName(), module);
55         try {
56             if (targetMetaDef instanceof EJBTargetMetaDef){
57                 return createEJB2(targetMetaDef);
58             }else if (targetMetaDef instanceof DistributedTargetMetaDef){
59                 DistributedTargetMetaDef dtargetMetaDef = (DistributedTargetMetaDef)targetMetaDef;
60                 Debug.logVerbose("[JdonFramework] this is EJB3 JNDIName=" + dtargetMetaDef.getJndiName(), module);
61                 InitialContext JavaDoc ctx = new InitialContext JavaDoc();
62                 obj = ctx.lookup(dtargetMetaDef.getJndiName());
63             }
64         } catch (Exception JavaDoc ex) {
65             Debug.logError("[JdonFramework]create ejb error: " + ex, module);
66             throw new Exception JavaDoc(ex);
67         }
68         Debug.logVerbose("[JdonFramework] enter createObject in EJBTargetService " + obj.getClass().getName(), module);
69         return obj;
70     }
71
72    private Object JavaDoc createEJB2(TargetMetaDef targetMetaDef) throws Exception JavaDoc {
73         EJBTargetMetaDef eJBMetaDef = (EJBTargetMetaDef) targetMetaDef;
74         if (eJBMetaDef.isLocal()) {
75             return createEJBLocal(eJBMetaDef);
76         } else {
77             return createEJBRemote(eJBMetaDef);
78         }
79     }
80     
81     private Object JavaDoc createEJBLocal(EJBTargetMetaDef eJBMetaDef) throws Exception JavaDoc {
82         Debug.logVerbose("[JdonFramework] this is EJB2 local "
83                 + eJBMetaDef.getClassName(), module);
84         Object JavaDoc obj = null;
85         try {
86             Class JavaDoc[] params = null;
87             Object JavaDoc[] paramos = null;
88             EJBLocalHome JavaDoc home = (EJBLocalHome JavaDoc) sl.getLocalHome(eJBMetaDef
89                     .getJndiName());
90             Method JavaDoc createMethod = home.getClass().getMethod("create", params);
91             obj = (EJBLocalObject JavaDoc) createMethod.invoke(home, paramos);
92         } catch (SecurityException JavaDoc e) {
93             e.printStackTrace();
94             throw new Exception JavaDoc(e);
95         } catch (IllegalArgumentException JavaDoc e) {
96             e.printStackTrace();
97             throw new Exception JavaDoc(e);
98         } catch (ServiceLocatorException e) {
99             Debug.logError("[JdonFramework]locator error: " + e, module);
100             throw new Exception JavaDoc("JNID error:" + e);
101         } catch (NoSuchMethodException JavaDoc e) {
102             e.printStackTrace();
103             throw new Exception JavaDoc(e);
104         } catch (IllegalAccessException JavaDoc e) {
105             e.printStackTrace();
106             throw new Exception JavaDoc(e);
107         } catch (InvocationTargetException JavaDoc e) {
108             e.printStackTrace();
109             throw new Exception JavaDoc(e);
110         }
111         return obj;
112     }
113     
114     private Object JavaDoc createEJBRemote(EJBTargetMetaDef eJBMetaDef)
115             throws Exception JavaDoc {
116         Debug.logVerbose("[JdonFramework] this is EJB2 remote "
117                 + eJBMetaDef.getClassName(), module);
118         Object JavaDoc obj = null;
119         Class JavaDoc[] params = null;
120         Object JavaDoc[] paramos = null;
121         try {
122             EJBHome JavaDoc home = sl.getRemoteHome(eJBMetaDef.getJndiName(),
123                     eJBMetaDef.getHomeClass());
124             Method JavaDoc createMethod = home.getClass().getMethod("create", params);
125             obj = (EJBObject JavaDoc) createMethod.invoke(home, paramos);
126         } catch (SecurityException JavaDoc e) {
127             e.printStackTrace();
128             throw new Exception JavaDoc(e);
129         } catch (IllegalArgumentException JavaDoc e) {
130             e.printStackTrace();
131             throw new Exception JavaDoc(e);
132         } catch (ServiceLocatorException e) {
133             Debug.logError("[JdonFramework]locator error: " + e, module);
134             throw new Exception JavaDoc("JNID error:" + e);
135         } catch (NoSuchMethodException JavaDoc e) {
136             e.printStackTrace();
137             throw new Exception JavaDoc(e);
138         } catch (IllegalAccessException JavaDoc e) {
139             e.printStackTrace();
140             throw new Exception JavaDoc(e);
141         } catch (InvocationTargetException JavaDoc e) {
142             e.printStackTrace();
143             throw new Exception JavaDoc(e);
144         }
145         return obj;
146     }
147
148 }
149
Popular Tags