1 23 package com.sun.enterprise.deployment.annotation.handlers; 24 25 import java.lang.annotation.Annotation ; 26 import java.lang.reflect.AnnotatedElement ; 27 import java.lang.reflect.Method ; 28 29 import java.util.Set ; 30 import java.util.List ; 31 import java.util.LinkedList ; 32 import java.util.HashSet ; 33 import java.util.logging.Level ; 34 import javax.ejb.Timeout ; 35 import javax.ejb.EJBHome ; 36 import javax.ejb.EJBLocalHome ; 37 import javax.ejb.Remote ; 38 import javax.ejb.Local ; 39 import javax.ejb.RemoteHome ; 40 import javax.ejb.LocalHome ; 41 import javax.ejb.Stateless ; 42 43 import com.sun.enterprise.deployment.EjbBundleDescriptor; 44 import com.sun.enterprise.deployment.EjbDescriptor; 45 import com.sun.enterprise.deployment.EjbSessionDescriptor; 46 import com.sun.enterprise.deployment.MethodDescriptor; 47 48 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 49 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 50 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 51 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 52 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext; 53 import com.sun.enterprise.deployment.annotation.context.EjbContext; 54 55 70 abstract class AbstractEjbHandler extends AbstractHandler { 71 76 protected abstract String getAnnotatedName(Annotation annotation); 77 78 84 protected abstract boolean isValidEjbDescriptor(EjbDescriptor ejbDesc, 85 Annotation annotation); 86 87 93 protected abstract EjbDescriptor createEjbDescriptor(String elementName, 94 AnnotationInfo ainfo) throws AnnotationProcessorException; 95 96 104 protected abstract HandlerProcessingResult setEjbDescriptorInfo( 105 EjbDescriptor ejbDesc, AnnotationInfo ainfo) 106 throws AnnotationProcessorException; 107 108 117 public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo) 118 throws AnnotationProcessorException { 119 120 Class ejbClass = (Class ) ainfo.getAnnotatedElement(); 121 Annotation annotation = ainfo.getAnnotation(); 122 if (logger.isLoggable(Level.FINER)) { 123 logger.finer("@ process ejb annotation " + 124 annotation + " in " + ejbClass); 125 } 126 AnnotatedElementHandler aeHandler = 127 ainfo.getProcessingContext().getHandler(); 128 if (aeHandler != null && aeHandler instanceof EjbContext) { 129 EjbContext context = (EjbContext)aeHandler; 130 EjbDescriptor desc = context.getDescriptor(); 131 if (isValidEjbDescriptor(desc, annotation)) { 132 return getDefaultProcessedResult(); 133 } else { 134 log(Level.SEVERE, ainfo, 135 localStrings.getLocalString( 136 "enterprise.deployment.annotation.handlers.notcompsuperclass", 137 "The annotation symbol defined in super-class is not compatible with {0} ejb {1}.", 138 new Object [] { desc.getType(), desc.getName() })); 139 return getDefaultFailedResult(); 140 } 141 } else if (aeHandler == null || !(aeHandler instanceof EjbBundleContext)) { 142 return getInvalidAnnotatedElementHandlerResult( 143 ainfo.getProcessingContext().getHandler(), ainfo); 144 } 145 146 EjbBundleContext ctx = (EjbBundleContext)aeHandler; 147 148 if (logger.isLoggable(Level.FINE)) { 149 logger.fine("My context is " + ctx); 150 } 151 152 String elementName = getAnnotatedName(annotation); 153 if (elementName.length() == 0) { 154 elementName = ejbClass.getSimpleName(); 155 } 156 157 EjbBundleDescriptor currentBundle = ctx.getDescriptor(); 158 EjbDescriptor ejbDesc = null; 159 try { 160 ejbDesc = currentBundle.getEjbByName(elementName); 161 } catch(IllegalArgumentException ex) { 162 } 164 165 if (ejbDesc != null) { 166 if (logger.isLoggable(Level.FINE)) { 169 logger.fine("Overriding rules apply for " + ejbClass.getName()); 170 } 171 172 if (!isValidEjbDescriptor(ejbDesc, annotation)) { 174 log(Level.SEVERE, ainfo, 176 localStrings.getLocalString( 177 "enterprise.deployment.annotation.handlers.wrongejbtype", 178 "Wrong annotation symbol for ejb {1}", 179 new Object [] { ejbDesc })); 180 return getDefaultFailedResult(); 181 } 182 183 String descriptorEjbClass = ejbDesc.getEjbClassName(); 188 if( descriptorEjbClass == null ) { 189 ejbDesc.setEjbClassName(ejbClass.getName()); 190 ejbDesc.applyDefaultClassToLifecycleMethods(); 191 } else if( !descriptorEjbClass.equals(ejbClass.getName()) ) { 192 log(Level.SEVERE, ainfo, 193 localStrings.getLocalString( 194 "enterprise.deployment.annotation.handlers.ejbclsmismatch", 195 "", 196 new Object [] { descriptorEjbClass, elementName, 197 ejbClass.getName() })); 198 return getDefaultFailedResult(); 199 } 200 201 202 } else { 203 if (logger.isLoggable(Level.FINE)) { 204 logger.fine("Creating a new descriptor for " 205 + ejbClass.getName()); 206 } 207 208 ejbDesc = createEjbDescriptor(elementName, ainfo); 209 currentBundle.addEjb(ejbDesc); 210 if (logger.isLoggable(Level.FINE)) { 211 logger.fine("New " + 212 getAnnotationType().getName() + " bean " + elementName); 213 } 214 } 215 216 HandlerProcessingResult procResult = setEjbDescriptorInfo(ejbDesc, ainfo); 217 doTimedObjectProcessing(ejbClass, ejbDesc); 218 219 EjbContext ejbContext = new EjbContext(ejbDesc, ejbClass); 220 ctx.getProcessingContext().pushHandler(ejbContext); 222 223 return procResult; 224 } 225 226 232 private void doTimedObjectProcessing(Class ejbClass, 233 EjbDescriptor ejbDesc) { 234 235 241 MethodDescriptor timeoutMethodDesc = null; 242 Class nextClass = ejbClass; 243 while((nextClass != Object .class) && (nextClass != null) 244 && (timeoutMethodDesc == null) ) { 245 Method [] methods = nextClass.getDeclaredMethods(); 246 for(Method m : methods) { 247 if( (m.getAnnotation(Timeout .class) != null) ) { 248 timeoutMethodDesc = 249 new MethodDescriptor(m, MethodDescriptor.EJB_BEAN); 250 break; 251 } 252 } 253 nextClass = nextClass.getSuperclass(); 254 } 255 256 if( (timeoutMethodDesc == null) && 257 javax.ejb.TimedObject .class.isAssignableFrom(ejbClass) ) { 258 timeoutMethodDesc = new MethodDescriptor 261 ("ejbTimeout", "@Timeout method", 262 new String [] { "javax.ejb.Timer" }, 263 MethodDescriptor.EJB_BEAN); 264 } 265 266 if( timeoutMethodDesc != null ) { 267 ejbDesc.setEjbTimeoutMethod(timeoutMethodDesc); 268 } 269 270 return; 271 } 272 273 279 protected HandlerProcessingResult setBusinessAndHomeInterfaces( 280 EjbDescriptor ejbDesc, AnnotationInfo ainfo) 281 throws AnnotationProcessorException { 282 283 Set <String > localIntfNames = new HashSet <String >(); 284 Set <String > remoteIntfNames = new HashSet <String >(); 285 286 Class ejbClass = (Class )ainfo.getAnnotatedElement(); 287 288 294 Remote remoteBusAnn = (Remote ) ejbClass.getAnnotation(Remote .class); 295 boolean emptyRemoteBusAnn = false; 296 if( remoteBusAnn != null ) { 297 for(Class next : remoteBusAnn.value()) { 298 remoteIntfNames.add(next.getName()); 299 } 300 emptyRemoteBusAnn = remoteIntfNames.isEmpty(); 301 } 302 303 Local localBusAnn = (Local ) ejbClass.getAnnotation(Local .class); 304 if( localBusAnn != null ) { 305 for(Class next : localBusAnn.value()) { 306 localIntfNames.add(next.getName()); 307 } 308 } 309 310 List <Class > eligibleInterfaces = new LinkedList <Class >(); 311 for(Class next : ejbClass.getInterfaces()) { 312 if( !excludedFromImplementsClause(next) ) { 313 eligibleInterfaces.add(next); 314 } 315 } 316 317 int nonImplementsClauseBusinessInterfaceCount = 320 remoteIntfNames.size() + localIntfNames.size() + 321 ejbDesc.getRemoteBusinessClassNames().size() + 322 ejbDesc.getLocalBusinessClassNames().size(); 323 324 for(Class next : eligibleInterfaces) { 325 String nextIntfName = next.getName(); 326 327 if( remoteIntfNames.contains(nextIntfName) 328 || 329 localIntfNames.contains(nextIntfName) 330 || 331 ejbDesc.getRemoteBusinessClassNames().contains(nextIntfName) 332 || 333 ejbDesc.getLocalBusinessClassNames().contains(nextIntfName)){ 334 335 338 } else if( next.getAnnotation(Local .class) != null ) { 339 340 localIntfNames.add(nextIntfName); 341 342 } else if( next.getAnnotation(Remote .class) != null ) { 343 344 remoteIntfNames.add(nextIntfName); 345 346 } else { 347 348 if( nonImplementsClauseBusinessInterfaceCount == 0 ) { 349 350 if( emptyRemoteBusAnn ) { 354 remoteIntfNames.add(nextIntfName); 355 } else { 356 localIntfNames.add(nextIntfName); 357 } 358 359 } else { 360 361 366 } 367 } 368 } 369 370 if (localIntfNames.size() > 0) { 371 for(String next : localIntfNames) { 372 ejbDesc.addLocalBusinessClassName(next); 373 } 374 } 375 if (remoteIntfNames.size() > 0) { 376 for(String next : remoteIntfNames) { 377 ejbDesc.addRemoteBusinessClassName(next); 378 } 379 } 380 381 RemoteHome remoteHomeAnn = (RemoteHome ) 384 ejbClass.getAnnotation(RemoteHome .class); 385 386 if( remoteHomeAnn != null ) { 387 Class remoteHome = remoteHomeAnn.value(); 388 Class remoteIntf = getComponentIntfFromHome(remoteHome); 389 if( EJBHome .class.isAssignableFrom(remoteHome) && 390 (remoteIntf != null) ) { 391 392 ejbDesc.setHomeClassName(remoteHome.getName()); 393 ejbDesc.setRemoteClassName(remoteIntf.getName()); 394 395 } else { 396 log(Level.SEVERE, ainfo, 397 localStrings.getLocalString( 398 "enterprise.deployment.annotation.handlers.invalidremotehome", 399 "Encountered invalid @RemoteHome interface {0}.", 400 new Object [] { remoteHome })); 401 return getDefaultFailedResult(); 402 } 403 } 404 405 LocalHome localHomeAnn = (LocalHome ) 406 ejbClass.getAnnotation(LocalHome .class); 407 408 if( localHomeAnn != null ) { 409 Class localHome = localHomeAnn.value(); 410 Class localIntf = getComponentIntfFromHome(localHome); 411 if( EJBLocalHome .class.isAssignableFrom(localHome) && 412 (localIntf != null) ) { 413 414 ejbDesc.setLocalHomeClassName(localHome.getName()); 415 ejbDesc.setLocalClassName(localIntf.getName()); 416 417 } else { 418 log(Level.SEVERE, ainfo, 419 localStrings.getLocalString( 420 "enterprise.deployment.annotation.handlers.invalidlocalhome", 421 "Encountered invalid @LocalHome interface {0}.", 422 new Object [] { localHome })); 423 return getDefaultFailedResult(); 424 } 425 } 426 427 return getDefaultProcessedResult(); 428 } 429 430 private Class getComponentIntfFromHome(Class homeIntf) { 431 432 Class componentIntf = null; 433 434 Method [] methods = homeIntf.getMethods(); 435 for (Method m : methods) { 436 if (m.getName().startsWith("create")) { 437 componentIntf = m.getReturnType(); 438 break; 439 } 440 } 441 442 return componentIntf; 443 } 444 445 protected boolean excludedFromImplementsClause(Class intf) { 446 return ( (intf == java.io.Serializable .class) || 447 (intf == java.io.Externalizable .class) || 448 ( (intf.getPackage() != null) && 449 intf.getPackage().getName().equals("javax.ejb")) ); 450 } 451 452 protected void doDescriptionProcessing(String description, 453 EjbDescriptor ejbDescriptor) { 454 if( (description != null) && !description.equals("") ) { 459 ejbDescriptor.setDescription(description); 460 } 461 462 } 463 464 protected void doMappedNameProcessing(String mappedName, 465 EjbDescriptor ejbDesc) { 466 467 if( ejbDesc.getMappedName().equals("") ) { 470 if( !mappedName.equals("") ) { 471 ejbDesc.setMappedName(mappedName); 472 } 473 } 474 475 476 } 477 478 } 479 | Popular Tags |