1 package org.apache.maven.archiva.database;
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
23 import com.ibatis.sqlmap.client.SqlMapClient;
25 import org.apache.maven.archiva.database.key.MetadataKey;
26 import org.apache.maven.artifact.repository.metadata.Metadata;
27 import org.codehaus.plexus.ibatis.PlexusIbatisHelper;
28 import org.codehaus.plexus.logging.AbstractLogEnabled;
29 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
32 import java.sql.Connection;
33 import java.sql.DatabaseMetaData;
34 import java.sql.ResultSet;
35 import java.sql.SQLException;
42 * @author <a href="mailto:jmcconnell@apache.com">Jesse McConnell</a>
46 public class AbstractMetadataKeyDatabase
47 extends AbstractLogEnabled
48 implements Initializable
53 protected PlexusIbatisHelper ibatisHelper;
56 * @plexus.configuration default-value="create"
58 private String createPrefix;
61 * @plexus.configuration default-value="drop"
63 private String dropPrefix;
65 public MetadataKey getMetadataKey( Metadata metadata )
66 throws ArchivaDatabaseException
68 SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
72 sqlMap.startTransaction();
74 getLogger().info( "Getting metadata key" );
75 MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata );
77 if ( newMetadataKey == null )
79 getLogger().info( "added new metadata" );
80 sqlMap.update( "addMetadataKey", metadata );
82 newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata );
84 if ( newMetadataKey == null )
86 throw new ArchivaDatabaseException( "unable to create new MetadataKeys" );
90 return newMetadataKey;
93 catch ( SQLException e )
95 getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." );
99 getLogger().error( e.getMessage(), e );
101 e = e.getNextException();
104 throw new ArchivaDatabaseException ( "Error while interacting with the database.", e );
110 sqlMap.endTransaction();
112 catch ( SQLException e )
120 protected void initializeTable( String tableName )
121 throws ArchivaDatabaseException
123 SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
127 sqlMap.startTransaction();
129 Connection con = sqlMap.getCurrentConnection();
131 DatabaseMetaData databaseMetaData = con.getMetaData();
133 ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null );
135 // check if the index database exists in the database
138 String dbTableName = rs.getString( "TABLE_NAME" );
140 // if it does then we are already initialized
141 if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) )
149 getLogger().info( "Creating table: " + tableName );
150 sqlMap.update( createPrefix + tableName, null );
152 sqlMap.commitTransaction();
154 catch ( SQLException e )
156 getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." );
160 getLogger().error( e.getMessage(), e );
162 e = e.getNextException();
165 throw new ArchivaDatabaseException( "Error while setting up database.", e );
171 sqlMap.endTransaction();
173 catch ( SQLException e )
180 protected void dropTable( String tableName )
181 throws ArchivaDatabaseException
183 SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
187 sqlMap.startTransaction();
189 getLogger().info( "Dropping table: " + tableName );
190 sqlMap.update( dropPrefix + tableName, null );
192 sqlMap.commitTransaction();
194 catch ( SQLException e )
196 getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." );
200 getLogger().error( e.getMessage(), e );
202 e = e.getNextException();
205 throw new ArchivaDatabaseException( "Error while dropping database.", e );
211 sqlMap.endTransaction();
213 catch ( SQLException e )
220 public void initialize()
221 throws InitializationException
225 initializeTable( "MetadataKeys" );
227 catch ( ArchivaDatabaseException ade )
229 throw new InitializationException( "unable to initialize metadata keys database" );
234 protected boolean tableExists( String tableName )
235 throws ArchivaDatabaseException
237 SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
241 sqlMap.startTransaction();
243 Connection con = sqlMap.getCurrentConnection();
245 DatabaseMetaData databaseMetaData = con.getMetaData();
247 ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null );
249 // check if the index database exists in the database
252 String dbTableName = rs.getString( "TABLE_NAME" );
254 // if it does then we are already initialized
255 if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) )
262 catch ( SQLException e )
264 getLogger().error( "Error while check database, showing all linked exceptions in SQLException." );
268 getLogger().error( e.getMessage(), e );
270 e = e.getNextException();
273 throw new ArchivaDatabaseException( "Error while checking database.", e );
279 sqlMap.endTransaction();
281 catch ( SQLException e )