1 19 20 package org.apache.cayenne.tools; 21 22 import java.io.File ; 23 import java.sql.Driver ; 24 25 import org.apache.cayenne.access.DbGenerator; 26 import org.apache.cayenne.conn.DriverDataSource; 27 import org.apache.cayenne.dba.DbAdapter; 28 import org.apache.cayenne.dba.JdbcAdapter; 29 import org.apache.cayenne.map.DataMap; 30 import org.apache.cayenne.map.MapLoader; 31 import org.apache.cayenne.util.Util; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.Project; 34 import org.apache.tools.ant.Task; 35 import org.xml.sax.InputSource ; 36 37 44 public class DbGeneratorTask extends Task { 46 47 protected DbAdapter adapter; 48 protected File map; 49 protected String driver; 50 protected String url; 51 protected String userName; 52 protected String password; 53 54 protected boolean dropTables; 57 protected boolean dropPK; 58 protected boolean createTables = true; 59 protected boolean createPK = true; 60 protected boolean createFK = true; 61 62 63 public void execute() { 64 65 if (adapter == null) { 67 adapter = new JdbcAdapter(); 68 } 69 70 log("connection settings - [driver: " 71 + driver 72 + ", url: " 73 + url 74 + ", username: " 75 + userName 76 + "]", Project.MSG_VERBOSE); 77 78 log("generator options - [dropTables: " 79 + dropTables 80 + ", dropPK: " 81 + dropPK 82 + ", createTables: " 83 + createTables 84 + ", createPK: " 85 + createPK 86 + ", createFK: " 87 + createFK 88 + "]", Project.MSG_VERBOSE); 89 90 validateAttributes(); 91 92 try { 93 94 DataMap dataMap = loadDataMap(); 96 DbGenerator generator = new DbGenerator(adapter, dataMap); 97 generator.setShouldCreateFKConstraints(createFK); 98 generator.setShouldCreatePKSupport(createPK); 99 generator.setShouldCreateTables(createTables); 100 generator.setShouldDropPKSupport(dropPK); 101 generator.setShouldDropTables(dropTables); 102 103 DriverDataSource dataSource = new DriverDataSource((Driver ) Class.forName( 105 driver).newInstance(), url, userName, password); 106 107 generator.runGenerator(dataSource); 108 } 109 catch (Exception ex) { 110 Throwable th = Util.unwindException(ex); 111 112 String message = "Error generating database"; 113 114 if (th.getLocalizedMessage() != null) { 115 message += ": " + th.getLocalizedMessage(); 116 } 117 118 super.log(message); 119 throw new BuildException(message, th); 120 } 121 } 122 123 127 protected void validateAttributes() throws BuildException { 128 StringBuffer error = new StringBuffer (""); 129 130 if (map == null) { 131 error.append("The 'map' attribute must be set.\n"); 132 } 133 134 if (driver == null) { 135 error.append("The 'driver' attribute must be set.\n"); 136 } 137 138 if (url == null) { 139 error.append("The 'adapter' attribute must be set.\n"); 140 } 141 142 if (error.length() > 0) { 143 throw new BuildException(error.toString()); 144 } 145 } 146 147 148 protected DataMap loadDataMap() throws Exception { 149 InputSource in = new InputSource (map.getCanonicalPath()); 150 return new MapLoader().loadDataMap(in); 151 } 152 153 public void setCreateFK(boolean createFK) { 154 this.createFK = createFK; 155 } 156 157 public void setCreatePK(boolean createPK) { 158 this.createPK = createPK; 159 } 160 161 public void setCreateTables(boolean createTables) { 162 this.createTables = createTables; 163 } 164 165 public void setDropPK(boolean dropPK) { 166 this.dropPK = dropPK; 167 } 168 169 public void setDropTables(boolean dropTables) { 170 this.dropTables = dropTables; 171 } 172 173 178 public void setMap(File map) { 179 this.map = map; 180 } 181 182 187 public void setAdapter(String adapter) { 188 189 if (adapter != null) { 190 try { 192 Class c = Class.forName(adapter); 193 this.adapter = (DbAdapter) c.newInstance(); 194 } 195 catch (Exception e) { 196 throw new BuildException("Can't load DbAdapter: " + adapter); 197 } 198 } 199 } 200 201 206 public void setDriver(String driver) { 207 this.driver = driver; 208 } 209 210 215 public void setUrl(String url) { 216 this.url = url; 217 } 218 219 224 public void setUserName(String username) { 225 this.userName = username; 226 } 227 228 233 public void setPassword(String password) { 234 this.password = password; 235 } 236 237 } 238 | Popular Tags |