SimpleRemoteStatelessSessionProxyFactoryBean.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.ejb.access;
  17. import javax.naming.NamingException;
  18. import org.springframework.aop.framework.ProxyFactory;
  19. import org.springframework.beans.factory.FactoryBean;
  20. /**
  21.  * <p>Convenient factory for remote SLSB proxies.
  22.  * If you want control over interceptor chaining, use an AOP ProxyFactoryBean
  23.  * with SimpleRemoteSlsbInvokerInterceptor rather than rely on this class.
  24.  * 
  25.  * <p>See {@link org.springframework.jndi.JndiObjectLocator} for info on
  26.  * how to specify the JNDI location of the target EJB.
  27.  * 
  28.  * <p>In a bean container, this class is normally best used as a singleton. However,
  29.  * if that bean container pre-instantiates singletons (as do the XML ApplicationContext
  30.  * variants) you may have a problem if the bean container is loaded before the EJB
  31.  * container loads the target EJB. That is because the JNDI lookup will be performed in
  32.  * the init method of this class and cached, but the EJB will not have been bound at the
  33.  * target location yet. The solution is to not pre-instantiate this factory object, but
  34.  * allow it to be created on first use. In the XML containers, this is controlled via
  35.  * the "lazy-init" attribute.
  36.  * 
  37.  * <p>This proxy factory is typically used with an RMI business interface, which serves
  38.  * as super-interface of the EJB component interface. Alternatively, this factory
  39.  * can also proxy a remote SLSB with a matching non-RMI business interface, i.e. an
  40.  * interface that mirrors the EJB business methods but does not declare RemoteExceptions.
  41.  * In the latter case, RemoteExceptions thrown by the EJB stub will automatically get
  42.  * converted to Spring's unchecked RemoteAccessException.
  43.  *
  44.  * @author Rod Johnson
  45.  * @author Colin Sampaleanu
  46.  * @author Juergen Hoeller
  47.  * @since 09-May-2003
  48.  * @see org.springframework.remoting.RemoteAccessException
  49.  */
  50. public class SimpleRemoteStatelessSessionProxyFactoryBean extends SimpleRemoteSlsbInvokerInterceptor
  51.     implements FactoryBean {
  52. /*
  53.  * Instead of a separate subclass for each type of SLSBInvoker, we could have added
  54.  * this functionality to AbstractSlsbInvokerInterceptor. However, the avoiding of
  55.  * code duplication would be outweighed by the confusion this would produce over the
  56.  * purpose of AbstractSlsbInvokerInterceptor.
  57.  */
  58. /** The business interface of the EJB we're proxying */
  59. private Class businessInterface;
  60. /** EJBObject */
  61. private Object proxy;
  62. /**
  63.  * Set the business interface of the EJB we're proxying.
  64.  * This will normally be a super-interface of the EJB remote component interface.
  65.  * Using a business methods interface is a best practice when implementing EJBs.
  66.  * <p>You can also specify a matching non-RMI business interface, i.e. an interface
  67.  * that mirrors the EJB business methods but does not declare RemoteExceptions.
  68.  * In this case, RemoteExceptions thrown by the EJB stub will automatically get
  69.  * converted to Spring's generic RemoteAccessException.
  70.  * @param businessInterface the business interface of the EJB
  71.  */
  72. public void setBusinessInterface(Class businessInterface) {
  73. this.businessInterface = businessInterface;
  74. }
  75. /**
  76.  * Return the business interface of the EJB we're proxying.
  77.  */
  78. public Class getBusinessInterface() {
  79. return businessInterface;
  80. }
  81. public void afterPropertiesSet() throws NamingException {
  82. super.afterPropertiesSet();
  83. if (this.businessInterface == null) {
  84. throw new IllegalArgumentException("businessInterface is required");
  85. }
  86. this.proxy = ProxyFactory.getProxy(this.businessInterface, this);
  87. }
  88. public Object getObject() {
  89. return this.proxy;
  90. }
  91. public Class getObjectType() {
  92. return (this.proxy != null) ? this.proxy.getClass() : this.businessInterface;
  93. }
  94. public boolean isSingleton() {
  95. return true;
  96. }
  97. }