Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.spring » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.spring;
   19   
   20   import org.apache.openejb.DeploymentInfo;
   21   import org.apache.openejb.assembler.classic.JndiBuilder;
   22   import org.apache.openejb.loader.SystemInstance;
   23   import org.apache.openejb.spi.ContainerSystem;
   24   import org.springframework.beans.factory.FactoryBean;
   25   import org.springframework.beans.factory.annotation.Required;
   26   
   27   @Exported
   28   public class EJB<T> implements FactoryBean {
   29       private Object deploymentId;
   30       private DeploymentInfo deploymentInfo;
   31       private Class<T> intf;
   32   
   33       public EJB() {
   34       }
   35   
   36       public EJB(DeploymentInfo deploymentInfo, Class<T> intf) {
   37           this.deploymentInfo = deploymentInfo;
   38           this.intf = intf;
   39       }
   40   
   41       public Object getDeploymentId() {
   42           if (deploymentId != null) {
   43               return deploymentId;
   44           } else if (deploymentInfo != null) {
   45               return deploymentInfo.getDeploymentID();
   46           }
   47           return null;
   48       }
   49   
   50       public void setDeploymentId(Object deploymentId) {
   51           this.deploymentId = deploymentId;
   52       }
   53   
   54       public DeploymentInfo getDeploymentInfo() {
   55           if (deploymentInfo != null) {
   56               return deploymentInfo;
   57           } else if (deploymentId != null) {
   58               ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
   59               DeploymentInfo deploymentInfo = containerSystem.getDeploymentInfo(deploymentId);
   60               return deploymentInfo;
   61           }
   62           return null;
   63       }
   64   
   65       public void setDeploymentInfo(DeploymentInfo deploymentInfo) {
   66           this.deploymentInfo = deploymentInfo;
   67       }
   68   
   69       public Class<T> getInterface() {
   70           return intf;
   71       }
   72   
   73       @Required
   74       public void setInterface(Class<T> intf) {
   75           this.intf = intf;
   76       }
   77   
   78       public T getObject() throws Exception {
   79           if (intf == null) throw new NullPointerException("intf is null");
   80   
   81           DeploymentInfo deploymentInfo = getDeploymentInfo();
   82           if (deploymentInfo == null) {
   83               throw new IllegalStateException("DeploymentInfo or DeploymentID must be set before EJB can be retrieved");
   84           }
   85   
   86           // this is the pattern for the internal jndi name
   87           String jndiName = "java:openejb/Deployment/" + JndiBuilder.format(deploymentInfo.getDeploymentID(), getInterface().getName());
   88   
   89           // perform the lookup against the jndi context in the container system
   90           ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
   91           Object proxy = containerSystem.getJNDIContext().lookup(jndiName);
   92           if (!intf.isInstance(proxy)) {
   93               throw new IllegalArgumentException("EJB at " + jndiName + " is not an instance of " + intf.getName() + ", but is " + proxy.getClass().getName());
   94           }
   95           return intf.cast(proxy);
   96       }
   97   
   98       public Class<T> getObjectType() {
   99           return getInterface();
  100       }
  101   
  102       public boolean isSingleton() {
  103           return false;
  104       }
  105   }

Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.spring » [javadoc | source]