001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.persistence;
019
020 import java.util.Map;
021 import java.util.HashMap;
022 import java.util.Iterator;
023
024 import javax.transaction.Transaction;
025
026 /**
027 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
028 */
029 public class EntityManagerExtendedRegistry {
030
031 private static final ThreadLocal<Map<String, InternalCMPEntityManagerExtended>> entityManagerMaps = new ThreadLocal<Map<String, InternalCMPEntityManagerExtended>>() {
032 protected Map<String, InternalCMPEntityManagerExtended> initialValue() {
033 return new HashMap<String, InternalCMPEntityManagerExtended>();
034 }
035 };
036
037 public static InternalCMPEntityManagerExtended getEntityManager(String persistenceUnit) {
038 Map<String, InternalCMPEntityManagerExtended> entityManagerMap = entityManagerMaps.get();
039 return entityManagerMap.get(persistenceUnit);
040 }
041
042 public static void putEntityManager(String persistenceUnit, InternalCMPEntityManagerExtended entityManager) {
043 Map<String, InternalCMPEntityManagerExtended> entityManagerMap = entityManagerMaps.get();
044 InternalCMPEntityManagerExtended oldEntityManager = entityManagerMap.put(persistenceUnit, entityManager);
045 if (oldEntityManager != null) {
046 throw new IllegalStateException("There was already an EntityManager registered for persistenceUnit " + persistenceUnit);
047 }
048 }
049
050 public static void clearEntityManager(String persistenceUnit) {
051 Map<String, InternalCMPEntityManagerExtended> entityManagerMap = entityManagerMaps.get();
052 entityManagerMap.remove(persistenceUnit);
053 }
054
055 public static void threadAssociated(Transaction transaction) {
056 Map<String, InternalCMPEntityManagerExtended> entityManagerMap = entityManagerMaps.get();
057 for (Iterator i = entityManagerMap.values().iterator(); i.hasNext(); ) {
058 InternalCMPEntityManagerExtended entityManager = (InternalCMPEntityManagerExtended) i.next();
059 entityManager.joinTransaction();
060 }
061 }
062
063 public static void threadUnassociated(Transaction transaction) {
064 //Any way to unassociate?
065 }
066 }