Skip to content

Commit

Permalink
TEIIDDES-2118: Factor out all connectivity plugin's message strings
Browse files Browse the repository at this point in the history
* Uses enums in Messages class to refactor all exception messages into
  separate resource properties file
  • Loading branch information
Paul Richardson committed Aug 7, 2014
1 parent cb232b2 commit 2478e6c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static String acquireDriverDefinition(ITeiidServerVersion serverVersion,
}

if (driverDefinitionId == null)
throw new Exception("Failure to find or create Teiid driver for version" + serverVersion); //$NON-NLS-1$
throw new Exception(Messages.getString(Messages.ConnectivityUtil.noTeiidDriverDefinitionFound, serverVersion));

storeJDBCPassword(connectionURL, password);
return driverDefinitionId;
Expand Down Expand Up @@ -232,7 +232,8 @@ public static IConnectionProfile createTransientTeiidProfile( ITeiidServerVersio
username,
vdbName));
} catch (Exception e) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "error getting profile", e); //$NON-NLS-1$
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0,
Messages.getString(Messages.ConnectivityUtil.errorGettingProfile), e);
throw new CoreException(status);
}

Expand Down Expand Up @@ -270,7 +271,8 @@ public static Properties createVDBTeiidProfileProperties(ITeiidServerVersion ser
driverDefinitionId = acquireDriverDefinition(serverVersion, driverPath, connectionURL, username, password);
return createDriverProps(serverVersion, driverDefinitionId, driverPath, connectionURL, username, vdbName);
} catch (Exception ex) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "error getting profile properties", ex); //$NON-NLS-1$
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0,
Messages.getString(Messages.ConnectivityUtil.errorGettingProfileProperties), ex);
throw new CoreException(status);
}
}
Expand Down Expand Up @@ -311,7 +313,8 @@ public static IConnectionProfile createVDBTeiidProfile( ITeiidServerVersion serv
username,
vdbName));
} catch (Exception e) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "error getting profile", e); //$NON-NLS-1$
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0,
Messages.getString(Messages.ConnectivityUtil.errorGettingProfile), e);
throw new CoreException(status);
}
}
Expand Down Expand Up @@ -362,7 +365,7 @@ public static Driver getTeiidDriver(ITeiidServerVersion teiidServerVersion, Stri
if (driver != null && driver.getClass().getName().equals(driverClass))
return driver;

String msg = "No Teiid Driver ( "+ driverClass + " ) available for version " + teiidServerVersion; //$NON-NLS-1$//$NON-NLS-2$
String msg = Messages.getString(Messages.ConnectivityUtil.noTeiidDriverFound, driverClass, teiidServerVersion);
throw new IllegalStateException(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.datatools.connectivity;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
*
*/
public class Messages {

private static final String BUNDLE_NAME = "org.teiid.datatools.connectivity.messages"; //$NON-NLS-1$

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

private static final String DOT = "."; //$NON-NLS-1$

@SuppressWarnings( "javadoc" )
public enum ConnectivityUtil {
noTeiidDriverDefinitionFound,
errorGettingProfile,
errorGettingProfileProperties,
noTeiidDriverFound;

@Override
public String toString() {
return getEnumName(this) + DOT + name();
}
}

@SuppressWarnings( "javadoc" )
public enum TeiidJDBCConnection {
noDriverFound,
invalidUserPassword;

@Override
public String toString() {
return getEnumName(this) + DOT + name();
}
}

private static String getEnumName(Enum<?> enumValue) {
String className = enumValue.getClass().getName();
String[] components = className.split("\\$"); //$NON-NLS-1$
return components[components.length - 1];
}

private Messages() {
}

/**
* Get message string
*
* @param key
*
* @return i18n string
*/
private static String getString(Enum<?> key) {
try {
return RESOURCE_BUNDLE.getString(key.toString());
} catch (final Exception err) {
String msg;

if (err instanceof NullPointerException) {
msg = "<No message available>"; //$NON-NLS-1$
} else if (err instanceof MissingResourceException) {
msg = "<Missing message for key \"" + key + "\" in: " + BUNDLE_NAME + '>'; //$NON-NLS-1$ //$NON-NLS-2$
} else {
msg = err.getLocalizedMessage();
}

return msg;
}
}

/**
* Get message string with parameters
*
* @param key
* @param parameters
*
* @return i18n string
*/
public static String getString(Enum<?> key, Object... parameters) {
String text = getString(key);

// Check the trivial cases ...
if (text == null) {
return '<' + key.toString() + '>';
}
if (parameters == null || parameters.length == 0) {
return text;
}

return MessageFormat.format(text, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public TeiidJDBCConnection( IConnectionProfile profile, Class factoryClass ) {

private Connection connect(Driver jdbcDriver, String connectURL, Properties connectionProps) throws Exception {
if (jdbcDriver == null)
throw new IllegalArgumentException("No driver found for connection to " + connectURL); //$NON-NLS-1$
throw new IllegalArgumentException(Messages.getString(Messages.TeiidJDBCConnection.noDriverFound, connectURL));

try {
return jdbcDriver.connect(connectURL, connectionProps);
} catch (SQLException ex) {
/* Found the driver ok but failed to connect */
String msg = "The teiid driver failed to connect to " + connectURL + " with the given username and password."; //$NON-NLS-1$//$NON-NLS-2$
String msg = Messages.getString(Messages.TeiidJDBCConnection.invalidUserPassword, connectURL);
throw new Exception(msg, ex);
}
}
Expand Down Expand Up @@ -122,6 +122,6 @@ protected Object createConnection( ClassLoader classloader ) throws Throwable {
}
}

throw new Exception("Cannot find suitable Teiid Driver for JDBC connection"); //$NON-NLS-1$
throw new Exception(Messages.getString(Messages.TeiidJDBCConnection.noDriverFound, connectURL));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# JBoss, Home of Professional Open Source.
#
# See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
#
# See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.

ConnectivityUtil.noTeiidDriverDefinitionFound=Failure to find or create Teiid driver definition for version {0}
ConnectivityUtil.errorGettingProfile=Error acquiring connection profile
ConnectivityUtil.errorGettingProfileProperties=Error acquiring connection profile properties
ConnectivityUtil.noTeiidDriverFound=No Teiid Driver ({0}) available for version {1}

TeiidJDBCConnection.noDriverFound=No driver found for connection to {0}
TeiidJDBCConnection.invalidUserPassword=The teiid driver failed to connect to {0} with the given username and password

0 comments on commit 2478e6c

Please sign in to comment.