
javax.xml.ws.spi.FactoryFinder Maven / Gradle / Ivy
Show all versions of jboss-jaxws-api_2.3_spec
/*
* Copyright (c) 2005, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package javax.xml.ws.spi;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.ws.WebServiceException;
class FactoryFinder {
private static final String JBOSS_JAXWS_CLIENT_MODULE = "org.jboss.ws.jaxws-client";
private static final Logger logger = Logger.getLogger("javax.xml.ws");
private static final ServiceLoaderUtil.ExceptionHandler EXCEPTION_HANDLER = new ServiceLoaderUtil.ExceptionHandler() {
@Override
public WebServiceException createException(Throwable throwable,
String message) {
return new WebServiceException(message, throwable);
}
};
/**
* Finds the implementation {@code Class} object for the given factory name, or if that fails, finds the {@code Class}
* object for the given fallback class name. The arguments supplied MUST be used in order. If using the first argument is
* successful, the second one will not be used.
*
* This method is package private so that this code can be shared.
*
* @return the {@code Class} object of the specified message factory; may not be {@code null}
*
* @param factoryClass the name of the factory to find, which is a system property
* @param fallbackClassName the implementation class name, which is to be used only if nothing else is found; {@code null}
* to indicate that there is no fallback class name
* @exception WebServiceException if there is an error
*/
@SuppressWarnings("unchecked")
static T find(Class factoryClass, String fallbackClassName) {
ClassLoader classLoader = ServiceLoaderUtil
.contextClassLoader(EXCEPTION_HANDLER);
T provider = ServiceLoaderUtil.firstByServiceLoader(factoryClass,
logger, EXCEPTION_HANDLER);
if (provider != null)
return provider;
String factoryId = factoryClass.getName();
// try to read from $java.home/lib/jaxws.properties
provider = (T) fromJDKProperties(factoryId, fallbackClassName,
classLoader);
if (provider != null)
return provider;
// Use the system property
provider = (T) fromSystemProperty(factoryId, fallbackClassName,
classLoader);
if (provider != null)
return provider;
ClassLoader moduleClassLoader = getModuleClassLoader();
if (moduleClassLoader != null) {
try {
String serviceId = "META-INF/services/" + factoryId;
InputStream is = moduleClassLoader
.getResourceAsStream(serviceId);
if (is != null) {
BufferedReader rd = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
String factoryClassName = rd.readLine();
rd.close();
if (factoryClassName != null
&& !"".equals(factoryClassName)) {
return (T) ServiceLoaderUtil.newInstance(
factoryClassName, factoryClassName,
moduleClassLoader, EXCEPTION_HANDLER);
}
}
} catch (Exception ex) {
}
}
// handling Glassfish (platform specific default)
if (isOsgi()) {
return (T) lookupUsingOSGiServiceLoader(factoryId);
}
if (fallbackClassName == null) {
throw new WebServiceException("Provider for " + factoryId
+ " cannot be found", null);
}
return (T) ServiceLoaderUtil.newInstance(fallbackClassName,
fallbackClassName, classLoader, EXCEPTION_HANDLER);
}
private static ClassLoader getModuleClassLoader()
throws WebServiceException {
try {
final Class> moduleClass = Class
.forName("org.jboss.modules.Module");
final Class> moduleIdentifierClass = Class
.forName("org.jboss.modules.ModuleIdentifier");
final Class> moduleLoaderClass = Class
.forName("org.jboss.modules.ModuleLoader");
final Object moduleLoader;
final SecurityManager sm = System.getSecurityManager();
if (sm == null) {
moduleLoader = moduleClass.getMethod("getBootModuleLoader")
.invoke(null);
} else {
try {
moduleLoader = AccessController
.doPrivileged(new PrivilegedExceptionAction