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