buildframework/helium/sf/java/jpa/src/com/nokia/helium/jpa/ORMUtil.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 629 541af5ee3ed9
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
     1 
       
     2 /*
       
     3  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     4  * All rights reserved.
       
     5  * This component and the accompanying materials are made available
       
     6  * under the terms of the License "Eclipse Public License v1.0"
       
     7  * which accompanies this distribution, and is available
       
     8  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9  *
       
    10  * Initial Contributors:
       
    11  * Nokia Corporation - initial contribution.
       
    12  *
       
    13  * Contributors:
       
    14  *
       
    15  * Description:  
       
    16  *
       
    17  */
       
    18 
       
    19 package com.nokia.helium.jpa;
       
    20 
       
    21 import org.apache.log4j.Logger;
       
    22 import java.util.HashMap;
       
    23 import org.apache.tools.ant.BuildException;
       
    24 import java.io.IOException;
       
    25 
       
    26 /**
       
    27  * Utility class to communicate to the database using JPA entity
       
    28  * manager.
       
    29  */
       
    30 public final class ORMUtil {
       
    31 
       
    32     private static Logger log = Logger.getLogger(ORMUtil.class);
       
    33     
       
    34     private static final int PERSISTANCE_COUNT_LIMIT = 1000;
       
    35 
       
    36     private static final int READ_CACHE_LIMIT = 30000;
       
    37 
       
    38     private static HashMap<String, ORMEntityManager> emMap = 
       
    39         new HashMap<String, ORMEntityManager>();
       
    40 
       
    41     private static HashMap<String, Integer> emMapCount = 
       
    42         new HashMap<String, Integer>();
       
    43 
       
    44     private static Object mutexObject = new Object();
       
    45     private ORMUtil() {
       
    46     }
       
    47     
       
    48     /**
       
    49      * Initializes the entity manager and begins the transcations.
       
    50      * @param urlPath - database path to be connected to.
       
    51      */
       
    52     public static synchronized void initializeORM(String urlPath) {
       
    53         ORMEntityManager manager = emMap.get(urlPath);
       
    54         log.debug("initializeORM: urlpath: " + urlPath);
       
    55         if (manager == null) {
       
    56             try {
       
    57                 log.debug("initializing for the first time");
       
    58                 manager = new ORMEntityManager(urlPath);
       
    59                 emMap.put(urlPath, manager);
       
    60                 Integer countObj = new Integer(1);
       
    61                 emMapCount.put(urlPath, countObj);
       
    62                 log.debug("initializeORM: manager: " + manager);
       
    63                 log.debug("initializeORM: manager: " + manager.getEntityManager());
       
    64             } catch ( IOException ex ) {
       
    65                 throw new BuildException("Entity Manager creation failure");
       
    66             }
       
    67         } else {
       
    68             Integer countObj = emMapCount.get(urlPath);
       
    69             log.debug("object exists and incrementing the value");
       
    70             countObj = new Integer(countObj.intValue() + 1);
       
    71             log.debug("object exists count value: " + countObj.intValue());
       
    72             emMapCount.put(urlPath, countObj);
       
    73         }
       
    74     }
       
    75 
       
    76     /**
       
    77      * Helper Function to return the entity manager.
       
    78      * @return entity manager created during initialization.
       
    79      */
       
    80     public static synchronized ORMEntityManager getEntityManager(String urlPath) {
       
    81         log.debug("getEntityManager: urlpath: " + urlPath);
       
    82         ORMEntityManager manager = emMap.get(urlPath);
       
    83         if (manager != null) {
       
    84             log.debug("getEntityManager: manager: " + manager);
       
    85             log.debug("getEntityManager: manager.entityManager: " + manager.getEntityManager());
       
    86             return manager;
       
    87         } else {
       
    88             log.debug("getEntityManager: manager: is null");
       
    89             throw new BuildException("ORM entity manager is null");
       
    90         }
       
    91     }
       
    92     
       
    93     /**
       
    94      * Finalize the entity manager and release all the objects.
       
    95      */
       
    96     public static synchronized void finalizeORM(String urlPath) {
       
    97         ORMEntityManager manager = emMap.get(urlPath);
       
    98         log.debug("finalizeORM: urlpath: " + urlPath);
       
    99         if (manager != null) {
       
   100             Integer countObj = emMapCount.get(urlPath);
       
   101             if (countObj != null) {
       
   102                 int count = countObj.intValue();
       
   103                 count = count - 1;
       
   104                 if (count > 0) {
       
   105                     countObj = new Integer(count);
       
   106                     log.debug("countOBj value: " + countObj.intValue());
       
   107                     emMapCount.put(urlPath, countObj);
       
   108                 } else {
       
   109                     manager.finalizeEntityManager();
       
   110                     manager = null;
       
   111                     log.debug("finalizeORM: manager" + manager);
       
   112                     emMap.remove(urlPath);
       
   113                     emMapCount.remove(urlPath);
       
   114                 }
       
   115             }
       
   116         }
       
   117     }
       
   118     
       
   119     public static Object getMutexObject() {
       
   120         return mutexObject;
       
   121     }
       
   122 }