KickJava   Java API By Example, From Geeks To Geeks.

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


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 cmp view.
42  */

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

52   public EjbCmpView(EjbEntityBean bean, JClass apiClass, String JavaDoc prefix)
53     throws ConfigException
54   {
55     super(bean, apiClass, prefix);
56
57     _entityBean = bean;
58   }
59
60   /**
61    * Introspects an ejb method.
62    */

63   protected EjbMethod introspectEJBMethod(JMethod method)
64     throws ConfigException
65   {
66     String JavaDoc methodName = method.getName();
67     JClass []paramTypes = method.getParameterTypes();
68
69     if (methodName.startsWith("ejbSelect") && method.isAbstract()) {
70       _entityBean.addStubMethod(method);
71
72       return null;
73     }
74     
75     return super.introspectEJBMethod(method);
76   }
77
78   /**
79    * Creates a new business method.
80    */

81   protected EjbMethod createBusinessMethod(JMethod apiMethod,
82                        JMethod implMethod)
83     throws ConfigException
84   {
85     String JavaDoc methodName = implMethod.getName();
86     JClass []paramTypes = implMethod.getParameterTypes();
87
88     if (methodName.startsWith("get") &&
89     methodName.length() > 3 &&
90     paramTypes.length == 0) {
91       String JavaDoc fieldName = toFieldName(methodName.substring(3));
92
93       CmpField field = _entityBean.getCmpField(fieldName);
94
95       if (field != null) {
96     validateCmpMethod(implMethod);
97     
98     if (isLocal() && apiMethod.getExceptionTypes().length != 0) {
99       throw new ConfigException(L.l("{0}: '{1}' must not throw {2}. Container managed fields and relations must not throw exceptions.",
100                     apiMethod.getDeclaringClass().getName(),
101                     EjbBean.getFullMethodName(apiMethod),
102                     apiMethod.getExceptionTypes()[0].getName()));
103     }
104
105     if (field.isId())
106       return new CmpIdGetter(this, apiMethod, implMethod);
107     else
108       return new CmpGetter(this, apiMethod, implMethod);
109       }
110       
111       CmrRelation rel = _entityBean.getRelation(fieldName);
112
113       if (rel != null) {
114     validateCmpMethod(implMethod);
115
116     rel.setHasGetter(true);
117
118     return rel.createGetter(this, apiMethod, implMethod);
119       }
120       
121       if (! implMethod.isAbstract()) {
122     validateCmpMethod(implMethod);
123
124     // return new CmpGetter(this, apiMethod, implMethod);
125

126     return new EjbMethod(this, apiMethod, implMethod);
127       }
128     }
129     else if (methodName.startsWith("get") &&
130     methodName.length() > 3 &&
131     paramTypes.length == 1) {
132       String JavaDoc fieldName = toFieldName(methodName.substring(3));
133       
134       CmrRelation rel = _entityBean.getRelation(fieldName);
135
136       if (rel instanceof CmrMap) {
137     CmrMap map = (CmrMap) rel;
138     
139     validateCmpMethod(implMethod);
140
141     rel.setHasGetter(true);
142
143     // return new EjbMapGetter(this, apiMethod, implMethod, map);
144
return new CmpGetter(this, apiMethod, implMethod);
145       }
146     }
147     else if (methodName.startsWith("set") &&
148          methodName.length() > 3 &&
149          paramTypes.length == 1) {
150       String JavaDoc fieldName = toFieldName(methodName.substring(3));
151
152       CmpField field = _entityBean.getCmpField(fieldName);
153       
154       if (field != null) {
155     validateCmpMethod(implMethod);
156     
157     if (isLocal() && apiMethod.getExceptionTypes().length != 0) {
158       throw new ConfigException(L.l("{0}: '{1}' must not throw {2}. Container managed fields and relations must not throw exceptions.",
159                     _entityBean.getEJBClass().getName(),
160                     EjbBean.getFullMethodName(apiMethod),
161                     apiMethod.getExceptionTypes()[0].getName()));
162     }
163
164     return new EjbMethod(this, apiMethod, implMethod);
165       }
166       
167       CmrRelation rel = _entityBean.getRelation(fieldName);
168
169       if (rel instanceof CmrOneToMany) {
170     validateCmpMethod(implMethod);
171
172     return new CmpCollectionSetter(this, apiMethod, implMethod);
173     // return new CmpRelationGetter(this, apiMethod, implMethod, rel);
174
}
175       else if (rel instanceof CmrManyToOne) {
176     validateCmpMethod(implMethod);
177     
178     CmrManyToOne manyToOne = (CmrManyToOne) rel;
179     
180     return new EjbManyToOneSetMethod(this, apiMethod, implMethod, manyToOne);
181       }
182       else if (rel instanceof CmrManyToMany) {
183     validateCmpMethod(implMethod);
184
185     return new CmpCollectionSetter(this, apiMethod, implMethod);
186     // return new CmpRelationGetter(this, apiMethod, implMethod, rel);
187
}
188       
189       if (! implMethod.isAbstract()) {
190     validateCmpMethod(implMethod);
191
192     return new EjbMethod(this, apiMethod, implMethod);
193       }
194       else
195     throw new ConfigException(L.l("{0}: abstract setter {1}.",
196                       implMethod.getDeclaringClass().getName(),
197                       getFullMethodName(implMethod)));
198     }
199       
200
201     return super.createBusinessMethod(apiMethod, implMethod);
202   }
203
204   protected String JavaDoc toFieldName(String JavaDoc name)
205   {
206     if (name.length() == 0)
207       return "";
208     else if (name.length() == 1)
209       return String.valueOf(Character.toLowerCase(name.charAt(0)));
210     else if (Character.isUpperCase(name.charAt(1)))
211       return name;
212     else
213       return Character.toLowerCase(name.charAt(0)) + name.substring(1);
214   }
215
216   /**
217    * Validate impl method.
218    */

219   protected void validateCmpMethod(JMethod implMethod)
220     throws ConfigException
221   {
222     if (! implMethod.isPublic()) {
223       throw error(L.l("{0}: `{1}' must be public. CMP method implementations must be public.",
224                       implMethod.getDeclaringClass().getName(),
225                       getFullMethodName(implMethod)));
226     }
227     
228     if (implMethod.isStatic()) {
229       throw error(L.l("{0}: `{1}' must not be static. CMP method implementations must not be static.",
230                       implMethod.getDeclaringClass().getName(),
231                       getFullMethodName(implMethod)));
232     }
233   }
234 }
235
Popular Tags