Some work on auto-cleanup of classloader for dynamic client
@@ -284,6 +284,25 @@ public class DynamicClientFactory {
return createClient(wsdlUrl.toString(), service, classLoader, port, bindingFiles);
}
+ static class DynamicClientImpl extends ClientImpl implements AutoCloseable {
+ final ClassLoader cl;
+ final ClassLoader orig;
+ DynamicClientImpl(Bus bus, Service svc, QName port,
+ EndpointImplFactory endpointImplFactory,
+ ClassLoader l) {
+ super(bus, svc, port, endpointImplFactory);
+ cl = l;
+ orig = Thread.currentThread().getContextClassLoader();
+ }
+ @Override
+ public void close() throws Exception {
+ destroy();
+ if (Thread.currentThread().getContextClassLoader() == cl) {
+ Thread.currentThread().setContextClassLoader(orig);
+ }
+ }
+ }
+
public Client createClient(String wsdlUrl, QName service,
ClassLoader classLoader, QName port,
List<String> bindingFiles) {
@@ -298,9 +317,6 @@ public class DynamicClientFactory {
sf.setAllowElementRefs(allowRefs);
Service svc = sf.create();
- ClientImpl client = new ClientImpl(bus, svc, port,
- getEndpointImplFactory());
-
//all SI's should have the same schemas
SchemaCollection schemas = svc.getServiceInfos().get(0).getXmlSchemaCollection();
@@ -378,7 +394,7 @@ public class DynamicClientFactory {
throw new IllegalStateException("Internal error; a directory returns a malformed URL: "
+ mue.getMessage(), mue);
}
- ClassLoader cl = ClassLoaderUtils.getURLClassLoader(urls, classLoader);
+ final ClassLoader cl = ClassLoaderUtils.getURLClassLoader(urls, classLoader);
JAXBContext context;
Map<String, Object> contextProperties = jaxbContextProperties;
@@ -402,6 +418,9 @@ public class DynamicClientFactory {
databinding.setContext(context);
svc.setDataBinding(databinding);
+ ClientImpl client = new DynamicClientImpl(bus, svc, port,
+ getEndpointImplFactory(), cl);
+
ServiceInfo svcfo = client.getEndpoint().getEndpointInfo().getService();
// Setup the new classloader!
