-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a common ConnectionProfileDetailsPage in both the CreateConnectio…
…nProfileCommand and the ConnectionProfileDetailsStep
- Loading branch information
Showing
5 changed files
with
185 additions
and
194 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
impl/src/main/java/org/hibernate/forge/addon/connections/ConnectionProfileDetailsPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package org.hibernate.forge.addon.connections; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.sql.Driver; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.hibernate.forge.addon.util.HibernateToolsHelper; | ||
import org.jboss.forge.addon.resource.FileResource; | ||
import org.jboss.forge.addon.ui.context.UIBuilder; | ||
import org.jboss.forge.addon.ui.context.UIValidationContext; | ||
import org.jboss.forge.addon.ui.input.UIInput; | ||
import org.jboss.forge.addon.ui.metadata.WithAttributes; | ||
|
||
public class ConnectionProfileDetailsPage | ||
{ | ||
|
||
@Inject | ||
@WithAttributes( | ||
label = "JDBC URL", | ||
description = "The jdbc url for the database tables", | ||
required = true) | ||
protected UIInput<String> jdbcUrl; | ||
|
||
@Inject | ||
@WithAttributes( | ||
label = "User Name", | ||
description = "The user name for the database connection", | ||
required = true) | ||
protected UIInput<String> userName; | ||
|
||
@Inject | ||
@WithAttributes( | ||
label = "User Password", | ||
description = "The password for the database connection", | ||
required = false, | ||
defaultValue = "") | ||
protected UIInput<String> userPassword; | ||
|
||
@Inject | ||
@WithAttributes( | ||
label = "Hibernate Dialect", | ||
description = "The Hibernate dialect to use", | ||
required = true) | ||
protected UIInput<String> hibernateDialect; | ||
|
||
@Inject | ||
@WithAttributes( | ||
label = "Driver Location", | ||
description = "The location of the jar file that contains the JDBC driver", | ||
required = true) | ||
protected UIInput<FileResource<?>> driverLocation; | ||
|
||
@Inject | ||
@WithAttributes( | ||
label = "Driver Class", | ||
description = "The class name of the JDBC driver", | ||
required = true) | ||
protected UIInput<String> driverClass; | ||
|
||
@Inject | ||
private HibernateToolsHelper helper; | ||
|
||
protected URL[] urls; | ||
protected Driver driver; | ||
|
||
public void initializeUI(UIBuilder builder) throws Exception | ||
{ | ||
builder | ||
.add(jdbcUrl) | ||
.add(userName) | ||
.add(userPassword) | ||
.add(hibernateDialect) | ||
.add(driverLocation) | ||
.add(driverClass); | ||
|
||
} | ||
|
||
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.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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.