Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.injection;
 
 import java.util.Map;
 
 
Searches bean class for all

Author(s):
Bill Burke
Version:
$Revision: 67628 $
Inject:
and create Injectors
 
 public class PersistenceContextHandler<X extends RemoteEnvironmentimplements InjectionHandler<X>
 {
    @SuppressWarnings("unused")
    private static final Logger log = Logger
            .getLogger(PersistenceContextHandler.class);
 
    public void loadXml(X xmlInjectionContainer container)
    {
       if (xml == nullreturn;
       if (xml instanceof Environment == falsereturn;
       Environment env = (Environmentxml;
       if (env.getPersistenceContextRefs() == nullreturn;
       {
          String encName = "env/" + ref.getPersistenceContextRefName();
          // we add injection target no matter what.  enc injection might be overridden but
          // XML injection cannot be overriden
          Class<?> injectionType = InjectionUtil.injectionTarget(encNamerefcontainercontainer.getEncInjections());
 
          if (container.getEncInjectors().containsKey(encName))
             continue;
          // add it to list of
          String error = "unable to load <persistence-context-ref> for unitName: "
                  + ref.getPersistenceUnitName() + " <ref-name>: " + ref.getPersistenceContextRefName();
          PersistenceContextType type = ref.getPersistenceContextType();
          String unitName = ref.getPersistenceUnitName();
          container.getEncInjectors().put(encNamenew PcEncInjector(encNameunitNametypeinjectionTypeerror));
          try
          {
             PersistenceUnitHandler.addPUDependency(ref.getPersistenceUnitName(), container);
          }
          catch (NameNotFoundException e)
          {
             throw new RuntimeException("Illegal <persistence-context-ref> of " + ref.getPersistenceContextRefName() + " :" + e.getMessage());
          }
       }
    }
 
    public void handleClassAnnotations(Class<?> clazzInjectionContainer container)
    {
       PersistenceContexts resources = container.getAnnotation(PersistenceContexts.classclazz);
       if (resources != null)
       {
          for (PersistenceContext ref : resources.value())
          {
             loadPersistenceContextClassAnnotation(refcontainerclazz);
          }
       }
       PersistenceContext pc = container.getAnnotation(PersistenceContext.classclazz);
 
       if (pc != null)
       {
          loadPersistenceContextClassAnnotation(pccontainerclazz);
      }
   }
   private static void loadPersistenceContextClassAnnotation(
           PersistenceContext refInjectionContainer containerClass<?> clazz)
   {
      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         throw new RuntimeException(
                 "JBoss requires name() for class level @PersistenceContext");
      }
      encName = "env/" + ref.name();
      if (container.getEncInjectors().containsKey(encName)) return;
      String error = "Unable to load class-level @PersistenceContext("
              + ref.unitName() + ") on " + container.getIdentifier();
      container.getEncInjectors().put(encNamenew PcEncInjector(encNameref.unitName(), ref.type(), nullerror));
      try
      {
         PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
      }
      catch (NameNotFoundException e)
      {
         throw new RuntimeException("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
      }
   }
   public void handleMethodAnnotations(Method methodInjectionContainer containerMap<AccessibleObjectInjectorinjectors)
   {
      PersistenceContext ref = method.getAnnotation(PersistenceContext.class);
      if (ref == nullreturn;
      if (!method.getName().startsWith("set"))
         throw new RuntimeException("@PersistenceUnit can only be used with a set method: " + method);
      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         encName = InjectionUtil.getEncName(method);
      }
      else
      {
         encName = "env/" + ref.name();
      }
      if (!container.getEncInjectors().containsKey(encName))
      {
         try
         {
            if (!method.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
         }
         String error = "@PersistenceContext(name='" + encName
                 + "',unitName='" + ref.unitName() + "') on EJB: "
                 + container.getIdentifier() + " failed to inject on method "
                 + method.toString();
         container.getEncInjectors().put(encNamenew PcEncInjector(encNameref.unitName(), ref.type(), method.getParameterTypes()[0], error));
      }
      injectors.put(methodnew JndiMethodInjector(method,
              encNamecontainer.getEnc()));
   }
   public void handleFieldAnnotations(Field fieldInjectionContainer containerMap<AccessibleObjectInjectorinjectors)
   {
      PersistenceContext ref = field.getAnnotation(PersistenceContext.class);
      if (ref == nullreturn;
      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         encName = InjectionUtil.getEncName(field);
      }
      else
      {
         encName = "env/" + ref.name();
      }
      if (!container.getEncInjectors().containsKey(encName))
      {
         try
         {
            if (!field.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
         }
         String error = "@PersistenceContext(name='" + encName
                 + "',unitName='" + ref.unitName() + "') on EJB: "
                 + container.getIdentifier() + " failed to inject on field "
                 + field.toString();
         container.getEncInjectors().put(encNamenew PcEncInjector(encNameref.unitName(), ref.type(), field.getType(), error));
      }
      injectors.put(fieldnew JndiFieldInjector(field,
              encNamecontainer.getEnc()));
   }
New to GrepCode? Check out our FAQ X