< prev index next >
src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java
Print this page
*** 27,41 ****
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
--- 27,42 ----
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
+ import java.security.PrivilegedActionException;
+ import java.security.PrivilegedExceptionAction;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
*** 103,126 ****
}
};
/**
* If the {@link InvocationTargetException} wraps an exception that shouldn't be wrapped,
! * throw the wrapped exception.
*/
! private static void handleInvocationTargetException(InvocationTargetException x) throws JAXBException {
Throwable t = x.getTargetException();
if (t != null) {
if (t instanceof JAXBException)
// one of our exceptions, just re-throw
throw (JAXBException) t;
if (t instanceof RuntimeException)
// avoid wrapping exceptions unnecessarily
throw (RuntimeException) t;
if (t instanceof Error)
throw (Error) t;
}
}
/**
* Determine if two types (JAXBContext in this case) will generate a ClassCastException.
--- 104,130 ----
}
};
/**
* If the {@link InvocationTargetException} wraps an exception that shouldn't be wrapped,
! * throw the wrapped exception. Otherwise returns exception to be wrapped for further processing.
*/
! private static Throwable handleInvocationTargetException(InvocationTargetException x) throws JAXBException {
Throwable t = x.getTargetException();
if (t != null) {
if (t instanceof JAXBException)
// one of our exceptions, just re-throw
throw (JAXBException) t;
if (t instanceof RuntimeException)
// avoid wrapping exceptions unnecessarily
throw (RuntimeException) t;
if (t instanceof Error)
throw (Error) t;
+
+ return t;
}
+ return x;
}
/**
* Determine if two types (JAXBContext in this case) will generate a ClassCastException.
*** 155,167 ****
Class spFactory = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
return newInstance(contextPath, spFactory, classLoader, properties);
} catch (ClassNotFoundException x) {
throw new JAXBException(Messages.format(Messages.PROVIDER_NOT_FOUND, className), x);
! } catch (RuntimeException x) {
// avoid wrapping RuntimeException to JAXBException,
// because it indicates a bug in this code.
throw x;
} catch (Exception x) {
// can't catch JAXBException because the method is hidden behind
// reflection. Root element collisions detected in the call to
// createContext() are reported as JAXBExceptions - just re-throw it
--- 159,172 ----
Class spFactory = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
return newInstance(contextPath, spFactory, classLoader, properties);
} catch (ClassNotFoundException x) {
throw new JAXBException(Messages.format(Messages.PROVIDER_NOT_FOUND, className), x);
! } catch (RuntimeException | JAXBException x) {
// avoid wrapping RuntimeException to JAXBException,
// because it indicates a bug in this code.
+ // JAXBException re-thrown as is
throw x;
} catch (Exception x) {
// can't catch JAXBException because the method is hidden behind
// reflection. Root element collisions detected in the call to
// createContext() are reported as JAXBExceptions - just re-throw it
*** 187,236 ****
// first check the method that takes Map as the third parameter.
// this is added in 2.0.
try {
Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
// any failure in invoking this method would be considered fatal
! context = m.invoke(null, contextPath, classLoader, properties);
! } catch (NoSuchMethodException e) {
// it's not an error for the provider not to have this method.
}
if (context == null) {
// try the old method that doesn't take properties. compatible with 1.0.
// it is an error for an implementation not to have both forms of the createContext method.
Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
// any failure in invoking this method would be considered fatal
! context = m.invoke(null, contextPath, classLoader);
}
if (!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
throw handleClassCastException(context.getClass(), JAXBContext.class);
}
return (JAXBContext) context;
} catch (InvocationTargetException x) {
! handleInvocationTargetException(x);
! // for other exceptions, wrap the internal target exception
! // with a JAXBException
! Throwable e = x;
! if (x.getTargetException() != null)
! e = x.getTargetException();
!
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
! } catch (RuntimeException x) {
! // avoid wrapping RuntimeException to JAXBException,
! // because it indicates a bug in this code.
! throw x;
} catch (Exception x) {
// can't catch JAXBException because the method is hidden behind
// reflection. Root element collisions detected in the call to
// createContext() are reported as JAXBExceptions - just re-throw it
// some other type of exception - just wrap it
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
}
}
/**
* Create an instance of a class using the thread context ClassLoader
*/
static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException {
--- 192,253 ----
// first check the method that takes Map as the third parameter.
// this is added in 2.0.
try {
Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
// any failure in invoking this method would be considered fatal
! Object obj = instantiateProviderIfNecessary(m);
! context = m.invoke(obj, contextPath, classLoader, properties);
! } catch (NoSuchMethodException ignored) {
// it's not an error for the provider not to have this method.
}
if (context == null) {
// try the old method that doesn't take properties. compatible with 1.0.
// it is an error for an implementation not to have both forms of the createContext method.
Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
+ Object obj = instantiateProviderIfNecessary(m);
// any failure in invoking this method would be considered fatal
! context = m.invoke(obj, contextPath, classLoader);
}
if (!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
throw handleClassCastException(context.getClass(), JAXBContext.class);
}
return (JAXBContext) context;
} catch (InvocationTargetException x) {
! // throw if it is exception not to be wrapped
! // otherwise, wrap with a JAXBException
! Throwable e = handleInvocationTargetException(x);
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
!
} catch (Exception x) {
// can't catch JAXBException because the method is hidden behind
// reflection. Root element collisions detected in the call to
// createContext() are reported as JAXBExceptions - just re-throw it
// some other type of exception - just wrap it
throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
}
}
+ private static Object instantiateProviderIfNecessary(Method m) throws JAXBException {
+ Class<?> declaringClass = m.getDeclaringClass();
+ try {
+ if (JAXBContextFactory.class.isAssignableFrom(declaringClass)) {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ @Override
+ public Object run() throws Exception {
+ return declaringClass.newInstance();
+ }
+ });
+ }
+ return null;
+ } catch (PrivilegedActionException e) {
+ throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, declaringClass, e), e);
+ }
+ }
+
/**
* Create an instance of a class using the thread context ClassLoader
*/
static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException {
*** 253,278 ****
Map properties,
Class spFactory) throws JAXBException {
try {
Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
! Object context = m.invoke(null, classes, properties);
if (!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
throw handleClassCastException(context.getClass(), JAXBContext.class);
}
return (JAXBContext) context;
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new JAXBException(e);
-
} catch (InvocationTargetException e) {
! handleInvocationTargetException(e);
!
! Throwable x = e;
! if (e.getTargetException() != null)
! x = e.getTargetException();
throw new JAXBException(x);
}
}
--- 270,293 ----
Map properties,
Class spFactory) throws JAXBException {
try {
Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
! Object obj = instantiateProviderIfNecessary(m);
! Object context = m.invoke(obj, classes, properties);
if (!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
throw handleClassCastException(context.getClass(), JAXBContext.class);
}
return (JAXBContext) context;
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new JAXBException(e);
} catch (InvocationTargetException e) {
! // throw if it is exception not to be wrapped
! // otherwise, wrap with a JAXBException
! Throwable x = handleInvocationTargetException(e);
throw new JAXBException(x);
}
}
< prev index next >