sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arv...@apache.org
Subject svn commit: r1215051 - in /incubator/sqoop/branches/sqoop2: core/src/main/java/org/apache/sqoop/connector/ core/src/main/java/org/apache/sqoop/repository/ core/src/main/java/org/apache/sqoop/repository/model/ repository/repository-derby/src/main/java/o...
Date Fri, 16 Dec 2011 07:10:32 GMT
Author: arvind
Date: Fri Dec 16 07:10:32 2011
New Revision: 1215051

URL: http://svn.apache.org/viewvc?rev=1215051&view=rev
Log:
SQOOP-365. Implementation of derby repository in progress.

Added:
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java
  (with props)
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/MConnector.java
  (with props)
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaConstants.java
  (with props)
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaQuery.java
  (with props)
Modified:
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorHandler.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryContext.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/Repository.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryError.java
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepoError.java
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
Fri Dec 16 07:10:32 2011
@@ -37,7 +37,13 @@ public enum ConnectorError implements Er
   CONN_0004("Connector configuration did not include provider class name"),
 
   /** An exception occurred while attempting to instantiate the connector. */
-  CONN_0005("Failed to instantiate connector class");
+  CONN_0005("Failed to instantiate connector class"),
+
+  /** More than one connectors use the short name resulting in conflict. */
+  CONN_0006("More than one connector uses the same short name"),
+
+  /** The registration of connector during system initialization failed.*/
+  CONN_0007("Connector registration failed");
 
   private final String message;
 

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorHandler.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorHandler.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorHandler.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorHandler.java
Fri Dec 16 07:10:32 2011
@@ -32,10 +32,13 @@ public final class ConnectorHandler {
 
   private final Properties properties = new Properties();
 
+  private final String connectorUrl;
   private final String connectorClassName;
+  private final String connectorShortName;
   private final SqoopConnector connector;
 
   public ConnectorHandler(URL configFileUrl) {
+    connectorUrl = configFileUrl.toString();
     try {
       properties.load(configFileUrl.openStream());
     } catch (IOException ex) {
@@ -59,6 +62,8 @@ public final class ConnectorHandler {
               connectorClassName, ex);
     }
 
+    connectorShortName = connectorClass.getSimpleName();
+
     try {
       connector = (SqoopConnector) connectorClass.newInstance();
     } catch (IllegalAccessException ex) {
@@ -73,4 +78,21 @@ public final class ConnectorHandler {
       LOG.info("Connector [" + connectorClassName + "] initialized.");
     }
   }
+
+  public String toString() {
+    return "{" + connectorShortName + ":" + connectorClassName
+        + ":" + connectorUrl + "}";
+  }
+
+  public String getShortName() {
+    return connectorShortName;
+  }
+
+  public String getCanonicalName() {
+    return connectorClassName;
+  }
+
+  public String getConnectorUrl() {
+    return connectorUrl;
+  }
 }

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
Fri Dec 16 07:10:32 2011
@@ -21,18 +21,23 @@ import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 import org.apache.sqoop.core.ConfigurationConstants;
 import org.apache.sqoop.core.SqoopException;
+import org.apache.sqoop.repository.Repository;
+import org.apache.sqoop.repository.RepositoryManager;
+import org.apache.sqoop.repository.RepositoryTransaction;
 
 public class ConnectorManager {
 
   private static final Logger LOG = Logger.getLogger(ConnectorManager.class);
 
-  private static List<ConnectorHandler> handlers =
-      new ArrayList<ConnectorHandler>();
+  private static Map<String, ConnectorHandler> handlerMap =
+      new HashMap<String, ConnectorHandler>();
 
   public static synchronized void initialize() {
     if (LOG.isTraceEnabled()) {
@@ -71,11 +76,47 @@ public class ConnectorManager {
       }
 
       for (URL url : connectorConfigs) {
-        handlers.add(new ConnectorHandler(url));
+        ConnectorHandler handler = new ConnectorHandler(url);
+        ConnectorHandler handlerOld =
+            handlerMap.put(handler.getShortName(), handler);
+        if (handlerOld != null) {
+          throw new SqoopException(ConnectorError.CONN_0006,
+              handler + ", " + handlerOld);
+        }
       }
     } catch (IOException ex) {
       throw new SqoopException(ConnectorError.CONN_0001, ex);
     }
+
+    registerConnectors();
+
+    if (LOG.isInfoEnabled()) {
+      LOG.info("Connectors loaded: " + handlerMap);
+    }
+  }
+
+  private static synchronized void registerConnectors() {
+    Repository repository = RepositoryManager.getRepository();
+
+    RepositoryTransaction rtx = null;
+    try {
+      rtx = repository.getTransaction();
+      rtx.begin();
+      for (String name : handlerMap.keySet()) {
+        String connectorCanonicalName = handlerMap.get(name).getCanonicalName();
+        repository.registerConnector(name, connectorCanonicalName);
+      }
+      rtx.commit();
+    } catch (Exception ex) {
+      if (rtx != null) {
+        rtx.rollback();
+      }
+      throw new SqoopException(ConnectorError.CONN_0007, ex);
+    } finally {
+      if (rtx != null) {
+        rtx.close();
+      }
+    }
   }
 
 

Added: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java?rev=1215051&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java
Fri Dec 16 07:10:32 2011
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.repository;
+
+import java.sql.Connection;
+
+import org.apache.log4j.Logger;
+import org.apache.sqoop.core.SqoopException;
+import org.apache.sqoop.repository.model.MConnector;
+
+public class JdbcRepository implements Repository {
+
+  private static final Logger LOG =
+      Logger.getLogger(JdbcRepository.class);
+
+  private final JdbcRepositoryHandler handler;
+  private final JdbcRepositoryContext repoContext;
+
+  protected JdbcRepository(JdbcRepositoryHandler handler,
+      JdbcRepositoryContext repoContext) {
+    this.handler = handler;
+    this.repoContext = repoContext;
+  }
+
+  @Override
+  public JdbcRepositoryTransaction getTransaction() {
+    return repoContext.getTransactionFactory().get();
+  }
+
+  @Override
+  public void registerConnector(String shortName,
+      String canonicalName) {
+    JdbcRepositoryTransaction tx = null;
+    try {
+      tx = getTransaction();
+      tx.begin();
+      Connection conn = tx.getConnection();
+      MConnector connector = handler.findConnector(shortName, conn);
+      if (connector == null) {
+        // Insert (Register) connector
+      } else {
+        if (connector.getCanonicalName().equals(canonicalName)) {
+          // Already Registered
+          LOG.info("Connector (" + shortName + ":" + canonicalName
+              + ") already registered");
+        } else {
+          throw new SqoopException(RepositoryError.JDBCREPO_0013,
+              "(" + shortName + ":" + canonicalName + ") != ("
+              + connector.getShortName() + ":" + connector.getCanonicalName()
+              + ")");
+        }
+      }
+      tx.commit();
+    } catch (Exception ex) {
+      if (tx != null) {
+        tx.rollback();
+      }
+      if (ex instanceof SqoopException) {
+        throw (SqoopException) ex;
+      }
+      throw new SqoopException(RepositoryError.JDBCREPO_0012,
+          "(" + shortName + ":" + canonicalName + ")", ex);
+    } finally {
+      if (tx != null) {
+        tx.close();
+      }
+    }
+  }
+
+}

Propchange: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryContext.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryContext.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryContext.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryContext.java
Fri Dec 16 07:10:32 2011
@@ -20,10 +20,10 @@ package org.apache.sqoop.repository;
 import java.util.Map;
 import java.util.Properties;
 
+import javax.sql.DataSource;
+
 import org.apache.log4j.Logger;
-import org.apache.sqoop.core.ConfigurationConstants;
 import org.apache.sqoop.core.Context;
-import org.apache.sqoop.core.CoreError;
 import org.apache.sqoop.core.SqoopException;
 
 
@@ -41,6 +41,8 @@ public final class JdbcRepositoryContext
   private final JdbcTransactionIsolation transactionIsolation;
   private final int maxConnections;
 
+  private DataSource dataSource;
+  private JdbcRepositoryTransactionFactory txFactory;
 
   public JdbcRepositoryContext(Context context) {
     this.context = context;
@@ -158,6 +160,23 @@ public final class JdbcRepositoryContext
     }
   }
 
+  void initialize(DataSource source, JdbcRepositoryTransactionFactory factory) {
+    if (source != null || factory != null) {
+      throw new SqoopException(RepositoryError.JDBCREPO_0011);
+    }
+
+    dataSource = source;
+    txFactory = factory;
+  }
+
+  public DataSource getDataSource() {
+    return dataSource;
+  }
+
+  public JdbcRepositoryTransactionFactory getTransactionFactory() {
+    return txFactory;
+  }
+
   public String getHandlerClassName() {
     return handlerClassName;
   }

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java
Fri Dec 16 07:10:32 2011
@@ -17,14 +17,19 @@
  */
 package org.apache.sqoop.repository;
 
-import javax.sql.DataSource;
+import java.sql.Connection;
+
+import org.apache.sqoop.repository.model.MConnector;
 
 public interface JdbcRepositoryHandler {
 
-  public void initialize(DataSource dataSource,
-      JdbcRepositoryContext repoContext);
+  public void initialize(JdbcRepositoryContext repoContext);
 
-  public void shutdown();
+  public MConnector findConnector(String shortName, Connection conn);
+
+  public boolean schemaExists();
 
-  public Repository getRepository();
+  public void createSchema();
+
+  public void shutdown();
 }

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java
Fri Dec 16 07:10:32 2011
@@ -45,12 +45,14 @@ public class JdbcRepositoryProvider impl
 
   private JdbcRepositoryContext repoContext;
 
+  private Driver driver;
   private GenericObjectPool connectionPool;
   private KeyedObjectPoolFactory statementPool;
   private DataSource dataSource;
 
   private JdbcRepositoryHandler handler;
-  private Driver driver;
+  private JdbcRepositoryTransactionFactory txFactory;
+  private JdbcRepository repository;
 
 
   public JdbcRepositoryProvider() {
@@ -148,12 +150,26 @@ public class JdbcRepositoryProvider impl
         repoContext.getTransactionIsolation().getCode());
 
     dataSource = new PoolingDataSource(connectionPool);
+    txFactory = new JdbcRepositoryTransactionFactory(dataSource);
+
+    repoContext.initialize(dataSource, txFactory);
+
+    handler.initialize(repoContext);
+
+    if (repoContext.shouldCreateSchema()) {
+      if (!handler.schemaExists()) {
+        LOG.info("Creating repository schema objects");
+        handler.createSchema();
+      }
+    }
+
+    repository = new JdbcRepository(handler, repoContext);
 
-    handler.initialize(dataSource, repoContext);
+    LOG.info("JdbcRepositoryProvider initialized");
   }
 
   @Override
   public synchronized Repository getRepository() {
-    return handler.getRepository();
+    return repository;
   }
 }

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
Fri Dec 16 07:10:32 2011
@@ -35,10 +35,10 @@ public class JdbcRepositoryTransaction i
   private final DataSource dataSource;
   private Connection connection;
   private JdbcRepositoryTransactionFactory txFactory;
-  boolean active = true;
-  int count = 0;
+  private boolean active = true;
+  private int count = 0;
 
-  boolean rollback = false;
+  private boolean rollback = false;
 
   protected JdbcRepositoryTransaction(DataSource dataSource,
       JdbcRepositoryTransactionFactory factory) {
@@ -46,6 +46,10 @@ public class JdbcRepositoryTransaction i
     txFactory = factory;
   }
 
+  public Connection getConnection() {
+    return connection;
+  }
+
   @Override
   public void begin() {
     if (!active) {

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/Repository.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/Repository.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/Repository.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/Repository.java
Fri Dec 16 07:10:32 2011
@@ -26,4 +26,7 @@ package org.apache.sqoop.repository;
 public interface Repository {
 
   public RepositoryTransaction getTransaction();
+
+  public void registerConnector(
+      String connectorShortName, String connectorCanonicalName);
 }

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryError.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryError.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryError.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryError.java
Fri Dec 16 07:10:32 2011
@@ -65,7 +65,24 @@ public enum RepositoryError implements E
   JDBCREPO_0009("Failed to finalize transaction"),
 
   /** The system was not able to dergeister the driver during shutdown. */
-  JDBCREPO_0010("Unable to deregister driver during shutdown");
+  JDBCREPO_0010("Unable to deregister driver during shutdown"),
+
+  /**
+   * An attempt was made to reinitialize already
+   * initialized JDBC repository context.
+   */
+  JDBCREPO_0011("Attempt to reinitialize JDBC repository context"),
+
+  /** The system was unable to register the connector in its repository. */
+  JDBCREPO_0012("Failed to register connector in repository"),
+
+  /**
+   * More than one connectors installed in the system have the same
+   * short name.
+   */
+  JDBCREPO_0013("Could not register connectors due to conflicting names");
+
+
 
   private final String message;
 

Added: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/MConnector.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/MConnector.java?rev=1215051&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/MConnector.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/MConnector.java
Fri Dec 16 07:10:32 2011
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.repository.model;
+
+public class MConnector {
+
+  private String shortName;
+  private String canonicalName;
+
+  public MConnector(String shortName, String canonicalName) {
+    this.shortName = shortName;
+    this.canonicalName = canonicalName;
+  }
+
+  public String getShortName() {
+    return shortName;
+  }
+
+  public String getCanonicalName() {
+    return canonicalName;
+  }
+
+}

Propchange: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/model/MConnector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepoError.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepoError.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepoError.java
(original)
+++ incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepoError.java
Fri Dec 16 07:10:32 2011
@@ -28,7 +28,10 @@ public enum DerbyRepoError implements Er
   DERBYREPO_0001("Unable to determine if schema exists"),
 
   /** The system was unable to shutdown embedded derby repository server. */
-  DERBYREPO_0002("Unable to shutdown embedded  Derby instance");
+  DERBYREPO_0002("Unable to shutdown embedded  Derby instance"),
+
+  /** The system was unable to run the specified query. */
+  DERBYREPO_0003("Unable to run specified query");
 
 
   private final String message;

Modified: incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java?rev=1215051&r1=1215050&r2=1215051&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java
(original)
+++ incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java
Fri Dec 16 07:10:32 2011
@@ -29,7 +29,9 @@ import org.apache.log4j.Logger;
 import org.apache.sqoop.core.SqoopException;
 import org.apache.sqoop.repository.JdbcRepositoryContext;
 import org.apache.sqoop.repository.JdbcRepositoryHandler;
+import org.apache.sqoop.repository.JdbcRepositoryTransactionFactory;
 import org.apache.sqoop.repository.Repository;
+import org.apache.sqoop.repository.model.MConnector;
 
 public class DerbyRepositoryHandler implements JdbcRepositoryHandler {
 
@@ -48,33 +50,17 @@ public class DerbyRepositoryHandler impl
 
   private JdbcRepositoryContext repoContext;
   private DataSource dataSource;
+  private JdbcRepositoryTransactionFactory txFactory;
 
   @Override
-  public synchronized void initialize(DataSource dataSource,
-      JdbcRepositoryContext ctx) {
-    if (LOG.isTraceEnabled()) {
-      LOG.trace("DerbyRepositoryHandler begin initialization");
-    }
-
-    this.dataSource = dataSource;
+  public synchronized void initialize(JdbcRepositoryContext ctx) {
     repoContext = ctx;
-
-    if (repoContext.shouldCreateSchema()) {
-      if (!schemaExists()) {
-        createSchema();
-      }
-    }
-
+    dataSource = repoContext.getDataSource();
+    txFactory = repoContext.getTransactionFactory();
     LOG.info("DerbyRepositoryHandler initialized.");
   }
 
   @Override
-  public synchronized Repository getRepository() {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  @Override
   public synchronized void shutdown() {
     String driver = repoContext.getDriverClass();
     if (driver != null && driver.equals(EMBEDDED_DERBY_DRIVER_CLASSNAME)) {
@@ -113,13 +99,12 @@ public class DerbyRepositoryHandler impl
     }
   }
 
-
-  private void createSchema() {
-    // TODO implement this
+  public void createSchema() {
+    runQuery(DerbySchemaQuery.QUERY_CREATE_SCHEMA_SQOOP);
+    runQuery(DerbySchemaQuery.QUERY_CREATE_TABLE_SQ_CONNECTOR);
   }
 
-
-  private boolean schemaExists() {
+  public boolean schemaExists() {
     Connection connection = null;
     Statement stmt = null;
     try {
@@ -164,6 +149,54 @@ public class DerbyRepositoryHandler impl
     return true;
   }
 
+  private void runQuery(String query) {
+    Connection connection = null;
+    Statement stmt = null;
+    try {
+      connection = dataSource.getConnection();
+      stmt = connection.createStatement();
+      if (stmt.execute(query)) {
+        ResultSet rset = stmt.getResultSet();
+        int count = 0;
+        while (rset.next()) {
+          count++;
+        }
+        LOG.info("QUERY(" + query + ") produced unused resultset with "
+            + count + " rows");
+      } else {
+        int updateCount = stmt.getUpdateCount();
+        LOG.info("QUERY(" + query + ") Update count: " + updateCount);
+      }
+      connection.commit();
+    } catch (SQLException ex) {
+      try {
+        connection.rollback();
+      } catch (SQLException ex2) {
+        LOG.error("Unable to rollback transaction", ex2);
+      }
+      throw new SqoopException(DerbyRepoError.DERBYREPO_0003,
+          query, ex);
+    } finally {
+      if (stmt != null) {
+        try {
+          stmt.close();
+        } catch (SQLException ex) {
+          LOG.error("Unable to close statement", ex);
+        }
+        if (connection != null) {
+          try {
+            connection.close();
+          } catch (SQLException ex) {
+            LOG.error("Unable to close connection", ex);
+          }
+        }
+      }
+    }
+  }
 
-
+  @Override
+  public MConnector findConnector(String shortName, Connection conn) {
+    // FIXME Auto-generated method stub
+    return null;
+  }
 }

Added: incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaConstants.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaConstants.java?rev=1215051&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaConstants.java
(added)
+++ incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaConstants.java
Fri Dec 16 07:10:32 2011
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.repository.derby;
+
+public final class DerbySchemaConstants {
+
+  public static final String SCHEMA_SQOOP = "SQOOP";
+
+  public static final String TABLE_SQ_CONNECTOR_NAME = "SQ_CONNECTOR";
+
+  public static final String TABLE_SQ_CONNECTOR = SCHEMA_SQOOP + "."
+      + TABLE_SQ_CONNECTOR_NAME;
+
+  public static final String COLUMN_SQC_NAME = "SQC_NAME";
+
+  public static final String COLUMN_SQC_CLASS = "SQC_CLASS";
+
+  private DerbySchemaConstants() {
+    // Disable explicit object creation
+  }
+}

Propchange: incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaQuery.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaQuery.java?rev=1215051&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaQuery.java
(added)
+++ incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaQuery.java
Fri Dec 16 07:10:32 2011
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.repository.derby;
+
+public final class DerbySchemaQuery {
+
+  public static final String QUERY_CREATE_SCHEMA_SQOOP =
+      "CREATE SCHEMA " + DerbySchemaConstants.SCHEMA_SQOOP;
+
+  public static final String QUERY_CREATE_TABLE_SQ_CONNECTOR =
+      "CREATE TABLE " + DerbySchemaConstants.TABLE_SQ_CONNECTOR
+      + " (" + DerbySchemaConstants.COLUMN_SQC_NAME
+      + " VARCHAR(64) PRIMARY KEY, "
+      + DerbySchemaConstants.COLUMN_SQC_CLASS
+      + " VARCHAR(255))";
+
+
+  private DerbySchemaQuery() {
+    // Disable explicit object creation
+  }
+}

Propchange: incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbySchemaQuery.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message