Skip to content

Commit

Permalink
fix compiler warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Dec 13, 2021
1 parent bbda663 commit d912347
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 62 deletions.
5 changes: 5 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<spotbugs.skip>false</spotbugs.skip>
<spotbugs.threshold>Low</spotbugs.threshold>
<spotbugs.version>4.5.0.0</spotbugs.version>
<bld.failOnWarning>true</bld.failOnWarning>

<release.spec.feedback>jaxws-dev@eclipse.org</release.spec.feedback>

Expand Down Expand Up @@ -305,6 +306,7 @@
<release>11</release>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Xdoclint:all,-missing</arg>
</compilerArgs>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
Expand Down Expand Up @@ -398,6 +400,9 @@
</manifest>
</archive>
<release>11</release>
<doclint>all,-missing</doclint>
<quiet>true</quiet>
<failOnWarnings>${bld.failOnWarning}</failOnWarnings>
<notimestamp>true</notimestamp>
<docfilessubdirs>true</docfilessubdirs>
<!-- we don't want @since in generated javadoc -->
Expand Down
5 changes: 4 additions & 1 deletion api/src/main/java/jakarta/xml/ws/ProtocolException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021 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
Expand All @@ -18,6 +18,9 @@
* @since 1.6, JAX-WS 2.0
**/
public class ProtocolException extends WebServiceException {

private static final long serialVersionUID = -5162391758599890377L;

/**
* Constructs a new protocol exception with {@code null} as its detail message. The
* cause is not initialized, and may subsequently be initialized by a call
Expand Down
6 changes: 4 additions & 2 deletions api/src/main/java/jakarta/xml/ws/WebServiceException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021 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
Expand All @@ -17,7 +17,9 @@
**/

public class WebServiceException extends java.lang.RuntimeException {


private static final long serialVersionUID = 6923454787998151997L;

/** Constructs a new exception with {@code null} as its
* detail message. The cause is not initialized.
**/
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/jakarta/xml/ws/http/HTTPException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021 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
Expand All @@ -20,7 +20,8 @@
* @since 1.6, JAX-WS 2.0
**/
public class HTTPException extends jakarta.xml.ws.ProtocolException {


private static final long serialVersionUID = 508892871321531629L;
private int statusCode;

/** Constructor for the HTTPException
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/jakarta/xml/ws/soap/SOAPFaultException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021 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
Expand Down Expand Up @@ -36,7 +36,8 @@
* @since 1.6, JAX-WS 2.0
**/
public class SOAPFaultException extends jakarta.xml.ws.ProtocolException {


private static final long serialVersionUID = -5717334416389757052L;
private SOAPFault fault;

/** Constructor for SOAPFaultException
Expand Down
94 changes: 48 additions & 46 deletions api/src/main/java/jakarta/xml/ws/spi/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class FactoryFinder {

private static final Logger logger;
private static final Logger LOGGER;

private static final ServiceLoaderUtil.ExceptionHandler<WebServiceException> EXCEPTION_HANDLER =
new ServiceLoaderUtil.ExceptionHandler<WebServiceException>() {
Expand All @@ -39,16 +39,16 @@ 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");
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);
LOGGER.setUseParentHandlers(false);
LOGGER.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);
LOGGER.addHandler(handler);
} else {
// don't change the setting of this logger
// to honor what other frameworks
Expand All @@ -57,7 +57,7 @@ public WebServiceException createException(Throwable throwable, String message)
} 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);
LOGGER.log(Level.SEVERE, "Exception during loading the class", t);
}
}
/**
Expand All @@ -69,37 +69,38 @@ public WebServiceException createException(Throwable throwable, String message)
* <P>
* 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 <T> type of the factory class
* @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
* @return the {@code Class} object of the specified message factory;
* may not be {@code null}
*
* @exception WebServiceException if there is an error
*/
@SuppressWarnings("unchecked")
static <T> T find(Class<T> factoryClass, String fallbackClassName) {
ClassLoader classLoader = ServiceLoaderUtil.contextClassLoader(EXCEPTION_HANDLER);

T provider = ServiceLoaderUtil.firstByServiceLoader(factoryClass, logger, 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);
provider = fromJDKProperties(factoryId, fallbackClassName, classLoader);
if (provider != null) return provider;

// Use the system property
provider = (T) fromSystemProperty(factoryId, fallbackClassName, classLoader);
provider = fromSystemProperty(factoryId, fallbackClassName, classLoader);
if (provider != null) return provider;

// handling Glassfish (platform specific default)
if (isOsgi()) {
provider = (T) lookupUsingOSGiServiceLoader(factoryId);
provider = lookupUsingOSGiServiceLoader(factoryId);
if (provider != null) {
return provider;
}
Expand All @@ -114,22 +115,22 @@ static <T> T find(Class<T> factoryClass, String fallbackClassName) {
fallbackClassName, classLoader, EXCEPTION_HANDLER);
}

private static Object fromSystemProperty(String factoryId,
private static <T> T fromSystemProperty(String factoryId,
String fallbackClassName,
ClassLoader classLoader) {
try {
String systemProp = System.getProperty(factoryId);
if (systemProp != null) {
logger.log(Level.FINE, "Found system property {0}", systemProp);
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);
} catch (SecurityException se) {
LOGGER.log(Level.SEVERE, "Access is not allowed to the system property with key " + factoryId, se);
}
return null;
}

private static Object fromJDKProperties(String factoryId,
private static <T> T fromJDKProperties(String factoryId,
String fallbackClassName,
ClassLoader classLoader) {
Path path = null;
Expand All @@ -143,17 +144,17 @@ private static Object fromJDKProperties(String factoryId,
}

if (Files.exists(path)) {
logger.log(Level.FINE, "Found {0}", 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 newInstance(factoryClassName, fallbackClassName, classLoader);
}
} catch (Exception ignored) {
logger.log(Level.SEVERE, "Error reading Jakarta XML Web Services configuration from [" + path +
"] file. Check it is accessible and has correct format.", ignored);
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Error reading Jakarta XML Web Services configuration from [" + path +
"] file. Check it is accessible and has correct format.", t);
}
return null;
}
Expand All @@ -164,44 +165,46 @@ private static boolean isOsgi() {
try {
Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
return true;
} catch (ClassNotFoundException ignored) {
logger.log(
} catch (ClassNotFoundException cnfe) {
LOGGER.log(
Level.SEVERE,
"Class " + OSGI_SERVICE_LOADER_CLASS_NAME + " cannot be loaded",
ignored
cnfe
);
}
return false;
}

private static Object lookupUsingOSGiServiceLoader(String factoryId) {
private static <T> T lookupUsingOSGiServiceLoader(String factoryId) {
try {
logger.fine("Trying to create the provider from the OSGi ServiceLoader");
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};
Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
Class<?> serviceClass = Class.forName(factoryId);
Class<?>[] args = new Class<?>[]{serviceClass};
Class<?> target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class);
java.util.Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator();
@SuppressWarnings({"unchecked"})
java.util.Iterator<? extends T> iter = ((Iterable<? extends T>) m.invoke(null, (Object[]) args)).iterator();
return iter.hasNext() ? iter.next() : null;
} catch (Exception ignored) {
} catch (Throwable t) {
// log and continue
logger.log(
LOGGER.log(
Level.SEVERE,
"Access to the system property with key " + factoryId + " is not allowed",
ignored
t
);
return null;
}
}

private static Object newInstance(String className, String defaultImplClassName, ClassLoader classLoader){
Object newInstance = ServiceLoaderUtil.newInstance(className,
private static <T> T newInstance(String className, String defaultImplClassName, ClassLoader classLoader){
@SuppressWarnings({"unchecked"})
T newInstance = (T) ServiceLoaderUtil.newInstance(className,
defaultImplClassName, classLoader, EXCEPTION_HANDLER);
if (logger.isLoggable(Level.FINE)) {
if (LOGGER.isLoggable(Level.FINE)) {
// extra check to avoid costly which operation if not logged
Class<?> newInstanceClass = newInstance.getClass();
logger.log(
LOGGER.log(
Level.FINE,
"loaded {0} from {1}", new Object[]{newInstanceClass.getName(), which(newInstanceClass)}
);
Expand All @@ -221,7 +224,7 @@ private static Object newInstance(String className, String defaultImplClassName,
* @return
* the URL for the class or null if it wasn't found
*/
static URL which(Class clazz) {
static URL which(Class<?> clazz) {
return which(clazz, getClassClassLoader(clazz));
}

Expand All @@ -237,7 +240,7 @@ static URL which(Class clazz) {
* @return
* the URL for the class or null if it wasn't found
*/
static URL which(Class clazz, ClassLoader loader) {
static URL which(Class<?> clazz, ClassLoader loader) {

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

Expand All @@ -252,18 +255,17 @@ private static ClassLoader getSystemClassLoader() {
if (System.getSecurityManager() == null) {
return ClassLoader.getSystemClassLoader();
} else {
return (ClassLoader) java.security.AccessController.doPrivileged(
(PrivilegedAction) ClassLoader::getSystemClassLoader);
return AccessController.doPrivileged(
(PrivilegedAction<ClassLoader>) ClassLoader::getSystemClassLoader);
}
}

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

Expand Down
21 changes: 12 additions & 9 deletions api/src/main/java/jakarta/xml/ws/spi/ServiceLoaderUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 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
Expand Down Expand Up @@ -54,30 +54,33 @@ static void checkPackageAccess(String className) {
}
}

static Class nullSafeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
@SuppressWarnings({"unchecked"})
static <T> Class<T> nullSafeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
if (classLoader == null) {
return Class.forName(className);
return (Class<T>) Class.forName(className);
} else {
return classLoader.loadClass(className);
return (Class<T>) classLoader.loadClass(className);
}
}

// Returns instance of required class. It checks package access (security)
// unless it is defaultClassname. It means if you are trying to instantiate
// default implementation (fallback), pass the class name to both first and second parameter.
static <T extends Exception> Object newInstance(String className,
static <T, E extends Exception> T newInstance(String className,
String defaultImplClassName, ClassLoader classLoader,
final ExceptionHandler<T> handler) throws T {
final ExceptionHandler<E> handler) throws E {
try {
return safeLoadClass(className, defaultImplClassName, classLoader).newInstance();
Class<T> cls = safeLoadClass(className, defaultImplClassName, classLoader);
return cls.getConstructor().newInstance();
} catch (ClassNotFoundException x) {
throw handler.createException(x, "Provider " + className + " not found");
} catch (Exception x) {
throw handler.createException(x, "Provider " + className + " could not be instantiated: " + x);
}
}

static Class safeLoadClass(String className,
@SuppressWarnings({"unchecked"})
static <T> Class<T> safeLoadClass(String className,
String defaultImplClassName,
ClassLoader classLoader) throws ClassNotFoundException {

Expand All @@ -86,7 +89,7 @@ static Class safeLoadClass(String className,
} catch (SecurityException se) {
// anyone can access the platform default factory class without permission
if (defaultImplClassName != null && defaultImplClassName.equals(className)) {
return Class.forName(className);
return (Class<T>) Class.forName(className);
}
// not platform default implementation ...
throw se;
Expand Down

0 comments on commit d912347

Please sign in to comment.