KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > ejb > EJBInfo


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

18 package org.apache.beehive.controls.system.ejb;
19
20 import org.apache.beehive.controls.api.ControlException;
21
22 import javax.ejb.EJBLocalHome JavaDoc;
23 import javax.ejb.FinderException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27
28 /**
29  * The EJBInfo class is a support class that derives EJB information by
30  * reflecting on an EJB control interface. This is implemented by a
31  * static inner class to make this functionality accesible in both static
32  * and non-static contexts
33  */

34 public class EJBInfo
35 {
36     public String JavaDoc _refName;
37     public Class JavaDoc _homeInterface;
38     public Class JavaDoc _beanInterface;
39     public String JavaDoc _beanType;
40     public boolean _isLocal;
41
42     public String JavaDoc toString()
43     {
44         return "{refname=" + _refName + " home=" + _homeInterface.getName() + " remote=" + _beanInterface.getName() +
45                " type=" + _beanType + " local=" + _isLocal + "}";
46     }
47
48     /**
49      * Derives bean attributes from the control interface
50      */

51     public EJBInfo(Class JavaDoc controlInterface)
52     {
53         _refName = getEJBRefName( controlInterface );
54
55         Class JavaDoc localHome = null;
56         Class JavaDoc localBean = null;
57         Class JavaDoc remoteHome = null;
58         Class JavaDoc remoteBean = null;
59
60         //
61
// To identify the identify home and bean interfaces, we
62
// must reflect the interface hierarchy of the provided
63
// class.
64
//
65
Vector JavaDoc checkList = new Vector JavaDoc();
66         Class JavaDoc [] subintfs = controlInterface.getInterfaces();
67         for (int i = 0; i < subintfs.length; i++)
68             checkList.add(subintfs[i]);
69
70         HashMap JavaDoc derivesFrom = new HashMap JavaDoc();
71         for (int i = 0; i < checkList.size(); i++)
72         {
73             Class JavaDoc intf = (Class JavaDoc)checkList.elementAt(i);
74
75             if (javax.ejb.EJBHome JavaDoc.class.isAssignableFrom(intf))
76                 remoteHome = intf;
77             else if (javax.ejb.EJBLocalHome JavaDoc.class.isAssignableFrom(intf))
78                 localHome = intf;
79             else if (javax.ejb.EJBObject JavaDoc.class.isAssignableFrom(intf))
80                 remoteBean = intf;
81             else if (javax.ejb.EJBLocalObject JavaDoc.class.isAssignableFrom(intf))
82                 localBean = intf;
83             else
84             {
85                 //
86
// If none of the above, add any new subinterfaces to
87
// the search list.
88
//
89
subintfs = intf.getInterfaces();
90                 for (int j = 0; j < subintfs.length; j++)
91                 {
92                     if (!checkList.contains(subintfs[j]))
93                     {
94                         checkList.add(subintfs[j]);
95                         derivesFrom.put(subintfs[j], intf);
96                     }
97                 }
98             }
99         }
100
101         //
102
// From the located methods, identify the home/bean interfaces.
103
//
104
if (remoteHome != null)
105         {
106             if (localHome != null)
107             {
108                 throw new ControlException(controlInterface +
109                                            " extends multiple EJB home interfaces.");
110             }
111             _homeInterface = getRoot(remoteHome, derivesFrom);
112         }
113         else if (localHome != null)
114         {
115             _homeInterface = getRoot(localHome, derivesFrom);
116         }
117         else
118         {
119             throw new ControlException(controlInterface +
120                 " does not extend the EJBHome or EJBLocalHome interfaces");
121         }
122
123         if (remoteBean != null)
124         {
125             if (localBean != null)
126             {
127                 throw new ControlException("Interface " + controlInterface +
128                             " extends multiple EJB object interfaces.");
129             }
130             _beanInterface = getRoot(remoteBean, derivesFrom);
131         }
132         else if (localBean != null)
133         {
134             _beanInterface = getRoot(localBean, derivesFrom);
135         }
136         else
137         {
138             throw new ControlException("Interface " + controlInterface +
139              " does not extend the EJBObject or EJBLocalObject interfaces");
140         }
141
142         // Identify the bean type via bean interface reflection
143
_beanType = "Session";
144         Method JavaDoc [] homeMethods = _homeInterface.getMethods();
145         for (int i = 0; i < homeMethods.length; i++)
146         {
147             if (isFinderMethod(homeMethods[i]))
148             {
149                 _beanType = "Entity";
150                 break;
151             }
152         }
153
154         _isLocal = (EJBLocalHome JavaDoc.class.isAssignableFrom(_homeInterface));
155     }
156
157     /**
158      * Unwinds the results of reflecting through the interface inheritance
159      * hierachy to find the original root class from a derived class
160      */

161     public Class JavaDoc getRoot(Class JavaDoc clazz, HashMap JavaDoc derivesFrom)
162     {
163         while (derivesFrom.containsKey(clazz))
164             clazz = (Class JavaDoc)derivesFrom.get(clazz);
165         return clazz;
166     }
167
168     static protected boolean isFinderMethod(Method JavaDoc m)
169     {
170         if (!m.getName().startsWith("find")) // EJB enforced pattern
171
return false;
172         return methodThrows(m, FinderException JavaDoc.class);
173     }
174
175     static protected boolean methodThrows(Method JavaDoc m, Class JavaDoc exceptionClass)
176     {
177         Class JavaDoc [] exceptions = m.getExceptionTypes();
178         for (int j = 0; j < exceptions.length; j++)
179             if (exceptionClass.isAssignableFrom(exceptions[j]))
180                 return true;
181         return false;
182     }
183
184     /**
185      * Computes a unique local ejb ref name based upon the JCX class name
186      */

187     static public String JavaDoc getEJBRefName(Class JavaDoc jcxClass)
188     {
189         return jcxClass.getName() + ".jcx";
190     }
191
192 }
193
194
Popular Tags