1 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 ; 30 import java.util.List ; 31 import java.util.LinkedHashMap ; 32 33 39 public class DeploymentPersistenceUnitResolver 40 { 41 protected List <PersistenceUnitDeployment> persistenceUnitDeployments; 42 protected DeploymentScope deploymentScope; 43 protected LinkedHashMap ejbContainers; 44 45 public DeploymentPersistenceUnitResolver(List <PersistenceUnitDeployment> persistenceUnitDeployments, DeploymentScope deploymentScope, LinkedHashMap ejbContainers) 46 { 47 this.persistenceUnitDeployments = persistenceUnitDeployments; 48 this.deploymentScope = deploymentScope; 49 this.ejbContainers = ejbContainers; 50 } 51 52 public PersistenceUnitDeployment getPersistenceUnitDeployment(String unitName) throws NameNotFoundException 53 { 54 if ("".equals(unitName)) 55 { 56 if (persistenceUnitDeployments == null) 57 { 58 throw new NameNotFoundException ("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 ("EMPTY STRING unitName and there is more than one scoped persistence unit"); 67 } 68 throw new NameNotFoundException ("There is no default persistence unit in this deployment."); 69 } 70 int hashIndex = unitName.indexOf('#'); 71 if (hashIndex != -1) 72 { 73 String relativePath = unitName.substring(0, hashIndex); 74 String name = unitName.substring(hashIndex + 1); 75 if (deploymentScope == null) 76 { 77 String relativeJarName = relativePath.substring(3); 78 for (PersistenceUnitDeployment pud : PersistenceUnitRegistry.getPersistenceUnits()) 80 { 81 String 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 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 |