/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.remoting.rmi; import javax.naming.NamingException; import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.FactoryBean; import org.springframework.util.ClassUtils; /** * Factory bean for RMI proxies from JNDI. * * <p>Typically used for RMI-IIOP (CORBA), but can also be used for EJB home objects * (for example, a Stateful Session Bean home). In contrast to a plain JNDI lookup, * this accessor also performs narrowing through {@link javax.rmi.PortableRemoteObject}. * * <p>With conventional RMI services, this invoker is typically used with the RMI * service interface. Alternatively, this invoker can also proxy a remote RMI service * with a matching non-RMI business interface, i.e. an interface that mirrors the RMI * service methods but does not declare RemoteExceptions. In the latter case, * RemoteExceptions thrown by the RMI stub will automatically get converted to * Spring's unchecked RemoteAccessException. * * <p>The JNDI environment can be specified as "jndiEnvironment" property, * or be configured in a <code>jndi.properties</code> file or as system properties. * For example: * * <pre class="code"><property name="jndiEnvironment"> * <props> * <prop key="java.naming.factory.initial">com.sun.jndi.cosnaming.CNCtxFactory</prop> * <prop key="java.naming.provider.url">iiop://localhost:1050</prop> * </props> * </property></pre> * * @author Juergen Hoeller * @since 1.1 * @see #setServiceInterface * @see #setJndiName * @see #setJndiTemplate * @see #setJndiEnvironment * @see #setJndiName * @see JndiRmiServiceExporter * @see org.springframework.remoting.RemoteAccessException * @see java.rmi.RemoteException * @see java.rmi.Remote * @see javax.rmi.PortableRemoteObject#narrow */ public class JndiRmiProxyFactoryBean extends JndiRmiClientInterceptor implements FactoryBean, BeanClassLoaderAware { private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); private Object serviceProxy; public void setBeanClassLoader(ClassLoader classLoader) { this.beanClassLoader = classLoader; } public void afterPropertiesSet() throws NamingException { super.afterPropertiesSet(); if (getServiceInterface() == null) { throw new IllegalArgumentException("Property 'serviceInterface' is required"); } this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(this.beanClassLoader); } public Object getObject() { return this.serviceProxy; } public Class getObjectType() { return getServiceInterface(); } public boolean isSingleton() { return true; } }