Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.resource.spi.se » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.resource.spi.se;
   15   
   16   import java.util.HashMap;
   17   import java.util.Map;
   18   
   19   import javax.persistence.EntityManager;
   20   
   21   public class EntityManagersManager
   22   {
   23   
   24       /**
   25        * key = unitName + "." + "name"
   26        * value = ThreadLocal<EntityManager>
   27        */
   28       private Map<String, ThreadLocal<EntityManager>> entityManagers = new HashMap<String, ThreadLocal<EntityManager>>();
   29       
   30       
   31       /**
   32        * @param unitName
   33        * @param name
   34        * @return the EntityManager or <code>null</code> if not yet set
   35        */
   36       public EntityManager get(String unitName, String name)
   37       {
   38           EntityManager em = null;
   39           String key = getKey(unitName, name);
   40           
   41           ThreadLocal<EntityManager> tl = entityManagers.get(key);
   42           if (tl != null)
   43           {
   44               return tl.get();
   45           }
   46           
   47           return em;
   48       }
   49   
   50       /**
   51        * Set the given EntityManager for the current Thread.
   52        * This will replace any previously stored EntityManager for this
   53        * unitName and name.
   54        *  
   55        * @param unitName
   56        * @param name
   57        * @param em
   58        */
   59       public void set(String unitName, String name, EntityManager em)
   60       {
   61           String key = getKey(unitName, name);
   62   
   63           ThreadLocal<EntityManager> tl = entityManagers.get(key);
   64           if (tl == null)
   65           {
   66               tl = new ThreadLocal<EntityManager>();
   67               entityManagers.put(key, tl);
   68           }
   69   
   70           tl.set(em);
   71       }
   72   
   73       
   74       /**
   75        * construct the key for the {@link #entityManagers} Map
   76        * @return String key for the map
   77        */
   78       private String getKey(String unitName, String name)
   79       {
   80           if (unitName == null)
   81           {
   82               unitName = "";
   83           }
   84           
   85           if (name == null)
   86           {
   87               name = "";
   88           }
   89           
   90           return unitName + "." + name;
   91       }
   92       
   93   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.resource.spi.se » [javadoc | source]