View Javadoc
1 /*** $Id: DAOGeneratorTask.java,v 1.1.1.1 2004/05/18 10:50:16 mochoa Exp $*/ 2 package org.j2ee.dao.gen; 3 4 //ant 5 import org.apache.tools.ant.Task; 6 import org.apache.tools.ant.BuildException; 7 import org.apache.tools.ant.AntClassLoader; 8 import org.apache.tools.ant.types.Path; 9 import org.apache.tools.ant.types.Reference; 10 //sql 11 import java.sql.Driver; 12 import java.sql.SQLException; 13 import java.sql.Connection; 14 import java.sql.DatabaseMetaData; 15 //test 16 import java.util.Properties; 17 import java.util.Map; 18 import java.util.HashMap; 19 20 /*** 21 * Last modified $Date: 2004/05/18 10:50:16 $ 22 * @version $Revision: 1.1.1.1 $ 23 * @author jvlio (jvlio@users.sourceforge.net) - 04/11/2002 - 01:51:09 24 */ 25 public class DAOGeneratorTask extends Task { 26 27 private Path classpath = null; 28 private String driver = null; 29 private String url = null; 30 private String userid = null; 31 private String password = null; 32 private String rdbms = null; 33 private String version = null; 34 35 private String schema = null; 36 private String table = null; 37 private String pk = null; 38 private String field_prop_map = null; 39 private String field_jtype_map = null; 40 41 private String actions = null; 42 private String base_path = null; 43 private String common_name = null; 44 private String dao_package = null; 45 private String dto_package = null; 46 private String dto_name = null; 47 48 49 public void setClasspath(Path path) { 50 if(classpath == null) classpath = path; 51 else classpath.append(path); 52 } 53 54 public Path createClasspath() { 55 if(classpath == null) classpath = new Path(project); 56 return classpath.createPath(); 57 } 58 59 public void setClasspathRef(Reference reference) { 60 createClasspath().setRefid(reference); 61 } 62 63 public void setDriver(String s) { driver = s; } 64 public void setUrl(String s) { url = s; } 65 public void setUserid(String s) { userid = s; } 66 public void setPassword(String s) { password = s; } 67 public void setRdbms(String s) { rdbms = s.toLowerCase(); } 68 public void setVersion(String s) { version = s.toLowerCase(); } 69 70 public void setSchema(String schema) { this.schema = schema; } 71 public void setTable(String table) { this.table = table; } 72 public void setPk(String pk) { this.pk = pk; } 73 public void setField_prop_map(String field_prop_map) { this.field_prop_map = field_prop_map; } 74 public void setField_jtype_map(String field_jtype_map) { this.field_jtype_map = field_jtype_map; } 75 76 public void setActions(String actions) { this.actions = actions; } 77 public void setBase_path(String base_path) { this.base_path = base_path; } 78 public void setCommon_name(String common_name) { this.common_name = common_name; } 79 public void setDao_package(String dao_package) { this.dao_package = dao_package; } 80 public void setDto_package(String dto_package) { this.dto_package = dto_package; } 81 public void setDto_name(String dto_name) { this.dto_name = dto_name; } 82 83 84 85 public void execute() throws BuildException { 86 if(driver == null) throw new BuildException("Driver attribute must be set!", location); 87 if(userid == null) throw new BuildException("User Id attribute must be set!", location); 88 if(password == null) throw new BuildException("Password attribute must be set!", location); 89 if(url == null) throw new BuildException("Url attribute must be set!", location); 90 91 //load driver 92 ClassLoader loader = null; 93 Driver cdriver = null; 94 try { 95 Class class_driver = null; 96 if(classpath != null) { 97 log("Using AntClassLoader with classpath " + classpath, 3); 98 loader = new AntClassLoader(project, classpath); 99 } 100 else { 101 log("Using system loader.", 3); 102 loader = ClassLoader.getSystemClassLoader(); 103 } 104 log("Loading " + driver, 3); 105 class_driver = Class.forName(driver,true,loader); 106 cdriver = (Driver)class_driver.newInstance(); 107 } 108 catch(ClassNotFoundException classnotfoundexception) { 109 throw new BuildException("Class Not Found: JDBC driver " + driver + " could not be loaded", location); 110 } 111 catch(IllegalAccessException illegalaccessexception) { 112 throw new BuildException("Illegal Access: JDBC driver " + driver + " could not be loaded", location); 113 } 114 catch(InstantiationException instantiationexception) { 115 throw new BuildException("Instantiation Exception: JDBC driver " + driver + " could not be loaded", location); 116 } 117 // create GenData 118 GenData gd = null; 119 Connection conn=null; 120 try 121 { 122 log("connecting to " + url, 3); 123 Properties properties = new Properties(); 124 properties.put("user", userid); 125 properties.put("password", password); 126 conn = cdriver.connect(url, properties); 127 if(conn == null) throw new SQLException("No suitable Driver for " + url); 128 if(!isValidRdbms(conn)) 129 return; 130 //create gendata params 131 Map params = new HashMap(); 132 params.put(Util.SCHEMA,schema); 133 params.put(Util.TABLE,table); 134 params.put(Util.PK,pk); 135 params.put(Util.FIELD_PROP_MAP,field_prop_map); 136 params.put(Util.FIELD_JTYPE_MAP,field_jtype_map); 137 params.put(Util.BASE_PATH,base_path); 138 params.put(Util.COMMON_NAME,common_name); 139 params.put(Util.DAO_PACKAGE,dao_package); 140 params.put(Util.DTO_PACKAGE,dto_package); 141 params.put(Util.DTO_NAME,dto_name); 142 //create gendata 143 gd = Util.getInstance().createGeneratorData(conn,params); 144 } catch(SQLException ex) { 145 throw new BuildException(ex, location); 146 } finally { 147 try { 148 if(conn != null) conn.close(); 149 } catch(SQLException ex1) { } 150 } 151 // create Generator 152 DAOGenerator gen = null; 153 try { 154 gen = (DAOGenerator)DAOGenerator.class.newInstance(); 155 } catch (Exception e) { 156 e.printStackTrace(); 157 } 158 /*DAOGenerator gen = null; 159 try { 160 log("Pre create Generator"); 161 Class cgen = loadClass("org.j2ee.dao.gen.DAOGenerator"); 162 log("cgen ="+cgen); 163 gen = (DAOGenerator) cgen.newInstance(); 164 log("Post create Generator"); 165 } catch (Exception e) { 166 e.printStackTrace(); 167 }*/ 168 /*DAOGenerator gen = null; 169 try { 170 log("Pre create Generator"); 171 gen = (DAOGenerator)Class.forName("org.j2ee.dao.gen.DAOGenerator",true,loader).newInstance(); 172 log("Post create Generator"); 173 } catch (Exception e) { 174 e.printStackTrace(); 175 }*/ 176 //parse actions 177 Map actions_map = Util.getInstance().splitCSV(actions); 178 // create actions 179 Util.getInstance().createActionFiles(gen, gd, actions_map.keySet(), base_path, loader/*(loader!=null)? loader : ClassLoader.getSystemClassLoader()*/); 180 } 181 182 private Class loadClass(String string) throws Exception { 183 if (classpath == null) 184 return Class.forName(string); 185 AntClassLoader antclassloader = new AntClassLoader(project, classpath); 186 Class var_class = antclassloader.loadClass(string); 187 AntClassLoader.initializeClass(var_class); 188 return var_class; 189 } 190 191 private boolean isValidRdbms(Connection connection) { 192 if(rdbms == null && version == null) 193 return true; 194 try { 195 DatabaseMetaData databasemetadata = connection.getMetaData(); 196 if(rdbms != null) { 197 String s = databasemetadata.getDatabaseProductName().toLowerCase(); 198 log("RDBMS = " + s, 3); 199 if(s == null || s.indexOf(rdbms) < 0) { 200 log("Not the required RDBMS: " + rdbms, 3); 201 return false; 202 } 203 } 204 if(version != null) { 205 String s1 = databasemetadata.getDatabaseProductVersion().toLowerCase(); 206 log("Version = " + s1, 3); 207 if(s1 == null || !s1.startsWith(version) && s1.indexOf(" " + version) < 0) { 208 log("Not the required version: \"" + version + "\"", 3); 209 return false; 210 } 211 } 212 } catch(SQLException ex) { 213 log("Failed to obtain required RDBMS information", 0); 214 return false; 215 } 216 return true; 217 } 218 219 }

This page was automatically generated by Maven