001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019package org.apache.james.mailbox.jpa.migrator; 020 021import java.util.Locale; 022 023import javax.persistence.EntityManager; 024import javax.persistence.EntityManagerFactory; 025import javax.persistence.Persistence; 026 027import org.apache.james.mailbox.jpa.migrator.command.JpaMigrateCommand; 028import org.apache.james.mailbox.jpa.migrator.exception.JpaMigrateException; 029 030/** 031 * The class that will manage the migration commands for the James JPA database. 032 * All SQL commands should be moved from JAVA code to a separate file. 033 */ 034public class JpaMigrator { 035 036 /** 037 * The package name where all commands reside. 038 */ 039 private static final String JPA_MIGRATION_COMMAND_PACKAGE = JpaMigrateCommand.class.getPackage().getName(); 040 041 /**<p>Executes the database migration for the provided JIRAs numbers. 042 * For example, for the https://issues.apache.org/jira/browse/IMAP-165 JIRA, simply invoke 043 * with IMAP165 as parameter. 044 * You can also invoke with many JIRA at once. They will be all serially executed.</p> 045 * 046 * TODO Extract the SQL in JAVA classes to XML file. 047 * TODO Log with slf4j. 048 * 049 * @param jiras the JIRAs numbers 050 * @throws JpaMigrateException 051 */ 052 public static void main(String[] jiras) throws JpaMigrateException { 053 054 try { 055 056 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JamesMigrator"); 057 EntityManager em = factory.createEntityManager(); 058 059 for (String jira: jiras) { 060 JpaMigrateCommand jiraJpaMigratable = (JpaMigrateCommand) Class.forName(JPA_MIGRATION_COMMAND_PACKAGE + "." + jira.toUpperCase(Locale.US) + JpaMigrateCommand.class.getSimpleName()).newInstance(); 061 System.out.println("Now executing " + jira + " migration."); 062 em.getTransaction().begin(); 063 jiraJpaMigratable.migrate(em); 064 em.getTransaction().commit(); 065 System.out.println(jira + " migration is successfully achieved."); 066 } 067 068 } 069 070 catch (Throwable t) { 071 throw new JpaMigrateException(t); 072 } 073 074 } 075 076}