1 23 package com.sun.enterprise.deployment.annotation.handlers; 24 25 import java.lang.annotation.Annotation ; 26 import java.lang.annotation.ElementType ; 27 import java.lang.reflect.AnnotatedElement ; 28 import java.lang.reflect.Method ; 29 import java.lang.NoSuchMethodException ; 30 import java.util.logging.Level ; 31 32 import javax.ejb.Init ; 33 34 import com.sun.enterprise.deployment.EjbDescriptor; 35 import com.sun.enterprise.deployment.EjbInitInfo; 36 import com.sun.enterprise.deployment.EjbSessionDescriptor; 37 import com.sun.enterprise.deployment.MethodDescriptor; 38 import com.sun.enterprise.util.TypeUtil; 39 40 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 41 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 42 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 43 import com.sun.enterprise.deployment.annotation.context.EjbContext; 44 45 49 public class InitHandler extends AbstractAttributeHandler { 50 51 public InitHandler() { 52 } 53 54 57 public Class <? extends Annotation > getAnnotationType() { 58 return Init .class; 59 } 60 61 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 62 EjbContext[] ejbContexts) throws AnnotationProcessorException { 63 64 Init init = (Init ) ainfo.getAnnotation(); 65 66 for(EjbContext next : ejbContexts) { 67 68 EjbSessionDescriptor sessionDescriptor = 69 (EjbSessionDescriptor) next.getDescriptor(); 70 71 Method m = (Method ) ainfo.getAnnotatedElement(); 72 73 75 int numMatches = 0; 76 77 78 String adaptedCreateMethodName = init.value(); 79 80 try { 81 if( sessionDescriptor.isRemoteInterfacesSupported() ) { 82 addInitMethod(sessionDescriptor, m, 83 adaptedCreateMethodName, false); 84 numMatches++; 85 } 86 } catch(Exception e) { 87 } 88 89 try { 90 if( sessionDescriptor.isLocalInterfacesSupported() ) { 91 addInitMethod(sessionDescriptor, m, 92 adaptedCreateMethodName, true); 93 numMatches++; 94 } 95 } catch(Exception e) { 96 } 97 98 if( numMatches == 0 ) { 99 log(Level.SEVERE, ainfo, 100 localStrings.getLocalString( 101 "enterprise.deployment.annotation.handlers.notmatchcreate", 102 "Unable to find matching Home create method for Init method {0} on bean {1}.", 103 new Object [] { m, sessionDescriptor.getName() })); 104 return getDefaultFailedResult(); 105 } 106 } 107 108 return getDefaultProcessedResult(); 109 } 110 111 private void addInitMethod(EjbSessionDescriptor descriptor, 112 Method beanMethod, 113 String adaptedCreateMethodName, boolean local) 114 throws Exception { 115 116 String homeIntfName = local ? descriptor.getLocalHomeClassName() : 117 descriptor.getHomeClassName(); 118 119 ClassLoader cl = descriptor.getEjbBundleDescriptor().getClassLoader(); 120 121 Class homeIntf = cl.loadClass(homeIntfName); 122 123 Method createMethod = null; 124 if( (adaptedCreateMethodName == null) || 125 (adaptedCreateMethodName.equals("")) ) { 126 for(Method next : homeIntf.getMethods()) { 130 if( next.getName().startsWith("create") && 131 TypeUtil.sameParamTypes(next, beanMethod) ) { 132 createMethod = next; 133 break; 134 } 135 } 136 if( createMethod == null ) { 137 throw new NoSuchMethodException ("No matching adapted home " + 138 "method found for @Init " + 139 " method " + beanMethod); 140 } 141 } else { 142 createMethod = homeIntf.getMethod(adaptedCreateMethodName, 143 beanMethod.getParameterTypes()); 144 } 145 146 MethodDescriptor beanMethodDescriptor = 147 new MethodDescriptor(beanMethod, MethodDescriptor.EJB_BEAN); 148 149 MethodDescriptor createMethodDescriptor = 150 new MethodDescriptor(createMethod, 151 ( local ? 152 MethodDescriptor.EJB_HOME : 153 MethodDescriptor.EJB_LOCALHOME )); 154 155 EjbInitInfo initInfo = new EjbInitInfo(); 156 157 initInfo.setBeanMethod(beanMethodDescriptor); 158 initInfo.setCreateMethod(createMethodDescriptor); 159 160 descriptor.addInitMethod(initInfo); 161 162 } 163 164 169 public Class <? extends Annotation >[] getTypeDependencies() { 170 return getEjbAnnotationTypes(); 171 } 172 } 173 | Popular Tags |