artifact persistence work

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@512448 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-02-27 22:17:45 +00:00
parent 020e901225
commit 08d67a9bac
10 changed files with 625 additions and 12 deletions

View File

@ -71,19 +71,19 @@
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<groupId>logkit</groupId>
<artifactId>logkit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.7</version>
<version>1.2.8</version>
</dependency>
<!-- TEST DEPS
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.7.3.3</version>
<scope>test</scope>
</dependency> -->
<!-- TEST DEPS -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>

View File

@ -0,0 +1,180 @@
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.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;
/**
* AbstractIbatisStore
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public abstract class AbstractIbatisStore
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;
protected abstract String[] getTableNames();
public void initialize()
throws InitializationException
{
try
{
String tableNames[] = getTableNames();
for ( int i = 0; i < tableNames.length; i++ )
{
String tableName = tableNames[i];
initializeTable( tableName );
}
}
catch ( ArchivaDatabaseException e )
{
throw new InitializationException( "Unable to initialize the database: " + e.getMessage(), e );
}
}
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();
}
}
}
}

View File

@ -0,0 +1,101 @@
package org.apache.maven.archiva.database.artifact;
/*
* 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.
*/
/**
* ArtifactKey
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class ArtifactKey
{
private String groupId;
private String artifactId;
private String version;
private String classifier;
private String type;
private long id;
public String getArtifactId()
{
return artifactId;
}
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
public String getClassifier()
{
return classifier;
}
public void setClassifier( String classifier )
{
this.classifier = classifier;
}
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 getType()
{
return type;
}
public void setType( String type )
{
this.type = type;
}
public String getVersion()
{
return version;
}
public void setVersion( String version )
{
this.version = version;
}
}

View File

@ -0,0 +1,135 @@
package org.apache.maven.archiva.database.artifact;
/*
* 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.AbstractIbatisStore;
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.artifact.Artifact;
import java.sql.SQLException;
/**
* ArtifactPersistence
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*
* @plexus.component role="org.apache.maven.archiva.database.artifact.ArtifactPersistence"
*/
public class ArtifactPersistence
extends AbstractIbatisStore
{
protected String[] getTableNames()
{
return new String[] { "ArtifactKeys" };
}
private ArtifactKey toKey(Artifact artifact)
{
ArtifactKey key = new ArtifactKey();
key.setGroupId( artifact.getGroupId() );
key.setArtifactId( artifact.getArtifactId() );
key.set
return key;
}
public void create( Artifact artifact ) throws ArchivaDatabaseException
{
SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
try
{
sqlMap.startTransaction();
getLogger().info( "Adding artifact." );
sqlMap.update( "addArtifact", artifact );
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 Artifact read( String groupId, String artifactId, String version )
{
return null;
}
public Artifact read( String groupId, String artifactId, String version, String type )
{
return null;
}
public Artifact read( String groupId, String artifactId, String version, String classifier, String type )
{
return null;
}
public void update( Artifact artifact )
{
}
public void delete( Artifact artifact )
{
}
public void delete( String groupId, String artifactId, String version )
{
}
public void delete( String groupId, String artifactId, String version, String type )
{
}
public void delete( String groupId, String artifactId, String version, String classifier, String type )
{
}
}

View File

@ -0,0 +1,26 @@
<?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="ArtifactKey">
<select id="getArtifactKey" resultClass="org.apache.maven.archiva.database.artifact.ArtifactKey">
SELECT
ARTIFACT_KEY as id
GROUP_ID as groupId,
ARTIFACT_ID as artifactId,
VERSION as version,
CLASSIFER as classifier,
TYPE as type,
FROM ARTIFACT_KEYS
WHERE ARTIFACT_KEY = #value#
</select>
<insert id="addArtifactKey" parameterClass="org.apache.maven.archiva.database.artifact.ArtifactKey">
INSERT INTO
ARTIFACT_KEYS ( GROUP_ID, ARTIFACT_ID, VERSION_ID, CLASSIFIER, TYPE )
VALUES (#groupId#, #artifactId#, #version# )
</insert>
</sqlMap>

View File

@ -5,10 +5,33 @@
<sqlMap namespace="CreateTables">
<!-- .\ ARTIFACT \.________________________________________________________________________________________ -->
<statement id="createArtifactKeys">
CREATE TABLE ArtifactKeys (
GROUP_ID varchar (100) not null,
ARTIFACT_ID varchar (100) not null,
VERSION varchar (50) not null,
CLASSIFIER varchar (50),
TYPE varchar (20),
ARTIFACT_KEY integer generated always as identity ( start with 1 ),
primary key ( GROUP_ID, ARTIFACT_ID, VERSION, CLASSIFIER, TYPE )
)
</statement>
<statement id="dropArtifactKeys">
DROP TABLE ArtifactKeys
</statement>
<!-- .\ METADATA \.________________________________________________________________________________________ -->
<!--
METADATA_KEYS is the index table for all other tables
need to make the lookup on this table fast, perhaps by indexing the combination of g:a:v in a lookup column
TODO: Ensure that there is never a duplicate of the multi-part complex key (groupId, artifactId, version)
-->
<statement id="createMetadataKeys">
CREATE TABLE MetadataKeys (

View File

@ -0,0 +1,47 @@
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 org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
/**
* AbstractArchivaDatabaseTestCase
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class AbstractArchivaDatabaseTestCase
extends PlexusTestCase
{
protected void setUp()
throws Exception
{
File derbyDbDir = new File( "target/plexus-home/testdb" );
if ( derbyDbDir.exists() )
{
FileUtils.deleteDirectory( derbyDbDir );
}
super.setUp();
}
}

View File

@ -1,10 +1,35 @@
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 org.apache.maven.archiva.database.key.MetadataKey;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.ibatis.PlexusIbatisHelper;
/**
* RepositoryMetadataDatabaseTest
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class RepositoryMetadataDatabaseTest
extends PlexusTestCase
{
@ -16,7 +41,6 @@ public class RepositoryMetadataDatabaseTest
protected void setUp()
throws Exception
{
// TODO Auto-generated method stub
super.setUp();
}

View File

@ -0,0 +1,70 @@
package org.apache.maven.archiva.database.artifact;
/*
* 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 org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
/**
* ArtifactPersistenceTest
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class ArtifactPersistenceTest
extends AbstractArchivaDatabaseTestCase
{
private ArtifactFactory artifactFactory;
private ArtifactPersistence db;
protected void setUp()
throws Exception
{
super.setUp();
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
db = (ArtifactPersistence) lookup( ArtifactPersistence.class.getName() );
}
public void testLookup()
{
assertNotNull( db );
}
public void testAddArtifact() throws ArchivaDatabaseException
{
String groupId = "org.apache.maven.archiva";
String artifactId = "archiva-test-artifact";
String version = "1.0";
Artifact artifact = artifactFactory
.createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" );
db.create( artifact );
Artifact fetched = db.read( groupId, artifactId, version );
assertNotNull( "Should have fetched an Artifact.", fetched );
assertEquals( "Should have fetched the expected Artifact.", artifact, fetched );
}
}

View File

@ -4,7 +4,6 @@
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
@ -24,7 +23,15 @@
</dataSource>
</transactionManager>
<!--
<resultObjectFactory type="org.codehaus.plexus.ibatis.PlexusResultObjectFactory" >
<property name="foo" value="bar"/>
</resultObjectFactory>
-->
<sqlMap resource="org/apache/maven/archiva/database/ManageTables.xml"/>
<sqlMap resource="org/apache/maven/archiva/database/MetadataKey.xml"/>
<sqlMap resource="org/apache/maven/archiva/database/ArtifactKey.xml"/>
<sqlMap resource="org/apache/maven/archiva/database/RepositoryMetadata.xml"/>
</sqlMapConfig>