KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > enc > DeploymentPersistenceUnitResolver


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.ejb3.enc;
23
24 import org.jboss.ejb3.entity.PersistenceUnitDeployment;
25 import org.jboss.ejb3.PersistenceUnitRegistry;
26 import org.jboss.ejb3.DeploymentScope;
27 import org.jboss.ejb3.Ejb3Deployment;
28
29 import javax.naming.NameNotFoundException JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.LinkedHashMap JavaDoc;
32
33 /**
34  * Resolves persistence units for @PersistenceContext and @PersistenceUnit
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 57481 $
38  */

39 public class DeploymentPersistenceUnitResolver
40 {
41    protected List JavaDoc<PersistenceUnitDeployment> persistenceUnitDeployments;
42    protected DeploymentScope deploymentScope;
43    protected LinkedHashMap JavaDoc ejbContainers;
44
45    public DeploymentPersistenceUnitResolver(List JavaDoc<PersistenceUnitDeployment> persistenceUnitDeployments, DeploymentScope deploymentScope, LinkedHashMap JavaDoc ejbContainers)
46    {
47       this.persistenceUnitDeployments = persistenceUnitDeployments;
48       this.deploymentScope = deploymentScope;
49       this.ejbContainers = ejbContainers;
50    }
51
52    public PersistenceUnitDeployment getPersistenceUnitDeployment(String JavaDoc unitName) throws NameNotFoundException JavaDoc
53    {
54       if ("".equals(unitName))
55       {
56          if (persistenceUnitDeployments == null)
57          {
58             throw new NameNotFoundException JavaDoc("EMPTY STRING unitName but there is no deployments in scope");
59          }
60          if (persistenceUnitDeployments.size() == 1 && ejbContainers.size() > 0)
61          {
62             return persistenceUnitDeployments.get(0);
63          }
64          else if (persistenceUnitDeployments.size() > 1)
65          {
66             throw new NameNotFoundException JavaDoc("EMPTY STRING unitName and there is more than one scoped persistence unit");
67          }
68          throw new NameNotFoundException JavaDoc("There is no default persistence unit in this deployment.");
69       }
70       int hashIndex = unitName.indexOf('#');
71       if (hashIndex != -1)
72       {
73          String JavaDoc relativePath = unitName.substring(0, hashIndex);
74          String JavaDoc name = unitName.substring(hashIndex + 1);
75          if (deploymentScope == null)
76          {
77             String JavaDoc relativeJarName = relativePath.substring(3);
78             // look in global EJB jars.
79
for (PersistenceUnitDeployment pud : PersistenceUnitRegistry.getPersistenceUnits())
80             {
81                String JavaDoc jarName = pud.getDeployment().getDeploymentUnit().getShortName() + ".jar";
82                if (pud.getDeployment().getEar() == null
83                        && jarName.equals(relativeJarName)
84                        && pud.getEntityManagerName().equals(name)
85                        && pud.isScoped())
86                {
87                   return pud;
88                }
89             }
90             return null;
91          }
92          Ejb3Deployment dep = deploymentScope.findRelativeDeployment(relativePath);
93          if (dep == null)
94          {
95             return null;
96          }
97          PersistenceUnitDeployment rtn = dep.getPersistenceUnitDeploymentInternal(name);
98          return rtn;
99       }
100       PersistenceUnitDeployment rtn = getPersistenceUnitDeploymentInternal(unitName);
101       if (rtn != null) return rtn;
102
103       for (PersistenceUnitDeployment deployment : PersistenceUnitRegistry.getPersistenceUnits())
104       {
105          if (deployment.isScoped()) continue;
106          if (deployment.getEntityManagerName().equals(unitName)) return deployment;
107       }
108       return rtn;
109    }
110
111    public PersistenceUnitDeployment getPersistenceUnitDeploymentInternal(String JavaDoc unitName)
112    {
113       if (persistenceUnitDeployments != null)
114       {
115          for (PersistenceUnitDeployment deployment : persistenceUnitDeployments)
116          {
117             if (deployment.getEntityManagerName().equals(unitName))
118             {
119                return deployment;
120             }
121          }
122       }
123       return null;
124    }
125 }
126
Popular Tags