1 package org.apache.maven.archiva.database;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.ibatis.sqlmap.client.SqlMapClient;
24 import org.codehaus.plexus.ibatis.PlexusIbatisHelper;
25 import org.codehaus.plexus.logging.AbstractLogEnabled;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
27 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
29 import java.sql.Connection;
30 import java.sql.DatabaseMetaData;
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
37 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
40 public abstract class AbstractIbatisStore
41 extends AbstractLogEnabled
42 implements Initializable
47 protected PlexusIbatisHelper ibatisHelper;
50 * @plexus.configuration default-value="create"
52 private String createPrefix;
55 * @plexus.configuration default-value="drop"
57 private String dropPrefix;
59 protected abstract String[] getTableNames();
61 public void initialize()
62 throws InitializationException
66 String tableNames[] = getTableNames();
67 for ( int i = 0; i < tableNames.length; i++ )
69 String tableName = tableNames[i];
70 initializeTable( tableName );
73 catch ( ArchivaDatabaseException e )
75 throw new InitializationException( "Unable to initialize the database: " + e.getMessage(), e );
79 protected void initializeTable( String tableName )
80 throws ArchivaDatabaseException
82 SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
86 sqlMap.startTransaction();
88 Connection con = sqlMap.getCurrentConnection();
90 DatabaseMetaData databaseMetaData = con.getMetaData();
92 ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null );
94 // check if the index database exists in the database
97 String dbTableName = rs.getString( "TABLE_NAME" );
99 // if it does then we are already initialized
100 if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) )
108 getLogger().info( "Creating table: " + tableName );
109 sqlMap.update( createPrefix + tableName, null );
111 sqlMap.commitTransaction();
113 catch ( SQLException e )
115 getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." );
119 getLogger().error( e.getMessage(), e );
121 e = e.getNextException();
124 throw new ArchivaDatabaseException( "Error while setting up database.", e );
130 sqlMap.endTransaction();
132 catch ( SQLException e )
139 protected void dropTable( String tableName )
140 throws ArchivaDatabaseException
142 SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
146 sqlMap.startTransaction();
148 getLogger().info( "Dropping table: " + tableName );
149 sqlMap.update( dropPrefix + tableName, null );
151 sqlMap.commitTransaction();
153 catch ( SQLException e )
155 getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." );
159 getLogger().error( e.getMessage(), e );
161 e = e.getNextException();
164 throw new ArchivaDatabaseException( "Error while dropping database.", e );
170 sqlMap.endTransaction();
172 catch ( SQLException e )