Author: jochen
Date: Thu Dec 23 14:25:39 2010
New Revision: 1052280
URL: http://svn.apache.org/viewvc?rev=1052280&view=rev
Log:
Added the SqlBatchProcessor.
Added:
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/SqlBatchProcessor.java
(with props)
Modified:
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/DbInitializer.java
Modified:
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/DbInitializer.java
URL:
http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/DbInitializer.java?rev=1052280&r1=1052279&r2=1052280&view=diff
==============================================================================
---
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/DbInitializer.java
(original)
+++
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/DbInitializer.java
Thu Dec 23 14:25:39 2010
@@ -20,12 +20,8 @@ package org.apache.labs.jaxmas.registry.
import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
import java.net.URL;
import java.sql.Connection;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.xml.registry.JAXRException;
@@ -34,9 +30,6 @@ import javax.xml.registry.RegistryServic
import org.apache.labs.jaxmas.registry.infomodel.ConnectionImpl;
import org.apache.labs.jaxmas.registry.infomodel.RegistryServiceImpl;
import org.apache.labs.jaxmas.registry.sql.DbDriver;
-import org.apache.labs.jaxmas.registry.util.DefaultLoggerFactory;
-import org.apache.labs.jaxmas.registry.util.Logger;
-import org.apache.labs.jaxmas.registry.util.Strings;
/**
* The database initializer is invoked at startup. It checks,
@@ -44,8 +37,6 @@ import org.apache.labs.jaxmas.registry.u
* it, if required.
*/
public class DbInitializer {
- private static final Logger log =
DefaultLoggerFactory.getInstance().newLogger(DbInitializer.class);
-
private ConnectionImpl connection;
/**
@@ -77,127 +68,38 @@ public class DbInitializer {
return ((RegistryServiceImpl)
getRegistryService()).getDbDriver();
}
- /**
- * Returns, whether the character sequence contains a string, which
- * ends with the character ';'. Terminating white space is ignored.
- */
- private boolean endsWithSemicolon(CharSequence pCharSequence) {
- for (int i = pCharSequence.length()-1; i >= 0; i--) {
- char c = pCharSequence.charAt(i);
- if (!Character.isWhitespace(c)) {
- return c == ';';
- }
- }
- return false; // Only white space found
- }
-
- private String replaceProperties(String pCommand) throws JAXRException {
- String command = pCommand;
- for (;;) {
- int offset = command.indexOf("${"); //$NON-NLS-1$
- if (offset == -1) {
- return command;
- }
- int end = command.indexOf('}', offset+2);
- if (offset == -1) {
- throw new IllegalStateException("Failed to parse command: "
+ command); //$NON-NLS-1$
- }
- final String property = command.substring(offset+2, end);
- command = command.substring(0, offset) +
getDbDriver().getDbProperty(property) + command.substring(end+1);
- }
- }
-
- /**
- * Runs a single SQL command.
- */
- protected void sql(Connection pConnection, String pCommand) throws
SQLException, JAXRException {
- final String mName = "sql"; //$NON-NLS-1$
- if (Strings.isTrimmedEmpty(pCommand)) {
- return;
- }
- final String command = replaceProperties(pCommand);
- final int offset = command.lastIndexOf(';');
- final String cmd = command.substring(0, offset);
- log.debug(mName, cmd);
- PreparedStatement stmt = null;
- try {
- stmt = pConnection.prepareStatement(cmd);
- stmt.executeUpdate();
- stmt.close();
- stmt = null;
- } catch (SQLException e) {
- // Ignore "Table does not exist" messages in DROP TABLE statements.
- if
(RegistryServiceImpl.getDbDriver(connection.getRegistryService()).isUnknownTableError(e))
{
- return;
- }
- throw e;
- } finally {
- if (stmt != null) { try { stmt.close(); } catch
(Throwable t) { /* Ignore me */ } }
- }
- }
-
- /**
- * Updates the database by applying a schema update.
- */
- protected void initialize(Connection pConnection, BufferedReader
pReader)
- throws IOException, SQLException, JAXRException {
- final StringBuilder sb = new StringBuilder();
- for (;;) {
- final String s = pReader.readLine();
- if (s == null) {
- sql(pConnection, sb.toString());
- return;
- }
- if (s.trim().startsWith("--")) { //$NON-NLS-1$
- continue;
- }
- sb.append(s);
- sb.append('\n');
- if (endsWithSemicolon(sb)) {
- sql(pConnection, sb.toString());
- sb.setLength(0);
- }
- }
- }
/**
* Updates the database by applying a schema update.
*/
- protected void initialize(SchemaUpdater pSchemaUpdater, URL
pSchemaFile) throws JAXRException {
- Connection conn = connection.getConnection();
- InputStream stream = null;
- Reader reader = null;
- BufferedReader bReader = null;
- try {
- conn.setAutoCommit(false);
- stream = pSchemaFile.openStream();
- reader = new InputStreamReader(stream, "UTF-8");
//$NON-NLS-1$
- bReader = new
BufferedReader(MacroProcessor.newInstance(reader));
- if (pSchemaUpdater != null) {
- pSchemaUpdater.beforeUpdate(conn,
connection.getRegistryService());
- }
- initialize(conn, bReader);
- if (pSchemaUpdater != null) {
- pSchemaUpdater.afterUpdate(conn,
connection.getRegistryService());
+ protected void initialize(final SchemaUpdater pSchemaUpdater, URL
pSchemaFile) throws JAXRException {
+ final Connection conn = connection.getConnection();
+ final SqlBatchProcessor sqlBatchProcessor = new
SqlBatchProcessor() {
+ @Override
+ protected String getProperty(String pKey) throws
JAXRException {
+ return getDbDriver().getDbProperty(pKey);
+ }
+
+ @Override
+ protected boolean isUnknownTableError(SQLException
pException) throws JAXRException {
+ final DbDriver dbDriver =
RegistryServiceImpl.getDbDriver(connection.getRegistryService());
+ return dbDriver.isUnknownTableError(pException);
+ }
+
+ @Override
+ protected void process(BufferedReader pReader,
+ Connection pConnection) throws
IOException, SQLException,
+ JAXRException {
+ if (pSchemaUpdater != null) {
+ pSchemaUpdater.beforeUpdate(conn,
connection.getRegistryService());
+ }
+ super.process(pReader, pConnection);
+ if (pSchemaUpdater != null) {
+ pSchemaUpdater.afterUpdate(conn,
connection.getRegistryService());
+ }
}
- bReader.close();
- bReader = null;
- reader.close();
- reader = null;
- stream.close();
- stream = null;
- conn.commit();
- conn = null;
- } catch (IOException e) {
- throw new JAXRException(e);
- } catch (SQLException e) {
- throw new JAXRException(e);
- } finally {
- if (bReader != null) { try { bReader.close(); } catch
(Throwable t) { /* Ignore me */ } }
- if (reader != null) { try { reader.close(); } catch
(Throwable t) { /* Ignore me */ } }
- if (stream != null) { try { stream.close(); } catch
(Throwable t) { /* Ignore me */ } }
- if (conn != null) { try { conn.rollback(); } catch
(Throwable t) { /* Ignore me */ } }
- }
+ };
+ sqlBatchProcessor.process(pSchemaFile, conn);
}
private String getSchemaUpdaterClassName(int pNum) {
Added:
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/SqlBatchProcessor.java
URL:
http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/SqlBatchProcessor.java?rev=1052280&view=auto
==============================================================================
---
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/SqlBatchProcessor.java
(added)
+++
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/SqlBatchProcessor.java
Thu Dec 23 14:25:39 2010
@@ -0,0 +1,140 @@
+package org.apache.labs.jaxmas.registry.schema;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import javax.xml.registry.JAXRException;
+
+import org.apache.labs.jaxmas.registry.util.DefaultLoggerFactory;
+import org.apache.labs.jaxmas.registry.util.Logger;
+import org.apache.labs.jaxmas.registry.util.Strings;
+
+
+public abstract class SqlBatchProcessor {
+ private static final Logger log =
DefaultLoggerFactory.getInstance().newLogger(SqlBatchProcessor.class);
+
+ protected abstract String getProperty(String pName) throws
JAXRException;
+ protected abstract boolean isUnknownTableError(SQLException pException)
throws JAXRException;
+
+ /**
+ * Returns, whether the character sequence contains a string, which
+ * ends with the character ';'. Terminating white space is ignored.
+ */
+ private boolean endsWithSemicolon(CharSequence pCharSequence) {
+ for (int i = pCharSequence.length()-1; i >= 0; i--) {
+ char c = pCharSequence.charAt(i);
+ if (!Character.isWhitespace(c)) {
+ return c == ';';
+ }
+ }
+ return false; // Only white space found
+ }
+
+ private String replaceProperties(String pCommand) throws JAXRException {
+ String command = pCommand;
+ for (;;) {
+ int offset = command.indexOf("${"); //$NON-NLS-1$
+ if (offset == -1) {
+ return command;
+ }
+ int end = command.indexOf('}', offset+2);
+ if (offset == -1) {
+ throw new IllegalStateException("Failed to parse command: "
+ command); //$NON-NLS-1$
+ }
+ final String property = command.substring(offset+2, end);
+ command = command.substring(0, offset) + getProperty(property)
+ command.substring(end+1);
+ }
+ }
+
+ /**
+ * Runs a single SQL command.
+ */
+ protected void sql(Connection pConnection, String pCommand) throws
SQLException, JAXRException {
+ final String mName = "sql"; //$NON-NLS-1$
+ if (Strings.isTrimmedEmpty(pCommand)) {
+ return;
+ }
+ final String command = replaceProperties(pCommand);
+ final int offset = command.lastIndexOf(';');
+ final String cmd = command.substring(0, offset);
+ log.debug(mName, cmd);
+ PreparedStatement stmt = null;
+ try {
+ stmt = pConnection.prepareStatement(cmd);
+ stmt.executeUpdate();
+ stmt.close();
+ stmt = null;
+ } catch (SQLException e) {
+ // Ignore "Table does not exist" messages in DROP TABLE statements.
+ if (isUnknownTableError(e)) {
+ return;
+ }
+ throw e;
+ } finally {
+ if (stmt != null) { try { stmt.close(); } catch
(Throwable t) { /* Ignore me */ } }
+ }
+ }
+
+ protected void process(BufferedReader pReader, Connection pConnection)
+ throws IOException, SQLException, JAXRException {
+ final StringBuilder sb = new StringBuilder();
+ for (;;) {
+ final String s = pReader.readLine();
+ if (s == null) {
+ sql(pConnection, sb.toString());
+ return;
+ }
+ if (s.trim().startsWith("--")) { //$NON-NLS-1$
+ continue;
+ }
+ sb.append(s);
+ sb.append('\n');
+ if (endsWithSemicolon(sb)) {
+ sql(pConnection, sb.toString());
+ sb.setLength(0);
+ }
+ }
+ }
+
+ /**
+ * Updates the database by applying a schema update.
+ */
+ public void process(URL pURL, Connection pConnection) throws
JAXRException {
+ Connection conn = pConnection;
+ InputStream stream = null;
+ Reader reader = null;
+ BufferedReader bReader = null;
+ try {
+ conn.setAutoCommit(false);
+ stream = pURL.openStream();
+ reader = new InputStreamReader(stream, "UTF-8");
//$NON-NLS-1$
+ bReader = new
BufferedReader(MacroProcessor.newInstance(reader));
+ process(bReader, conn);
+ bReader.close();
+ bReader = null;
+ reader.close();
+ reader = null;
+ stream.close();
+ stream = null;
+ conn.commit();
+ conn = null;
+ } catch (IOException e) {
+ throw new JAXRException(e);
+ } catch (SQLException e) {
+ throw new JAXRException(e);
+ } finally {
+ if (bReader != null) { try { bReader.close(); } catch
(Throwable t) { /* Ignore me */ } }
+ if (reader != null) { try { reader.close(); } catch
(Throwable t) { /* Ignore me */ } }
+ if (stream != null) { try { stream.close(); } catch
(Throwable t) { /* Ignore me */ } }
+ if (conn != null) { try { conn.rollback(); } catch
(Throwable t) { /* Ignore me */ } }
+ }
+ }
+
+}
Propchange:
labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/schema/SqlBatchProcessor.java
------------------------------------------------------------------------------
svn:executable = *
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]