KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > rules > CheckMethods


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: CheckMethods.java 2082 2005-08-16 04:18:56Z dblevins $
44  */

45 package org.openejb.alt.config.rules;
46
47 import org.openejb.OpenEJBException;
48 import org.openejb.alt.config.Bean;
49 import org.openejb.alt.config.EjbSet;
50 import org.openejb.alt.config.EntityBean;
51 import org.openejb.alt.config.SessionBean;
52 import org.openejb.alt.config.ValidationFailure;
53 import org.openejb.alt.config.ValidationRule;
54 import org.openejb.alt.config.ValidationWarning;
55 import org.openejb.util.SafeToolkit;
56
57 import javax.ejb.EJBLocalObject JavaDoc;
58 import java.lang.reflect.Method JavaDoc;
59
60
61
62 /**
63  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
64  */

65 public class CheckMethods implements ValidationRule {
66
67
68
69     EjbSet set;
70
71
72
73     public void validate( EjbSet set ) {
74
75         this.set = set;
76
77         Bean[] beans = set.getBeans();
78         for ( int i=0; i < beans.length; i++ ) {
79             Bean b = beans[i];
80             if (b.getHome() != null){
81                 check_remoteInterfaceMethods( b );
82                 check_homeInterfaceMethods( b );
83             }
84             if (b.getLocalHome() != null){
85                 check_localInterfaceMethods( b );
86                 check_localHomeInterfaceMethods( b );
87             }
88         }
89     }
90
91
92     private void check_localHomeInterfaceMethods(Bean b) {
93         Class JavaDoc home = null;
94         Class JavaDoc bean = null;
95         try {
96             home = loadClass(b.getLocalHome());
97             bean = loadClass(b.getEjbClass());
98         } catch ( OpenEJBException e ) {
99             return;
100         }
101
102         if ( check_hasCreateMethod(b, bean, home) ){
103             check_createMethodsAreImplemented(b, bean, home);
104             check_postCreateMethodsAreImplemented(b, bean, home);
105         }
106
107         check_unusedCreateMethods(b, bean, home);
108     }
109
110     private void check_localInterfaceMethods(Bean b) {
111         Class JavaDoc intrface = null;
112         Class JavaDoc beanClass = null;
113         try {
114             intrface = loadClass(b.getLocal());
115             beanClass = loadClass(b.getEjbClass());
116         } catch ( OpenEJBException e ) {
117             return;
118         }
119
120         Method JavaDoc[] interfaceMethods = intrface.getMethods();
121         Method JavaDoc[] beanClassMethods = intrface.getMethods();
122
123         for(int i = 0; i < interfaceMethods.length; i++){
124             if( interfaceMethods[i].getDeclaringClass() == EJBLocalObject JavaDoc.class) continue;
125             try{
126                 String JavaDoc name = interfaceMethods[i].getName();
127                 Class JavaDoc[] params = interfaceMethods[i].getParameterTypes();
128                 Method JavaDoc beanMethod = beanClass.getMethod( name, params );
129             }catch(NoSuchMethodException JavaDoc nsme){
130                 // 0 - method name
131
// 1 - full method name
132
// 2 - remote|home|local|local-home
133
// 3 - interface name
134
// 4 - EJB Class name
135
ValidationFailure failure = new ValidationFailure("no.busines.method");
136                 failure.setDetails( interfaceMethods[i].getName(),interfaceMethods[i].toString(), "local", intrface.getName(), beanClass.getName());
137                 failure.setBean( b );
138
139                 set.addFailure( failure );
140
141                 //set.addFailure( new ValidationFailure("no.busines.method", interfaceMethods[i].toString(), "remote", intrface.getName(), beanClass.getName()));
142
}
143         }
144
145     }
146
147
148
149     private void check_remoteInterfaceMethods( Bean b ){
150
151         Class JavaDoc intrface = null;
152         Class JavaDoc beanClass = null;
153         try {
154             intrface = loadClass(b.getRemote());
155             beanClass = loadClass(b.getEjbClass());
156         } catch ( OpenEJBException e ) {
157             return;
158         }
159
160         Method JavaDoc[] interfaceMethods = intrface.getMethods();
161         Method JavaDoc[] beanClassMethods = intrface.getMethods();
162
163         for(int i = 0; i < interfaceMethods.length; i++){
164             if( interfaceMethods[i].getDeclaringClass() == javax.ejb.EJBObject JavaDoc.class) continue;
165             try{
166                 String JavaDoc name = interfaceMethods[i].getName();
167                 Class JavaDoc[] params = interfaceMethods[i].getParameterTypes();
168                 Method JavaDoc beanMethod = beanClass.getMethod( name, params );
169             }catch(NoSuchMethodException JavaDoc nsme){
170                 // 0 - method name
171
// 1 - full method name
172
// 2 - remote|home
173
// 3 - interface name
174
// 4 - EJB Class name
175
ValidationFailure failure = new ValidationFailure("no.busines.method");
176                 failure.setDetails( interfaceMethods[i].getName(),interfaceMethods[i].toString(), "remote", intrface.getName(), beanClass.getName());
177                 failure.setBean( b );
178
179                 set.addFailure( failure );
180
181                 //set.addFailure( new ValidationFailure("no.busines.method", interfaceMethods[i].toString(), "remote", intrface.getName(), beanClass.getName()));
182
}
183         }
184     }
185
186     private void check_homeInterfaceMethods( Bean b ){
187         Class JavaDoc home = null;
188         Class JavaDoc bean = null;
189         try {
190             home = loadClass(b.getHome());
191             bean = loadClass(b.getEjbClass());
192         } catch ( OpenEJBException e ) {
193             return;
194         }
195
196         if ( check_hasCreateMethod(b, bean, home) ){
197             check_createMethodsAreImplemented(b, bean, home);
198             check_postCreateMethodsAreImplemented(b, bean, home);
199         }
200
201         check_unusedCreateMethods(b, bean, home);
202     }
203
204     /**
205      * Must have at least one create method in the home interface.
206      *
207      * @param b
208      * @param bean
209      * @param home
210      */

211     public boolean check_hasCreateMethod(Bean b, Class JavaDoc bean, Class JavaDoc home){
212
213         Method JavaDoc[] homeMethods = home.getMethods();
214
215         boolean hasCreateMethod = false;
216
217         for (int i=0; i < homeMethods.length && !hasCreateMethod; i++){
218             hasCreateMethod = homeMethods[i].getName().equals("create");
219         }
220
221         if ( !hasCreateMethod ) {
222             // 1 - home interface
223
// 2 - remote interface
224
ValidationFailure failure = new ValidationFailure("no.home.create");
225             failure.setDetails( b.getHome(), b.getRemote());
226             failure.setBean( b );
227
228             set.addFailure( failure );
229             //set.addFailure( new ValidationFailure("no.home.create", b.getHome(), b.getRemote()));
230
}
231         //-------------------------------------------------------------
232
return hasCreateMethod;
233     }
234
235     /**
236      * Create methods must me implemented
237      *
238      * @param b
239      * @param bean
240      * @param home
241      * @return boolean
242      */

243     public boolean check_createMethodsAreImplemented(Bean b, Class JavaDoc bean, Class JavaDoc home){
244         boolean result = true;
245
246         Method JavaDoc[] homeMethods = home.getMethods();
247         Method JavaDoc[] beanMethods = bean.getMethods();
248
249         //-------------------------------------------------------------
250
// Create methods must me implemented
251
for (int i=0; i < homeMethods.length; i++){
252             if (!homeMethods[i].getName().equals("create")) continue;
253             Method JavaDoc create = homeMethods[i];
254             Method JavaDoc ejbCreate = null;
255             try{
256                 ejbCreate = bean.getMethod( "ejbCreate", create.getParameterTypes() );
257             } catch ( NoSuchMethodException JavaDoc e ){
258                 result = false;
259
260                 String JavaDoc paramString = getParameters( create );
261
262                 if ( b instanceof EntityBean ) {
263                     EntityBean entity = (EntityBean)b;
264                     // 1 - EJB Class name
265
// 2 - primary key class
266
// 3 - create params
267
ValidationFailure failure = new ValidationFailure("entity.no.ejb.create");
268                     failure.setDetails( b.getEjbClass(), entity.getPrimaryKey(), paramString);
269                     failure.setBean( b );
270
271                     set.addFailure( failure );
272                     //set.addFailure( new ValidationFailure("entity.no.ejb.create", b.getEjbClass(), entity.getPrimaryKey(), paramString));
273
} else {
274                     // 1 - EJB Class name
275
// 2 - create params
276
ValidationFailure failure = new ValidationFailure("session.no.ejb.create");
277                     failure.setDetails( b.getEjbClass(), paramString);
278                     failure.setBean( b );
279
280                     set.addFailure( failure );
281                     //set.addFailure( new ValidationFailure("session.no.ejb.create", b.getEjbClass(), paramString));
282
}
283             }
284         }
285         //-------------------------------------------------------------
286
return result;
287     }
288
289     /**
290      * Validate that the ejbPostCreate methods of entity beans are
291      * implemented in the bean class
292      *
293      * @param b
294      * @param bean
295      * @param home
296      * @return boolean
297      */

298     public boolean check_postCreateMethodsAreImplemented(Bean b, Class JavaDoc bean, Class JavaDoc home){
299         boolean result = true;
300
301         if (b instanceof SessionBean) return true;
302
303         Method JavaDoc[] homeMethods = home.getMethods();
304         Method JavaDoc[] beanMethods = bean.getMethods();
305
306         //-------------------------------------------------------------
307
// Create methods must me implemented
308
for (int i=0; i < homeMethods.length; i++){
309             if (!homeMethods[i].getName().equals("create")) continue;
310             Method JavaDoc create = homeMethods[i];
311             Method JavaDoc ejbCreate = null;
312             try{
313                 ejbCreate = bean.getMethod( "ejbPostCreate", create.getParameterTypes() );
314             } catch ( NoSuchMethodException JavaDoc e ){
315                 result = false;
316
317                 String JavaDoc paramString = getParameters( create );
318
319                 // 1 - EJB Class name
320
// 2 - primary key class
321
// 3 - create params
322
ValidationFailure failure = new ValidationFailure("no.ejb.post.create");
323                 failure.setDetails( b.getEjbClass(), paramString);
324                 failure.setBean( b );
325
326                 set.addFailure( failure );
327                 //set.addFailure( new ValidationFailure("no.ejb.post.create", b.getEjbClass(), paramString));
328
}
329         }
330         //-------------------------------------------------------------
331
return result;
332     }
333
334     /**
335      * Check for any create methods in the bean that
336      * aren't in the home interface as well
337      *
338      * @param b
339      * @param bean
340      * @param home
341      * @return boolean
342      */

343     public boolean check_unusedCreateMethods(Bean b, Class JavaDoc bean, Class JavaDoc home){
344         boolean result = true;
345
346         Method JavaDoc[] homeMethods = home.getMethods();
347         Method JavaDoc[] beanMethods = bean.getMethods();
348
349         for (int i=0; i < homeMethods.length; i++){
350             if (!beanMethods[i].getName().equals("ejbCreate")) continue;
351             Method JavaDoc ejbCreate = beanMethods[i];
352             Method JavaDoc create = null;
353             try{
354                 create = home.getMethod( "create", ejbCreate.getParameterTypes() );
355             } catch ( NoSuchMethodException JavaDoc e ){
356                 result = false;
357
358                 String JavaDoc paramString = getParameters( ejbCreate );
359                 // 1 - bean class
360
// 2 - create params
361
// 3 - home interface
362
ValidationWarning warning = new ValidationWarning("unused.ejb.create");
363                 warning.setDetails( b.getEjbClass(), paramString, home.getName());
364                 warning.setBean( b );
365
366                 set.addWarning( warning );
367                 //set.addWarning( new ValidationWarning("unused.ejb.create", b.getEjbClass(), paramString, home.getName()));
368
}
369         }
370         //-------------------------------------------------------------
371
return result;
372     }
373
374 /// public void check_findMethods(){
375
/// if(this.componentType == this.BMP_ENTITY ){
376
/// // CMP 1.1 beans do not define a find method in the bean class
377
/// String beanMethodName = "ejbF"+method.getName().substring(1);
378
/// beanMethod = beanClass.getMethod(beanMethodName,method.getParameterTypes());
379
/// }
380
/// }
381
///
382
/// public void check_homeMethods(){
383
/// String beanMethodName = "ejbHome"+method.getName().substring(0,1).toUpperCase()+method.getName().substring(1);
384
/// beanMethod = beanClass.getMethod(beanMethodName,method.getParameterTypes());
385
/// }
386

387     private String JavaDoc getParameters(Method JavaDoc method){
388         Class JavaDoc[] params = method.getParameterTypes();
389         StringBuffer JavaDoc paramString = new StringBuffer JavaDoc(512);
390
391         if (params.length > 0) {
392             paramString.append(params[0].getName());
393         }
394
395         for (int i=1; i < params.length; i++){
396             paramString.append(", ");
397             paramString.append(params[i]);
398         }
399
400         return paramString.toString();
401     }
402
403     private Class JavaDoc loadClass(String JavaDoc clazz) throws OpenEJBException {
404         ClassLoader JavaDoc cl = set.getClassLoader();
405         try {
406             return cl.loadClass(clazz);
407         } catch (ClassNotFoundException JavaDoc cnfe) {
408             throw new OpenEJBException(SafeToolkit.messages.format("cl0007", clazz, set.getJarPath()));
409         }
410     }
411 }
412
413
414
Popular Tags