628
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.metadata;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.OutputStream;
|
|
21 |
import java.sql.Connection;
|
|
22 |
import java.sql.DriverManager;
|
|
23 |
import java.sql.ResultSet;
|
|
24 |
import java.sql.SQLException;
|
|
25 |
import java.sql.Statement;
|
|
26 |
import java.util.Hashtable;
|
|
27 |
|
|
28 |
import javax.persistence.EntityManager;
|
|
29 |
import javax.persistence.EntityManagerFactory;
|
|
30 |
import javax.persistence.Persistence;
|
|
31 |
|
|
32 |
import org.apache.commons.io.FileUtils;
|
|
33 |
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
|
34 |
|
|
35 |
import com.nokia.helium.metadata.ant.types.SeverityEnum;
|
|
36 |
import com.nokia.helium.metadata.model.Version;
|
|
37 |
import com.nokia.helium.metadata.model.metadata.Severity;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Class implementing a EntityManagerFactoryCreator for the derby database.
|
|
41 |
* This will be used to create new EntityManagerFactory for each database. It also
|
|
42 |
* handles the driver management, and basic factory settings.
|
|
43 |
*
|
|
44 |
*/
|
|
45 |
public class DerbyFactoryManagerCreator implements EntityManagerFactoryCreator {
|
|
46 |
|
|
47 |
public static final OutputStream DEV_NULL = new OutputStream() {
|
|
48 |
public void write(int b) { }
|
|
49 |
};
|
|
50 |
|
|
51 |
public synchronized EntityManagerFactory create(File database) throws MetadataException {
|
|
52 |
EntityManagerFactory factory;
|
|
53 |
String name = "metadata";
|
|
54 |
Hashtable<String, String> persistProperties = new Hashtable<String, String>();
|
|
55 |
persistProperties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
|
|
56 |
// This swallow all the output log from derby.
|
|
57 |
System.setProperty("derby.stream.error.field", "com.nokia.helium.metadata.DerbyFactoryManagerCreator.DEV_NULL");
|
|
58 |
persistProperties.put("javax.persistence.jdbc.url",
|
|
59 |
"jdbc:derby:" + database.getAbsolutePath());
|
|
60 |
persistProperties.put(
|
|
61 |
PersistenceUnitProperties.PERSISTENCE_CONTEXT_CLOSE_ON_COMMIT,
|
|
62 |
"false");
|
|
63 |
persistProperties.put(
|
|
64 |
PersistenceUnitProperties.PERSISTENCE_CONTEXT_REFERENCE_MODE,
|
|
65 |
"WEAK");
|
|
66 |
persistProperties.put(PersistenceUnitProperties.BATCH_WRITING,
|
|
67 |
"JDBC");
|
|
68 |
persistProperties.put("eclipselink.read-only", "true");
|
|
69 |
persistProperties.put(PersistenceUnitProperties.LOGGING_LEVEL, "warning");
|
|
70 |
if (database.exists()) {
|
|
71 |
if (!checkDatabaseIntegrity(database)) {
|
|
72 |
try {
|
|
73 |
FileUtils.forceDelete(database);
|
|
74 |
} catch (java.io.IOException iex) {
|
|
75 |
throw new MetadataException("Failed deleting corrupted db: " + iex, iex);
|
|
76 |
}
|
|
77 |
} else {
|
|
78 |
return
|
|
79 |
Persistence.createEntityManagerFactory(
|
|
80 |
name,
|
|
81 |
persistProperties);
|
|
82 |
}
|
|
83 |
}
|
|
84 |
persistProperties.put("javax.persistence.jdbc.url",
|
|
85 |
"jdbc:derby:" + database + ";create=true");
|
|
86 |
persistProperties.put(PersistenceUnitProperties.DDL_GENERATION,
|
|
87 |
"create-tables");
|
|
88 |
persistProperties.put(
|
|
89 |
PersistenceUnitProperties.DDL_GENERATION_MODE,
|
|
90 |
"database");
|
|
91 |
persistProperties.put(
|
|
92 |
PersistenceUnitProperties.PERSISTENCE_CONTEXT_CLOSE_ON_COMMIT,
|
|
93 |
"false");
|
|
94 |
persistProperties.put(
|
|
95 |
PersistenceUnitProperties.PERSISTENCE_CONTEXT_REFERENCE_MODE,
|
|
96 |
"WEAK");
|
|
97 |
persistProperties.put(PersistenceUnitProperties.BATCH_WRITING,
|
|
98 |
"JDBC");
|
|
99 |
persistProperties.put("eclipselink.read-only", "true");
|
|
100 |
factory = Persistence.createEntityManagerFactory(
|
|
101 |
name,
|
|
102 |
persistProperties);
|
|
103 |
EntityManager entityManager = factory.createEntityManager();
|
|
104 |
// Pushing default data into the current schema
|
|
105 |
try {
|
|
106 |
entityManager.getTransaction().begin();
|
|
107 |
// Version of the schema is pushed.
|
|
108 |
entityManager.persist(new Version());
|
|
109 |
// Default set of severity is pushed.
|
|
110 |
for (SeverityEnum.Severity severity : SeverityEnum.Severity.values()) {
|
|
111 |
Severity pData = new Severity();
|
|
112 |
pData.setSeverity(severity.toString());
|
|
113 |
entityManager.persist(pData);
|
|
114 |
}
|
|
115 |
entityManager.getTransaction().commit();
|
|
116 |
} finally {
|
|
117 |
if (entityManager.getTransaction().isActive()) {
|
|
118 |
entityManager.getTransaction().rollback();
|
|
119 |
entityManager.clear();
|
|
120 |
}
|
|
121 |
entityManager.close();
|
|
122 |
}
|
|
123 |
return factory;
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Checks the database integrity.
|
|
128 |
* @param urlPath - database path to be connected to.
|
|
129 |
* @return boolean - true if db is valid false otherwise.
|
|
130 |
*/
|
|
131 |
private static boolean checkDatabaseIntegrity(File database) throws MetadataException {
|
|
132 |
boolean result = false;
|
|
133 |
Connection connection = null;
|
|
134 |
try {
|
|
135 |
connection = DriverManager.getConnection("jdbc:derby:" + database);
|
|
136 |
if (connection != null) {
|
|
137 |
Statement stmt = connection.createStatement();
|
|
138 |
ResultSet rs = stmt.executeQuery("select version from version");
|
|
139 |
int version = -1;
|
|
140 |
if ( rs.next()) {
|
|
141 |
version = rs.getInt(1);
|
|
142 |
}
|
|
143 |
rs.close();
|
|
144 |
stmt.close();
|
|
145 |
connection.close();
|
|
146 |
connection = null;
|
|
147 |
result = version == Version.DB_VERSION;
|
|
148 |
}
|
|
149 |
} catch (SQLException ex) {
|
|
150 |
try {
|
|
151 |
DriverManager.getConnection("jdbc:derby:;shutdown=true");
|
|
152 |
} catch (java.sql.SQLException sex) {
|
|
153 |
// normal exception during database shutdown
|
|
154 |
connection = null;
|
|
155 |
}
|
|
156 |
return false;
|
|
157 |
} finally {
|
|
158 |
try {
|
|
159 |
if (connection != null) {
|
|
160 |
connection.close();
|
|
161 |
}
|
|
162 |
} catch (java.sql.SQLException sex) {
|
|
163 |
// normal exception during database shutdown
|
|
164 |
connection = null;
|
|
165 |
}
|
|
166 |
connection = null;
|
|
167 |
if (!result) {
|
|
168 |
try {
|
|
169 |
DriverManager.getConnection("jdbc:derby:;shutdown=true");
|
|
170 |
} catch (java.sql.SQLException sex) {
|
|
171 |
// normal exception during database shutdown
|
|
172 |
connection = null;
|
|
173 |
}
|
|
174 |
}
|
|
175 |
}
|
|
176 |
//shutdown unloads the driver, driver need to be loaded again.
|
|
177 |
return result;
|
|
178 |
}
|
|
179 |
|
|
180 |
public synchronized void unload(File database) {
|
|
181 |
try {
|
|
182 |
DriverManager.getConnection("jdbc:derby:" + database + ";shutdown=true");
|
|
183 |
} catch (SQLException e) {
|
|
184 |
// normal exception during database shutdown
|
|
185 |
e = null;
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
public synchronized void initialize() throws MetadataException {
|
|
190 |
try {
|
|
191 |
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
|
|
192 |
} catch (java.lang.ClassNotFoundException e) {
|
|
193 |
throw new MetadataException("JDBC Driver could not be found", e);
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
}
|