/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* This class is package private and therefore is not exposed as part of the
* JAXB API.
*
* This code is designed to implement the JAXB 1.0 spec pluggability feature
*
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
* @see JAXBContext
*/
class ContextFinder {
static {
try {
// disconnect the logger from a bigger framework (if any)
// and take the matters into our own hands
logger.setUseParentHandlers(false);
} 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.
}
}
/**
* 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.
*
* For example, (targetType)originalType
*
* @param originalType
* The Class object of the type being cast
* @param targetType
* The Class object of the type that is being cast to
* @return JAXBException to be thrown.
*/
// we don't care where the impl class is, we want to know where JAXBContext lives in the impl
// class' ClassLoader
}
/**
* Create an instance of a class using the specified ClassLoader
*/
throws JAXBException {
try {
} catch (ClassNotFoundException x) {
throw new JAXBException(
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
// some other type of exception - just wrap it
throw new JAXBException(
x);
}
}
throws JAXBException
{
try {
/*
* javax.xml.bind.context.factory points to a class which has a
* static method called 'createContext' that
* returns a javax.xml.JAXBContext.
*/
// first check the method that takes Map as the third parameter.
// this is added in 2.0.
try {
// any failure in invoking this method would be considered fatal
} catch (NoSuchMethodException e) {
// it's not an error for the provider not to have this method.
}
// 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.
// any failure in invoking this method would be considered fatal
}
if(!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
}
return (JAXBContext)context;
} catch (InvocationTargetException x) {
// for other exceptions, wrap the internal target exception
// with a JAXBException
Throwable e = x;
if(x.getTargetException()!=null)
e = x.getTargetException();
} 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(
x);
}
}
/**
* Create an instance of a class using the thread context ClassLoader
*/
try {
} catch (ClassNotFoundException e) {
throw new JAXBException(e);
}
// extra check to avoid costly which operation if not logged
}
}
Method m;
try {
} catch (NoSuchMethodException e) {
throw new JAXBException(e);
}
try {
if(!(context instanceof JAXBContext)) {
// the cast would fail, so generate an exception with a nice message
}
return (JAXBContext)context;
} catch (IllegalAccessException e) {
throw new JAXBException(e);
} catch (InvocationTargetException e) {
Throwable x = e;
if (e.getTargetException() != null)
x = e.getTargetException();
throw new JAXBException(x);
}
}
static JAXBContext find(String factoryId, String contextPath, ClassLoader classLoader, Map properties ) throws JAXBException {
// TODO: do we want/need another layer of searching in $java.home/lib/jaxb.properties like JAXP?
// search context path for jaxb.properties first
if(!packages.hasMoreTokens())
// no context is specified
while( packages.hasMoreTokens() ) {
// com.acme.foo - > com/acme/foo/jaxb.properties
} else {
}
}
}
// search for a system property second (javax.xml.bind.JAXBContext)
factoryClassName = AccessController.doPrivileged(new GetPropertyAction(JAXBContext.JAXB_CONTEXT_FACTORY));
if( factoryClassName != null ) {
} else { // leave this here to assure compatibility
if( factoryClassName != null ) {
}
}
}
}
// search META-INF services next
try {
final StringBuilder resource = new StringBuilder().append("META-INF/services/").append(jaxbContextFQCN);
final InputStream resourceStream =
if (resourceStream != null) {
r.close();
} else {
}
} catch (UnsupportedEncodingException e) {
// should never happen
throw new JAXBException(e);
} catch (IOException e) {
throw new JAXBException(e);
}
// else no provider found
}
// search for jaxb.properties in the class loader of each class first
// this classloader is used only to load jaxb.properties, so doing this should be safe.
public ClassLoader run() {
return c.getClassLoader();
}
});
continue; // this is possible for primitives, arrays, and classes that are loaded by poorly implemented ClassLoaders
// TODO: do we want to optimize away searching the same package? org.Foo, org.Bar, com.Baz
// classes from the same package might come from different class loades, so it might be a bad idea
// TODO: it's easier to look things up from the class
// c.getResourceAsStream("jaxb.properties");
// build the resource name and use the property loader code
} else {
// trim() seems redundant, but adding to satisfy customer complaint
} else {
throw new JAXBException(Messages.format(Messages.MISSING_PROPERTY, packageName, JAXB_CONTEXT_FACTORY));
}
}
}
// search for a system property second (javax.xml.bind.JAXBContext)
factoryClassName = AccessController.doPrivileged(new GetPropertyAction(JAXBContext.JAXB_CONTEXT_FACTORY));
if (factoryClassName != null) {
} else { // leave it here for compatibility reasons
if (factoryClassName != null) {
} else {
}
}
}
// search META-INF services next
try {
if(classLoader==null)
else
if (resourceURL != null) {
} else {
}
} catch (UnsupportedEncodingException e) {
// should never happen
throw new JAXBException(e);
} catch (IOException e) {
throw new JAXBException(e);
}
// else no provider found
}
try {
// Use reflection to avoid having any dependendcy on ServiceLoader class
} catch(Exception e) {
return null;
}
}
throws JAXBException {
try {
if(classLoader==null)
else
props = new Properties();
}
} catch( IOException ioe ) {
}
return props;
}
/**
* 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
*/
}
}
/**
* 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
*/
}
/**
* When JAXB is in J2SE, rt.jar has to have a JAXB implementation.
* However, rt.jar cannot have META-INF/services/javax.xml.bind.JAXBContext
* because if it has, it will take precedence over any file that applications have
* in their jar files.
*
* <p>
* When the user bundles his own JAXB implementation, we'd like to use it, and we
* want the platform default to be used only when there's no other JAXB provider.
*
* <p>
* For this reason, we have to hard-code the class name into the API.
*/
private static final String PLATFORM_DEFAULT_FACTORY_CLASS = "com.sun.xml.internal.bind.v2.ContextFactory";
/**
* Loads the class, provided that the calling thread has an access to the class being loaded.
*/
private static Class safeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
try {
// make sure that the current thread has an access to the package of the given name.
if (s != null) {
if (i != -1) {
}
}
if (classLoader == null) {
} else {
}
} catch (SecurityException se) {
// anyone can access the platform default factory class without permission
}
throw se;
}
}
}