1 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 ; 58 import java.lang.reflect.Method ; 59 60 61 62 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 home = null; 94 Class 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 intrface = null; 112 Class beanClass = null; 113 try { 114 intrface = loadClass(b.getLocal()); 115 beanClass = loadClass(b.getEjbClass()); 116 } catch ( OpenEJBException e ) { 117 return; 118 } 119 120 Method [] interfaceMethods = intrface.getMethods(); 121 Method [] beanClassMethods = intrface.getMethods(); 122 123 for(int i = 0; i < interfaceMethods.length; i++){ 124 if( interfaceMethods[i].getDeclaringClass() == EJBLocalObject .class) continue; 125 try{ 126 String name = interfaceMethods[i].getName(); 127 Class [] params = interfaceMethods[i].getParameterTypes(); 128 Method beanMethod = beanClass.getMethod( name, params ); 129 }catch(NoSuchMethodException nsme){ 130 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 } 143 } 144 145 } 146 147 148 149 private void check_remoteInterfaceMethods( Bean b ){ 150 151 Class intrface = null; 152 Class beanClass = null; 153 try { 154 intrface = loadClass(b.getRemote()); 155 beanClass = loadClass(b.getEjbClass()); 156 } catch ( OpenEJBException e ) { 157 return; 158 } 159 160 Method [] interfaceMethods = intrface.getMethods(); 161 Method [] beanClassMethods = intrface.getMethods(); 162 163 for(int i = 0; i < interfaceMethods.length; i++){ 164 if( interfaceMethods[i].getDeclaringClass() == javax.ejb.EJBObject .class) continue; 165 try{ 166 String name = interfaceMethods[i].getName(); 167 Class [] params = interfaceMethods[i].getParameterTypes(); 168 Method beanMethod = beanClass.getMethod( name, params ); 169 }catch(NoSuchMethodException nsme){ 170 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 } 183 } 184 } 185 186 private void check_homeInterfaceMethods( Bean b ){ 187 Class home = null; 188 Class 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 211 public boolean check_hasCreateMethod(Bean b, Class bean, Class home){ 212 213 Method [] 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 ValidationFailure failure = new ValidationFailure("no.home.create"); 225 failure.setDetails( b.getHome(), b.getRemote()); 226 failure.setBean( b ); 227 228 set.addFailure( failure ); 229 } 231 return hasCreateMethod; 233 } 234 235 243 public boolean check_createMethodsAreImplemented(Bean b, Class bean, Class home){ 244 boolean result = true; 245 246 Method [] homeMethods = home.getMethods(); 247 Method [] beanMethods = bean.getMethods(); 248 249 for (int i=0; i < homeMethods.length; i++){ 252 if (!homeMethods[i].getName().equals("create")) continue; 253 Method create = homeMethods[i]; 254 Method ejbCreate = null; 255 try{ 256 ejbCreate = bean.getMethod( "ejbCreate", create.getParameterTypes() ); 257 } catch ( NoSuchMethodException e ){ 258 result = false; 259 260 String paramString = getParameters( create ); 261 262 if ( b instanceof EntityBean ) { 263 EntityBean entity = (EntityBean)b; 264 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 } else { 274 ValidationFailure failure = new ValidationFailure("session.no.ejb.create"); 277 failure.setDetails( b.getEjbClass(), paramString); 278 failure.setBean( b ); 279 280 set.addFailure( failure ); 281 } 283 } 284 } 285 return result; 287 } 288 289 298 public boolean check_postCreateMethodsAreImplemented(Bean b, Class bean, Class home){ 299 boolean result = true; 300 301 if (b instanceof SessionBean) return true; 302 303 Method [] homeMethods = home.getMethods(); 304 Method [] beanMethods = bean.getMethods(); 305 306 for (int i=0; i < homeMethods.length; i++){ 309 if (!homeMethods[i].getName().equals("create")) continue; 310 Method create = homeMethods[i]; 311 Method ejbCreate = null; 312 try{ 313 ejbCreate = bean.getMethod( "ejbPostCreate", create.getParameterTypes() ); 314 } catch ( NoSuchMethodException e ){ 315 result = false; 316 317 String paramString = getParameters( create ); 318 319 ValidationFailure failure = new ValidationFailure("no.ejb.post.create"); 323 failure.setDetails( b.getEjbClass(), paramString); 324 failure.setBean( b ); 325 326 set.addFailure( failure ); 327 } 329 } 330 return result; 332 } 333 334 343 public boolean check_unusedCreateMethods(Bean b, Class bean, Class home){ 344 boolean result = true; 345 346 Method [] homeMethods = home.getMethods(); 347 Method [] beanMethods = bean.getMethods(); 348 349 for (int i=0; i < homeMethods.length; i++){ 350 if (!beanMethods[i].getName().equals("ejbCreate")) continue; 351 Method ejbCreate = beanMethods[i]; 352 Method create = null; 353 try{ 354 create = home.getMethod( "create", ejbCreate.getParameterTypes() ); 355 } catch ( NoSuchMethodException e ){ 356 result = false; 357 358 String paramString = getParameters( ejbCreate ); 359 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 } 369 } 370 return result; 372 } 373 374 387 private String getParameters(Method method){ 388 Class [] params = method.getParameterTypes(); 389 StringBuffer paramString = new StringBuffer (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 loadClass(String clazz) throws OpenEJBException { 404 ClassLoader cl = set.getClassLoader(); 405 try { 406 return cl.loadClass(clazz); 407 } catch (ClassNotFoundException cnfe) { 408 throw new OpenEJBException(SafeToolkit.messages.format("cl0007", clazz, set.getJarPath())); 409 } 410 } 411 } 412 413 414 | Popular Tags |