--- /dev/null
+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" );
+ }
+ }
+
+
+
+
+}
--- /dev/null
+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 );
+ }
+
+}
+++ /dev/null
-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();
- }
- }
-}
-
-}
+++ /dev/null
-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;
-
-}
+++ /dev/null
-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 );
- }
-
-}
* 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 );
+ }
}
+
+
}
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;
}