Skip to content

Commit d5a8206

Browse files
sebsoftwareJens Heitmann
andauthored
GERONIMO-6884 - Fix SocketFactory creation (#2)
* Fix SocketFactory creation try to instantiate socketFactoryClass. If it fails try to use the geDefault method. This is the way documented in JavaDoc and also used by i.E. angus-mail. getDefault is the only way to get fallback to "javax.net.ssl.SSLSocketFactory" work. Because SSLSocketFactory is an abstract class and not instantiable without subclassing. * improved error handling information --------- Co-authored-by: Jens Heitmann <jens.heitmann@sebsoftware.de>
1 parent 38991ff commit d5a8206

File tree

1 file changed

+13
-1
lines changed
  • geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/util

1 file changed

+13
-1
lines changed

geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/util/MailConnection.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,22 @@ private boolean createSocketFromFactory(boolean ssl, boolean layer) throws IOExc
354354
// done indirectly, we need to invoke the method using reflection.
355355
// This retrieves a factory instance.
356356
//Method getDefault = factoryClass.getMethod("getDefault", new Class[0]); //TODO check instantiation of socket factory
357-
Object defFactory = factoryClass.newInstance();// getDefault.invoke(new Object(), new Object[0]);
357+
// Object defFactory = factoryClass.newInstance();// getDefault.invoke(new Object(), new Object[0]);
358358
// now that we have the factory, there are two different createSocket() calls we use,
359359
// depending on whether we have a localAddress override.
360360

361+
Object defFactory;
362+
try {
363+
defFactory = factoryClass.newInstance();
364+
} catch (Throwable t) {
365+
Method getDefault = factoryClass.getMethod("getDefault", new Class[0]); //TODO check instantiation of socket factory
366+
defFactory = getDefault.invoke(new Object(), new Object[0]);
367+
368+
if (defFactory == null) {
369+
throw new Exception("Can not create factory class '" + factoryClass.getName() + "' neither by creating a new instance or using getDefault()", t);
370+
}
371+
}
372+
361373
if (localAddress != null && !layer) {
362374
// retrieve the createSocket(String, int, InetAddress, int) method.
363375
Class[] createSocketSig = new Class[] { String.class, Integer.TYPE, InetAddress.class, Integer.TYPE };

0 commit comments

Comments
 (0)