Skip to content

Commit

Permalink
dependency between jdbc driver location field and class dropdown is n…
Browse files Browse the repository at this point in the history
…ow working properly
  • Loading branch information
koentsje committed Oct 11, 2013
1 parent 77f680f commit e3b2ecf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -69,12 +69,6 @@ public class ConnectionProfileDetailsPage
required = true)
protected UISelectOne<String> driverClass;

@Inject
private HibernateToolsHelper helper;

protected URL[] urls;
protected Driver driver;

public void initializeUI(UIBuilder builder) throws Exception
{
builder
Expand All @@ -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<Iterable<String>>() {
@Override
public Iterable<String> call() throws Exception
{
return getDriverClassNames();
}
});
driverClass.setDefaultValue(new Callable<String>() {
@Override
public String call() throws Exception
{
String result = null;
Iterator<String> 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<URL> result = new ArrayList<URL>(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<String> getDriverClassNames(File file) {
private List<String> getDriverClassNames() {
ArrayList<String> result = new ArrayList<String>();
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<JarEntry> 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<JarEntry> 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)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +37,9 @@ public class ConnectionProfileDetailsStep extends ConnectionProfileDetailsPage i
@Inject
private ResourceFactory factory;

@Inject
private HibernateToolsHelper helper;

@Override
public UICommandMetadata getMetadata(UIContext context)
{
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> iterator = jmdc.getTableMappings();
ArrayList<String> tables = new ArrayList<String>();
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +15,7 @@ public class GenerateEntitiesCommandDescriptor
Project selectedProject;
ConnectionProfile connectionProfile;
URL[] urls;
Driver driver;
String driverClass;
Properties connectionProperties;

}
Loading

0 comments on commit e3b2ecf

Please sign in to comment.