1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.proxy.provider.remoting;
19
20 import org.apache.commons.proxy.ObjectProvider;
21 import org.apache.commons.proxy.ProxyUtils;
22 import org.apache.commons.proxy.exception.ObjectProviderException;
23
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import javax.rmi.PortableRemoteObject;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.util.Properties;
30
31 /**
32 * Provides a reference to a session bean by looking up the home object and calling (via reflection) the no-argument
33 * create() method. This will work for both local and remote session beans.
34 *
35 * @author James Carman
36 * @since 1.0
37 */
38 public class SessionBeanProvider implements ObjectProvider
39 {
40 //----------------------------------------------------------------------------------------------------------------------
41 // Fields
42 //----------------------------------------------------------------------------------------------------------------------
43 private final String jndiName;
44 private final Class homeInterface;
45 private final Properties properties;
46
47 //----------------------------------------------------------------------------------------------------------------------
48 // Constructors
49 //----------------------------------------------------------------------------------------------------------------------
50
51 public SessionBeanProvider( String jndiName, Class homeInterface )
52 {
53 this.jndiName = jndiName;
54 this.homeInterface = homeInterface;
55 this.properties = null;
56 }
57
58 public SessionBeanProvider( String jndiName, Class homeInterface, Properties properties )
59 {
60 this.jndiName = jndiName;
61 this.homeInterface = homeInterface;
62 this.properties = properties;
63 }
64
65 //----------------------------------------------------------------------------------------------------------------------
66 // ObjectProvider Implementation
67 //----------------------------------------------------------------------------------------------------------------------
68
69 public Object getObject()
70 {
71 try
72 {
73 final InitialContext initialContext = properties == null ? new InitialContext() :
74 new InitialContext( properties );
75 Object homeObject = PortableRemoteObject.narrow( initialContext.lookup( jndiName ), homeInterface );
76 final Method createMethod = homeObject.getClass().getMethod( "create", ProxyUtils.EMPTY_ARGUMENT_TYPES );
77 return createMethod.invoke( homeObject, ProxyUtils.EMPTY_ARGUMENTS );
78 }
79 catch( NoSuchMethodException e )
80 {
81 throw new ObjectProviderException(
82 "Unable to find no-arg create() method on home interface " + homeInterface.getName() + ".", e );
83 }
84 catch( IllegalAccessException e )
85 {
86 throw new ObjectProviderException(
87 "No-arg create() method on home interface " + homeInterface.getName() + " is not accessible.",
88 e ); // Should never happen!
89 }
90 catch( NamingException e )
91 {
92 throw new ObjectProviderException( "Unable to lookup EJB home object in JNDI.", e );
93 }
94 catch( InvocationTargetException e )
95 {
96 throw new ObjectProviderException(
97 "No-arg create() method on home interface " + homeInterface.getName() + " threw an exception.", e );
98 }
99 }
100 }
101