KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > injection > PersistenceContextHandler


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.injection;
23
24 import org.jboss.metamodel.descriptor.PersistenceContextRef;
25 import org.jboss.logging.Logger;
26 import org.jboss.annotation.IgnoreDependency;
27 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
28
29 import javax.naming.NameNotFoundException JavaDoc;
30 import javax.persistence.PersistenceContext;
31 import javax.persistence.PersistenceContextType;
32 import javax.persistence.PersistenceContexts;
33 import java.lang.reflect.AccessibleObject JavaDoc;
34 import java.lang.reflect.Field JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * Searches bean class for all
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 55144 $
43  * @Inject and create Injectors
44  */

45 public class PersistenceContextHandler implements InjectionHandler
46 {
47    private static final Logger log = Logger
48            .getLogger(PersistenceContextHandler.class);
49
50    public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
51    {
52       if (xml == null) return;
53       if (xml.getPersistenceContextRefs() == null) return;
54       for (PersistenceContextRef ref : xml.getPersistenceContextRefs())
55       {
56          String JavaDoc encName = "env/" + ref.getRefName();
57          // we add injection target no matter what. enc injection might be overridden but
58
// XML injection cannot be overriden
59
Class JavaDoc injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
60
61          if (container.getEncInjectors().containsKey(encName))
62             continue;
63          // add it to list of
64
String JavaDoc error = "unable to load <persistence-context-ref> for unitName: "
65                  + ref.getUnitName() + " <ref-name>: " + ref.getRefName();
66          PersistenceContextType type = ref.getPersistenceContextType();
67          String JavaDoc unitName = ref.getUnitName();
68          container.getEncInjectors().put(encName, new PcEncInjector(encName, unitName, type, injectionType, error));
69          try
70          {
71             PersistenceUnitHandler.addPUDependency(ref.getUnitName(), container);
72          }
73          catch (NameNotFoundException JavaDoc e)
74          {
75             throw new RuntimeException JavaDoc("Illegal <persistence-context-ref> of " + ref.getRefName() + " :" + e.getMessage());
76          }
77       }
78    }
79
80    public void handleClassAnnotations(Class JavaDoc clazz, InjectionContainer container)
81    {
82       PersistenceContexts resources = container.getAnnotation(PersistenceContexts.class, clazz);
83       if (resources != null)
84       {
85          for (PersistenceContext ref : resources.value())
86          {
87             loadPersistenceContextClassAnnotation(ref, container, clazz);
88          }
89       }
90       PersistenceContext pc = container.getAnnotation(PersistenceContext.class, clazz);
91
92       if (pc != null)
93       {
94          loadPersistenceContextClassAnnotation(pc, container, clazz);
95       }
96
97    }
98
99    private static void loadPersistenceContextClassAnnotation(
100            PersistenceContext ref, InjectionContainer container, Class JavaDoc clazz)
101    {
102       String JavaDoc encName = ref.name();
103       if (encName == null || encName.equals(""))
104       {
105          throw new RuntimeException JavaDoc(
106                  "JBoss requires name() for class level @PersistenceContext");
107       }
108       encName = "env/" + ref.name();
109       if (container.getEncInjectors().containsKey(encName)) return;
110
111       String JavaDoc error = "Unable to load class-level @PersistenceContext("
112               + ref.unitName() + ") on " + container.getIdentifier();
113       container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), null, error));
114       try
115       {
116          PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
117       }
118       catch (NameNotFoundException JavaDoc e)
119       {
120          throw new RuntimeException JavaDoc("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
121       }
122    }
123
124    public void handleMethodAnnotations(Method JavaDoc method, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
125    {
126       PersistenceContext ref = method.getAnnotation(PersistenceContext.class);
127       if (ref == null) return;
128       if (!method.getName().startsWith("set"))
129          throw new RuntimeException JavaDoc("@PersistenceUnit can only be used with a set method: " + method);
130
131       String JavaDoc encName = ref.name();
132       if (encName == null || encName.equals(""))
133       {
134          encName = InjectionUtil.getEncName(method);
135       }
136       else
137       {
138          encName = "env/" + ref.name();
139       }
140       if (!container.getEncInjectors().containsKey(encName))
141       {
142          try
143          {
144             if (!method.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
145          }
146          catch (NameNotFoundException JavaDoc e)
147          {
148             throw new RuntimeException JavaDoc("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
149          }
150          String JavaDoc error = "@PersistenceContext(name='" + encName
151                  + "',unitName='" + ref.unitName() + "') on EJB: "
152                  + container.getIdentifier() + " failed to inject on method "
153                  + method.toString();
154          container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), method.getParameterTypes()[0], error));
155       }
156       injectors.put(method, new JndiMethodInjector(method,
157               encName, container.getEnc()));
158    }
159
160    public void handleFieldAnnotations(Field JavaDoc field, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
161    {
162       PersistenceContext ref = field.getAnnotation(PersistenceContext.class);
163       if (ref == null) return;
164
165       String JavaDoc encName = ref.name();
166       if (encName == null || encName.equals(""))
167       {
168          encName = InjectionUtil.getEncName(field);
169       }
170       else
171       {
172          encName = "env/" + ref.name();
173       }
174       if (!container.getEncInjectors().containsKey(encName))
175       {
176          try
177          {
178             if (!field.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
179          }
180          catch (NameNotFoundException JavaDoc e)
181          {
182             throw new RuntimeException JavaDoc("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
183          }
184          String JavaDoc error = "@PersistenceContext(name='" + encName
185                  + "',unitName='" + ref.unitName() + "') on EJB: "
186                  + container.getIdentifier() + " failed to inject on field "
187                  + field.toString();
188          container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), field.getType(), error));
189       }
190       injectors.put(field, new JndiFieldInjector(field,
191               encName, container.getEnc()));
192    }
193 }
194
Popular Tags