Skip to content

Commit

Permalink
Fix Object Factory to check class type when instantiating an object f…
Browse files Browse the repository at this point in the history
…rom class name
  • Loading branch information
iggarish committed May 11, 2022
1 parent 0c956c7 commit 9999659
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static SocketFactory getSocketFactory(Properties info) throws RedshiftExc
return SocketFactory.getDefault();
}
try {
return (SocketFactory) ObjectFactory.instantiate(socketFactoryClassName, info, true,
return ObjectFactory.instantiate(SocketFactory.class, socketFactoryClassName, info, true,
RedshiftProperty.SOCKET_FACTORY_ARG.get(info));
} catch (Exception e) {
throw new RedshiftException(
Expand Down Expand Up @@ -66,7 +66,7 @@ public static SSLSocketFactory getSslSocketFactory(Properties info) throws Redsh
if (classname.equals(RedshiftConnectionImpl.NON_VALIDATING_SSL_FACTORY))
classname = NonValidatingFactory.class.getName();

return (SSLSocketFactory) ObjectFactory.instantiate(classname, info, true,
return ObjectFactory.instantiate(SSLSocketFactory.class, classname, info, true,
RedshiftProperty.SSL_FACTORY_ARG.get(info));
} catch (Exception e) {
throw new RedshiftException(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amazon/redshift/ssl/LibPQFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private CallbackHandler getCallbackHandler(Properties info) throws RedshiftExcep
String sslpasswordcallback = RedshiftProperty.SSL_PASSWORD_CALLBACK.get(info);
if (sslpasswordcallback != null) {
try {
cbh = (CallbackHandler) ObjectFactory.instantiate(sslpasswordcallback, info, false, null);
cbh = ObjectFactory.instantiate(CallbackHandler.class,sslpasswordcallback, info, false, null);
} catch (Exception e) {
throw new RedshiftException(
GT.tr("The password callback class provided {0} could not be instantiated.",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amazon/redshift/ssl/MakeSSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static void verifyPeerName(RedshiftStream stream, Properties info, SSLSo
sslhostnameverifier = "RedshiftjdbcHostnameVerifier";
} else {
try {
hvn = (HostnameVerifier) instantiate(sslhostnameverifier, info, false, null);
hvn = instantiate(HostnameVerifier.class, sslhostnameverifier, info, false, null);
} catch (Exception e) {
throw new RedshiftException(
GT.tr("The HostnameVerifier class provided {0} could not be instantiated.",
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/amazon/redshift/util/ObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class ObjectFactory {
* @throws IllegalAccessException if something goes wrong
* @throws InvocationTargetException if something goes wrong
*/
public static Object instantiate(String classname, Properties info, boolean tryString,
public static <T> T instantiate(Class<T> expectedClass, String classname, Properties info, boolean tryString,
String stringarg) throws ClassNotFoundException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InstantiationException, IllegalAccessException,
InvocationTargetException {
Object[] args = {info};
Constructor<?> ctor = null;
Class<?> cls = Class.forName(classname);
Constructor<? extends T> ctor = null;
Class<? extends T> cls = Class.forName(classname).asSubclass(expectedClass);
try {
ctor = cls.getConstructor(Properties.class);
} catch (NoSuchMethodException nsme) {
Expand Down

0 comments on commit 9999659

Please sign in to comment.