001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.provider.remoting;
019    
020    import org.apache.commons.proxy.ObjectProvider;
021    import org.apache.commons.proxy.ProxyUtils;
022    import org.apache.commons.proxy.exception.ObjectProviderException;
023    
024    import javax.naming.InitialContext;
025    import javax.naming.NamingException;
026    import javax.rmi.PortableRemoteObject;
027    import java.lang.reflect.InvocationTargetException;
028    import java.lang.reflect.Method;
029    import java.util.Properties;
030    
031    /**
032     * Provides a reference to a session bean by looking up the home object and calling (via reflection) the no-argument
033     * create() method.  This will work for both local and remote session beans.
034     *
035     * @author James Carman
036     * @since 1.0
037     */
038    public class SessionBeanProvider implements ObjectProvider
039    {
040    //**********************************************************************************************************************
041    // Fields
042    //**********************************************************************************************************************
043    
044        private final String jndiName;
045        private final Class homeInterface;
046        private final Properties properties;
047    
048    //**********************************************************************************************************************
049    // Constructors
050    //**********************************************************************************************************************
051    
052        public SessionBeanProvider( String jndiName, Class homeInterface )
053        {
054            this.jndiName = jndiName;
055            this.homeInterface = homeInterface;
056            this.properties = null;
057        }
058    
059        public SessionBeanProvider( String jndiName, Class homeInterface, Properties properties )
060        {
061            this.jndiName = jndiName;
062            this.homeInterface = homeInterface;
063            this.properties = properties;
064        }
065    
066    //**********************************************************************************************************************
067    // ObjectProvider Implementation
068    //**********************************************************************************************************************
069    
070        public Object getObject()
071        {
072            try
073            {
074                final InitialContext initialContext = properties == null ? new InitialContext() :
075                        new InitialContext(properties);
076                Object homeObject = PortableRemoteObject.narrow(initialContext.lookup(jndiName), homeInterface);
077                final Method createMethod = homeObject.getClass().getMethod("create", ProxyUtils.EMPTY_ARGUMENT_TYPES);
078                return createMethod.invoke(homeObject, ProxyUtils.EMPTY_ARGUMENTS);
079            }
080            catch( NoSuchMethodException e )
081            {
082                throw new ObjectProviderException(
083                        "Unable to find no-arg create() method on home interface " + homeInterface.getName() + ".", e);
084            }
085            catch( IllegalAccessException e )
086            {
087                throw new ObjectProviderException(
088                        "No-arg create() method on home interface " + homeInterface.getName() + " is not accessible.",
089                        e); // Should never happen!
090            }
091            catch( NamingException e )
092            {
093                throw new ObjectProviderException("Unable to lookup EJB home object in JNDI.", e);
094            }
095            catch( InvocationTargetException e )
096            {
097                throw new ObjectProviderException(
098                        "No-arg create() method on home interface " + homeInterface.getName() + " threw an exception.", e);
099            }
100        }
101    }
102