KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ejb > access > LocalSlsbInvokerInterceptor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.ejb.access;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.EJBLocalObject JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import org.aopalliance.intercept.MethodInvocation;
27
28 /**
29  * <p>Invoker for a local Stateless Session Bean.
30  * Caches the home object. A local EJB home can never go stale.
31  *
32  * <p>See {@link org.springframework.jndi.JndiObjectLocator} for info on
33  * how to specify the JNDI location of the target EJB.
34  *
35  * <p>In a bean container, this class is normally best used as a singleton. However,
36  * if that bean container pre-instantiates singletons (as do the XML ApplicationContext
37  * variants) you may have a problem if the bean container is loaded before the EJB
38  * container loads the target EJB. That is because by default the JNDI lookup will be
39  * performed in the init method of this class and cached, but the EJB will not have been
40  * bound at the target location yet. The best solution is to set the lookupHomeOnStartup
41  * property to false, in which case the home will be fetched on first access to the EJB.
42  * (This flag is only true by default for backwards compatibility reasons).</p>
43  *
44  * @author Rod Johnson
45  * @author Juergen Hoeller
46  * @see AbstractSlsbInvokerInterceptor#setLookupHomeOnStartup
47  * @see AbstractSlsbInvokerInterceptor#setCacheHome
48  */

49 public class LocalSlsbInvokerInterceptor extends AbstractSlsbInvokerInterceptor {
50
51     /**
52      * This implementation "creates" a new EJB instance for each invocation.
53      * Can be overridden for custom invocation strategies.
54      * <p>Alternatively, override getSessionBeanInstance and
55      * releaseSessionBeanInstance to change EJB instance creation,
56      * for example to hold a single shared EJB instance.
57      */

58     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
59         EJBLocalObject JavaDoc ejb = null;
60         try {
61             ejb = getSessionBeanInstance();
62             Method JavaDoc method = invocation.getMethod();
63             if (method.getDeclaringClass().isInstance(ejb)) {
64                 // directly implemented
65
return method.invoke(ejb, invocation.getArguments());
66             }
67             else {
68                 // not directly implemented
69
Method JavaDoc ejbMethod = ejb.getClass().getMethod(method.getName(), method.getParameterTypes());
70                 return ejbMethod.invoke(ejb, invocation.getArguments());
71             }
72         }
73         catch (InvocationTargetException JavaDoc ex) {
74             Throwable JavaDoc targetEx = ex.getTargetException();
75             if (logger.isDebugEnabled()) {
76                 logger.debug("Method of local EJB [" + getJndiName() + "] threw exception", targetEx);
77             }
78             if (targetEx instanceof CreateException JavaDoc) {
79                 throw new EjbAccessException("Could not create local EJB [" + getJndiName() + "]", targetEx);
80             }
81             else {
82                 throw targetEx;
83             }
84         }
85         catch (NamingException JavaDoc ex) {
86             throw new EjbAccessException("Failed to locate local EJB [" + getJndiName() + "]", ex);
87         }
88         catch (IllegalAccessException JavaDoc ex) {
89             throw new EjbAccessException("Could not access method [" + invocation.getMethod().getName() +
90                 "] of local EJB [" + getJndiName() + "]", ex);
91         }
92         finally {
93             releaseSessionBeanInstance(ejb);
94         }
95     }
96
97     /**
98      * Return an EJB instance to delegate the call to.
99      * Default implementation delegates to newSessionBeanInstance.
100      * @throws NamingException if thrown by JNDI
101      * @throws InvocationTargetException if thrown by the create method
102      * @see #newSessionBeanInstance
103      */

104     protected EJBLocalObject JavaDoc getSessionBeanInstance() throws NamingException JavaDoc, InvocationTargetException JavaDoc {
105         return newSessionBeanInstance();
106     }
107
108     /**
109      * Release the given EJB instance.
110      * Default implementation delegates to removeSessionBeanInstance.
111      * @param ejb the EJB instance to release
112      * @see #removeSessionBeanInstance
113      */

114     protected void releaseSessionBeanInstance(EJBLocalObject JavaDoc ejb) {
115         removeSessionBeanInstance(ejb);
116     }
117
118     /**
119      * Return a new instance of the stateless session bean.
120      * Can be overridden to change the algorithm.
121      * @throws NamingException if thrown by JNDI
122      * @throws InvocationTargetException if thrown by the create method
123      * @see #create
124      */

125     protected EJBLocalObject JavaDoc newSessionBeanInstance() throws NamingException JavaDoc, InvocationTargetException JavaDoc {
126         if (logger.isDebugEnabled()) {
127             logger.debug("Trying to create reference to local EJB");
128         }
129
130         // call superclass to invoke the EJB create method on the cached home
131
Object JavaDoc ejbInstance = create();
132         if (!(ejbInstance instanceof EJBLocalObject JavaDoc)) {
133             throw new EjbAccessException("EJB instance [" + ejbInstance + "] is not a local SLSB");
134         }
135
136         if (logger.isDebugEnabled()) {
137             logger.debug("Obtained reference to local EJB: " + ejbInstance);
138         }
139         return (EJBLocalObject JavaDoc) ejbInstance;
140     }
141
142     /**
143      * Remove the given EJB instance.
144      * @param ejb the EJB instance to remove
145      * @see javax.ejb.EJBLocalObject#remove
146      */

147     protected void removeSessionBeanInstance(EJBLocalObject JavaDoc ejb) {
148         if (ejb != null) {
149             try {
150                 ejb.remove();
151             }
152             catch (Throwable JavaDoc ex) {
153                 logger.warn("Could not invoke 'remove' on local EJB proxy", ex);
154             }
155         }
156     }
157
158 }
159
Popular Tags