From: Jesse McConnell Date: Tue, 27 Feb 2007 17:49:48 +0000 (+0000) Subject: remove the original IbatisMemoryStore and X-Git-Tag: archiva-0.9-alpha-1~78 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e43e5befe78bf3e9f5c948e1c0a67f663973bca0;p=archiva.git 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 --- 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 index 000000000..4b8c42776 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java @@ -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 Jesse McConnell + * @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 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/IbatisMetadataStore.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java deleted file mode 100644 index e65841bb7..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/IbatisMetadataStore.java +++ /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 Jesse McConnell - * @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 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 Joakim Erdfelt * @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; }