]> source.dussan.org Git - archiva.git/commitdiff
working ibatis example with two tables, only one being used in the test case though
authorJesse McConnell <jmcconnell@apache.org>
Mon, 26 Feb 2007 20:47:55 +0000 (20:47 +0000)
committerJesse McConnell <jmcconnell@apache.org>
Mon, 26 Feb 2007 20:47:55 +0000 (20:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@512002 13f79535-47bb-0310-9956-ffa450edef68

12 files changed:
archiva-database/pom.xml
archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java [new file with mode: 0644]
archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java [new file with mode: 0644]
archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStoreException.java [new file with mode: 0644]
archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java
archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java [new file with mode: 0644]
archiva-database/src/main/resources/ibatis-config.xml [new file with mode: 0644]
archiva-database/src/main/resources/org/apache/maven/archiva/database/CreateTables.xml [new file with mode: 0644]
archiva-database/src/main/resources/org/apache/maven/archiva/database/MetadataKey.xml [new file with mode: 0644]
archiva-database/src/test/java/org/apache/maven/archiva/database/IbatisMetadataStoreTest.java [new file with mode: 0644]
archiva-database/src/test/resources/META-INF/plexus/components.xml [new file with mode: 0644]
archiva-database/src/test/resources/log4j.properties [new file with mode: 0644]

index 57dcf32fc5d3747e799819d7291e9b5ab82b093f..be6ee86f0adf02c78f5ae0456645a84a02ec4297 100755 (executable)
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-repository-metadata</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-ibatis</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
     </dependency>
-    <!--  TEST DEPS -->
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.1</version>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <version>1.2.7</version>
+    </dependency>
+    <!--  TEST DEPS 
     <dependency>
       <groupId>hsqldb</groupId>
       <artifactId>hsqldb</artifactId>
       <version>1.7.3.3</version>
       <scope>test</scope>
+    </dependency> -->
+    <dependency>
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derby</artifactId>
+      <version>10.2.1.6</version>
+      <!--<scope>test</scope>-->
+    </dependency>
+    <dependency>
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derbytools</artifactId>
+      <version>10.2.1.6</version>
+      <scope>test</scope>
     </dependency>
   </dependencies>
   <build>
+  <plugins>
+    <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-maven-plugin</artifactId>
+    </plugin>
+  </plugins>
   </build>
 </project>
diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java
new file mode 100644 (file)
index 0000000..aaa78c9
--- /dev/null
@@ -0,0 +1,139 @@
+package org.apache.maven.archiva.database;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.maven.archiva.database.key.MetadataKey;
+import org.apache.maven.artifact.repository.metadata.Metadata;
+import org.codehaus.plexus.ibatis.PlexusIbatisHelper;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+
+import com.ibatis.sqlmap.client.SqlMapClient;
+
+/**
+ * 
+ * IbatisMetadataStore 
+ *
+ * @author <a href="mailto:jmcconnell@apache.com">Jesse McConnell</a>
+ * @version $Id$
+ * 
+ * @plexus.component role="org.apache.maven.archiva.database.MetadataStore" role-hint="ibatis"
+ */
+public class IbatisMetadataStore
+    extends AbstractLogEnabled
+    implements MetadataStore, Initializable
+{
+
+    /**
+     * @plexus.requirement 
+     */
+    private PlexusIbatisHelper ibatisHelper;
+
+    public void initialize()
+        throws InitializationException
+    {
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+        try
+        {
+            sqlMap.startTransaction();
+
+            Connection con = sqlMap.getCurrentConnection();
+
+            DatabaseMetaData databaseMetaData = con.getMetaData();
+
+            ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null );
+
+            // check if the index database exists in the database
+            while ( rs.next() )
+            {
+                String tableName = rs.getString( "TABLE_NAME" );
+
+                // if it does then we are already initialized
+                if ( tableName.toLowerCase().equals( "MetadataKeys" ) )
+                {
+                    return;
+                }
+            }
+            
+            // Create the tables
+            
+            getLogger().info( "Creating metadata keys instance table" );
+            sqlMap.update( "createMetadataKeyTable", null );
+            
+            getLogger().info( "Creating repository metadata instance table" );
+            sqlMap.update( "createRepositoryMetadataTable", null );
+
+            sqlMap.commitTransaction();
+        }
+        catch ( SQLException e )
+        {
+            getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." );
+
+            while ( e != null )
+            {
+                getLogger().error( e.getMessage(), e );
+
+                e = e.getNextException();
+            }
+
+            throw new InitializationException( "Error while setting up database.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
+    
+    public void addMetadataKey( MetadataKey metadataKey ) 
+        throws MetadataStoreException
+    {
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+        try
+        {
+            sqlMap.startTransaction();
+
+            getLogger().info( "Adding metadata key" );
+            sqlMap.update( "addMetadataKey", metadataKey );
+
+            sqlMap.commitTransaction();
+        }
+        catch ( SQLException e )
+        {
+            getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." );
+
+            while ( e != null )
+            {
+                getLogger().error( e.getMessage(), e );
+
+                e = e.getNextException();
+            }
+
+            throw new MetadataStoreException ( "Error while interacting with the database.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
+
+}
diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java
new file mode 100644 (file)
index 0000000..ccf5f8f
--- /dev/null
@@ -0,0 +1,12 @@
+package org.apache.maven.archiva.database;
+
+import org.apache.maven.archiva.database.key.MetadataKey;
+import org.apache.maven.artifact.repository.metadata.Metadata;
+
+public interface MetadataStore 
+{
+    public static final String ROLE = MetadataStore.class.getName();
+    
+    public void addMetadataKey( MetadataKey metadataKey ) throws MetadataStoreException;
+    
+}
diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStoreException.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStoreException.java
new file mode 100644 (file)
index 0000000..1e166fe
--- /dev/null
@@ -0,0 +1,17 @@
+package org.apache.maven.archiva.database;
+
+public class MetadataStoreException
+    extends Exception
+{
+    
+    public MetadataStoreException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    public MetadataStoreException( String message )
+    {
+        super( message );
+    }
+
+}
index e60fd67b3b8eceeab68a0d557ad4225bc62d45e7..37e0f07319e1651b4f6c36589448db9742a3c3b0 100644 (file)
@@ -29,9 +29,10 @@ import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
  */
 public class RepositoryMetadataDatabase
 {
+       
     public void create( RepositoryMetadata metadata )
     {
-
+       
     }
 
     public RepositoryMetadata read( String groupId, String artifactId, String version )
diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java
new file mode 100644 (file)
index 0000000..2bdf072
--- /dev/null
@@ -0,0 +1,37 @@
+package org.apache.maven.archiva.database.key;
+
+public class MetadataKey {
+
+       private String groupId;
+       private String artifactId;
+       private String version;
+       private long id;
+       
+       public String getArtifactId() {
+               return artifactId;
+       }
+       public void setArtifactId(String artifactId) {
+               this.artifactId = artifactId;
+       }
+       public String getGroupId() {
+               return groupId;
+       }
+       public void setGroupId(String groupId) {
+               this.groupId = groupId;
+       }
+       public long getId() {
+               return id;
+       }
+       public void setId(long id) {
+               this.id = id;
+       }
+       public String getVersion() {
+               return version;
+       }
+       public void setVersion(String version) {
+               this.version = version;
+       }
+       
+       
+       
+}
diff --git a/archiva-database/src/main/resources/ibatis-config.xml b/archiva-database/src/main/resources/ibatis-config.xml
new file mode 100644 (file)
index 0000000..b3b222d
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE sqlMapConfig
+    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
+    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
+
+<sqlMapConfig>
+  <settings
+    cacheModelsEnabled="true"
+    enhancementEnabled="true"
+    lazyLoadingEnabled="false"
+    maxRequests="32"
+    maxSessions="10"
+    maxTransactions="5"
+    useStatementNamespaces="false"
+    />
+
+  <transactionManager type="JDBC">
+    <dataSource type="SIMPLE">
+      <property name="JDBC.Driver" value="${jdbc.driver}"/>
+      <property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
+      <property name="JDBC.Username" value="${jdbc.username}"/>
+      <property name="JDBC.Password" value="${jdbc.password}"/>
+    </dataSource>
+  </transactionManager>
+
+  <sqlMap resource="org/apache/maven/archiva/database/CreateDatabases.xml"/>
+  <sqlMap resource="org/apache/maven/archiva/database/MetadataKey.xml"/>
+</sqlMapConfig>
\ No newline at end of file
diff --git a/archiva-database/src/main/resources/org/apache/maven/archiva/database/CreateTables.xml b/archiva-database/src/main/resources/org/apache/maven/archiva/database/CreateTables.xml
new file mode 100644 (file)
index 0000000..4d936a6
--- /dev/null
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
+"http://ibatis.apache.org/dtd/sql-map-2.dtd">
+
+<sqlMap namespace="CreateTables">
+
+<!-- 
+METADATA_KEYS is the index table for all other tables
+ -->
+<statement id="initalizeMetadataKeyTable">
+    CREATE TABLE MetadataKeys (
+      metadataKey     integer generated always as identity ( start with 1 ) primary key,
+      groupId         varchar(100) not null,
+      artifactId      varchar(100) not null,
+      version         varchar(100) not null
+    )
+</statement>
+
+<statement id="createRepositoryMetadataTable">
+    CREATE TABLE StepInstance (
+      repositoryId          integer,
+      repositoryName        varchar(100) not null,
+      latest        varchar(100) not null,
+      release            varchar(100) not null,
+      lastUpdated         long not null,
+      snapshotTimestamp   long not null,
+      snapshotBuildNumber long not null,
+      snapshotLocalCopy   boolean,
+      primary key( repositoryId ),
+      foreign key( metadataKey ) references MetadataKeys( metadataKey )
+    )
+</statement>
+
+</sqlMap>
\ No newline at end of file
diff --git a/archiva-database/src/main/resources/org/apache/maven/archiva/database/MetadataKey.xml b/archiva-database/src/main/resources/org/apache/maven/archiva/database/MetadataKey.xml
new file mode 100644 (file)
index 0000000..86bbcd4
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
+"http://ibatis.apache.org/dtd/sql-map-2.dtd">
+
+<sqlMap namespace="MetadataKey">
+
+<select id="getMetadataKey" resultClass="org.apache.maven.archiva.database.key.MetadataKey">
+  SELECT 
+    METADATA_KEY as id    
+    GROUP_ID   as groupId,
+    ARTIFACT_ID as artifactId,
+    VERSION    as version, 
+  FROM METADATA_KEYS 
+  WHERE METADATA_KEY = #value# 
+</select> 
+
+<insert id="addMetadataKey" parameterClass="org.apache.maven.archiva.database.key.MetadataKey"> 
+  INSERT INTO 
+  METADATA_KEYS  ( GROUP_ID, ARTIFACT_ID, VERSION_ID ) 
+  VALUES (#groupId#, #artifactId#, #version# ) 
+</insert> 
+
+</sqlMap>
\ No newline at end of file
diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/IbatisMetadataStoreTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/IbatisMetadataStoreTest.java
new file mode 100644 (file)
index 0000000..3526ead
--- /dev/null
@@ -0,0 +1,31 @@
+package org.apache.maven.archiva.database;
+
+import org.apache.maven.archiva.database.key.MetadataKey;
+import org.codehaus.plexus.PlexusTestCase;
+
+public class IbatisMetadataStoreTest
+    extends PlexusTestCase
+{
+
+    protected void setUp()
+        throws Exception
+    {
+        // TODO Auto-generated method stub
+        super.setUp();
+    }
+
+    public void testMetadataKeysInitialization() throws Exception
+    {
+        MetadataStore store = (MetadataStore) lookup( MetadataStore.ROLE, "ibatis" );
+        
+        MetadataKey testMetadataKey = new MetadataKey();
+        
+        testMetadataKey.setArtifactId( "testArtfiactId" );
+        testMetadataKey.setGroupId( "testGroupId" );
+        testMetadataKey.setVersion( "testVersion" );
+        
+        store.addMetadataKey( testMetadataKey );       
+    }
+    
+    
+}
diff --git a/archiva-database/src/test/resources/META-INF/plexus/components.xml b/archiva-database/src/test/resources/META-INF/plexus/components.xml
new file mode 100644 (file)
index 0000000..9be2e75
--- /dev/null
@@ -0,0 +1,30 @@
+<plexus>
+  <components>
+    <component>
+      <role>org.codehaus.plexus.ibatis.PlexusIbatisHelper</role>
+      <implementation>org.codehaus.plexus.ibatis.DefaultPlexusIbatisHelper</implementation>
+      <role-hint>metadata</role-hint>
+      <configuration>
+        <resource>ibatis-config.xml</resource>
+        <properties>
+          <property>
+            <name>jdbc.driver</name>
+            <value>org.apache.derby.jdbc.EmbeddedDriver</value>
+          </property>
+          <property>
+            <name>jdbc.url</name>
+            <value>jdbc:derby:${plexus.home}/testdb;create=true</value>
+          </property>
+          <property>
+            <name>jdbc.username</name>
+            <value>app</value>
+          </property>
+          <property>
+            <name>jdbc.password</name>
+            <value></value>
+          </property>
+        </properties>
+      </configuration>
+    </component>
+  </components>
+</plexus>
\ No newline at end of file
diff --git a/archiva-database/src/test/resources/log4j.properties b/archiva-database/src/test/resources/log4j.properties
new file mode 100644 (file)
index 0000000..a30a904
--- /dev/null
@@ -0,0 +1,10 @@
+log4j.rootCategory=DEBUG, root
+
+## Define the destination and format of our logging
+log4j.appender.root=org.apache.log4j.ConsoleAppender
+log4j.appender.root.layout=org.apache.log4j.PatternLayout
+log4j.appender.root.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n
+
+# SqlMap logging configuration...
+log4j.logger.com.ibatis=INFO
+log4j.logger.java.sql=INFO