1 25 26 package org.objectweb.easybeans.deployment.xml.helper; 27 28 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.POST_CONSTRUCT; 29 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.PRE_DESTROY; 30 31 import java.util.List ; 32 33 import org.objectweb.easybeans.deployment.annotations.ClassType; 34 import org.objectweb.easybeans.deployment.annotations.impl.JCommonBean; 35 import org.objectweb.easybeans.deployment.annotations.impl.JInterceptors; 36 import org.objectweb.easybeans.deployment.annotations.impl.JLocal; 37 import org.objectweb.easybeans.deployment.annotations.impl.JRemote; 38 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 39 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata; 40 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata; 41 import org.objectweb.easybeans.deployment.xml.struct.AbsBean; 42 import org.objectweb.easybeans.deployment.xml.struct.EJB3; 43 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans; 44 import org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback; 45 import org.objectweb.easybeans.deployment.xml.struct.Session; 46 47 52 public final class MetadataMerge { 53 54 57 private EjbJarAnnotationMetadata ejbMetadata = null; 58 59 63 private MetadataMerge(final EjbJarAnnotationMetadata ejbMetadata) { 64 this.ejbMetadata = ejbMetadata; 65 } 66 67 72 public static void merge(final EjbJarAnnotationMetadata ejbMetadata) { 73 new MetadataMerge(ejbMetadata).resolve(); 74 } 75 76 79 private void resolve() { 80 81 EJB3 ejb3 = ejbMetadata.getEjb3(); 82 if (ejb3 != null) { 83 EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans(); 85 if (enterpriseBeans != null) { 86 87 List <Session> sessionList = enterpriseBeans.getSessionList(); 89 for (Session session : sessionList) { 90 String ejbName = session.getEjbName(); 92 String ejbClass = session.getEjbClass(); 93 ClassAnnotationMetadata classAnnotationMetadata = findClassForEjb(ejbName, ejbClass); 94 95 applySessionBean(session, classAnnotationMetadata); 97 } 98 } 99 } 100 } 101 102 107 private void applySessionBean(final Session sessionbean, final ClassAnnotationMetadata classAnnotationMetadata) { 108 applyCommonBean(sessionbean, classAnnotationMetadata); 109 110 String sessionType = sessionbean.getSessionType(); 112 if (sessionType == null && !classAnnotationMetadata.isSession()) { 113 throw new IllegalStateException ("Missing session-type for bean '" + sessionbean 114 + "' and no annotation in '" + classAnnotationMetadata + "'."); 115 } 116 if ("Stateless".equals(sessionType)) { 118 classAnnotationMetadata.setClassType(ClassType.STATELESS); 119 } else if ("Stateful".equals(sessionType)) { 120 classAnnotationMetadata.setClassType(ClassType.STATEFUL); 121 } 122 123 if (sessionbean.getBusinessLocalList().size() > 0) { 125 JLocal jLocal = classAnnotationMetadata.getLocalInterfaces(); 126 if (jLocal == null) { 127 jLocal = new JLocal(); 128 classAnnotationMetadata.setLocalInterfaces(jLocal); 129 } 130 for (String itf : sessionbean.getBusinessLocalList()) { 131 String encodedItf = encode(itf); 132 if (!jLocal.contains(encodedItf)) { 133 jLocal.addInterface(encodedItf); 134 } 135 } 136 } 137 138 if (sessionbean.getBusinessRemoteList().size() > 0) { 140 JRemote jRemote = classAnnotationMetadata.getRemoteInterfaces(); 141 if (jRemote == null) { 142 jRemote = new JRemote(); 143 classAnnotationMetadata.setRemoteInterfaces(jRemote); 144 } 145 for (String itf : sessionbean.getBusinessRemoteList()) { 146 String encodedItf = encode(itf); 147 if (!jRemote.contains(encodedItf)) { 148 jRemote.addInterface(encodedItf); 149 } 150 } 151 } 152 } 153 154 159 private void applyCommonBean(final AbsBean bean, final ClassAnnotationMetadata classAnnotationMetadata) { 160 if (classAnnotationMetadata.getJCommonBean() == null) { 162 classAnnotationMetadata.setJCommonBean(new JCommonBean()); 163 } 164 JCommonBean jcommonBean = classAnnotationMetadata.getJCommonBean(); 166 jcommonBean.setName(bean.getEjbName()); 167 168 applyLifeCycle(bean, bean.getPostConstructList(), classAnnotationMetadata, POST_CONSTRUCT); 171 applyLifeCycle(bean, bean.getPreDestroyList(), classAnnotationMetadata, PRE_DESTROY); 172 173 } 174 175 182 private void applyLifeCycle(final AbsBean bean, final List <LifeCycleCallback> lifecycleList, 183 final ClassAnnotationMetadata classAnnotationMetadata, final String type) { 184 for (LifeCycleCallback lifecycleCallback : lifecycleList) { 185 186 String encodedClassName = null; 187 188 if (lifecycleCallback.getClassName() == null) { 190 encodedClassName = classAnnotationMetadata.getClassName(); 191 } else { 192 encodedClassName = encode(lifecycleCallback.getClassName()); 193 } 194 JInterceptors interceptors = classAnnotationMetadata.getAnnotationInterceptors(); 195 if (interceptors == null) { 196 interceptors = new JInterceptors(); 197 classAnnotationMetadata.setAnnotationsInterceptors(interceptors); 198 } 199 if (!interceptors.contains(encodedClassName)) { 200 interceptors.addClass(encodedClassName); 201 } 202 ClassAnnotationMetadata lifeCycleMetadata = ejbMetadata.getClassAnnotationMetadata(encodedClassName); 204 if (lifeCycleMetadata == null) { 205 throw new IllegalStateException ("Cannot find metadata for class '" + encodedClassName 206 + "' which is in a lifecycle."); 207 } 208 209 for (MethodAnnotationMetadata methodMetadata : lifeCycleMetadata.getMethodAnnotationMetadataCollection()) { 211 if (methodMetadata.getMethodName().equals(lifecycleCallback.getMethod())) { 212 if (POST_CONSTRUCT.equals(type)) { 213 methodMetadata.setPostConstruct(true); 214 lifeCycleMetadata.addPostConstructMethodMetadata(methodMetadata); 215 } else if (PRE_DESTROY.equals(type)) { 216 methodMetadata.setPreDestroy(true); 217 lifeCycleMetadata.addPreDestroyMethodMetadata(methodMetadata); 218 } else { 219 throw new IllegalStateException ("No case for type '" + type + "'.."); 220 } 221 } 222 } 223 } 224 } 225 226 233 private ClassAnnotationMetadata findClassForEjb(final String ejbName, final String ejbClass) { 234 ClassAnnotationMetadata classAnnotationMetadata = null; 235 236 if (ejbClass != null) { 238 classAnnotationMetadata = ejbMetadata.getClassAnnotationMetadata(encode(ejbClass)); 239 if (classAnnotationMetadata != null) { 241 return classAnnotationMetadata; 242 } 243 } 244 245 for (ClassAnnotationMetadata tmpMetaData : ejbMetadata.getClassAnnotationMetadataCollection()) { 247 if (tmpMetaData.isBean()) { 248 JCommonBean bean = tmpMetaData.getJCommonBean(); 250 if (bean != null) { 251 if (ejbName != null && ejbName.equals(bean.getName())) { 253 return tmpMetaData; 254 } 255 } 256 } 257 } 258 throw new IllegalStateException ("No class with ejb-name '" + ejbName + "' or ejb-class '" + ejbClass 259 + "' was found in the EJB-JAR file '" + ejbMetadata); 260 } 261 262 268 private static String encode(final String className) { 269 return className.replace(".", "/"); 270 } 271 } 272 | Popular Tags |