Skip to content

Commit

Permalink
Change logging in the FactoryFinder class
Browse files Browse the repository at this point in the history
Signed-off-by: aserkes <andrii.serkes@oracle.com>
  • Loading branch information
aserkes authored and lukasj committed Dec 9, 2021
1 parent 36e744c commit 2505f0e
Showing 1 changed file with 117 additions and 5 deletions.
122 changes: 117 additions & 5 deletions api/src/main/java/jakarta/xml/ws/spi/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@

import java.io.*;

import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.xml.ws.WebServiceException;

class FactoryFinder {

private static final Logger logger = Logger.getLogger("jakarta.xml.ws");
private static final Logger logger;

private static final ServiceLoaderUtil.ExceptionHandler<WebServiceException> EXCEPTION_HANDLER =
new ServiceLoaderUtil.ExceptionHandler<WebServiceException>() {
Expand All @@ -32,6 +36,30 @@ public WebServiceException createException(Throwable throwable, String message)
}
};

private final static PrivilegedAction<String> propertyAction = () -> System.getProperty("jax-ws.debug");

static {
logger = Logger.getLogger("jakarta.xml.ws");
try {
if (AccessController.doPrivileged(propertyAction) != null) {
// disconnect the logger from a bigger framework (if any)
// and take the matters into our own hands
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);
} else {
// don't change the setting of this logger
// to honor what other frameworks
// have done on configurations.
}
} catch (Throwable t) {
// just to be extra safe. in particular System.getProperty may throw
// SecurityException.
logger.log(Level.SEVERE, "Exception during loading the class", t);
}
}
/**
* Finds the implementation {@code Class} object for the given
* factory name, or if that fails, finds the {@code Class} object
Expand Down Expand Up @@ -92,10 +120,11 @@ private static Object fromSystemProperty(String factoryId,
try {
String systemProp = System.getProperty(factoryId);
if (systemProp != null) {
return ServiceLoaderUtil.newInstance(systemProp,
fallbackClassName, classLoader, EXCEPTION_HANDLER);
logger.log(Level.FINE, "Found system property {0}", systemProp);
return newInstance(systemProp, fallbackClassName, classLoader);
}
} catch (SecurityException ignored) {
logger.log(Level.SEVERE, "Access is not allowed to the system property with key " + factoryId, ignored);
}
return null;
}
Expand All @@ -114,13 +143,13 @@ private static Object fromJDKProperties(String factoryId,
}

if (Files.exists(path)) {
logger.log(Level.FINE, "Found {0}", path);
Properties props = new Properties();
try (InputStream inStream = Files.newInputStream(path)) {
props.load(inStream);
}
String factoryClassName = props.getProperty(factoryId);
return ServiceLoaderUtil.newInstance(factoryClassName,
fallbackClassName, classLoader, EXCEPTION_HANDLER);
return newInstance(factoryClassName, fallbackClassName, classLoader);
}
} catch (Exception ignored) {
logger.log(Level.SEVERE, "Error reading Jakarta XML Web Services configuration from [" + path +
Expand All @@ -136,12 +165,18 @@ private static boolean isOsgi() {
Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
return true;
} catch (ClassNotFoundException ignored) {
logger.log(
Level.SEVERE,
"Class " + OSGI_SERVICE_LOADER_CLASS_NAME + " cannot be loaded",
ignored
);
}
return false;
}

private static Object lookupUsingOSGiServiceLoader(String factoryId) {
try {
logger.fine("Trying to create the provider from the OSGi ServiceLoader");
// Use reflection to avoid having any dependendcy on ServiceLoader class
Class serviceClass = Class.forName(factoryId);
Class[] args = new Class[]{serviceClass};
Expand All @@ -151,8 +186,85 @@ private static Object lookupUsingOSGiServiceLoader(String factoryId) {
return iter.hasNext() ? iter.next() : null;
} catch (Exception ignored) {
// log and continue
logger.log(
Level.SEVERE,
"Access to the system property with key " + factoryId + " is not allowed",
ignored
);
return null;
}
}

private static Object newInstance(String className, String defaultImplClassName, ClassLoader classLoader){
Object newInstance = ServiceLoaderUtil.newInstance(className,
defaultImplClassName, classLoader, EXCEPTION_HANDLER);
if (logger.isLoggable(Level.FINE)) {
// extra check to avoid costly which operation if not logged
Class<?> newInstanceClass = newInstance.getClass();
logger.log(
Level.FINE,
"loaded {0} from {1}", new Object[]{newInstanceClass.getName(), which(newInstanceClass)}
);
}
return newInstance;
}

/**
* Get the URL for the Class from it's ClassLoader.
*
* Convenience method for {@link #which(Class, ClassLoader)}.
*
* Equivalent to calling: which(clazz, clazz.getClassLoader())
*
* @param clazz
* The class to search for
* @return
* the URL for the class or null if it wasn't found
*/
static URL which(Class clazz) {
return which(clazz, getClassClassLoader(clazz));
}

/**
* Search the given ClassLoader for an instance of the specified class and
* return a string representation of the URL that points to the resource.
*
* @param clazz
* The class to search for
* @param loader
* The ClassLoader to search. If this parameter is null, then the
* system class loader will be searched
* @return
* the URL for the class or null if it wasn't found
*/
static URL which(Class clazz, ClassLoader loader) {

String classnameAsResource = clazz.getName().replace('.', '/') + ".class";

if (loader == null) {
loader = getSystemClassLoader();
}

return loader.getResource(classnameAsResource);
}

private static ClassLoader getSystemClassLoader() {
if (System.getSecurityManager() == null) {
return ClassLoader.getSystemClassLoader();
} else {
return (ClassLoader) java.security.AccessController.doPrivileged(
(PrivilegedAction) ClassLoader::getSystemClassLoader);
}
}

@SuppressWarnings("unchecked")
private static ClassLoader getClassClassLoader(final Class c) {
if (System.getSecurityManager() == null) {
return c.getClassLoader();
} else {
return (ClassLoader) java.security.AccessController.doPrivileged(
(PrivilegedAction) c::getClassLoader);
}
}

}

0 comments on commit 2505f0e

Please sign in to comment.