diff options
author | Jesse McConnell <jmcconnell@apache.org> | 2007-02-27 17:49:48 +0000 |
---|---|---|
committer | Jesse McConnell <jmcconnell@apache.org> | 2007-02-27 17:49:48 +0000 |
commit | e43e5befe78bf3e9f5c948e1c0a67f663973bca0 (patch) | |
tree | 2e0f99cedd526bc074bf8a7a64a63d6ffe69c729 /archiva-database | |
parent | 61bf12f9716fe591a6bcfefd2e888d653481b06d (diff) | |
download | archiva-e43e5befe78bf3e9f5c948e1c0a67f663973bca0.tar.gz archiva-e43e5befe78bf3e9f5c948e1c0a67f663973bca0.zip |
remove the original IbatisMemoryStore and
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
Diffstat (limited to 'archiva-database')
-rw-r--r-- | archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java (renamed from archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java) | 170 | ||||
-rw-r--r-- | archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDatabaseException.java | 17 | ||||
-rw-r--r-- | archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java | 14 | ||||
-rw-r--r-- | archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStoreException.java | 17 | ||||
-rw-r--r-- | archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java | 185 | ||||
-rw-r--r-- | archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java | 11 |
6 files changed, 317 insertions, 97 deletions
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/AbstractMetadataKeyDatabase.java index e65841bb7..4b8c42776 100644 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java @@ -1,9 +1,26 @@ package org.apache.maven.archiva.database; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; + +/* + * 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; @@ -12,7 +29,11 @@ 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; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; + /** * @@ -21,20 +42,28 @@ import com.ibatis.sqlmap.client.SqlMapClient; * @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 +public class AbstractMetadataKeyDatabase + extends AbstractLogEnabled + implements Initializable { - /** * @plexus.requirement */ - private PlexusIbatisHelper ibatisHelper; - - public void initialize() - throws InitializationException + 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(); @@ -42,43 +71,28 @@ public class IbatisMetadataStore { 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() ) + getLogger().info( "Getting metadata key" ); + MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); + + if ( newMetadataKey == null ) { - String tableName = rs.getString( "TABLE_NAME" ); - - // if it does then we are already initialized - if ( tableName.toLowerCase().equals( "metadatakeys" ) ) + getLogger().info( "added new metadata" ); + sqlMap.update( "addMetadataKey", metadata ); + + newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); + + if ( newMetadataKey == null ) { - return; + throw new ArchivaDatabaseException( "unable to create new MetadataKeys" ); } } - // 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 ); + return newMetadataKey; - sqlMap.commitTransaction(); } catch ( SQLException e ) { - getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." ); + getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." ); while ( e != null ) { @@ -87,7 +101,7 @@ public class IbatisMetadataStore e = e.getNextException(); } - throw new InitializationException( "Error while setting up database.", e ); + throw new ArchivaDatabaseException ( "Error while interacting with the database.", e ); } finally { @@ -101,9 +115,10 @@ public class IbatisMetadataStore } } } - - public void addMetadata( Metadata metadata ) - throws MetadataStoreException + + + protected void initializeTable( String tableName ) + throws ArchivaDatabaseException { SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); @@ -111,14 +126,34 @@ public class IbatisMetadataStore { sqlMap.startTransaction(); - getLogger().info( "Adding metadata key" ); - sqlMap.update( "addMetadataKey", metadata ); + 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 adding metadata, showing all linked exceptions in SQLException." ); + getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." ); while ( e != null ) { @@ -127,7 +162,7 @@ public class IbatisMetadataStore e = e.getNextException(); } - throw new MetadataStoreException ( "Error while interacting with the database.", e ); + throw new ArchivaDatabaseException( "Error while setting up database.", e ); } finally { @@ -142,24 +177,23 @@ public class IbatisMetadataStore } } - public MetadataKey getMetadataKey( Metadata metadata ) - throws MetadataStoreException - { + protected void dropTable( String tableName ) + throws ArchivaDatabaseException +{ SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); try { sqlMap.startTransaction(); - getLogger().info( "Getting metadata key" ); - MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); - - return newMetadataKey; + getLogger().info( "Dropping table: " + tableName ); + sqlMap.update( dropPrefix + tableName, null ); + sqlMap.commitTransaction(); } catch ( SQLException e ) { - getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." ); + getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." ); while ( e != null ) { @@ -168,7 +202,7 @@ public class IbatisMetadataStore e = e.getNextException(); } - throw new MetadataStoreException ( "Error while interacting with the database.", e ); + throw new ArchivaDatabaseException( "Error while dropping database.", e ); } finally { @@ -182,5 +216,21 @@ public class IbatisMetadataStore } } } - + + 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 index 000000000..67865df75 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDatabaseException.java @@ -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/MetadataStore.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java deleted file mode 100644 index deb4590c7..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStore.java +++ /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 index 1e166fe17..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/MetadataStoreException.java +++ /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 ); - } - -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java index 37e0f0731..5434e6071 100644 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java @@ -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 ); + } } + + } 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 index 7a60dad83..b06e03b17 100644 --- 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 @@ -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; } |