Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.spi.ee.openejb » [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 this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.spi.ee.openejb;
   15   
   16   import java.lang.reflect.Field;
   17   import java.lang.reflect.InvocationTargetException;
   18   
   19   import javax.annotation.Resource;
   20   import javax.ejb.EJB;
   21   import javax.naming.Context;
   22   import javax.naming.NamingException;
   23   import javax.persistence.PersistenceContext;
   24   import javax.persistence.PersistenceUnit;
   25   import javax.xml.ws.WebServiceRef;
   26   
   27   import org.apache.webbeans.spi.ee.openejb.jpa.JPAServiceOpenEJBImpl;
   28   
   29   
   30   class ResourceInjectionProcessor
   31   {
   32       private Context context = null;
   33   
   34       private JPAServiceOpenEJBImpl jpaService = null;
   35       
   36       ResourceInjectionProcessor(Context context)
   37       {
   38           this.context = context;
   39           this.jpaService = new JPAServiceOpenEJBImpl(this.context);
   40       }
   41   
   42       public Object getResourceObject(Field field) throws IllegalAccessException, InvocationTargetException, NamingException
   43       {
   44           if (context == null)
   45           {
   46               // No resource injection
   47               return null;
   48           }
   49   
   50           if (field.isAnnotationPresent(Resource.class))
   51           {
   52               Resource annotation = field.getAnnotation(Resource.class);
   53               
   54               return lookupFieldResource(context, "openejb/Resource/"+annotation.name());
   55               
   56           }
   57           
   58           if (field.isAnnotationPresent(EJB.class))
   59           {
   60               EJB annotation = field.getAnnotation(EJB.class);
   61               
   62               return lookupFieldResource(context, annotation.name());
   63           }
   64           
   65           if (field.isAnnotationPresent(WebServiceRef.class))
   66           {
   67               WebServiceRef annotation = field.getAnnotation(WebServiceRef.class);
   68               
   69               return lookupFieldResource(context, annotation.name());
   70           }
   71           
   72           if (field.isAnnotationPresent(PersistenceContext.class))
   73           {
   74               PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
   75               
   76               return this.jpaService.getPersistenceContext(annotation.unitName());
   77               
   78           }
   79           
   80           if (field.isAnnotationPresent(PersistenceUnit.class))
   81           {
   82               PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
   83               
   84               return this.jpaService.getPersistenceUnit(annotation.unitName());
   85           }
   86           
   87           return null;
   88       }
   89   
   90       /**
   91        * Inject resources in specified field.
   92        */
   93       protected Object lookupFieldResource(javax.naming.Context context, String name) throws NamingException, IllegalAccessException
   94       {
   95           Object lookedupResource = null;
   96   
   97           if ((name != null) && (name.length() > 0))
   98           {
   99               lookedupResource = context.lookup("java:/" + name);                        
  100           }
  101           
  102           return lookedupResource;
  103       }    
  104   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.spi.ee.openejb » [javadoc | source]