1 25 26 package org.objectweb.easybeans.deployment.annotations.helper.bean.session; 27 28 import static org.objectweb.asm.Opcodes.ACC_PUBLIC; 29 import static org.objectweb.easybeans.deployment.annotations.helper.bean.InheritanceInterfacesHelper.JAVA_LANG_OBJECT; 30 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 import org.objectweb.easybeans.deployment.annotations.JMethod; 35 import org.objectweb.easybeans.deployment.annotations.impl.JAnnotationResource; 36 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 37 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata; 38 44 public final class SessionBeanInterface { 45 46 49 private static final String SESSION_BEAN_INTERFACE = "javax/ejb/SessionBean"; 50 51 52 55 private static final JMethod SETSESSIONCONTEXT_METHOD = new JMethod(ACC_PUBLIC, "setSessionContext", 56 "(Ljavax/ejb/SessionContext;)V", null, new String [] {"javax/ejb/EJBException", "java/rmi/RemoteException"}); 57 58 59 62 private static final JMethod EJBREMOVE_METHOD = new JMethod(ACC_PUBLIC, "ejbRemove", 63 "()V", null, new String [] {"javax/ejb/EJBException", "java/rmi/RemoteException"}); 64 65 68 private static final JMethod EJBACTIVATE_METHOD = new JMethod(ACC_PUBLIC, "ejbActivate", 69 "()V", null, new String [] {"javax/ejb/EJBException", "java/rmi/RemoteException"}); 70 71 72 75 private static final JMethod EJBPASSIVATE_METHOD = new JMethod(ACC_PUBLIC, "ejbPassivate", 76 "()V", null, new String [] {"javax/ejb/EJBException", "java/rmi/RemoteException"}); 77 78 79 82 private SessionBeanInterface() { 83 } 84 85 89 public static void resolve(final ClassAnnotationMetadata sessionBean) { 90 List <String > allInterfaces = getAllInterfacesFromClass(sessionBean); 92 93 if (allInterfaces.contains(SESSION_BEAN_INTERFACE)) { 95 JAnnotationResource jAnnotationResource = new JAnnotationResource(); 97 98 MethodAnnotationMetadata setCtxMethod = getMethod(sessionBean, SETSESSIONCONTEXT_METHOD, false); 100 setCtxMethod.setJAnnotationResource(jAnnotationResource); 101 102 103 MethodAnnotationMetadata ejbRemoveMethod = getMethod(sessionBean, EJBREMOVE_METHOD, true); 105 ejbRemoveMethod.setPreDestroy(true); 106 if (!sessionBean.getPreDestroyMethodsMetadata().contains(ejbRemoveMethod)) { 107 sessionBean.addPreDestroyMethodMetadata(ejbRemoveMethod); 108 } 109 110 MethodAnnotationMetadata ejbActivateMethod = getMethod(sessionBean, EJBACTIVATE_METHOD, true); 112 ejbRemoveMethod.setPostActivate(true); 113 if (!sessionBean.getPostActivateMethodsMetadata().contains(ejbActivateMethod)) { 114 sessionBean.addPostActivateMethodMetadata(ejbActivateMethod); 115 } 116 117 MethodAnnotationMetadata ejbPassivateMethod = getMethod(sessionBean, EJBPASSIVATE_METHOD, true); 119 ejbRemoveMethod.setPrePassivate(true); 120 if (!sessionBean.getPrePassivateMethodsMetadata().contains(ejbPassivateMethod)) { 121 sessionBean.addPrePassivateMethodMetadata(ejbPassivateMethod); 122 } 123 124 } 125 126 127 } 128 129 136 private static MethodAnnotationMetadata getMethod(final ClassAnnotationMetadata sessionBean, final JMethod jMethod, final boolean inherited) { 137 MethodAnnotationMetadata method = sessionBean.getMethodAnnotationMetadata(jMethod); 138 if (method == null) { 139 throw new IllegalStateException ("Bean '" + sessionBean + "' implements " + SESSION_BEAN_INTERFACE 140 + " but no " + jMethod + " method found in metadata"); 141 } 142 if (method.isInherited() && !inherited) { 144 String superClassName = sessionBean.getSuperName(); 145 while (!JAVA_LANG_OBJECT.equals(superClassName)) { 147 ClassAnnotationMetadata superMetaData = sessionBean.getEjbJarAnnotationMetadata() 148 .getClassAnnotationMetadata(superClassName); 149 if (superMetaData != null) { 151 MethodAnnotationMetadata superMethod = superMetaData.getMethodAnnotationMetadata(jMethod); 152 if (superMethod != null && !superMethod.isInherited()) { 153 return superMethod; 154 } 155 superClassName = superMetaData.getSuperName(); 156 } else { 157 superClassName = JAVA_LANG_OBJECT; 159 } 160 } 161 162 } 163 164 return method; 165 } 166 167 168 173 public static List <String > getAllInterfacesFromClass(final ClassAnnotationMetadata sessionBean) { 174 List <String > allInterfaces = new ArrayList <String >(); 176 177 String className = sessionBean.getClassName(); 179 180 while (!JAVA_LANG_OBJECT.equals(className)) { 182 ClassAnnotationMetadata metaData = sessionBean.getEjbJarAnnotationMetadata() 183 .getClassAnnotationMetadata(className); 184 if (metaData != null) { 186 String [] interfaces = metaData.getInterfaces(); 187 if (interfaces != null) { 188 for (String itf : interfaces) { 189 allInterfaces.add(itf); 190 } 191 } 192 className = metaData.getSuperName(); 193 } else { 194 className = JAVA_LANG_OBJECT; 196 } 197 } 198 return allInterfaces; 199 } 200 } 201 | Popular Tags |