001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SessionBeanInterface.java 2057 2007-11-21 15:35:32Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.helper.bean.session;
025:
026: import static org.ow2.easybeans.asm.Opcodes.ACC_PUBLIC;
027: import static org.ow2.easybeans.deployment.annotations.helper.ResolverHelper.getAllInterfacesFromClass;
028: import static org.ow2.easybeans.deployment.annotations.helper.ResolverHelper.getMethod;
029:
030: import java.util.List;
031:
032: import org.ow2.easybeans.deployment.annotations.JMethod;
033: import org.ow2.easybeans.deployment.annotations.impl.JAnnotationResource;
034: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
035: import org.ow2.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
036:
037: /**
038: * This class analyze interfaces of the session bean. If the session bean
039: * implements javax.ejb.SessionBean interface, add lifecycle callbacks and add
040: * resource injection for setSessionContext method.
041: * @author Florent Benoit
042: */
043: public final class SessionBeanInterface {
044:
045: /**
046: * SessionBean interface.
047: */
048: private static final String SESSION_BEAN_INTERFACE = "javax/ejb/SessionBean";
049:
050: /**
051: * setSessionContext() method.
052: */
053: private static final JMethod SETSESSIONCONTEXT_METHOD = new JMethod(
054: ACC_PUBLIC, "setSessionContext",
055: "(Ljavax/ejb/SessionContext;)V", null, new String[] {
056: "javax/ejb/EJBException",
057: "java/rmi/RemoteException" });
058:
059: /**
060: * ejbRemove() method.
061: */
062: private static final JMethod EJBREMOVE_METHOD = new JMethod(
063: ACC_PUBLIC, "ejbRemove", "()V", null, new String[] {
064: "javax/ejb/EJBException",
065: "java/rmi/RemoteException" });
066:
067: /**
068: * ejbActivate() method.
069: */
070: private static final JMethod EJBACTIVATE_METHOD = new JMethod(
071: ACC_PUBLIC, "ejbActivate", "()V", null, new String[] {
072: "javax/ejb/EJBException",
073: "java/rmi/RemoteException" });
074:
075: /**
076: * ejbPassivate() method.
077: */
078: private static final JMethod EJBPASSIVATE_METHOD = new JMethod(
079: ACC_PUBLIC, "ejbPassivate", "()V", null, new String[] {
080: "javax/ejb/EJBException",
081: "java/rmi/RemoteException" });
082:
083: /**
084: * Helper class, no public constructor.
085: */
086: private SessionBeanInterface() {
087: }
088:
089: /**
090: * Try to see if bean implements javax.ejb.SessionBean interface.
091: * @param sessionBean Session bean to analyze
092: */
093: public static void resolve(final ClassAnnotationMetadata sessionBean) {
094: // Make a list of interfaces
095: List<String> allInterfaces = getAllInterfacesFromClass(sessionBean);
096:
097: // if SESSION_BEAN_INTERFACE is contained in the list, add some metadata
098: if (allInterfaces.contains(SESSION_BEAN_INTERFACE)) {
099: // first add dependency injection for setSessionContext method.
100: JAnnotationResource jAnnotationResource = new JAnnotationResource();
101:
102: // add resource on setSessionContext method
103: MethodAnnotationMetadata setCtxMethod = getMethod(
104: sessionBean, SETSESSIONCONTEXT_METHOD, false,
105: SESSION_BEAN_INTERFACE);
106: setCtxMethod.setJAnnotationResource(jAnnotationResource);
107:
108: // ejbRemove() method
109: MethodAnnotationMetadata ejbRemoveMethod = getMethod(
110: sessionBean, EJBREMOVE_METHOD, true,
111: SESSION_BEAN_INTERFACE);
112: ejbRemoveMethod.setPreDestroy(true);
113: if (!sessionBean.getPreDestroyMethodsMetadata().contains(
114: ejbRemoveMethod)) {
115: sessionBean
116: .addPreDestroyMethodMetadata(ejbRemoveMethod);
117: }
118:
119: // ejbActivate() method
120: MethodAnnotationMetadata ejbActivateMethod = getMethod(
121: sessionBean, EJBACTIVATE_METHOD, true,
122: SESSION_BEAN_INTERFACE);
123: ejbRemoveMethod.setPostActivate(true);
124: if (!sessionBean.getPostActivateMethodsMetadata().contains(
125: ejbActivateMethod)) {
126: sessionBean
127: .addPostActivateMethodMetadata(ejbActivateMethod);
128: }
129:
130: // ejbPassivate() method
131: MethodAnnotationMetadata ejbPassivateMethod = getMethod(
132: sessionBean, EJBPASSIVATE_METHOD, true,
133: SESSION_BEAN_INTERFACE);
134: ejbRemoveMethod.setPrePassivate(true);
135: if (!sessionBean.getPrePassivateMethodsMetadata().contains(
136: ejbPassivateMethod)) {
137: sessionBean
138: .addPrePassivateMethodMetadata(ejbPassivateMethod);
139: }
140:
141: }
142:
143: }
144:
145: }
|