|
|
@@ -1,9 +1,26 @@ |
|
|
|
package org.apache.maven.archiva.database; |
|
|
|
|
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.DatabaseMetaData; |
|
|
|
import java.sql.ResultSet; |
|
|
|
import java.sql.SQLException; |
|
|
|
|
|
|
|
/* |
|
|
|
* 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.key.MetadataKey; |
|
|
|
import org.apache.maven.artifact.repository.metadata.Metadata; |
|
|
@@ -12,7 +29,11 @@ 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; |
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.DatabaseMetaData; |
|
|
|
import java.sql.ResultSet; |
|
|
|
import java.sql.SQLException; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
@@ -21,20 +42,28 @@ import com.ibatis.sqlmap.client.SqlMapClient; |
|
|
|
* @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 |
|
|
|
public class AbstractMetadataKeyDatabase |
|
|
|
extends AbstractLogEnabled |
|
|
|
implements Initializable |
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
* @plexus.requirement |
|
|
|
*/ |
|
|
|
private PlexusIbatisHelper ibatisHelper; |
|
|
|
|
|
|
|
public void initialize() |
|
|
|
throws InitializationException |
|
|
|
protected PlexusIbatisHelper ibatisHelper; |
|
|
|
|
|
|
|
/** |
|
|
|
* @plexus.configuration default-value="create" |
|
|
|
*/ |
|
|
|
private String createPrefix; |
|
|
|
|
|
|
|
/** |
|
|
|
* @plexus.configuration default-value="drop" |
|
|
|
*/ |
|
|
|
private String dropPrefix; |
|
|
|
|
|
|
|
public MetadataKey getMetadataKey( Metadata metadata ) |
|
|
|
throws ArchivaDatabaseException |
|
|
|
{ |
|
|
|
SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); |
|
|
|
|
|
|
@@ -42,43 +71,28 @@ public class IbatisMetadataStore |
|
|
|
{ |
|
|
|
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() ) |
|
|
|
getLogger().info( "Getting metadata key" ); |
|
|
|
MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); |
|
|
|
|
|
|
|
if ( newMetadataKey == null ) |
|
|
|
{ |
|
|
|
String tableName = rs.getString( "TABLE_NAME" ); |
|
|
|
|
|
|
|
// if it does then we are already initialized |
|
|
|
if ( tableName.toLowerCase().equals( "metadatakeys" ) ) |
|
|
|
getLogger().info( "added new metadata" ); |
|
|
|
sqlMap.update( "addMetadataKey", metadata ); |
|
|
|
|
|
|
|
newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); |
|
|
|
|
|
|
|
if ( newMetadataKey == null ) |
|
|
|
{ |
|
|
|
return; |
|
|
|
throw new ArchivaDatabaseException( "unable to create new MetadataKeys" ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Create the tables |
|
|
|
|
|
|
|
getLogger().info( "Creating metadata keys instance table" ); |
|
|
|
sqlMap.update( "initializeMetadataKeyTable", null ); |
|
|
|
|
|
|
|
getLogger().info( "Creating repository metadata instance table" ); |
|
|
|
sqlMap.update( "initializeRepositoryMetadataTable", null ); |
|
|
|
|
|
|
|
getLogger().info( "Creating repository health metadata instance table" ); |
|
|
|
sqlMap.update( "initializeHealthMetadataTable", null ); |
|
|
|
|
|
|
|
getLogger().info( "Creating repository versions metadata instance table" ); |
|
|
|
sqlMap.update( "initializeVersionsMetadataTable", null ); |
|
|
|
return newMetadataKey; |
|
|
|
|
|
|
|
sqlMap.commitTransaction(); |
|
|
|
} |
|
|
|
catch ( SQLException e ) |
|
|
|
{ |
|
|
|
getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." ); |
|
|
|
getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." ); |
|
|
|
|
|
|
|
while ( e != null ) |
|
|
|
{ |
|
|
@@ -87,7 +101,7 @@ public class IbatisMetadataStore |
|
|
|
e = e.getNextException(); |
|
|
|
} |
|
|
|
|
|
|
|
throw new InitializationException( "Error while setting up database.", e ); |
|
|
|
throw new ArchivaDatabaseException ( "Error while interacting with the database.", e ); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
@@ -101,9 +115,10 @@ public class IbatisMetadataStore |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void addMetadata( Metadata metadata ) |
|
|
|
throws MetadataStoreException |
|
|
|
|
|
|
|
|
|
|
|
protected void initializeTable( String tableName ) |
|
|
|
throws ArchivaDatabaseException |
|
|
|
{ |
|
|
|
SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); |
|
|
|
|
|
|
@@ -111,14 +126,34 @@ public class IbatisMetadataStore |
|
|
|
{ |
|
|
|
sqlMap.startTransaction(); |
|
|
|
|
|
|
|
getLogger().info( "Adding metadata key" ); |
|
|
|
sqlMap.update( "addMetadataKey", metadata ); |
|
|
|
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 adding metadata, showing all linked exceptions in SQLException." ); |
|
|
|
getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." ); |
|
|
|
|
|
|
|
while ( e != null ) |
|
|
|
{ |
|
|
@@ -127,7 +162,7 @@ public class IbatisMetadataStore |
|
|
|
e = e.getNextException(); |
|
|
|
} |
|
|
|
|
|
|
|
throw new MetadataStoreException ( "Error while interacting with the database.", e ); |
|
|
|
throw new ArchivaDatabaseException( "Error while setting up database.", e ); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
@@ -142,24 +177,23 @@ public class IbatisMetadataStore |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public MetadataKey getMetadataKey( Metadata metadata ) |
|
|
|
throws MetadataStoreException |
|
|
|
{ |
|
|
|
protected void dropTable( String tableName ) |
|
|
|
throws ArchivaDatabaseException |
|
|
|
{ |
|
|
|
SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
sqlMap.startTransaction(); |
|
|
|
|
|
|
|
getLogger().info( "Getting metadata key" ); |
|
|
|
MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); |
|
|
|
|
|
|
|
return newMetadataKey; |
|
|
|
getLogger().info( "Dropping table: " + tableName ); |
|
|
|
sqlMap.update( dropPrefix + tableName, null ); |
|
|
|
|
|
|
|
sqlMap.commitTransaction(); |
|
|
|
} |
|
|
|
catch ( SQLException e ) |
|
|
|
{ |
|
|
|
getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." ); |
|
|
|
getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." ); |
|
|
|
|
|
|
|
while ( e != null ) |
|
|
|
{ |
|
|
@@ -168,7 +202,7 @@ public class IbatisMetadataStore |
|
|
|
e = e.getNextException(); |
|
|
|
} |
|
|
|
|
|
|
|
throw new MetadataStoreException ( "Error while interacting with the database.", e ); |
|
|
|
throw new ArchivaDatabaseException( "Error while dropping database.", e ); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
@@ -182,5 +216,21 @@ public class IbatisMetadataStore |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void initialize() |
|
|
|
throws InitializationException |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
initializeTable( "MetadataKeys" ); |
|
|
|
} |
|
|
|
catch ( ArchivaDatabaseException ade ) |
|
|
|
{ |
|
|
|
throw new InitializationException( "unable to initialize metadata keys database" ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |