From 08d67a9baccb1a0194b9fd1bb8b8b838743e537a Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 27 Feb 2007 22:17:45 +0000 Subject: [PATCH] artifact persistence work git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@512448 13f79535-47bb-0310-9956-ffa450edef68 --- archiva-database/pom.xml | 18 +- .../archiva/database/AbstractIbatisStore.java | 180 ++++++++++++++++++ .../database/artifact/ArtifactKey.java | 101 ++++++++++ .../artifact/ArtifactPersistence.java | 135 +++++++++++++ .../maven/archiva/database/ArtifactKey.xml | 26 +++ .../maven/archiva/database/ManageTables.xml | 23 +++ .../AbstractArchivaDatabaseTestCase.java | 47 +++++ .../RepositoryMetadataDatabaseTest.java | 26 ++- .../artifact/ArtifactPersistenceTest.java | 70 +++++++ .../resources/ibatis-config.xml | 11 +- 10 files changed, 625 insertions(+), 12 deletions(-) create mode 100644 archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java create mode 100644 archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java create mode 100644 archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java create mode 100644 archiva-database/src/main/resources/org/apache/maven/archiva/database/ArtifactKey.xml create mode 100644 archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java create mode 100644 archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java rename archiva-database/src/{main => test}/resources/ibatis-config.xml (79%) diff --git a/archiva-database/pom.xml b/archiva-database/pom.xml index b96104d55..f978fdd2f 100755 --- a/archiva-database/pom.xml +++ b/archiva-database/pom.xml @@ -38,7 +38,7 @@ org.apache.maven maven-artifact-manager - + org.apache.maven maven-project @@ -71,19 +71,19 @@ commons-logging commons-logging 1.1 + + + logkit + logkit + + log4j log4j - 1.2.7 + 1.2.8 - + org.apache.derby derby 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 Joakim Erdfelt + * @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 Joakim Erdfelt + * @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 Joakim Erdfelt + * @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 @@ + + + + + + + + + + INSERT INTO + ARTIFACT_KEYS ( GROUP_ID, ARTIFACT_ID, VERSION_ID, CLASSIFIER, TYPE ) + VALUES (#groupId#, #artifactId#, #version# ) + + + \ 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 @@ + + + + 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 ) + ) + + + + DROP TABLE ArtifactKeys + + + + + 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 Joakim Erdfelt + * @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 Joakim Erdfelt + * @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 Joakim Erdfelt + * @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 similarity index 79% rename from archiva-database/src/main/resources/ibatis-config.xml rename to 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"> - + + + - \ No newline at end of file + + \ No newline at end of file -- 2.39.5