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 package com.nokia.helium.jpa.entity.metadata; |
|
19 |
|
20 import java.util.HashMap; |
|
21 import com.nokia.helium.jpa.ORMEntityManager; |
|
22 import com.nokia.helium.jpa.ORMUtil; |
|
23 import org.apache.log4j.Logger; |
|
24 |
|
25 /** |
|
26 * Utility class for writing metadata information to db |
|
27 * using JPA. |
|
28 */ |
|
29 public final class MetadataUtil { |
|
30 |
|
31 private static Logger log = Logger.getLogger(MetadataUtil.class); |
|
32 |
|
33 private static Metadata metadata; |
|
34 |
|
35 private static HashMap<String, Metadata> metadataMap = new HashMap<String, Metadata>(); |
|
36 |
|
37 private static Object mutexObject = new Object(); |
|
38 |
|
39 /** |
|
40 * Make sure the class cannot be instantiated |
|
41 */ |
|
42 private MetadataUtil() { |
|
43 } |
|
44 |
|
45 /** |
|
46 * Initialize the orm, calls ORMUtil initialize function to create |
|
47 * entity manager and commit count objects. |
|
48 * @param urlPath - url path for which the connection needs to be |
|
49 * initialized. |
|
50 */ |
|
51 public static void initializeORM(String urlPath) { |
|
52 synchronized (mutexObject) { |
|
53 ORMUtil.initializeORM(urlPath); |
|
54 } |
|
55 } |
|
56 |
|
57 /** |
|
58 * Finalize the orm, calls ORMUtil finalize function to close |
|
59 * entity manager. |
|
60 */ |
|
61 public static void finalizeORM(String urlPath) { |
|
62 synchronized (mutexObject) { |
|
63 log.debug("finalizing orm"); |
|
64 ORMUtil.finalizeORM(urlPath); |
|
65 } |
|
66 } |
|
67 |
|
68 /** |
|
69 * Finalize the orm, calls ORMUtil finalize function to close |
|
70 * entity manager. |
|
71 */ |
|
72 public static void finalizeMetadata(String urlPath, String logPath) { |
|
73 synchronized (mutexObject) { |
|
74 ORMEntityManager manager = ORMUtil.getEntityManager(urlPath); |
|
75 manager.commitToDB(); |
|
76 log.debug("finalizing metadata: " + logPath); |
|
77 metadataMap.remove(logPath); |
|
78 } |
|
79 } |
|
80 |
|
81 /** |
|
82 * Adding entry to the database. |
|
83 * @param entry - Adding a log entry |
|
84 */ |
|
85 public static void addEntry(String urlPath, Metadata.LogEntry entry) { |
|
86 synchronized (mutexObject) { |
|
87 metadata = getMetadata(entry.getLogPath(), urlPath); |
|
88 metadata.addEntry(entry); |
|
89 } |
|
90 } |
|
91 |
|
92 /** |
|
93 * |
|
94 */ |
|
95 public static void addEntry(String urlPath, String logPath, int time) { |
|
96 synchronized (mutexObject) { |
|
97 metadata = getMetadata(logPath, urlPath); |
|
98 metadata.addExecutionTime(time); |
|
99 } |
|
100 } |
|
101 |
|
102 /** |
|
103 * Remove entry from the database for specific log file. |
|
104 * @param urlPath - db path |
|
105 * @param logPath - log file for which all the entries to be removed. |
|
106 */ |
|
107 public static void removeEntries(String urlPath, String logPath) { |
|
108 synchronized (mutexObject) { |
|
109 metadata = getMetadata(logPath, urlPath); |
|
110 metadata.removeEntries(); |
|
111 finalizeMetadata(urlPath, logPath); |
|
112 } |
|
113 } |
|
114 |
|
115 /** |
|
116 * Returns the metadata associated with the log path, if metadata doesn't |
|
117 * exists in the cache, creates it. |
|
118 * @param urlPath - db path |
|
119 * @param logPath - log file for which all the entries to be removed. |
|
120 */ |
|
121 private static Metadata getMetadata(String logPath, String urlPath) { |
|
122 ORMEntityManager manager = ORMUtil.getEntityManager(urlPath); |
|
123 metadata = metadataMap.get(logPath); |
|
124 if (metadata == null) { |
|
125 log.debug("initializing metadatamap for logpath" + logPath); |
|
126 metadata = new Metadata(manager, logPath); |
|
127 |
|
128 metadataMap.put(logPath, metadata); |
|
129 } |
|
130 return metadata; |
|
131 } |
|
132 } |
|