KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbView


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.cfg;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.config.ConfigException;
35 import com.caucho.ejb.gen.BeanAssembler;
36 import com.caucho.ejb.gen.ViewClass;
37 import com.caucho.java.gen.CallChain;
38 import com.caucho.log.Log;
39 import com.caucho.util.L10N;
40
41 import java.rmi.RemoteException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Configuration for a particular view.
48  */

49 public class EjbView {
50   private static final Logger JavaDoc log = Log.open(EjbView.class);
51   private static final L10N L = new L10N(EjbView.class);
52
53   private EjbBean _bean;
54   
55   private JClass _apiClass;
56
57   private String JavaDoc _prefix;
58
59   private HashMap JavaDoc<String JavaDoc,EjbMethod> _methodMap =
60     new HashMap JavaDoc<String JavaDoc,EjbMethod>();
61
62   /**
63    * Creates a new entity bean configuration.
64    */

65   public EjbView(EjbBean bean, JClass apiClass, String JavaDoc prefix)
66     throws ConfigException
67   {
68     _bean = bean;
69     _apiClass = apiClass;
70     _prefix = prefix;
71   }
72
73   /**
74    * Returns the bean.
75    */

76   protected EjbBean getBean()
77   {
78     return _bean;
79   }
80
81   /**
82    * Returns the api class for the view.
83    */

84   protected JClass getApiClass()
85   {
86     return _apiClass;
87   }
88
89   /**
90    * Returns the implementation class for the view.
91    */

92   protected JClass getImplClass()
93   {
94     return _bean.getEJBClassWrapper();
95   }
96
97   /**
98    * Returns the prefix.
99    */

100   protected String JavaDoc getPrefix()
101   {
102     return _prefix;
103   }
104
105   /**
106    * Returns true for a local view.
107    */

108   protected boolean isLocal()
109   {
110     return "Local".equals(_prefix);
111   }
112
113   /**
114    * Returns the list of methods.
115    */

116   public ArrayList JavaDoc<EjbMethod> getMethods()
117   {
118     ArrayList JavaDoc<EjbMethod> methods = new ArrayList JavaDoc<EjbMethod>();
119     
120     methods.addAll(_methodMap.values());
121
122     return methods;
123   }
124
125   /**
126    * Introspects the bean's methods.
127    */

128   protected void introspect()
129     throws ConfigException
130   {
131     // find API methods matching an implementation method
132
JMethod []implMethods = EjbBean.getMethods(getImplClass());
133
134     for (int i = 0; i < implMethods.length; i++) {
135       JMethod method = implMethods[i];
136
137       EjbMethod ejbMethod = null;
138       
139       String JavaDoc name = method.getName();
140
141       if (JClass.OBJECT.getMethod(name, method.getParameterTypes()) != null &&
142       ! name.equals("toString")) {
143       }
144       else if (name.startsWith("ejb"))
145     ejbMethod = introspectEJBMethod(method);
146       else
147     ejbMethod = introspectBusinessMethod(method);
148
149       if (ejbMethod != null) {
150     _methodMap.put(getFullMethodName(ejbMethod.getApiMethod()), ejbMethod);
151       }
152     }
153     
154     // find API methods with no matching implementation method
155
JMethod []apiMethods = EjbBean.getMethods(_apiClass);
156
157     for (int i = 0; i < apiMethods.length; i++) {
158       JMethod method = apiMethods[i];
159
160       if (method.getDeclaringClass().getName().startsWith("javax.ejb"))
161     continue;
162       else if (JClass.OBJECT.getMethod(method.getName(),
163                        method.getParameterTypes()) != null) {
164     continue;
165       }
166       
167       EjbMethod ejbMethod = _methodMap.get(getFullMethodName(method));
168
169       if (ejbMethod != null)
170     continue;
171
172       ejbMethod = introspectApiMethod(method);
173
174       if (ejbMethod != null) {
175     _methodMap.put(getFullMethodName(ejbMethod.getApiMethod()), ejbMethod);
176
177     validateApiMethod(ejbMethod.getApiMethod());
178       }
179     }
180   }
181
182   /**
183    * Introspects an ejb method.
184    */

185   protected EjbMethod introspectEJBMethod(JMethod method)
186     throws ConfigException
187   {
188     return null;
189   }
190
191   /**
192    * Introspects a business method.
193    */

194   protected EjbMethod introspectBusinessMethod(JMethod implMethod)
195     throws ConfigException
196   {
197     JMethod apiMethod = EjbBean.getMethod(_apiClass,
198                       implMethod.getName(),
199                       implMethod.getParameterTypes());
200
201     if (apiMethod == null)
202       return null;
203
204     return createBusinessMethod(apiMethod, implMethod);
205   }
206
207   /**
208    * Creates a new business method.
209    */

210   protected EjbMethod createBusinessMethod(JMethod apiMethod,
211                        JMethod implMethod)
212     throws ConfigException
213   {
214     validateImplMethod(implMethod);
215     
216     return new EjbMethod(this, apiMethod, implMethod);
217   }
218
219   /**
220    * Validate impl method.
221    */

222   protected void validateImplMethod(JMethod implMethod)
223     throws ConfigException
224   {
225     if (! implMethod.isPublic()) {
226       throw error(L.l("{0}: `{1}' must be public. Business method implementations must be public.",
227                       implMethod.getDeclaringClass().getName(),
228                       getFullMethodName(implMethod)));
229     }
230     
231     if (implMethod.isStatic()) {
232       throw error(L.l("{0}: `{1}' must not be static. Business method implementations must not be static.",
233                       implMethod.getDeclaringClass().getName(),
234                       getFullMethodName(implMethod)));
235     }
236     
237     if (implMethod.isAbstract()) {
238       throw error(L.l("{0}: `{1}' must not be abstract. Business methods must be implemented.",
239                       implMethod.getDeclaringClass().getName(),
240                       getFullMethodName(implMethod)));
241     }
242   }
243
244   /**
245    * Introspects a method in the view api which does not exist in
246    * implementation bean.
247    */

248   protected EjbMethod introspectApiMethod(JMethod apiMethod)
249     throws ConfigException
250   {
251     /*
252     if (apiMethod.getName().startsWith("ejbPostCreate")) {
253       // XXX: properly checked?
254       return null;
255     }
256     else
257     */

258     if (apiMethod.getName().startsWith("ejb")) {
259       throw error(L.l("{0}: '{1}' must not start with 'ejb'. The EJB spec reserves all methods starting with ejb.",
260                       apiMethod.getDeclaringClass().getName(),
261                       getFullMethodName(apiMethod)));
262     }
263     else
264       throw errorMissingMethod(getImplClass(), apiMethod.getName(), apiMethod);
265   }
266
267   /**
268    * Validates an API method.
269    */

270   protected void validateApiMethod(JMethod apiMethod)
271     throws ConfigException
272   {
273     if ("Remote".equals(_prefix))
274       validateException(apiMethod, RemoteException JavaDoc.class);
275   }
276
277   protected ConfigException errorMissingMethod(JClass expectedClass,
278                            String JavaDoc expectedName,
279                            JMethod matchMethod)
280   {
281     return error(L.l("{0}: missing `{1}' method needed to match {2}.{3}",
282              expectedClass.getName(),
283              getFullMethodName(expectedName,
284                        matchMethod.getParameterTypes()),
285              getShortClassName(matchMethod.getDeclaringClass()),
286              getFullMethodName(matchMethod)));
287   }
288
289   /**
290    * Assembles the generator.
291    */

292   protected void assembleView(BeanAssembler assembler,
293                   String JavaDoc fullClassName)
294     throws ConfigException
295   {
296   }
297
298   /**
299    * Assembles the generator methods.
300    */

301   protected void assembleMethods(BeanAssembler assembler,
302                  ViewClass viewClass,
303                  String JavaDoc fullClassName)
304     throws ConfigException
305   {
306     ArrayList JavaDoc<EjbMethod> methods = getMethods();
307
308     for (int i = 0; i < methods.size(); i++) {
309       EjbMethod method = methods.get(i);
310
311       method.assembleBean(assembler, fullClassName);
312
313       viewClass.addMethod(method.assemble(viewClass, fullClassName));
314     }
315   }
316
317   /**
318    * Finds the matching method pattern.
319    */

320   protected EjbMethodPattern findMethodPattern(JMethod apiMethod,
321                            String JavaDoc prefix)
322   {
323     return _bean.getMethodPattern(apiMethod, prefix);
324   }
325
326   /**
327    * Returns the transaction chain.
328    */

329   protected CallChain getTransactionChain(CallChain callChain,
330                       JMethod apiMethod,
331                       String JavaDoc prefix)
332   {
333     return _bean.getTransactionChain(callChain, apiMethod, prefix);
334   }
335
336   /**
337    * Returns the transaction chain.
338    */

339   protected CallChain getSecurityChain(CallChain callChain,
340                        JMethod apiMethod,
341                        String JavaDoc prefix)
342   {
343     return _bean.getSecurityChain(callChain, apiMethod, prefix);
344   }
345
346   /**
347    * Validate the exceptions.
348    */

349   protected void validateException(JMethod method, Class JavaDoc exn)
350     throws ConfigException
351   {
352     _bean.validateException(method, exn);
353   }
354
355   /**
356    * Validate the exceptions.
357    */

358   protected void validateException(JMethod method, JClass exn)
359     throws ConfigException
360   {
361     _bean.validateException(method, exn);
362   }
363
364   /**
365    * Validate the exceptions.
366    */

367   protected void validateExceptions(JMethod method, JClass []exn)
368     throws ConfigException
369   {
370     _bean.validateExceptions(method, exn);
371   }
372
373   /**
374    * Returns a printable class name.
375    */

376   static String JavaDoc getClassName(Class JavaDoc cl)
377   {
378     if (cl != null)
379       return cl.getName();
380     else
381       return null;
382   }
383
384   static String JavaDoc getFullMethodName(JMethod method)
385   {
386     return EjbBean.getFullMethodName(method);
387   }
388
389   static String JavaDoc getFullMethodName(String JavaDoc methodName, JClass []paramTypes)
390   {
391     return EjbBean.getFullMethodName(methodName, paramTypes);
392   }
393
394   static String JavaDoc getShortClassName(JClass cl)
395   {
396     return EjbBean.getShortClassName(cl);
397   }
398   
399   /**
400    * Returns an error.
401    */

402   public ConfigException error(String JavaDoc msg)
403   {
404     return new ConfigException(msg);
405   }
406 }
407
Popular Tags