KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.AccessibleObject JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.naming.NameNotFoundException JavaDoc;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.PersistenceUnit;
31 import javax.persistence.PersistenceUnits;
32
33 import org.hibernate.SessionFactory;
34 import org.jboss.annotation.IgnoreDependency;
35 import org.jboss.ejb3.entity.InjectedEntityManagerFactory;
36 import org.jboss.ejb3.entity.InjectedSessionFactory;
37 import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
38 import org.jboss.ejb3.entity.PersistenceUnitDeployment;
39 import org.jboss.logging.Logger;
40 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
41 import org.jboss.metamodel.descriptor.PersistenceUnitRef;
42
43 /**
44  * Searches bean class for all @Inject and create Injectors
45  *
46  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
47  * @version $Revision: 57946 $
48  */

49 public class PersistenceUnitHandler implements InjectionHandler
50 {
51    private static final Logger log = Logger.getLogger(PersistenceUnitHandler.class);
52
53    public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
54    {
55       if (xml == null) return;
56       if (xml.getPersistenceUnitRefs() == null) return;
57
58       for (PersistenceUnitRef ref : xml.getPersistenceUnitRefs())
59       {
60          String JavaDoc encName = "env/" + ref.getRefName();
61          // we add injection target no matter what. enc injection might be overridden but
62
// XML injection cannot be overriden
63
Class JavaDoc injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
64          if (container.getEncInjectors().containsKey(encName))
65             return;
66          container.getEncInjectors().put(encName, new PuEncInjector(encName, injectionType, ref.getUnitName(), "<persistence-unit-ref>"));
67          try
68          {
69             addPUDependency(ref.getUnitName(), container);
70          }
71          catch (NameNotFoundException JavaDoc e)
72          {
73             throw new RuntimeException JavaDoc("Illegal <persistence-unit-ref> of " + ref.getRefName() + " :" + e.getMessage());
74          }
75       }
76    }
77
78
79    public void handleClassAnnotations(Class JavaDoc clazz, InjectionContainer container)
80    {
81       PersistenceUnits resources = container.getAnnotation(
82               PersistenceUnits.class, clazz);
83       if (resources != null)
84       {
85          for (PersistenceUnit ref : resources.value())
86          {
87             handleClassAnnotation(ref, container, clazz);
88          }
89       }
90       PersistenceUnit pu = container.getAnnotation(PersistenceUnit.class, clazz);
91       if (pu != null)
92       {
93          handleClassAnnotation(pu, container, clazz);
94       }
95    }
96
97    private static void handleClassAnnotation(PersistenceUnit ref, InjectionContainer container, Class JavaDoc clazz)
98    {
99       String JavaDoc encName = ref.name();
100       if (encName == null || encName.equals(""))
101       {
102          throw new RuntimeException JavaDoc("JBoss requires name() for class level @PersistenceUnit");
103       }
104       encName = "env/" + encName;
105       if (container.getEncInjectors().containsKey(encName)) return;
106       container.getEncInjectors().put(encName, new PuEncInjector(encName, null, ref.unitName(), "@PersistenceUnit"));
107       try
108       {
109          addPUDependency(ref.unitName(), container);
110       }
111       catch (NameNotFoundException JavaDoc e)
112       {
113          throw new RuntimeException JavaDoc("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
114       }
115    }
116
117    public static void addPUDependency(String JavaDoc unitName, InjectionContainer container) throws NameNotFoundException JavaDoc
118    {
119       PersistenceUnitDeployment deployment = null;
120       // look in EAR first
121
deployment = container.getPersistenceUnitDeployment(unitName);
122       if (deployment != null)
123       {
124          container.getDependencyPolicy().addDependency(deployment.getKernelName());
125          log.debug("***** adding PU dependency from located persistence unit: " + deployment.getKernelName());
126          return;
127       }
128       // probably not deployed yet.
129
// todo not sure if we should do this in JBoss 5
130
log.debug("******* could not find PU dependency so adding a default: " + PersistenceUnitDeployment.getDefaultKernelName(unitName));
131       container.getDependencyPolicy().addDependency(PersistenceUnitDeployment.getDefaultKernelName(unitName));
132    }
133
134    public static ManagedEntityManagerFactory getManagedEntityManagerFactory(InjectionContainer container, String JavaDoc unitName)
135            throws NameNotFoundException JavaDoc
136    {
137       ManagedEntityManagerFactory factory;
138       PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName);
139       if (deployment != null)
140       {
141          factory = deployment.getManagedFactory();
142       }
143       else
144       {
145          throw new NameNotFoundException JavaDoc("Unable to find persistence unit: " + unitName + " for deployment: " + container.getIdentifier());
146       }
147       return factory;
148    }
149
150
151    public static EntityManagerFactory getEntityManagerFactory(PersistenceUnit ref, InjectionContainer container) throws NameNotFoundException JavaDoc
152    {
153       return getEntityManagerFactory(ref.unitName(), container);
154    }
155
156    public static Object JavaDoc getFactory(Class JavaDoc type, String JavaDoc unitName, InjectionContainer container) throws NameNotFoundException JavaDoc
157    {
158       if (type != null && type.getName().equals(SessionFactory.class.getName()))
159          return getSessionFactory(unitName, container);
160       return getEntityManagerFactory(unitName, container);
161    }
162
163    public static EntityManagerFactory getEntityManagerFactory(String JavaDoc unitName, InjectionContainer container) throws NameNotFoundException JavaDoc
164    {
165       ManagedEntityManagerFactory managedFactory;
166       PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName);
167       if (deployment != null)
168       {
169          managedFactory = deployment.getManagedFactory();
170       }
171       else
172       {
173          return null;
174       }
175       return new InjectedEntityManagerFactory(managedFactory);
176    }
177
178
179    private static SessionFactory getSessionFactory(String JavaDoc ref, InjectionContainer container) throws NameNotFoundException JavaDoc
180    {
181       ManagedEntityManagerFactory managedFactory;
182       PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(ref);
183       if (deployment != null)
184       {
185          managedFactory = deployment.getManagedFactory();
186       }
187       else
188       {
189          return null;
190       }
191       return new InjectedSessionFactory(managedFactory);
192    }
193
194    public void handleMethodAnnotations(Method JavaDoc method, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
195    {
196       PersistenceUnit ref = method.getAnnotation(PersistenceUnit.class);
197       if (ref == null) return;
198       if (!method.getName().startsWith("set"))
199          throw new RuntimeException JavaDoc("@PersistenceUnit can only be used with a set method: " + method);
200       String JavaDoc encName = ref.name();
201       if (encName == null || encName.equals(""))
202       {
203          encName = InjectionUtil.getEncName(method);
204       }
205       else
206       {
207          encName = "env/" + encName;
208       }
209       if (!container.getEncInjectors().containsKey(encName))
210       {
211          container.getEncInjectors().put(encName, new PuEncInjector(encName, method.getParameterTypes()[0], ref.unitName(), "@PersistenceUnit"));
212          try
213          {
214             if (!method.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container);
215          }
216          catch (NameNotFoundException JavaDoc e)
217          {
218             throw new RuntimeException JavaDoc("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
219          }
220       }
221
222       injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
223    }
224
225    public void handleFieldAnnotations(Field JavaDoc field, InjectionContainer container, Map JavaDoc<AccessibleObject JavaDoc, Injector> injectors)
226    {
227       PersistenceUnit ref = field.getAnnotation(PersistenceUnit.class);
228       if (ref == null) return;
229       String JavaDoc encName = ref.name();
230       if (encName == null || encName.equals(""))
231       {
232          encName = InjectionUtil.getEncName(field);
233       }
234       else
235       {
236          encName = "env/" + encName;
237       }
238       if (!container.getEncInjectors().containsKey(encName))
239       {
240          container.getEncInjectors().put(encName, new PuEncInjector(encName, field.getType(), ref.unitName(), "@PersistenceUnit"));
241          try
242          {
243             if (!field.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container);
244          }
245          catch (NameNotFoundException JavaDoc e)
246          {
247             throw new RuntimeException JavaDoc("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
248          }
249       }
250
251       injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
252    }
253 }
254
Popular Tags