]> source.dussan.org Git - archiva.git/commitdiff
remove the original IbatisMemoryStore and
authorJesse McConnell <jmcconnell@apache.org>
Tue, 27 Feb 2007 17:49:48 +0000 (17:49 +0000)
committerJesse McConnell <jmcconnell@apache.org>
Tue, 27 Feb 2007 17:49:48 +0000 (17:49 +0000)
its interface, switch impl over to joakims
Repository Metadata database set and start
getting an abstract helper class that helps
manage the MetadataKey

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@512340 13f79535-47bb-0310-9956-ffa450edef68

archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java [new file with mode: 0644]
archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDatabaseException.java [new file with mode: 0644]
archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java [deleted file]
archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java [deleted file]
archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStoreException.java [deleted file]
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

diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java
new file mode 100644 (file)
index 0000000..4b8c427
--- /dev/null
@@ -0,0 +1,236 @@
+package org.apache.maven.archiva.database;
+
+
+/*
+ * 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.
+ */
+
+import com.ibatis.sqlmap.client.SqlMapClient;
+
+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 java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+
+/**
+ * 
+ * IbatisMetadataStore 
+ *
+ * @author <a href="mailto:jmcconnell@apache.com">Jesse McConnell</a>
+ * @version $Id$
+ * 
+ */
+public class AbstractMetadataKeyDatabase 
+    extends AbstractLogEnabled 
+    implements Initializable
+{
+    /**
+     * @plexus.requirement 
+     */
+    protected PlexusIbatisHelper ibatisHelper;
+    
+    /**
+     * @plexus.configuration default-value="create"
+     */
+    private String createPrefix;
+    
+    /**
+     * @plexus.configuration default-value="drop"
+     */
+    private String dropPrefix;
+    
+    public MetadataKey getMetadataKey( Metadata metadata ) 
+        throws ArchivaDatabaseException
+    {
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+        try
+        {
+            sqlMap.startTransaction();
+
+            getLogger().info( "Getting metadata key" );
+            MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata );
+            
+            if ( newMetadataKey == null )
+            {
+                getLogger().info( "added new metadata" );
+                sqlMap.update( "addMetadataKey", metadata );
+                
+                newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata );
+                
+                if ( newMetadataKey == null )
+                {
+                    throw new ArchivaDatabaseException( "unable to create new MetadataKeys" );
+                }
+            }
+            
+            return newMetadataKey;
+            
+        }
+        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 ArchivaDatabaseException ( "Error while interacting with the database.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
+
+
+    protected void initializeTable( String tableName )
+        throws ArchivaDatabaseException    
+    {
+        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 dbTableName = rs.getString( "TABLE_NAME" );
+
+                // if it does then we are already initialized
+                if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) )
+                {
+                    return;
+                }
+            }
+            
+            // Create the tables
+            
+            getLogger().info( "Creating table: " + tableName );
+            sqlMap.update( createPrefix + tableName, 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 ArchivaDatabaseException( "Error while setting up database.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
+    
+    protected void dropTable( String tableName )
+    throws ArchivaDatabaseException    
+{
+    SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+    try
+    {
+        sqlMap.startTransaction();
+
+        getLogger().info( "Dropping table: " + tableName );
+        sqlMap.update( dropPrefix + tableName, null );
+        
+        sqlMap.commitTransaction();
+    }
+    catch ( SQLException e )
+    {
+        getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." );
+
+        while ( e != null )
+        {
+            getLogger().error( e.getMessage(), e );
+
+            e = e.getNextException();
+        }
+
+        throw new ArchivaDatabaseException( "Error while dropping database.", e );
+    }
+    finally
+    {
+        try
+        {
+            sqlMap.endTransaction();
+        }
+        catch ( SQLException e )
+        {
+            e.printStackTrace();
+        }
+    }
+}
+    
+    public void initialize()
+        throws InitializationException
+    {
+        try
+        {
+            initializeTable( "MetadataKeys" );
+        }
+        catch ( ArchivaDatabaseException ade )
+        {
+            throw new InitializationException( "unable to initialize metadata keys database" );
+        }
+    }
+    
+    
+    
+    
+}
diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDatabaseException.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDatabaseException.java
new file mode 100644 (file)
index 0000000..67865df
--- /dev/null
@@ -0,0 +1,17 @@
+package org.apache.maven.archiva.database;
+
+public class ArchivaDatabaseException
+    extends Exception
+{
+    
+    public ArchivaDatabaseException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    public ArchivaDatabaseException( String message )
+    {
+        super( message );
+    }
+
+}
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
deleted file mode 100644 (file)
index e65841b..0000000
+++ /dev/null
@@ -1,186 +0,0 @@
-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( "initializeMetadataKeyTable", null );
-            
-            getLogger().info( "Creating repository metadata instance table" );
-            sqlMap.update( "initializeRepositoryMetadataTable", null );
-
-            getLogger().info( "Creating repository health metadata instance table" );
-            sqlMap.update( "initializeHealthMetadataTable", null );
-            
-            getLogger().info( "Creating repository versions metadata instance table" );
-            sqlMap.update( "initializeVersionsMetadataTable", 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 addMetadata( Metadata metadata ) 
-        throws MetadataStoreException
-    {
-        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
-
-        try
-        {
-            sqlMap.startTransaction();
-
-            getLogger().info( "Adding metadata key" );
-            sqlMap.update( "addMetadataKey", metadata );
-
-            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();
-            }
-        }
-    }
-    
-    public MetadataKey getMetadataKey( Metadata metadata ) 
-    throws MetadataStoreException
-    {
-    SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
-
-    try
-    {
-        sqlMap.startTransaction();
-
-        getLogger().info( "Getting metadata key" );
-        MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata );
-        
-        return newMetadataKey;
-        
-    }
-    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
deleted file mode 100644 (file)
index deb4590..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-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 addMetadata( Metadata metadata ) throws MetadataStoreException;
-    
-    public MetadataKey getMetadataKey( Metadata metadata ) 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
deleted file mode 100644 (file)
index 1e166fe..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-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 37e0f07319e1651b4f6c36589448db9742a3c3b0..5434e6071b5549661f889d9153832c024d786c96 100644 (file)
@@ -19,39 +19,214 @@ package org.apache.maven.archiva.database;
  * under the License.
  */
 
+import com.ibatis.sqlmap.client.SqlMapClient;
+
+import org.apache.maven.archiva.database.key.MetadataKey;
 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+
+import java.sql.SQLException;
 
 /**
  * RepositoryMetadataDatabase 
  *
  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
  * @version $Id$
+ * 
+ * @plexus.component role="org.apache.maven.archiva.database.RepositoryMetadataDatabase" role-hint="default"
  */
-public class RepositoryMetadataDatabase
+public class RepositoryMetadataDatabase extends AbstractMetadataKeyDatabase
 {
-       
     public void create( RepositoryMetadata metadata )
+        throws ArchivaDatabaseException
     {
-       
+    
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+        try
+        {
+            sqlMap.startTransaction();
+
+            getLogger().info( "Adding repository metadata" );
+            sqlMap.update( "addRepositoryMetadata", metadata );
+            
+            sqlMap.commitTransaction();
+        }
+        catch ( SQLException e )
+        {
+            getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." );
+
+            while ( e != null )
+            {
+                getLogger().error( e.getMessage(), e );
+
+                e = e.getNextException();
+            }
+
+            throw new ArchivaDatabaseException( "Error while executing statement.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
     }
 
     public RepositoryMetadata read( String groupId, String artifactId, String version )
+        throws ArchivaDatabaseException
     {
-        return null;
+        
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+        try
+        {
+            sqlMap.startTransaction();
+
+            getLogger().info( "Reading repository metadata" );
+            RepositoryMetadata repositoryMetadata = (RepositoryMetadata) sqlMap.queryForObject( "getRepositoryMetadata", new MetadataKey( groupId, artifactId, version ) );
+            
+            return repositoryMetadata;
+        }
+        catch ( SQLException e )
+        {
+            getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." );
+
+            while ( e != null )
+            {
+                getLogger().error( e.getMessage(), e );
+
+                e = e.getNextException();
+            }
+
+            throw new ArchivaDatabaseException( "Error while executing statement.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
     }
 
+    /**
+     * not implemented yet
+     * 
+     * @param metadata
+     * @throws ArchivaDatabaseException
+     */
     public void update( RepositoryMetadata metadata )
+        throws ArchivaDatabaseException
     {
+        
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
 
+        try
+        {
+            sqlMap.startTransaction();
+
+            getLogger().info( "Updating repository metadata" );
+            sqlMap.update( "updateRepositoryMetadata", metadata );
+            
+            sqlMap.commitTransaction();
+        }
+        catch ( SQLException e )
+        {
+            getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." );
+
+            while ( e != null )
+            {
+                getLogger().error( e.getMessage(), e );
+
+                e = e.getNextException();
+            }
+
+            throw new ArchivaDatabaseException( "Error while executing statement.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
     }
 
     public void delete( RepositoryMetadata metadata )
+        throws ArchivaDatabaseException
     {
-
+        // FIXME is this right? baseVersion seems wrong but I don't know enough about the metadata to say
+        delete( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion() );
     }
 
     public void delete( String groupId, String artifactId, String version )
+        throws ArchivaDatabaseException
     {
+        SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
+
+        try
+        {
+            sqlMap.startTransaction();
+
+            getLogger().info( "Removing repository metadata" );
+            sqlMap.update( "removeRepositoryMetadata", new MetadataKey( groupId, artifactId, version ) );
+            
+            sqlMap.commitTransaction();
+        }
+        catch ( SQLException e )
+        {
+            getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." );
+
+            while ( e != null )
+            {
+                getLogger().error( e.getMessage(), e );
+
+                e = e.getNextException();
+            }
+
+            throw new ArchivaDatabaseException( "Error while executing statement.", e );
+        }
+        finally
+        {
+            try
+            {
+                sqlMap.endTransaction();
+            }
+            catch ( SQLException e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
 
+    public void initialize()
+        throws InitializationException
+    {        
+        super.initialize();
+        try
+        {
+            initializeTable( "RepositoryMetadata" );
+        }
+        catch ( ArchivaDatabaseException ade )
+        {
+            throw new InitializationException( "unable to initialize repository metadata table", ade );
+        }
     }
+    
+    
 }
index 7a60dad832c426ea21494fbe91e86aaff62235d3..b06e03b174738bd0cfa209283db79b93d9177824 100644 (file)
@@ -6,7 +6,16 @@ public class MetadataKey {
        private String artifactId;
        private String version;
        private int metadataKey;
-       
+    
+    public MetadataKey( String groupId, String artifactId, String version )
+    {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.version = version;
+    }
+   
+    public MetadataKey() {}
+    
        public String getArtifactId() {
                return artifactId;
        }