aboutsummaryrefslogtreecommitdiffstats
path: root/archiva-database/src/main
diff options
context:
space:
mode:
authorJoakim Erdfelt <joakime@apache.org>2007-02-27 22:17:45 +0000
committerJoakim Erdfelt <joakime@apache.org>2007-02-27 22:17:45 +0000
commit08d67a9baccb1a0194b9fd1bb8b8b838743e537a (patch)
tree207f6e7a4c510f68743a7e620fe815149a2c3d9a /archiva-database/src/main
parent020e901225a3f67213b38e4c9cf6eb53ff9bcacf (diff)
downloadarchiva-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/main')
-rw-r--r--archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java180
-rw-r--r--archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java101
-rw-r--r--archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java135
-rw-r--r--archiva-database/src/main/resources/ibatis-config.xml30
-rw-r--r--archiva-database/src/main/resources/org/apache/maven/archiva/database/ArtifactKey.xml26
-rw-r--r--archiva-database/src/main/resources/org/apache/maven/archiva/database/ManageTables.xml23
6 files changed, 465 insertions, 30 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/ibatis-config.xml b/archiva-database/src/main/resources/ibatis-config.xml
deleted file mode 100644
index c31618727..000000000
--- a/archiva-database/src/main/resources/ibatis-config.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?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/ManageTables.xml"/>
- <sqlMap resource="org/apache/maven/archiva/database/MetadataKey.xml"/>
- <sqlMap resource="org/apache/maven/archiva/database/RepositoryMetadata.xml"/>
-</sqlMapConfig> \ No newline at end of file
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 (