Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.jpa.plugin » [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.jpa.plugin;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Type;
   18   
   19   import org.apache.webbeans.exception.WebBeansConfigurationException;
   20   import org.apache.webbeans.jpa.spi.JPAService;
   21   
   22   import javax.persistence.EntityManager;
   23   import javax.persistence.EntityManagerFactory;
   24   import javax.persistence.PersistenceContext;
   25   import javax.persistence.PersistenceContextType;
   26   import javax.persistence.PersistenceUnit;
   27   
   28   import org.apache.webbeans.plugins.AbstractOpenWebBeansPlugin;
   29   import org.apache.webbeans.spi.ServiceLoader;
   30   import org.apache.webbeans.util.AnnotationUtil;
   31   import org.apache.webbeans.util.Asserts;
   32   
   33   public class OpenWebBeansJpaPlugin extends AbstractOpenWebBeansPlugin 
   34   {
   35   
   36       /** {@inheritDoc} */
   37       public void startUp() throws WebBeansConfigurationException 
   38       {
   39           // nothing to do 
   40       }
   41   
   42       /** {@inheritDoc} */
   43       public void shutDown() throws WebBeansConfigurationException 
   44       {
   45           // nothing to do
   46       }
   47   
   48   
   49       /** {@inheritDoc} */
   50       public Object injectResource(Type type, Annotation[] annotations)
   51       {
   52           if (!isResource(annotations))
   53           {
   54               return null;
   55           }
   56           
   57           Annotation annot = AnnotationUtil.getAnnotation(annotations, PersistenceUnit.class);
   58           if (annot != null)
   59           {
   60               PersistenceUnit pu = (PersistenceUnit) annot;
   61               String unitName = pu.unitName();
   62               
   63               //X TODO what if the EntityManagerFactory is null?
   64               return getJPAService().getPersistenceUnit(unitName);
   65           }
   66           
   67           annot = AnnotationUtil.getAnnotation(annotations, PersistenceContext.class);
   68           if (annot != null)
   69           {
   70               PersistenceContext pc = (PersistenceContext) annot;
   71               String unitName = pc.unitName();
   72               String name = pc.name();
   73               
   74               //X TODO what if the EntityManager is null?
   75               return getJPAService().getPersistenceContext(unitName, name);
   76           }
   77           return null;
   78       }
   79   
   80       /**
   81        * {@inheritDoc}
   82        * Returns true if the annotation is a valid JPA Resource handled
   83        * by this very Plugin.
   84        * The following annotations indicate such resources:
   85        * <ol>
   86        * <li>&#x0040;CustomerDataservice</li>
   87        * <li>&#x0040;Resource</li>
   88        * <li>&#x0040;PersistenceContext</li>
   89        * <li>&#x0040;PersistenceUnit</li>
   90        * <li>&#x0040;</li>
   91        * </ol>
   92        * 
   93        * Please note that &#x0040;PersistenceContext(type=EXTENDED) 
   94        * is not supported for simple beans.
   95        * 
   96        * @param clazz type of the annotation
   97        * @return true if the annotation is defined in xml or annotated with
   98        *         {@link BindingType} false otherwise
   99        */
  100       public boolean isResourceAnnotation(Class<? extends Annotation> clazz)
  101       {
  102           Asserts.assertNotNull(clazz, "clazz parameter can not be null");
  103           
  104           if (clazz.equals(PersistenceContext.class) ||
  105                    clazz.equals(PersistenceUnit.class) )
  106           {
  107               return true;
  108           }
  109   
  110           return false;
  111       }
  112   
  113       /** {@inheritDoc} */
  114       public void checkForValidResources(Type type, Class<?> clazz, String name, Annotation[] annotations)
  115       {
  116           for (Annotation a : annotations)
  117           {
  118               if (a.annotationType().equals(PersistenceUnit.class))
  119               {
  120                   if (!type.equals(EntityManagerFactory.class))
  121                   {
  122                       throw new WebBeansConfigurationException("@PersistenceUnit must only be injected into field/method with type EntityManagerFactory! class : "  
  123                                                                + clazz.getName() + " in field/method : " + name);
  124                   }
  125               }
  126               
  127               if (a.annotationType().equals(PersistenceContext.class))
  128               {
  129                   PersistenceContext pc = (PersistenceContext) a;
  130                   
  131                   if (!type.equals(EntityManager.class))
  132                   {
  133                       throw new WebBeansConfigurationException("@PersistenceContext must only be injected into field/method with type EntityManager! class : "  
  134                                                                + clazz.getName() + " in field/method : " + name);
  135                   }
  136                
  137                   if (pc.type().equals(PersistenceContextType.EXTENDED))
  138                   {
  139                       throw new WebBeansConfigurationException("type of @PersistenceContext must not be 'EXTENDED'! class : "  
  140                               + clazz.getName() + " in field/method : " + name);
  141                       
  142                   }
  143               }
  144           }
  145       }
  146   
  147       private JPAService getJPAService()
  148       {
  149           return ServiceLoader.getService(JPAService.class);
  150       }
  151   
  152       private boolean isResource(Annotation[] annotations)
  153       {
  154           for (Annotation anno : annotations)
  155           {
  156               if (isResourceAnnotation(anno.annotationType()))
  157               {
  158                   return true;
  159               }
  160           }
  161           
  162           return false;
  163       }
  164   
  165   }

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