Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.resource » [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;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Field;
   18   import java.lang.reflect.Type;
   19   
   20   import javax.annotation.Resource;
   21   import javax.persistence.EntityManager;
   22   import javax.persistence.EntityManagerFactory;
   23   import javax.persistence.PersistenceContext;
   24   import javax.persistence.PersistenceContextType;
   25   import javax.persistence.PersistenceUnit;
   26   import javax.xml.ws.WebServiceRef;
   27   
   28   import org.apache.webbeans.exception.WebBeansConfigurationException;
   29   import org.apache.webbeans.plugins.AbstractOpenWebBeansPlugin;
   30   import org.apache.webbeans.resource.spi.ResourceService;
   31   import org.apache.webbeans.spi.ServiceLoader;
   32   
   33   public class OpenWebBeansResourcePlugin extends AbstractOpenWebBeansPlugin implements org.apache.webbeans.plugins.OpenWebBeansResourcePlugin
   34   {
   35   
   36       @Override
   37       public boolean isResourceAnnotation(Class<? extends Annotation> annotationClass)
   38       {
   39           if (annotationClass.equals(Resource.class) || 
   40                   annotationClass.equals(WebServiceRef.class) || 
   41                   annotationClass.equals(PersistenceUnit.class) || 
   42                   annotationClass.equals(PersistenceContext.class))
   43           {
   44               return true;
   45           }
   46   
   47           return false;
   48       }
   49   
   50       @Override
   51       public void injectResources(Object instance) throws RuntimeException
   52       {
   53           Field[] fields = instance.getClass().getDeclaredFields();
   54           ResourceService service = ServiceLoader.getService(ResourceService.class);
   55           for(Field field : fields)
   56           {
   57               boolean access = field.isAccessible();
   58               try
   59               {                
   60                   field.setAccessible(true);
   61                   Object resource = service.getResource(field);
   62                   
   63                   if(resource != null)
   64                   {
   65                       field.set(instance, resource);   
   66                   }                
   67               }
   68               catch (IllegalAccessException e)
   69               {
   70                   throw new RuntimeException(e);
   71               }finally
   72               {
   73                   field.setAccessible(access);
   74               }
   75           }
   76       }
   77   
   78       /** {@inheritDoc} */
   79       public void checkForValidResources(Type type, Class<?> clazz, String name, Annotation[] annotations)
   80       {
   81           for (Annotation a : annotations)
   82           {
   83               if (a.annotationType().equals(PersistenceUnit.class))
   84               {
   85                   if (!type.equals(EntityManagerFactory.class))
   86                   {
   87                       throw new WebBeansConfigurationException("@PersistenceUnit must only be injected into field/method with type EntityManagerFactory! class : " + clazz.getName() + " in field/method : " + name);
   88                   }
   89               }
   90   
   91               if (a.annotationType().equals(PersistenceContext.class))
   92               {
   93                   PersistenceContext pc = (PersistenceContext) a;
   94   
   95                   if (!type.equals(EntityManager.class))
   96                   {
   97                       throw new WebBeansConfigurationException("@PersistenceContext must only be injected into field/method with type EntityManager! class : " + clazz.getName() + " in field/method : " + name);
   98                   }
   99   
  100                   if (pc.type().equals(PersistenceContextType.EXTENDED))
  101                   {
  102                       throw new WebBeansConfigurationException("type of @PersistenceContext must not be 'EXTENDED'! class : " + clazz.getName() + " in field/method : " + name);
  103   
  104                   }
  105               }
  106           }
  107       }
  108   
  109   }

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