diff --git a/impl/src/main/java/org/hibernate/forge/addon/connections/ConnectionProfileDetailsPage.java b/impl/src/main/java/org/hibernate/forge/addon/connections/ConnectionProfileDetailsPage.java index 4a60553..0d196b7 100644 --- a/impl/src/main/java/org/hibernate/forge/addon/connections/ConnectionProfileDetailsPage.java +++ b/impl/src/main/java/org/hibernate/forge/addon/connections/ConnectionProfileDetailsPage.java @@ -1,19 +1,19 @@ package org.hibernate.forge.addon.connections; import java.io.File; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.sql.Driver; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.Callable; import java.util.jar.JarEntry; import java.util.jar.JarFile; import javax.inject.Inject; -import org.hibernate.forge.addon.util.HibernateToolsHelper; import org.jboss.forge.addon.convert.Converter; import org.jboss.forge.addon.resource.FileResource; import org.jboss.forge.addon.ui.UIValidator; @@ -69,12 +69,6 @@ public class ConnectionProfileDetailsPage required = true) protected UISelectOne driverClass; - @Inject - private HibernateToolsHelper helper; - - protected URL[] urls; - protected Driver driver; - public void initializeUI(UIBuilder builder) throws Exception { builder @@ -97,118 +91,72 @@ public String convert(HibernateDialect dialect) @Override public void validate(UIValidationContext context) { - File file = getDriverLocation(context); - if (file != null) { - driverClass.setValueChoices(getDriverClassNames(file)); + FileResource resource = driverLocation.getValue(); + if (resource != null && !resource.exists()) { + context.addValidationError(driverLocation, "The location '" + resource.getFullyQualifiedName() + "' does not exist"); } } }); + driverClass.setValueChoices(new Callable>() { + @Override + public Iterable call() throws Exception + { + return getDriverClassNames(); + } + }); + driverClass.setDefaultValue(new Callable() { + @Override + public String call() throws Exception + { + String result = null; + Iterator iterator = driverClass.getValueChoices().iterator(); + if (iterator.hasNext()) { + result = iterator.next(); + } + return result; + } + }); } - public void validate(UIValidationContext context) - { - File file = getDriverLocation(context); - if (file == null) return; - urls = getDriverUrls(file, context); - if (urls == null) { - return; - } - driver = getDriver(urls, context); - if (driver == null) { - return; - } - } - - private File getDriverLocation(UIValidationContext context) { - FileResource resource = driverLocation.getValue(); - if (resource == null) { - return null; - } else if (!resource.exists()) { - context.addValidationError(driverLocation, "The location '" + resource.getFullyQualifiedName() + "' does not exist"); - return null; - } - return resource.getUnderlyingResourceObject(); - } - - private URL[] getDriverUrls(File file, UIValidationContext context) - { - try { - ArrayList result = new ArrayList(1); - result.add(file.toURI().toURL()); - return result.toArray(new URL[1]); - } catch (MalformedURLException e) { - context.addValidationError(driverLocation, - "The location '" + - driverLocation.getValue() + - "' does not point to a valid file"); - return null; - } - } - - private Driver getDriver(URL[] urls, UIValidationContext context) { - Driver result = null; - String className = driverClass.getValue(); - try - { - result = helper.getDriver(className, urls); - } - catch (InstantiationException e) - { - context.addValidationError( - driverClass, - "The class '" + className + "' cannot not be instantiated"); - } - catch (IllegalAccessException e) - { - context.addValidationError( - driverClass, - "Illegal access for class '" + className + "'"); - } - catch (ClassNotFoundException e) - { - context.addValidationError( - driverClass, - "The class '" + className + "' does not exist"); - } - catch (SQLException e) { - context.addValidationError( - driverClass, - "An unexpected SQLException happened while registering class '" + className + "'"); - } - return result; - } - - protected ArrayList getDriverClassNames(File file) { + private List getDriverClassNames() { ArrayList result = new ArrayList(); - try { - URL[] urls = new URL[] { file.toURI().toURL() }; - URLClassLoader classLoader = URLClassLoader.newInstance(urls); - Class driverClass = classLoader.loadClass(Driver.class.getName()); - JarFile jarFile = new JarFile(file); - Enumeration iter = jarFile.entries(); - while (iter.hasMoreElements()) { - JarEntry entry = iter.nextElement(); - if (entry.getName().endsWith(".class")) { - String name = entry.getName(); - name = name.substring(0, name.length() - 6); - name = name.replace('/', '.'); - try { - Class clazz = classLoader.loadClass(name); - if (driverClass.isAssignableFrom(clazz)) { - result.add(clazz.getName()); + FileResource resource = driverLocation.getValue(); + if (resource != null && resource.exists()) { + try { + File file = (File)resource.getUnderlyingResourceObject(); + URL[] urls = new URL[] { file.toURI().toURL() }; + URLClassLoader classLoader = URLClassLoader.newInstance(urls); + Class driverClass = classLoader.loadClass(Driver.class.getName()); + JarFile jarFile = new JarFile(file); + Enumeration iter = jarFile.entries(); + while (iter.hasMoreElements()) { + JarEntry entry = iter.nextElement(); + if (entry.getName().endsWith(".class")) { + String name = entry.getName(); + name = name.substring(0, name.length() - 6); + name = name.replace('/', '.'); + try { + Class clazz = classLoader.loadClass(name); + if (driverClass.isAssignableFrom(clazz)) { + result.add(clazz.getName()); + } + } catch (ClassNotFoundException cnfe) { + //ignore + } catch (NoClassDefFoundError err) { + //ignore } - } catch (ClassNotFoundException cnfe) { - //ignore - } catch (NoClassDefFoundError err) { - //ignore } } + } catch (Exception e) { + // ignore and return an empty list } - } catch (Exception e) { - // ignore and return an empty list } return result; } - + + public void validate(UIValidationContext context) + { + } + } diff --git a/impl/src/main/java/org/hibernate/forge/addon/generate/ConnectionProfileDetailsStep.java b/impl/src/main/java/org/hibernate/forge/addon/generate/ConnectionProfileDetailsStep.java index 3152723..8cb828c 100644 --- a/impl/src/main/java/org/hibernate/forge/addon/generate/ConnectionProfileDetailsStep.java +++ b/impl/src/main/java/org/hibernate/forge/addon/generate/ConnectionProfileDetailsStep.java @@ -9,6 +9,7 @@ import org.hibernate.forge.addon.connections.ConnectionProfileDetailsPage; import org.hibernate.forge.addon.connections.ConnectionProfileManager; import org.hibernate.forge.addon.connections.HibernateDialect; +import org.hibernate.forge.addon.util.HibernateToolsHelper; import org.jboss.forge.addon.resource.FileResource; import org.jboss.forge.addon.resource.ResourceFactory; import org.jboss.forge.addon.ui.context.UIBuilder; @@ -36,6 +37,9 @@ public class ConnectionProfileDetailsStep extends ConnectionProfileDetailsPage i @Inject private ResourceFactory factory; + @Inject + private HibernateToolsHelper helper; + @Override public UICommandMetadata getMetadata(UIContext context) { @@ -64,15 +68,8 @@ public void initializeUI(UIBuilder builder) throws Exception userName.setValue(cp.user); userPassword.setValue(cp.password); hibernateDialect.setValue(HibernateDialect.fromClassName(cp.dialect)); - FileResource fileResource = createResource(cp.path); - if (fileResource != null) { - driverLocation.setValue(fileResource); - if (fileResource.exists()) { - File file = (File)fileResource.getUnderlyingResourceObject(); - driverClass.setValueChoices(getDriverClassNames(file)); - driverClass.setValue(cp.driver); - } - } + driverLocation.setValue(createResource(cp.path)); + driverClass.setValue(cp.driver); } } @@ -92,8 +89,8 @@ public NavigationResult next(UIContext context) throws Exception public void validate(UIValidationContext context) { super.validate(context); - descriptor.urls = urls; - descriptor.driver = driver; + descriptor.urls = helper.getDriverUrls(driverLocation.getValue()); + descriptor.driverClass = driverClass.getValue(); descriptor.connectionProperties = createConnectionProperties(); } diff --git a/impl/src/main/java/org/hibernate/forge/addon/generate/DatabaseTableSelectionStep.java b/impl/src/main/java/org/hibernate/forge/addon/generate/DatabaseTableSelectionStep.java index 4758660..1ab33cf 100644 --- a/impl/src/main/java/org/hibernate/forge/addon/generate/DatabaseTableSelectionStep.java +++ b/impl/src/main/java/org/hibernate/forge/addon/generate/DatabaseTableSelectionStep.java @@ -82,7 +82,7 @@ public void initializeUI(UIBuilder builder) throws Exception jmdc = new JDBCMetaDataConfiguration(); jmdc.setProperties(descriptor.connectionProperties); jmdc.setReverseEngineeringStrategy(createReverseEngineeringStrategy()); - helper.buildMappings(descriptor.urls, descriptor.driver, jmdc); + helper.buildMappings(descriptor.urls, descriptor.driverClass, jmdc); Iterator iterator = jmdc.getTableMappings(); ArrayList tables = new ArrayList(); while (iterator.hasNext()) { diff --git a/impl/src/main/java/org/hibernate/forge/addon/generate/GenerateEntitiesCommandDescriptor.java b/impl/src/main/java/org/hibernate/forge/addon/generate/GenerateEntitiesCommandDescriptor.java index a666f0b..04c4f91 100644 --- a/impl/src/main/java/org/hibernate/forge/addon/generate/GenerateEntitiesCommandDescriptor.java +++ b/impl/src/main/java/org/hibernate/forge/addon/generate/GenerateEntitiesCommandDescriptor.java @@ -1,7 +1,6 @@ package org.hibernate.forge.addon.generate; import java.net.URL; -import java.sql.Driver; import java.util.Properties; import org.hibernate.forge.addon.connections.ConnectionProfile; @@ -16,7 +15,7 @@ public class GenerateEntitiesCommandDescriptor Project selectedProject; ConnectionProfile connectionProfile; URL[] urls; - Driver driver; + String driverClass; Properties connectionProperties; } diff --git a/impl/src/main/java/org/hibernate/forge/addon/util/HibernateToolsHelper.java b/impl/src/main/java/org/hibernate/forge/addon/util/HibernateToolsHelper.java index 2f6e691..7f1a244 100644 --- a/impl/src/main/java/org/hibernate/forge/addon/util/HibernateToolsHelper.java +++ b/impl/src/main/java/org/hibernate/forge/addon/util/HibernateToolsHelper.java @@ -1,104 +1,54 @@ package org.hibernate.forge.addon.util; +import java.io.File; +import java.net.MalformedURLException; import java.net.URL; import java.sql.Driver; import java.sql.DriverManager; -import java.sql.SQLException; +import java.util.ArrayList; import org.hibernate.cfg.JDBCMetaDataConfiguration; +import org.jboss.forge.addon.resource.FileResource; public class HibernateToolsHelper { - - private Driver driver; - private InstantiationException instantiationException; - private IllegalAccessException illegalAccessException; - private ClassNotFoundException classNotFoundException; - private SQLException sqlException; - - public synchronized Driver getDriver(final String driverName, URL[] urls) - throws InstantiationException, - IllegalAccessException, - ClassNotFoundException, - SQLException { - reset(); - Driver result = null; - UrlClassLoaderExecutor.execute(urls, new Runnable() { - @Override - public void run() - { - try - { - driver = (Driver) Class.forName( - driverName, - true, - Thread.currentThread().getContextClassLoader()).newInstance(); - DriverManager.registerDriver(new DelegatingDriver(driver)); - } - catch (InstantiationException e) - { - instantiationException = e; - } - catch (IllegalAccessException e) - { - illegalAccessException = e; - } - catch (ClassNotFoundException e) - { - classNotFoundException = e; - } - catch (SQLException e) - { - sqlException = e; - } - } - }); - if (instantiationException != null) { - throw instantiationException; - } - if (illegalAccessException != null) { - throw illegalAccessException; - } - if (classNotFoundException != null) { - throw classNotFoundException; - } - if (sqlException != null) { - throw sqlException; - } - result = driver; - return result; - } - - public synchronized void buildMappings( + + public void buildMappings( URL[] urls, - final Driver driver, + final String driverName, final JDBCMetaDataConfiguration result) { - reset(); UrlClassLoaderExecutor.execute(urls, new Runnable() { @Override public void run() { try { + Driver driver = (Driver) Class.forName( + driverName, + true, + Thread.currentThread().getContextClassLoader()).newInstance(); DriverManager.registerDriver(new DelegatingDriver(driver)); result.readFromJDBC(); result.buildMappings(); } - catch (SQLException e) + catch (Exception e) { - // registering driver should not pose any problems at this point + e.printStackTrace(); } } }); } - private void reset() { - driver = null; - instantiationException = null; - illegalAccessException = null; - classNotFoundException = null; - sqlException = null; + public URL[] getDriverUrls(FileResource resource) + { + try { + File file = (File)resource.getUnderlyingResourceObject(); + ArrayList result = new ArrayList(1); + result.add(file.toURI().toURL()); + return result.toArray(new URL[1]); + } catch (MalformedURLException e) { + return null; + } } - }