sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arv...@apache.org
Subject svn commit: r1210712 - in /incubator/sqoop/branches/sqoop2: core/src/main/java/org/apache/sqoop/core/ core/src/main/java/org/apache/sqoop/repository/ core/src/test/java/org/apache/sqoop/core/ core/src/test/java/org/apache/sqoop/repository/ repository/ ...
Date Mon, 05 Dec 2011 23:36:19 GMT
Author: arvind
Date: Mon Dec  5 23:36:19 2011
New Revision: 1210712

URL: http://svn.apache.org/viewvc?rev=1210712&view=rev
Log:
SQOOP-365. Started implementing Derby backed JDBC repository.

Added:
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
  (with props)
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransactionFactory.java
  (with props)
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryTransaction.java
  (with props)
    incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/MockInvalidConfigurationProvider.java
  (with props)
    incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/repository/
    incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java
  (with props)
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java
  (with props)
Removed:
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepository.java
Modified:
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.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/Repository.java
    incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryError.java
    incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestConfiguration.java
    incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestUtils.java
    incubator/sqoop/branches/sqoop2/repository/   (props changed)
    incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepoError.java

Modified: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.java?rev=1210712&r1=1210711&r2=1210712&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.java
Mon Dec  5 23:36:19 2011
@@ -139,6 +139,13 @@ public final class SqoopConfiguration {
     return new Context(parameters);
   }
 
+  public synchronized static void destroy() {
+    initialized = false;
+    configDir = null;
+    provider = null;
+    config = null;
+  }
+
   private synchronized static void configureLogging() {
     Properties props = new Properties();
     for (String key : config.keySet()) {

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=1210712&r1=1210711&r2=1210712&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
Mon Dec  5 23:36:19 2011
@@ -42,11 +42,12 @@ public class JdbcRepositoryProvider impl
 
   private JdbcRepositoryContext repoContext;
 
-  private JdbcRepositoryHandler handler;
   private GenericObjectPool connectionPool;
   private KeyedObjectPoolFactory statementPool;
   private DataSource dataSource;
 
+  private JdbcRepositoryHandler handler;
+
   public JdbcRepositoryProvider() {
     // Default constructor
   }
@@ -55,6 +56,12 @@ public class JdbcRepositoryProvider impl
   public synchronized void initialize(Context context) {
     repoContext = new JdbcRepositoryContext(SqoopConfiguration.getContext());
 
+    initializeRepositoryHandler();
+
+    LOG.info("JdbcRepository initialized.");
+  }
+
+  private void initializeRepositoryHandler() {
     String jdbcHandlerClassName = repoContext.getHandlerClassName();
 
     Class<?> handlerClass = ClassLoadingUtils.loadClass(jdbcHandlerClassName);
@@ -107,7 +114,7 @@ public class JdbcRepositoryProvider impl
 
     handler.initialize(dataSource, repoContext);
 
-    LOG.info("JdbcRepository initialized.");
+
   }
 
   @Override

Added: 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=1210712&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransaction.java
Mon Dec  5 23:36:19 2011
@@ -0,0 +1,154 @@
+/**
+ * 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 java.sql.SQLException;
+import java.sql.SQLWarning;
+
+import javax.sql.DataSource;
+
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.sqoop.core.SqoopException;
+
+public class JdbcRepositoryTransaction implements RepositoryTransaction {
+
+  private static final Logger LOG =
+      LogManager.getLogger(JdbcRepositoryTransaction.class);
+
+  private final DataSource dataSource;
+  private Connection connection;
+  private JdbcRepositoryTransactionFactory txFactory;
+  boolean active = true;
+  int count = 0;
+
+  boolean rollback = false;
+
+  protected JdbcRepositoryTransaction(DataSource dataSource,
+      JdbcRepositoryTransactionFactory factory) {
+    this.dataSource = dataSource;
+    txFactory = factory;
+  }
+
+  @Override
+  public void begin() {
+    if (!active) {
+      throw new SqoopException(RepositoryError.JDBCREPO_0006);
+    }
+    if (count == 0) {
+      // Lease a connection now
+      try {
+        connection = dataSource.getConnection();
+      } catch (SQLException ex) {
+        throw new SqoopException(RepositoryError.JDBCREPO_0007, ex);
+      }
+      // Clear any prior warnings on the connection
+      try {
+        connection.clearWarnings();
+      } catch (SQLException ex) {
+        LOG.error("Error while clearing warnings: " + ex.getErrorCode(), ex);
+      }
+    }
+    count++;
+    LOG.debug("Tx count-begin: " + count + ", rollback: " + rollback);
+  }
+
+  @Override
+  public void commit() {
+    if (!active) {
+      throw new SqoopException(RepositoryError.JDBCREPO_0006);
+    }
+    if (rollback) {
+      throw new SqoopException(RepositoryError.JDBCREPO_0008);
+    }
+    LOG.debug("Tx count-commit: " + count + ", rollback: " + rollback);
+  }
+
+  @Override
+  public void rollback() {
+    if (!active) {
+      throw new SqoopException(RepositoryError.JDBCREPO_0006);
+    }
+    LOG.warn("Marking transaction for rollback");
+    rollback = true;
+    LOG.debug("Tx count-rollback: " + count + ", rollback: " + rollback);
+  }
+
+  @Override
+  public void close() {
+    if (!active) {
+      throw new SqoopException(RepositoryError.JDBCREPO_0006);
+    }
+    count--;
+    LOG.debug("Tx count-close: " + count + ", rollback: " + rollback);
+    if (count == 0) {
+      active = false;
+      try {
+        if (rollback) {
+          LOG.info("Attempting transaction roll-back");
+          connection.rollback();
+        } else {
+          LOG.info("Attempting transaction commit");
+          connection.commit();
+        }
+      } catch (SQLException ex) {
+        throw new SqoopException(RepositoryError.JDBCREPO_0009, ex);
+      } finally {
+        if (connection != null) {
+          // Log Warnings
+          try {
+            SQLWarning warning = connection.getWarnings();
+            if (warning != null) {
+              StringBuilder sb = new StringBuilder("Connection warnigns: ");
+              boolean first = true;
+              while (warning != null) {
+                if (first) {
+                  first = false;
+                } else {
+                  sb.append("; ");
+                }
+                sb.append("[").append(warning.getErrorCode()).append("] ");
+                sb.append(warning.getMessage());
+              }
+              LOG.warn(sb.toString());
+            }
+          } catch (SQLException ex) {
+            LOG.error("Error while retrieving warnigns: "
+                                + ex.getErrorCode(), ex);
+          }
+
+          // Close Connection
+          try {
+            connection.close();
+          } catch (SQLException ex) {
+            LOG.error(
+                "Unable to close connection: " + ex.getErrorCode(), ex);
+          }
+        }
+
+        // Clean up thread local
+        txFactory.remove();
+
+        // Destroy local state
+        connection = null;
+        txFactory = null;
+      }
+    }
+  }
+}

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

Added: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransactionFactory.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransactionFactory.java?rev=1210712&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransactionFactory.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryTransactionFactory.java
Mon Dec  5 23:36:19 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;
+
+import javax.sql.DataSource;
+
+public class JdbcRepositoryTransactionFactory extends
+      ThreadLocal<JdbcRepositoryTransaction> {
+
+  private final DataSource dataSource;
+
+  protected JdbcRepositoryTransactionFactory(DataSource dataSource) {
+    super();
+    this.dataSource = dataSource;
+  }
+
+  @Override
+  protected JdbcRepositoryTransaction initialValue() {
+    return new JdbcRepositoryTransaction(dataSource, this);
+  }
+}

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

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=1210712&r1=1210711&r2=1210712&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
Mon Dec  5 23:36:19 2011
@@ -17,7 +17,6 @@
  */
 package org.apache.sqoop.repository;
 
-import org.apache.sqoop.core.Context;
 
 /**
  * Defines the contract of a Repository used by Sqoop. A Repository allows
@@ -26,5 +25,5 @@ import org.apache.sqoop.core.Context;
  */
 public interface Repository {
 
-
+  public RepositoryTransaction getTransaction();
 }

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=1210712&r1=1210711&r2=1210712&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
Mon Dec  5 23:36:19 2011
@@ -29,8 +29,6 @@ public enum RepositoryError implements E
   /** The system was unable to find or load the repository provider. */
   REPO_0001("Invalid repository provider specified"),
 
-
-
   // JDBC Repository Errors: Prefix JDBCREP
 
   /** An unknown error has occurred. */
@@ -49,7 +47,22 @@ public enum RepositoryError implements E
   JDBCREPO_0004("Invalid JDBC transaction isolation level specified"),
 
   /** The value specified for maximum connection pool connections is invalid.*/
-  JDBCREPO_0005("Invalid maximum connections specified for connection pool");
+  JDBCREPO_0005("Invalid maximum connections specified for connection pool"),
+
+  /** The system attempted to use an incactive transaction. */
+  JDBCREPO_0006("Transaction is not active"),
+
+  /**
+   * The system was unable to obtain a connection lease for the
+   * requested transaction.
+   */
+  JDBCREPO_0007("Unable to lease connection"),
+
+  /** The system attempted to commit a transaction marked for rollback.*/
+  JDBCREPO_0008("Attempt to commit a transaction marked for rollback"),
+
+  /** The system was unable to finalize the transaction. */
+  JDBCREPO_0009("Failed to finalize transaction");
 
   private final String message;
 

Added: incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryTransaction.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryTransaction.java?rev=1210712&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryTransaction.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/repository/RepositoryTransaction.java
Mon Dec  5 23:36:19 2011
@@ -0,0 +1,33 @@
+/**
+ * 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;
+
+/**
+ * A transaction that can be used to group multiple repository operations into
+ * a single transaction.
+ */
+public interface RepositoryTransaction {
+
+  public void begin();
+
+  public void commit();
+
+  public void rollback();
+
+  public void close();
+}

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

Added: incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/MockInvalidConfigurationProvider.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/MockInvalidConfigurationProvider.java?rev=1210712&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/MockInvalidConfigurationProvider.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/MockInvalidConfigurationProvider.java
Mon Dec  5 23:36:19 2011
@@ -0,0 +1,48 @@
+/**
+ * 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.core;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Properties;
+
+public class MockInvalidConfigurationProvider implements ConfigurationProvider {
+
+  public MockInvalidConfigurationProvider() {
+    throw new RuntimeException("Cannot instantiate");
+  }
+
+  @Override
+  public void initialize(File configDir, Properties bootstrapCongiruation) {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public void registerListener(ConfigurationListener listener) {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public Map<String, String> getConfiguration() {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+}

Propchange: incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/MockInvalidConfigurationProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestConfiguration.java?rev=1210712&r1=1210711&r2=1210712&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestConfiguration.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestConfiguration.java
Mon Dec  5 23:36:19 2011
@@ -17,27 +17,148 @@
  */
 package org.apache.sqoop.core;
 
+import java.util.Properties;
+
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 public class TestConfiguration {
 
-  @Test
-  public void testConfigurationInitFailure() {
+  @Before
+  public void setUp() throws Exception {
     // Unset any configuration dir if it is set by another test
     System.getProperties().remove(ConfigurationConstants.SYSPROP_CONFIG_DIR);
+    SqoopConfiguration.destroy();
+  }
+
+  @Test
+  public void testConfigurationInitFailure() {
+    boolean success = false;
     try {
       SqoopConfiguration.initialize();
     } catch (Exception ex) {
       Assert.assertTrue(ex instanceof SqoopException);
       Assert.assertSame(((SqoopException) ex).getErrorCode(),
           CoreError.CORE_0001);
+      success = true;
+    }
+
+    Assert.assertTrue(success);
+  }
+
+  @Test
+  public void testBootstrapConfigurationInitFailure() {
+    boolean success = false;
+    try {
+      String configDirPath = TestUtils.createEmptyConfigDirectory();
+      System.setProperty(ConfigurationConstants.SYSPROP_CONFIG_DIR,
+          configDirPath);
+      SqoopConfiguration.initialize();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          CoreError.CORE_0002);
+      success = true;
+    }
+
+    Assert.assertTrue(success);
+  }
+
+  @Test
+  public void testConfigurationProviderNotSet() throws Exception {
+    boolean success = false;
+    Properties bootProps = new Properties();
+    bootProps.setProperty("foo", "bar");
+    TestUtils.setupTestConfigurationUsingProperties(bootProps, null);
+    try {
+      SqoopConfiguration.initialize();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          CoreError.CORE_0003);
+      success = true;
+    }
+
+    Assert.assertTrue(success);
+  }
+
+  @Test
+  public void testConfigurationProviderInvalid() throws Exception {
+    boolean success = false;
+    Properties bootProps = new Properties();
+    bootProps.setProperty(ConfigurationConstants.BOOTCFG_CONFIG_PROVIDER,
+        "foobar");
+    TestUtils.setupTestConfigurationUsingProperties(bootProps, null);
+    try {
+      SqoopConfiguration.initialize();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          CoreError.CORE_0004);
+
+      success = true;
+    }
+
+    Assert.assertTrue(success);
+  }
+
+  @Test
+  public void testConfiugrationProviderCannotLoad() throws Exception {
+    boolean success = false;
+    Properties bootProps = new Properties();
+    bootProps.setProperty(ConfigurationConstants.BOOTCFG_CONFIG_PROVIDER,
+        MockInvalidConfigurationProvider.class.getCanonicalName());
+    TestUtils.setupTestConfigurationUsingProperties(bootProps, null);
+    try {
+      SqoopConfiguration.initialize();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          CoreError.CORE_0005);
+      success = true;
+    }
+
+    Assert.assertTrue(success);
+  }
+
+  @Test
+  public void testPropertiesConfigProviderNoFile() throws Exception {
+    boolean success = false;
+    Properties bootProps = new Properties();
+    bootProps.setProperty(ConfigurationConstants.BOOTCFG_CONFIG_PROVIDER,
+        PropertiesConfigurationProvider.class.getCanonicalName());
+    TestUtils.setupTestConfigurationUsingProperties(bootProps, null);
+    try {
+      SqoopConfiguration.initialize();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          CoreError.CORE_0006);
+      success = true;
     }
+
+    Assert.assertTrue(success);
+  }
+
+  @Test
+  public void testSystemNotInitialized() throws Exception {
+    boolean success = false;
+    try {
+      SqoopConfiguration.getContext();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          CoreError.CORE_0007);
+      success = true;
+    }
+
+    Assert.assertTrue(success);
   }
 
   @Test
   public void testConfigurationInitSuccess() throws Exception {
-    TestUtils.setupTestConfiguration(null);
+    TestUtils.setupTestConfigurationWithExtraConfig(null, null);
     SqoopConfiguration.initialize();
   }
 }

Modified: incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestUtils.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestUtils.java?rev=1210712&r1=1210711&r2=1210712&view=diff
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestUtils.java
(original)
+++ incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/core/TestUtils.java
Mon Dec  5 23:36:19 2011
@@ -22,7 +22,9 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Enumeration;
 import java.util.Properties;
 
 import org.apache.log4j.Logger;
@@ -34,17 +36,7 @@ public class TestUtils {
   public static final String NEWLINE =
       System.getProperty("line.separator", "\n");
 
-  /**
-   * A helper method that creates a temporary directory, populates it with
-   * bootstrap and logging configuration and sets it up as the system config
-   * directory. This is useful for running tests without necessarily having to
-   * create an installation layout.
-   *
-   * @param config any properties that you would like to set in the system
-   * @throws Exception
-   */
-  public static void setupTestConfiguration(Properties config) throws Exception
-  {
+  public static String createEmptyConfigDirectory() throws Exception {
     File tempDir = null;
     File targetDir = new File("target");
     if (targetDir.exists() && targetDir.isDirectory()) {
@@ -65,6 +57,16 @@ public class TestUtils {
               + tempConfigDirPath);
     }
 
+    return tempConfigDirPath;
+  }
+
+  public static void setupTestConfigurationUsingProperties(
+      Properties bootstrapProps, Properties props)
+    throws Exception {
+
+    String tempConfigDirPath = createEmptyConfigDirectory();
+    File tempConfigDir = new File(tempConfigDirPath);
+
     File bootconfigFile = new File(tempConfigDir,
         ConfigurationConstants.FILENAME_BOOTCFG_FILE);
 
@@ -72,21 +74,26 @@ public class TestUtils {
       throw new Exception("Unable to create config file: " + bootconfigFile);
     }
 
-    BufferedWriter bootconfigWriter = null;
-    try {
-      bootconfigWriter = new BufferedWriter(new FileWriter(bootconfigFile));
-
-      bootconfigWriter.write("sqoop.config.provider = "
-            + PropertiesConfigurationProvider.class.getCanonicalName()
-            + NEWLINE);
+    if (bootstrapProps != null) {
+      BufferedWriter bootconfigWriter = null;
+      try {
+        bootconfigWriter = new BufferedWriter(new FileWriter(bootconfigFile));
+
+        Enumeration<?> bootstrapPropNames = bootstrapProps.propertyNames();
+        while (bootstrapPropNames.hasMoreElements()) {
+          String name = (String) bootstrapPropNames.nextElement();
+          String value = bootstrapProps.getProperty(name);
+          bootconfigWriter.write(name + " = " + value + NEWLINE);
+        }
 
-      bootconfigWriter.flush();
-    } finally {
-      if (bootconfigWriter != null) {
-        try {
-          bootconfigWriter.close();
-        } catch (IOException ex) {
-          LOG.error("Failed to close config file writer", ex);
+        bootconfigWriter.flush();
+      } finally {
+        if (bootconfigWriter != null) {
+          try {
+            bootconfigWriter.close();
+          } catch (IOException ex) {
+            LOG.error("Failed to close config file writer", ex);
+          }
         }
       }
     }
@@ -94,40 +101,79 @@ public class TestUtils {
     File sysconfigFile = new File(tempConfigDir,
         PropertiesConfigurationProvider.CONFIG_FILENAME);
 
-    BufferedWriter sysconfigWriter = null;
-    BufferedReader reader = null;
-    try {
-      reader = new BufferedReader(new InputStreamReader(
-          ClassLoader.getSystemResourceAsStream("test_config.properties")));
+    if (props != null) {
+      BufferedWriter sysconfigWriter = null;
+      try {
+        sysconfigWriter = new BufferedWriter(new FileWriter(sysconfigFile));
+
+        Enumeration<?> propNameEnum = props.propertyNames();
+        while (propNameEnum.hasMoreElements()) {
+          String name = (String) propNameEnum.nextElement();
+          String value = props.getProperty(name);
+          sysconfigWriter.write(name + " = " + value + NEWLINE);
+        }
+        sysconfigWriter.flush();
+      } finally {
+        if (sysconfigWriter != null) {
+          try {
+            sysconfigWriter.close();
+          } catch (IOException ex) {
+            LOG.error("Failed to close log config file writer", ex);
+          }
+        }
+      }
+    }
+    System.setProperty(ConfigurationConstants.SYSPROP_CONFIG_DIR,
+        tempConfigDirPath);
+  }
 
-      sysconfigWriter = new BufferedWriter(new FileWriter(sysconfigFile));
+  /**
+   * Sets up the test configuration using any properties specified in the
+   * test file test_config.properties. If the parameter <tt>extraConfig</tt>
+   * is specified, it is added to these properties. Consequently any property
+   * that exists in both the test_config.properties and the supplied extra
+   * properties will retain the value from the later.
+   *
+   * @param extraConfig any properties that you would like to set in the system
+   * @throws Exception
+   */
+  public static void setupTestConfigurationWithExtraConfig(
+      Properties extraBootstrapConfig, Properties extraConfig) throws Exception
+  {
 
-      String nextLine = null;
-      while ((nextLine = reader.readLine()) != null) {
-        sysconfigWriter.write(nextLine + NEWLINE);
-      }
+    Properties props = new Properties();
 
-      sysconfigWriter.flush();
+    InputStream istream = null;
+    try {
+      istream = ClassLoader.getSystemResourceAsStream("test_config.properties");
+      props.load(istream);
     } finally {
-      if (sysconfigWriter != null) {
+      if (istream != null) {
         try {
-          sysconfigWriter.close();
-        } catch (IOException ex) {
-          LOG.error("Failed to close log config file writer", ex);
+          istream.close();
+        } catch (Exception ex) {
+          LOG.warn("Failed to close input stream", ex);
         }
       }
+    }
 
-      if (reader != null) {
-        try {
-          reader.close();
-        } catch (IOException ex) {
-          LOG.error("Failed to close log config reader", ex);
-        }
-      }
+    if (props.size() == 0) {
+      throw new Exception("Unable to load test_config.properties");
     }
 
-    System.setProperty(ConfigurationConstants.SYSPROP_CONFIG_DIR,
-        tempConfigDirPath);
+    if (extraConfig != null && extraConfig.size() > 0) {
+      props.putAll(extraConfig);
+    }
+
+    Properties bootstrapProps = new Properties();
+    bootstrapProps.setProperty("sqoop.config.provider",
+        PropertiesConfigurationProvider.class.getCanonicalName());
+
+    if (extraBootstrapConfig != null && extraBootstrapConfig.size() > 0) {
+      bootstrapProps.putAll(extraBootstrapConfig);
+    }
+
+    setupTestConfigurationUsingProperties(bootstrapProps, props);
   }
 
 

Added: incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java
URL: http://svn.apache.org/viewvc/incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java?rev=1210712&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java
(added)
+++ incubator/sqoop/branches/sqoop2/core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java
Mon Dec  5 23:36:19 2011
@@ -0,0 +1,50 @@
+/**
+ * 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.util.Properties;
+
+import org.apache.sqoop.core.ConfigurationConstants;
+import org.apache.sqoop.core.PropertiesConfigurationProvider;
+import org.apache.sqoop.core.SqoopConfiguration;
+import org.apache.sqoop.core.SqoopException;
+import org.apache.sqoop.core.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestRepositoryManager {
+
+  @Test
+  public void testSystemNotInitialized() throws Exception {
+    // Unset any configuration dir if it is set by another test
+    System.getProperties().remove(ConfigurationConstants.SYSPROP_CONFIG_DIR);
+    Properties bootProps = new Properties();
+    bootProps.setProperty(ConfigurationConstants.BOOTCFG_CONFIG_PROVIDER,
+        PropertiesConfigurationProvider.class.getCanonicalName());
+    Properties configProps = new Properties();
+    TestUtils.setupTestConfigurationUsingProperties(bootProps, configProps);
+    try {
+      SqoopConfiguration.initialize();
+      RepositoryManager.initialize();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof SqoopException);
+      Assert.assertSame(((SqoopException) ex).getErrorCode(),
+          RepositoryError.REPO_0001);
+    }
+  }
+}

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

Propchange: incubator/sqoop/branches/sqoop2/repository/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Dec  5 23:36:19 2011
@@ -0,0 +1,4 @@
+.classpath
+.project
+.settings
+target

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=1210712&r1=1210711&r2=1210712&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
Mon Dec  5 23:36:19 2011
@@ -22,7 +22,10 @@ import org.apache.sqoop.core.ErrorCode;
 public enum DerbyRepoError implements ErrorCode {
 
   /** An unknown error has occurred. */
-  DERBYREPO_0000("An unknown error has occurred");
+  DERBYREPO_0000("An unknown error has occurred"),
+
+  /** The Derby Repository handler was unable to determine if schema exists.*/
+  DERBYREPO_0001("Unable to determine if schema exists");
 
 
   private final String message;

Added: 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=1210712&view=auto
==============================================================================
--- incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java
(added)
+++ incubator/sqoop/branches/sqoop2/repository/repository-derby/src/main/java/org/apache/sqoop/repository/derby/DerbyRepositoryHandler.java
Mon Dec  5 23:36:19 2011
@@ -0,0 +1,122 @@
+/**
+ * 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;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import javax.sql.DataSource;
+
+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.Repository;
+
+public class DerbyRepositoryHandler implements JdbcRepositoryHandler {
+
+  private static final Logger LOG =
+      Logger.getLogger(DerbyRepositoryHandler.class);
+
+  private static final String SCHEMA_SQOOP = "SQOOP";
+
+  private static final String QUERY_SYSSCHEMA_SQOOP =
+      "SELECT SCHEMAID FROM SYS.SYSSCHEMAS WHERE SCHEMANAME = '"
+          + SCHEMA_SQOOP + "'";
+
+  private JdbcRepositoryContext repoContext;
+  private DataSource dataSource;
+
+  @Override
+  public synchronized void initialize(DataSource dataSource,
+      JdbcRepositoryContext ctx) {
+    if (LOG.isTraceEnabled()) {
+      LOG.trace("DerbyRepositoryHandler begin initialization");
+    }
+
+    repoContext = ctx;
+
+    if (repoContext.shouldCreateSchema()) {
+      if (!schemaExists()) {
+        createSchema();
+      }
+    }
+
+    LOG.info("DerbyRepositoryHandler initialized.");
+  }
+
+  @Override
+  public synchronized Repository getRepository() {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+
+  private void createSchema() {
+    // TODO implement this
+  }
+
+
+  private boolean schemaExists() {
+    Connection connection = null;
+    Statement stmt = null;
+    try {
+      connection = dataSource.getConnection();
+      stmt = connection.createStatement();
+      ResultSet  rset = stmt.executeQuery(QUERY_SYSSCHEMA_SQOOP);
+
+      if (!rset.next()) {
+        LOG.warn("Schema for SQOOP does not exist");
+        return false;
+      }
+      String sqoopSchemaId = rset.getString(1);
+      LOG.debug("SQOOP schema ID: " + sqoopSchemaId);
+
+      connection.commit();
+    } catch (SQLException ex) {
+      try {
+        connection.rollback();
+      } catch (SQLException ex2) {
+        LOG.error("Unable to rollback transaction", ex2);
+      }
+      throw new SqoopException(DerbyRepoError.DERBYREPO_0001, ex);
+    } finally {
+      if (stmt != null) {
+        try {
+          stmt.close();
+        } catch(SQLException ex) {
+          LOG.error("Unable to  close schema lookup stmt", ex);
+        }
+      }
+      if (connection != null) {
+        try {
+          connection.close();
+        } catch (SQLException ex) {
+          LOG.error("Unable to close connection", ex);
+        }
+      }
+    }
+
+    return true;
+  }
+
+
+
+}

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



Mime
View raw message