KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > Evolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt;
25
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.sql.DataSource JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.riotfamily.revolt.support.DatabaseUtils;
36 import org.riotfamily.revolt.support.LogTable;
37 import org.springframework.beans.factory.BeanFactoryUtils;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.ApplicationContextAware;
40 import org.springframework.util.StringUtils;
41
42 /**
43  * @author Felix Gnass [fgnass at neteye dot de]
44  */

45 public class Evolver implements ApplicationContextAware {
46
47     private static final Log log = LogFactory.getLog(Evolver.class);
48     
49     private boolean enabled = true;
50     
51     private boolean automatic;
52     
53     private HashMap JavaDoc scripts = new HashMap JavaDoc();
54     
55     private HashMap JavaDoc logTables = new HashMap JavaDoc();
56     
57     /**
58      * Sets whether Revolt should automatically apply pending refactorings.
59      */

60     public void setAutomatic(boolean automatic) {
61         this.automatic = automatic;
62     }
63     
64     /**
65      * Sets whether Revolt should validate/evolve the schema.
66      * Default is <code>true</code>.
67      */

68     public void setEnabled(boolean enabled) {
69         this.enabled = enabled;
70     }
71
72     public void setApplicationContext(ApplicationContext applicationContext) {
73         Collection JavaDoc evolutions = BeanFactoryUtils.beansOfTypeIncludingAncestors(
74                 applicationContext, EvolutionHistory.class).values();
75         
76         if (enabled) {
77             Iterator JavaDoc it = evolutions.iterator();
78             
79             while (it.hasNext()) {
80                 EvolutionHistory history = (EvolutionHistory) it.next();
81                 history.init(getLogTable(history));
82                 getScript(history).append(history.getScript());
83             }
84             
85             if (automatic) {
86                 executeScripts();
87             }
88             
89             String JavaDoc instructions = getInstructions();
90             if (StringUtils.hasLength(instructions)) {
91                 throw new EvolutionInstructions(instructions);
92             }
93             
94             while (it.hasNext()) {
95                 EvolutionHistory history = (EvolutionHistory) it.next();
96                 history.validate();
97             }
98         }
99     }
100     
101     private LogTable getLogTable(EvolutionHistory history) {
102         DataSource JavaDoc dataSource = history.getDataSource();
103         LogTable logTable = (LogTable) logTables.get(dataSource);
104         if (logTable == null) {
105             logTable = new LogTable(dataSource, history.getDialect());
106             logTables.put(history.getDataSource(), logTable);
107             if (!logTable.exists()) {
108                 log.info("Revolt log-table does not exist.");
109                 getScript(history).append(logTable.getCreateTableScript());
110             }
111         }
112         return logTable;
113     }
114     
115     private Script getScript(EvolutionHistory history) {
116         DataSource JavaDoc dataSource = history.getDataSource();
117         Script script = (Script) scripts.get(dataSource);
118         if (script == null) {
119             script = new Script();
120             scripts.put(dataSource, script);
121         }
122         return script;
123     }
124     
125     private void executeScripts() {
126         Iterator JavaDoc it = scripts.entrySet().iterator();
127         while (it.hasNext()) {
128             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
129             DataSource JavaDoc dataSource = (DataSource JavaDoc) entry.getKey();
130             Script script = (Script) entry.getValue();
131             if (!script.isManualExecutionOnly()) {
132                 script.execute(dataSource);
133             }
134         }
135     }
136         
137     private String JavaDoc getInstructions() {
138         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
139         Iterator JavaDoc it = scripts.entrySet().iterator();
140         while (it.hasNext()) {
141             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
142             DataSource JavaDoc dataSource = (DataSource JavaDoc) entry.getKey();
143             Script script = (Script) entry.getValue();
144             if (!automatic || script.isManualExecutionOnly()) {
145                 String JavaDoc sql = script.getSql();
146                 if (StringUtils.hasLength(sql)) {
147                     sb.append("\n\n-------------------------------------------------------------------------\n\n");
148                     sb.append("The database ").append(DatabaseUtils.getUrl(dataSource));
149                     sb.append(" is not up-to-date.\nPlease execute the"
150                             + " following SQL commands to evolve the schema:\n\n");
151                             
152                     sb.append(sql);
153                     sb.append("\n\n-------------------------------------------------------------------------\n\n");
154                 }
155             }
156         }
157         return sb.toString();
158     }
159     
160 }
161
Popular Tags