Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.ejb.component » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    * 
    9    *       http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.webbeans.ejb.component;
   18   
   19   import javax.enterprise.context.spi.CreationalContext;
   20   import javax.enterprise.inject.spi.SessionBeanType;
   21   import javax.naming.Context;
   22   import javax.naming.NamingException;
   23   
   24   import org.apache.openejb.DeploymentInfo;
   25   import org.apache.openejb.InterfaceType;
   26   import org.apache.openejb.assembler.classic.JndiBuilder;
   27   import org.apache.openejb.loader.SystemInstance;
   28   import org.apache.openejb.spi.ContainerSystem;
   29   import org.apache.webbeans.component.AbstractInjectionTargetBean;
   30   import org.apache.webbeans.component.EnterpriseBeanMarker;
   31   import org.apache.webbeans.component.WebBeansType;
   32   
   33   /**
   34    * Defines bean contract for the session beans.
   35    * 
   36    * @version $Rev$ $Date$
   37    */
   38   public class EjbBean<T> extends AbstractInjectionTargetBean<T> implements EnterpriseBeanMarker
   39   {
   40       /**Session bean type*/
   41       private SessionBeanType ejbType;
   42       
   43       /**OpenEJB deployment info*/
   44       private DeploymentInfo deploymentInfo;    
   45       
   46       /**Current bean instance*/
   47       private T instance = null;
   48       
   49       /**Injected reference local interface type*/
   50       private Class<?> iface = null;
   51       
   52       /**Remove stateful bean instance*/
   53       private boolean removeStatefulInstance = false;
   54       
   55       /**
   56        * Creates a new instance of the session bean.
   57        * @param ejbClassType ebj class type
   58        */
   59       public EjbBean(Class<T> ejbClassType)
   60       {
   61           super(WebBeansType.ENTERPRISE,ejbClassType);
   62       }
   63   
   64       /**
   65        * Sets local interface type.
   66        * @param iface local interface type
   67        */
   68       public void setIface(Class<?> iface)
   69       {
   70           this.iface = iface;
   71       }
   72       
   73       /**
   74        * Sets remove flag.
   75        * @param remove flag
   76        */
   77       public void setRemoveStatefulInstance(boolean remove)
   78       {
   79           this.removeStatefulInstance = remove;
   80       }
   81       
   82       /**
   83        * Sets session bean's deployment info.
   84        * @param deploymentInfo deployment info
   85        */
   86       public void setDeploymentInfo(DeploymentInfo deploymentInfo)
   87       {
   88           this.deploymentInfo = deploymentInfo;
   89       }
   90           
   91       /**
   92        * Returns bean's deployment info.
   93        * @return bean's deployment info
   94        */
   95       public DeploymentInfo getDeploymentInfo()
   96       {
   97           return this.deploymentInfo;
   98       }
   99       
  100           
  101       /**
  102        * {@inheritDoc}
  103        */
  104       @Override
  105       public void injectFields(T instance, CreationalContext<T> creationalContext)
  106       {
  107           //No-operations
  108       }
  109       
  110       /**
  111        * Inject session bean injected fields. It is called from
  112        * interceptor.
  113        * @param instance bean instance
  114        * @param creationalContext creational context instance
  115        */
  116       @SuppressWarnings("unchecked")
  117       public void injectFieldInInterceptor(Object instance, CreationalContext<?> creationalContext)
  118       {
  119           super.injectFields((T)instance, (CreationalContext<T>)creationalContext);
  120       }
  121   
  122       /**
  123        * {@inheritDoc}
  124        */
  125       @Override
  126       @SuppressWarnings("unchecked")
  127       protected T createComponentInstance(CreationalContext<T> creationalContext)
  128       {
  129           if(this.instance == null)
  130           {
  131               ContainerSystem containerSystem =  SystemInstance.get().getComponent(ContainerSystem.class);
  132               Context jndiContext = containerSystem.getJNDIContext();
  133               DeploymentInfo deploymentInfo = this.getDeploymentInfo();
  134               try
  135               {
  136                   if(iface != null)
  137                   {
  138                       InterfaceType type = deploymentInfo.getInterfaceType(iface);
  139                       if(!type.equals(InterfaceType.BUSINESS_LOCAL))
  140                       {
  141                           throw new IllegalArgumentException("Interface type is not legal business local interface for session bean class : " + getReturnType().getName());
  142                       }   
  143                   }    
  144                   else
  145                   {
  146                       iface = this.deploymentInfo.getBusinessLocalInterface();
  147                   }
  148                   
  149                   String jndiName = "java:openejb/Deployment/" + JndiBuilder.format(deploymentInfo.getDeploymentID(), this.iface.getName()); 
  150                   this.instance = (T)this.iface.cast(jndiContext.lookup(jndiName));                             
  151                   
  152               }catch(NamingException e)
  153               {
  154                   throw new RuntimeException(e);
  155               }        
  156           }
  157   
  158           return instance;
  159       }
  160   
  161       /**
  162        * {@inheritDoc}
  163        */
  164       @Override
  165       protected void destroyComponentInstance(T instance)
  166       {
  167           if(removeStatefulInstance && getEjbType().equals(SessionBeanType.STATEFUL))
  168           {
  169               //Call remove method
  170           }
  171           
  172           this.instance = null;
  173       }
  174       
  175       /**
  176        * Sets session bean type.
  177        * @param type session bean type
  178        */
  179       public void setEjbType(SessionBeanType type)
  180       {
  181           this.ejbType = type;
  182           
  183       }
  184       
  185       /**
  186        * Gets ejb name.
  187        * @return ejb name
  188        */
  189       public String getEjbName()
  190       {
  191           return this.deploymentInfo.getEjbName();
  192       }
  193       
  194       /**
  195        * Gets ejb session type.
  196        * @return type of the ejb
  197        */
  198       public SessionBeanType getEjbType()
  199       {
  200           return this.ejbType;
  201       }
  202   
  203   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.ejb.component » [javadoc | source]