<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>
--- /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( "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();
+ }
+ }
+ }
+
+}
--- /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 addMetadataKey( MetadataKey metadataKey ) 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 );
+ }
+
+}
*/
public class RepositoryMetadataDatabase
{
+
public void create( RepositoryMetadata metadata )
{
-
+
}
public RepositoryMetadata read( String groupId, String artifactId, String version )
--- /dev/null
+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;
+ }
+
+
+
+}
--- /dev/null
+<?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
--- /dev/null
+<?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
--- /dev/null
+<?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
--- /dev/null
+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 );
+ }
+
+
+}
--- /dev/null
+<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
--- /dev/null
+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