diff options
author | Joakim Erdfelt <joakime@apache.org> | 2007-02-27 22:17:45 +0000 |
---|---|---|
committer | Joakim Erdfelt <joakime@apache.org> | 2007-02-27 22:17:45 +0000 |
commit | 08d67a9baccb1a0194b9fd1bb8b8b838743e537a (patch) | |
tree | 207f6e7a4c510f68743a7e620fe815149a2c3d9a /archiva-database/src | |
parent | 020e901225a3f67213b38e4c9cf6eb53ff9bcacf (diff) | |
download | archiva-08d67a9baccb1a0194b9fd1bb8b8b838743e537a.tar.gz archiva-08d67a9baccb1a0194b9fd1bb8b8b838743e537a.zip |
artifact persistence work
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@512448 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-database/src')
9 files changed, 616 insertions, 3 deletions
diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java new file mode 100644 index 000000000..0e25a028c --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java @@ -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(); + } + } + } + + +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java new file mode 100644 index 000000000..e1f00ab98 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java @@ -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; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java new file mode 100644 index 000000000..569e1d63f --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java @@ -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 ) + { + + } + +} diff --git a/archiva-database/src/main/resources/org/apache/maven/archiva/database/ArtifactKey.xml b/archiva-database/src/main/resources/org/apache/maven/archiva/database/ArtifactKey.xml new file mode 100644 index 000000000..82dc174b1 --- /dev/null +++ b/archiva-database/src/main/resources/org/apache/maven/archiva/database/ArtifactKey.xml @@ -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>
\ No newline at end of file diff --git a/archiva-database/src/main/resources/org/apache/maven/archiva/database/ManageTables.xml b/archiva-database/src/main/resources/org/apache/maven/archiva/database/ManageTables.xml index ee4205ce3..1f8f8d295 100644 --- a/archiva-database/src/main/resources/org/apache/maven/archiva/database/ManageTables.xml +++ b/archiva-database/src/main/resources/org/apache/maven/archiva/database/ManageTables.xml @@ -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 ( diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java new file mode 100644 index 000000000..6033a6924 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java @@ -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(); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java index 0d2a973ba..070e54958 100644 --- a/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java @@ -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(); } diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java new file mode 100644 index 000000000..007e027b2 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java @@ -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 ); + } +} diff --git a/archiva-database/src/main/resources/ibatis-config.xml b/archiva-database/src/test/resources/ibatis-config.xml index c31618727..b7013e013 100644 --- a/archiva-database/src/main/resources/ibatis-config.xml +++ b/archiva-database/src/test/resources/ibatis-config.xml @@ -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>
\ No newline at end of file + +</sqlMapConfig>
\ No newline at end of file |