1 25 26 package org.objectweb.easybeans.persistence; 27 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 import javax.persistence.EntityManager; 33 import javax.persistence.EntityManagerFactory; 34 import javax.persistence.PersistenceContextType; 35 36 import org.objectweb.easybeans.persistence.api.EZBPersistenceUnitManager; 37 import org.objectweb.easybeans.persistence.xml.JPersistenceUnitInfo; 38 39 44 public class PersistenceUnitManager implements EZBPersistenceUnitManager { 45 46 49 private Map <String , JPersistenceContext> persistenceContexts = null; 50 51 55 public PersistenceUnitManager(final JPersistenceUnitInfo[] persistenceUnitInfos) { 56 this.persistenceContexts = new HashMap <String , JPersistenceContext>(); 57 if (persistenceUnitInfos != null) { 59 for (JPersistenceUnitInfo pUnitInfo : persistenceUnitInfos) { 60 persistenceContexts.put(pUnitInfo.getPersistenceUnitName(), new JPersistenceContext(pUnitInfo)); 61 } 62 } 63 } 64 65 70 private JPersistenceContext getPersistenceContext(final String unitName) { 71 if (unitName == null || unitName.equals("")) { 72 if (persistenceContexts.size() == 0) { 73 throw new IllegalArgumentException ("No persistence-unit defined"); 74 } else if (persistenceContexts.size() > 1) { 75 throw new IllegalArgumentException ("Too many persistence-unit defined, cannot take the default one."); 76 } 77 return persistenceContexts.values().iterator().next(); 78 } 79 JPersistenceContext persistenceContext = persistenceContexts.get(unitName); 81 if (persistenceContext == null) { 82 throw new IllegalArgumentException ("No persistence-unit with name '" + unitName + "' defined."); 83 } 84 return persistenceContext; 85 } 86 87 93 public EntityManager getEntityManager(final String unitName, final PersistenceContextType type) { 94 return getPersistenceContext(unitName).getTxEntityManager(); 96 } 97 98 103 public EntityManagerFactory getEntityManagerFactory(final String unitName) { 104 return getPersistenceContext(unitName).getEntityManagerFactory(); 105 } 106 107 111 public void addCurrent() { 112 for (Iterator <JPersistenceContext> itContext = persistenceContexts.values().iterator(); itContext.hasNext();) { 113 JPersistenceContext pContext = itContext.next(); 114 pContext.addCurrent(); 115 } 116 } 117 118 122 public void closeCurrentAndReturnToPrevious() { 123 for (Iterator <JPersistenceContext> itContext = persistenceContexts.values().iterator(); itContext.hasNext();) { 124 JPersistenceContext pContext = itContext.next(); 125 pContext.closeCurrentAndReturnToPrevious(); 126 } 127 } 128 129 138 public void merge(final EZBPersistenceUnitManager otherEZBPersistenceUnitManager) { 139 if (otherEZBPersistenceUnitManager == null) { 141 return; 142 } 143 PersistenceUnitManager otherPersistenceUnitManager = null; 144 if (otherEZBPersistenceUnitManager instanceof PersistenceUnitManager) { 145 otherPersistenceUnitManager = (PersistenceUnitManager) otherEZBPersistenceUnitManager; 146 } else { 147 return; 148 } 149 150 for (Iterator <String > itContext = otherPersistenceUnitManager.persistenceContexts.keySet().iterator(); itContext 152 .hasNext();) { 153 String unitName = itContext.next(); 154 JPersistenceContext pContext = persistenceContexts.get(unitName); 156 if (pContext == null) { 157 persistenceContexts.put(unitName, otherPersistenceUnitManager.persistenceContexts.get(unitName)); 159 } 160 } 161 162 } 163 164 } 165 | Popular Tags |