1 24 package org.riotfamily.revolt; 25 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 import javax.sql.DataSource ; 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 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 scripts = new HashMap (); 54 55 private HashMap logTables = new HashMap (); 56 57 60 public void setAutomatic(boolean automatic) { 61 this.automatic = automatic; 62 } 63 64 68 public void setEnabled(boolean enabled) { 69 this.enabled = enabled; 70 } 71 72 public void setApplicationContext(ApplicationContext applicationContext) { 73 Collection evolutions = BeanFactoryUtils.beansOfTypeIncludingAncestors( 74 applicationContext, EvolutionHistory.class).values(); 75 76 if (enabled) { 77 Iterator 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 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 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 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 it = scripts.entrySet().iterator(); 127 while (it.hasNext()) { 128 Map.Entry entry = (Map.Entry ) it.next(); 129 DataSource dataSource = (DataSource ) entry.getKey(); 130 Script script = (Script) entry.getValue(); 131 if (!script.isManualExecutionOnly()) { 132 script.execute(dataSource); 133 } 134 } 135 } 136 137 private String getInstructions() { 138 StringBuffer sb = new StringBuffer (); 139 Iterator it = scripts.entrySet().iterator(); 140 while (it.hasNext()) { 141 Map.Entry entry = (Map.Entry ) it.next(); 142 DataSource dataSource = (DataSource ) entry.getKey(); 143 Script script = (Script) entry.getValue(); 144 if (!automatic || script.isManualExecutionOnly()) { 145 String 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
|