Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.resource.spi.se » [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.resource.spi.se;
   15   
   16   import java.lang.reflect.Field;
   17   
   18   import javax.annotation.Resource;
   19   import javax.naming.InitialContext;
   20   import javax.persistence.PersistenceContext;
   21   import javax.persistence.PersistenceUnit;
   22   
   23   import org.apache.webbeans.exception.WebBeansException;
   24   import org.apache.webbeans.logger.WebBeansLogger;
   25   import org.apache.webbeans.resource.spi.ResourceService;
   26   
   27   public class ResourceServiceImpl implements ResourceService
   28   { 
   29       private static InitialContext context = null;
   30       
   31       private static WebBeansLogger logger = WebBeansLogger.getLogger(ResourceServiceImpl.class);
   32       
   33       static
   34       {
   35           try
   36           {
   37               context = new InitialContext();
   38               
   39           }catch(Exception e)
   40           {
   41               throw new ExceptionInInitializerError(e);
   42           }
   43       }
   44   
   45       @Override
   46       public Object getResource(Field field) throws WebBeansException
   47       {
   48           Object obj = null;
   49           
   50           JPAServicePersistenceImpl impl = new JPAServicePersistenceImpl();
   51                   
   52           if (field.isAnnotationPresent(PersistenceContext.class))
   53           {
   54               PersistenceContext context = field.getAnnotation(PersistenceContext.class);            
   55               obj = impl.getPersistenceContext(context.unitName());
   56           }
   57           
   58           else if (field.isAnnotationPresent(PersistenceUnit.class))
   59           {
   60               PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
   61               
   62               obj = impl.getPersistenceUnit(annotation.unitName());
   63           }
   64           else if(field.isAnnotationPresent(Resource.class))
   65           {
   66               Resource resource = field.getAnnotation(Resource.class);            
   67               try
   68               {
   69                   obj = context.lookup("java:/comp/env/"+ resource.name()); 
   70               }
   71               catch(Exception e)
   72               {
   73                   logger.error("Unable to inject resource for :  " + resource );
   74               }             
   75           }        
   76           
   77           return obj;
   78       }
   79   
   80   }

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