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 private final String jndiName; 044 private final Class homeInterface; 045 private final Properties properties; 046 047 //---------------------------------------------------------------------------------------------------------------------- 048 // Constructors 049 //---------------------------------------------------------------------------------------------------------------------- 050 051 public SessionBeanProvider( String jndiName, Class homeInterface ) 052 { 053 this.jndiName = jndiName; 054 this.homeInterface = homeInterface; 055 this.properties = null; 056 } 057 058 public SessionBeanProvider( String jndiName, Class homeInterface, Properties properties ) 059 { 060 this.jndiName = jndiName; 061 this.homeInterface = homeInterface; 062 this.properties = properties; 063 } 064 065 //---------------------------------------------------------------------------------------------------------------------- 066 // ObjectProvider Implementation 067 //---------------------------------------------------------------------------------------------------------------------- 068 069 public Object getObject() 070 { 071 try 072 { 073 final InitialContext initialContext = properties == null ? new InitialContext() : 074 new InitialContext( properties ); 075 Object homeObject = PortableRemoteObject.narrow( initialContext.lookup( jndiName ), homeInterface ); 076 final Method createMethod = homeObject.getClass().getMethod( "create", ProxyUtils.EMPTY_ARGUMENT_TYPES ); 077 return createMethod.invoke( homeObject, ProxyUtils.EMPTY_ARGUMENTS ); 078 } 079 catch( NoSuchMethodException e ) 080 { 081 throw new ObjectProviderException( 082 "Unable to find no-arg create() method on home interface " + homeInterface.getName() + ".", e ); 083 } 084 catch( IllegalAccessException e ) 085 { 086 throw new ObjectProviderException( 087 "No-arg create() method on home interface " + homeInterface.getName() + " is not accessible.", 088 e ); // Should never happen! 089 } 090 catch( NamingException e ) 091 { 092 throw new ObjectProviderException( "Unable to lookup EJB home object in JNDI.", e ); 093 } 094 catch( InvocationTargetException e ) 095 { 096 throw new ObjectProviderException( 097 "No-arg create() method on home interface " + homeInterface.getName() + " threw an exception.", e ); 098 } 099 } 100 } 101