587
|
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 ORMUtil() {
|
|
42 |
}
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Initializes the entity manager and begins the transcations.
|
|
46 |
* @param urlPath - database path to be connected to.
|
|
47 |
*/
|
|
48 |
public static synchronized void initializeORM(String urlPath) {
|
|
49 |
ORMEntityManager manager = emMap.get(urlPath);
|
|
50 |
log.debug("initializeORM: urlpath: " + urlPath);
|
|
51 |
if (manager == null) {
|
|
52 |
try {
|
|
53 |
manager = new ORMEntityManager(urlPath);
|
|
54 |
emMap.put(urlPath, manager);
|
|
55 |
log.debug("initializeORM: manager: " + manager);
|
|
56 |
log.debug("initializeORM: manager: " + manager.getEntityManager());
|
|
57 |
} catch ( IOException ex ) {
|
|
58 |
throw new BuildException("Entity Manager creation failure");
|
|
59 |
}
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Helper Function to return the entity manager.
|
|
65 |
* @return entity manager created during initialization.
|
|
66 |
*/
|
|
67 |
public static ORMEntityManager getEntityManager(String urlPath) {
|
|
68 |
log.debug("getEntityManager: urlpath: " + urlPath);
|
|
69 |
ORMEntityManager manager = emMap.get(urlPath);
|
|
70 |
if (manager != null) {
|
|
71 |
log.debug("getEntityManager: manager: " + manager);
|
|
72 |
log.debug("getEntityManager: manager.entityManager: " + manager.getEntityManager());
|
|
73 |
return manager;
|
|
74 |
} else {
|
|
75 |
log.debug("getEntityManager: manager: is null");
|
|
76 |
throw new BuildException("ORM entity manager is null");
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Finalize the entity manager and release all the objects.
|
|
82 |
*/
|
|
83 |
public static void finalizeORM(String urlPath) {
|
|
84 |
ORMEntityManager manager = emMap.get(urlPath);
|
|
85 |
log.debug("finalizeORM: urlpath: " + urlPath);
|
|
86 |
if (manager != null) {
|
|
87 |
manager.finalizeEntityManager();
|
|
88 |
manager = null;
|
|
89 |
log.debug("finalizeORM: manager" + manager);
|
|
90 |
emMap.remove(urlPath);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
} |