hello
when i specify classpath by java.exe -classpath and create a new instance of HibernateToolTask , the hibernateToolTask that loaded by java class loader wants to load jdbcDriver by his classloader and and this class loader hasnt the path of jdbcDriver ,,,
i use reflection to resolve this problem ,,
try {
Project project = new Project();
project.setBaseDir(new File("D:/DevDesk/hibernateToolsTest/src"));
Path p = new Path(project);
// Reference reference = new Reference();
// reference.setRefId("toolslib");
// p.setRefid(reference);
p.add(new Path(project, "D:/DevDesk/hibernateToolsTest/src"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/tools/hibernate-tools.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/hibernate3.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/tools/freemarker.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/tools/jtidy-r8-21122004.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/dom4j-1.6.1.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/log4j-1.2.11.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/commons-logging-1.0.4.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/commons-collections-2.1.1.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/cglib-2.1.3.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/ehcache-1.1.jar"));
p.add(new Path(project, "D:/code.java/hibernate-tools/lib/hibernate/jta.jar"));
p.add(new Path(project, "C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/msbase.jar"));
p.add(new Path(project, "C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/mssqlserver.jar"));
p.add(new Path(project, "C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/msutil.jar"));
Taskdef hibernatetool = new Taskdef();
hibernatetool.setName("hibernatetool");
hibernatetool.setClassname("org.hibernate.tool.ant.HibernateToolTask");
// hibernatetool.setClasspathRef(reference);
hibernatetool.setClasspath(p);
hibernatetool.init();
hibernatetool.reconfigure();
Task htt = null;// new HibernateToolTask();
Class hibernateToolTaskClass = null;
AntClassLoader classLoader = project.createClassLoader(p);
//classLoader.setIsolated(true);
classLoader.setParentFirst(false);
//classLoader.setParent(this.getClass().getClassLoader());
//classLoader.setParentFirst(false);
hibernateToolTaskClass = classLoader.findClass("org.hibernate.tool.ant.HibernateToolTask");
htt = (Task) hibernateToolTaskClass.newInstance();
// Class<HibernateToolTask> cls=(Class<HibernateToolTask>) o.getClass();
// htt=cls.cast(o);
// System.out.println(o.getClass());//.cast(o);
Target pojo = new Target();
pojo.setName("pojo");
pojo.setDescription("generate java pojo classes with hbm2java");
//HibernateToolTask t = new HibernateToolTask();
Method createJDBCConfiguration = hibernateToolTaskClass.getMethod("createJDBCConfiguration");
Object jDBCConfigurationTask = createJDBCConfiguration.invoke(htt);
//JDBCConfigurationTask ct=(JDBCConfigurationTask)jDBCConfigurationTask;
Method setProject = jDBCConfigurationTask.getClass().getMethod("setProject", project.getClass());
setProject.invoke(jDBCConfigurationTask, project);
//ct.setProject(project);
Method setConfigurationFile = jDBCConfigurationTask.getClass().getMethod("setConfigurationFile", File.class);
setConfigurationFile.invoke(jDBCConfigurationTask, new File("D:/DevDesk/hibernateToolsTest/src/hibernate.cfg.xml"));
//ct.setConfigurationFile(new File("D:/DevDesk/hibernateToolsTest/src/hibernate.cfg.xml"));
Method setPackageName = jDBCConfigurationTask.getClass().getMethod("setPackageName", String.class);
setPackageName.invoke(jDBCConfigurationTask, new String("aria.roohi"));
//ct.setPackageName("aria.roohi");
Method getConfiguration = jDBCConfigurationTask.getClass().getMethod("getConfiguration");
//Object cfg =getConfiguration.invoke(jDBCConfigurationTask);
//Object cfg = ct.getConfiguration();
Method setDestDir = hibernateToolTaskClass.getMethod("setDestDir", File.class);
setDestDir.invoke(htt, new File("D:/DevDesk/hibernateToolsTest/src/generated"));
//htt.setDestDir(new File("D:/DevDesk/hibernateToolsTest/src/generated"));
Method createHbm2HbmXml = hibernateToolTaskClass.getMethod("createHbm2HbmXml");
createHbm2HbmXml.invoke(htt);
//htt.createHbm2HbmXml();
pojo.addTask(htt);
AntRunner ar = new AntRunner();
ar.runTask(htt, "D:/DevDesk/hibernateToolsTest/src");
} catch (Exception e) {
e.printStackTrace();
}
this code work correctly ,,
are there any better idea ???
|