KickJava   Java API By Example, From Geeks To Geeks.

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


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.log.Log;
36 import com.caucho.util.L10N;
37
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Configuration for a particular view.
42  */

43 public class EjbEntityHomeView extends EjbHomeView {
44   private static final Logger JavaDoc log = Log.open(EjbEntityHomeView.class);
45   private static final L10N L = new L10N(EjbEntityHomeView.class);
46
47   private boolean _hasFindByPrimaryKey;
48   
49   /**
50    * Creates a new entity bean configuration.
51    */

52   public EjbEntityHomeView(EjbBean bean, JClass apiClass, String JavaDoc prefix)
53     throws ConfigException
54   {
55     super(bean, apiClass, prefix);
56   }
57
58   protected boolean isCMP()
59   {
60     return ((EjbEntityBean) getBean()).isCMP();
61   }
62
63   /**
64    * Introspects the bean's methods.
65    */

66   protected void introspect()
67     throws ConfigException
68   {
69     super.introspect();
70
71     if (! _hasFindByPrimaryKey)
72       throw new ConfigException(L.l("{0}: expected 'findByPrimaryKey'. All entity homes must define findByPrimaryKey.",
73                     getApiClass().getName()));
74   }
75
76   /**
77    * Introspects an ejb method.
78    */

79   protected EjbMethod introspectEJBMethod(JMethod method)
80     throws ConfigException
81   {
82     String JavaDoc methodName = method.getName();
83     JClass []paramTypes = method.getParameterTypes();
84
85     if (methodName.startsWith("ejbCreate")) {
86       String JavaDoc apiMethodName = "c" + methodName.substring(4);
87
88       JMethod apiMethod = EjbBean.getMethod(getApiClass(),
89                         apiMethodName,
90                         paramTypes);
91
92       if (apiMethod == null) {
93     /*
94         validateNonFinalMethod(method.getName(), method.getParameterTypes());
95         if (! isPrimaryKeyClass(method.getReturnType()))
96           throw error(L.l("{0}: '{1}' must return '{2}'. ejbCreate methods must return the primary key.",
97                           method.getDeclaringClass().getName(),
98                           getFullMethodName(method),
99                           getClassName(primKeyClass)));
100         if (isCMP())
101           validateException(method, CreateException.class);
102     */

103     // XXX: should be fatal?
104
log.config(errorMissingMethod(getApiClass(), apiMethodName, method).getMessage());
105     return null;
106       }
107
108       String JavaDoc postMethodName = "ejbPost" + methodName.substring(3);
109       
110       JMethod postCreateMethod = EjbBean.getMethod(getImplClass(),
111                            postMethodName,
112                            method.getParameterTypes());
113
114       return new EjbEntityCreateMethod(this, apiMethod,
115                        method, postCreateMethod);
116     }
117     else if (methodName.startsWith("ejbFind")) {
118       /*
119       if (isCMP()) {
120     throw new ConfigException(L.l("{0}: '{1}' is not allowed. CMP beans can not define ejbFind method implementations.",
121                       method.getDeclaringClass().getName(),
122                       getFullMethodName(method)));
123       }
124       */

125       
126       if (methodName.equals("ejbFindByPrimaryKey")) {
127     _hasFindByPrimaryKey = true;
128     
129     JClass primKeyClass = ((EjbEntityBean) getBean()).getPrimKeyClass();
130       
131     if (paramTypes.length != 1 || ! paramTypes[0].equals(primKeyClass))
132       throw error(L.l("{0}: '{1}' expected as only argument of {2}. ejbFindByPrimaryKey must take the primary key as its only argument.",
133               method.getDeclaringClass().getName(),
134               primKeyClass.getName(),
135               methodName));
136       }
137       
138       String JavaDoc apiMethodName = "f" + methodName.substring(4);
139
140       JMethod apiMethod = EjbBean.getMethod(getApiClass(),
141                        apiMethodName,
142                        paramTypes);
143
144       if (apiMethod != null)
145     return new EjbEntityFindMethod(this, apiMethod, method);
146     }
147     else if (methodName.startsWith("ejbHome")) {
148       // XXX: test for "ejbHome"
149
String JavaDoc apiMethodName = (Character.toLowerCase(methodName.charAt(7)) +
150                   methodName.substring(8));
151
152       JMethod apiMethod = EjbBean.getMethod(getApiClass(),
153                        apiMethodName,
154                        paramTypes);
155
156       if (apiMethod == null) {
157     log.config(errorMissingMethod(getApiClass(), apiMethodName, method).getMessage());
158     return null;
159       }
160
161       validateImplMethod(method);
162
163       return new EjbMethod(this, apiMethod, method);
164     }
165     
166     return null;
167   }
168
169   /**
170    * Introspects an ejb method.
171    */

172   protected EjbMethod introspectApiMethod(JMethod apiMethod)
173     throws ConfigException
174   {
175     String JavaDoc methodName = apiMethod.getName();
176     JClass []paramTypes = apiMethod.getParameterTypes();
177
178     if (methodName.equals("findByPrimaryKey")) {
179       _hasFindByPrimaryKey = true;
180       
181       JClass primKeyClass = ((EjbEntityBean) getBean()).getPrimKeyClass();
182     
183       if (paramTypes.length != 1 || ! paramTypes[0].equals(primKeyClass))
184     throw error(L.l("{0}: `{1}' expected as only argument of {2}. findByPrimaryKey must take the primary key as its only argument.",
185             apiMethod.getDeclaringClass().getName(),
186             primKeyClass.getName(),
187             methodName));
188
189     
190       return new EjbEntityFindMethod(this, apiMethod);
191     }
192     else if (methodName.startsWith("find")) {
193       if (isCMP()) {
194     EjbMethodPattern pattern = findMethodPattern(apiMethod,
195                              getPrefix());
196
197     if (pattern == null)
198       throw error(L.l("{0}: '{1}' expects an ejb-ql query. All find methods need queries defined in the EJB deployment descriptor.",
199               apiMethod.getDeclaringClass().getName(),
200               getFullMethodName(apiMethod)));
201     
202     String JavaDoc query = pattern.getQuery();
203
204     EjbAmberFindMethod findMethod;
205     findMethod = new EjbAmberFindMethod(this, apiMethod, query,
206                         pattern.getQueryLocation());
207
208     findMethod.setQueryLoadsBean(pattern.getQueryLoadsBean());
209
210     return findMethod;
211       }
212
213       String JavaDoc name = methodName;
214       name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
215       
216       throw errorMissingMethod(getImplClass(), "ejb" + name, apiMethod);
217     }
218     else if (methodName.startsWith("create")) {
219       String JavaDoc name = apiMethod.getName();
220
221       name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
222       
223       throw errorMissingMethod(getImplClass(), "ejb" + name, apiMethod);
224     }
225     else if (methodName.startsWith("ejb")) {
226       throw new ConfigException(L.l("{0}: '{1}' forbidden. ejbXXX methods are reserved by the EJB specification.",
227                     apiMethod.getDeclaringClass().getName(),
228                     getFullMethodName(apiMethod)));
229     }
230     else if (methodName.startsWith("remove")) {
231       throw new ConfigException(L.l("{0}: '{1}' forbidden. removeXXX methods are reserved by the EJB specification.",
232                     apiMethod.getDeclaringClass().getName(),
233                     getFullMethodName(apiMethod)));
234     }
235     else {
236       String JavaDoc name = apiMethod.getName();
237
238       name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
239       
240       throw errorMissingMethod(getImplClass(), "ejbHome" + name, apiMethod);
241     }
242   }
243 }
244
Popular Tags