KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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.rmi.RemoteException JavaDoc;
21
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.EJBObject JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import org.aopalliance.intercept.MethodInvocation;
27
28 import org.springframework.remoting.RemoteLookupFailureException;
29 import org.springframework.remoting.rmi.RmiClientInterceptorUtils;
30
31 /**
32  * <p>Basic invoker for a remote Stateless Session Bean.
33  * "Creates" a new EJB instance for each invocation.
34  *
35  * <p>See {@link org.springframework.jndi.JndiObjectLocator} for info on
36  * how to specify the JNDI location of the target EJB.
37  *
38  * <p>In a bean container, this class is normally best used as a singleton. However,
39  * if that bean container pre-instantiates singletons (as do the XML ApplicationContext
40  * variants) you may have a problem if the bean container is loaded before the EJB
41  * container loads the target EJB. That is because by default the JNDI lookup will be
42  * performed in the init method of this class and cached, but the EJB will not have been
43  * bound at the target location yet. The best solution is to set the lookupHomeOnStartup
44  * property to false, in which case the home will be fetched on first access to the EJB.
45  * (This flag is only true by default for backwards compatibility reasons).</p>
46  *
47  * <p>This invoker is typically used with an RMI business interface, which serves
48  * as super-interface of the EJB component interface. Alternatively, this invoker
49  * can also proxy a remote SLSB with a matching non-RMI business interface, i.e. an
50  * interface that mirrors the EJB business methods but does not declare RemoteExceptions.
51  * In the latter case, RemoteExceptions thrown by the EJB stub will automatically get
52  * converted to Spring's unchecked RemoteAccessException.
53  *
54  * @author Rod Johnson
55  * @author Juergen Hoeller
56  * @since 09.05.2003
57  * @see org.springframework.remoting.RemoteAccessException
58  * @see AbstractSlsbInvokerInterceptor#setLookupHomeOnStartup
59  * @see AbstractSlsbInvokerInterceptor#setCacheHome
60  * @see AbstractRemoteSlsbInvokerInterceptor#setRefreshHomeOnConnectFailure
61  */

62 public class SimpleRemoteSlsbInvokerInterceptor extends AbstractRemoteSlsbInvokerInterceptor {
63     
64     /**
65      * This implementation "creates" a new EJB instance for each invocation.
66      * Can be overridden for custom invocation strategies.
67      * <p>Alternatively, override getSessionBeanInstance and
68      * releaseSessionBeanInstance to change EJB instance creation,
69      * for example to hold a single shared EJB instance.
70      */

71     protected Object JavaDoc doInvoke(MethodInvocation invocation) throws Throwable JavaDoc {
72         EJBObject JavaDoc ejb = null;
73         try {
74             ejb = getSessionBeanInstance();
75             return RmiClientInterceptorUtils.doInvoke(invocation, ejb);
76         }
77         catch (NamingException JavaDoc ex) {
78             throw new RemoteLookupFailureException("Failed to locate remote EJB [" + getJndiName() + "]", ex);
79         }
80         catch (InvocationTargetException JavaDoc ex) {
81             Throwable JavaDoc targetEx = ex.getTargetException();
82             if (targetEx instanceof RemoteException JavaDoc) {
83                 RemoteException JavaDoc rex = (RemoteException JavaDoc) targetEx;
84                 throw RmiClientInterceptorUtils.convertRmiAccessException(
85                     invocation.getMethod(), rex, isConnectFailure(rex), getJndiName());
86             }
87             else if (targetEx instanceof CreateException JavaDoc) {
88                 throw RmiClientInterceptorUtils.convertRmiAccessException(
89                     invocation.getMethod(), targetEx, "Could not create remote EJB [" + getJndiName() + "]");
90             }
91             throw targetEx;
92         }
93         finally {
94             if (ejb != null) {
95                 releaseSessionBeanInstance(ejb);
96             }
97         }
98     }
99
100     /**
101      * Return an EJB instance to delegate the call to.
102      * Default implementation delegates to newSessionBeanInstance.
103      * @throws NamingException if thrown by JNDI
104      * @throws InvocationTargetException if thrown by the create method
105      * @see #newSessionBeanInstance
106      */

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

117     protected void releaseSessionBeanInstance(EJBObject JavaDoc ejb) {
118         removeSessionBeanInstance(ejb);
119     }
120
121 }
122
Popular Tags