< prev index next >
src/java.xml.bind/share/classes/javax/xml/bind/JAXBContext.java
Print this page
*** 43,53 ****
* <p>A client application normally obtains new instances of this class using
* one of these two styles for newInstance methods, although there are other
* specialized forms of the method available:
*
* <ul>
! * <li>{@link #newInstance(String,ClassLoader) JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )} <br>
* The JAXBContext instance is initialized from a list of colon
* separated Java package names. Each java package contains
* JAXB mapped classes, schema-derived classes and/or user annotated
* classes. Additionally, the java package may contain JAXB package annotations
* that must be processed. (see JLS, Section 7.4.1 "Named Packages").
--- 43,53 ----
* <p>A client application normally obtains new instances of this class using
* one of these two styles for newInstance methods, although there are other
* specialized forms of the method available:
*
* <ul>
! * <li>{@link #newInstance(String, ClassLoader) JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )} <br>
* The JAXBContext instance is initialized from a list of colon
* separated Java package names. Each java package contains
* JAXB mapped classes, schema-derived classes and/or user annotated
* classes. Additionally, the java package may contain JAXB package annotations
* that must be processed. (see JLS, Section 7.4.1 "Named Packages").
*** 57,75 ****
* passed as parameter(s) and classes that are statically reachable from
* these class(es). See {@link #newInstance(Class...)} for details.
* </li>
* </ul>
*
- * <p>
- * <i><B>SPEC REQUIREMENT:</B> the provider must supply an implementation
- * class containing the following method signatures:</i>
- *
- * <pre>{@code
- * public static JAXBContext createContext( String contextPath, ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException
- * public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException
- * }</pre>
- *
* <p><i>
* The following JAXB 1.0 requirement is only required for schema to
* java interface/implementation binding. It does not apply to JAXB annotated
* classes. JAXB Providers must generate a <tt>jaxb.properties</tt> file in
* each package containing schema derived classes. The property file must
--- 57,66 ----
*** 144,154 ****
* <p>
* For more information on the generated <tt>ObjectFactory</tt> classes, see
* Section 4.2 <i>Java Package</i> of the specification.
*
* <p>
! * <i><B>SPEC REQUIREMENT:</B> the provider must generate a class in each
* package that contains all of the necessary object factory methods for that
* package named ObjectFactory as well as the static
* <tt>newInstance( javaContentInterface )</tt> method</i>
*
* <h3>Marshalling</h3>
--- 135,145 ----
* <p>
* For more information on the generated <tt>ObjectFactory</tt> classes, see
* Section 4.2 <i>Java Package</i> of the specification.
*
* <p>
! * <i>The provider must generate a class in each
* package that contains all of the necessary object factory methods for that
* package named ObjectFactory as well as the static
* <tt>newInstance( javaContentInterface )</tt> method</i>
*
* <h3>Marshalling</h3>
*** 212,260 ****
* <p>
* When one of the <tt>newInstance</tt> methods is called, a JAXB implementation is discovered
* by the following steps.
*
* <ol>
* <li>
* For each package/class explicitly passed in to the {@link #newInstance} method, in the order they are specified,
* <tt>jaxb.properties</tt> file is looked up in its package, by using the associated classloader —
* this is {@link Class#getClassLoader() the owner class loader} for a {@link Class} argument, and for a package
* the specified {@link ClassLoader}.
*
* <p>
* If such a file is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
* the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class.
! * This class is then loaded by the associated classloader discussed above.
*
* <p>
* This phase of the look up allows some packages to force the use of a certain JAXB implementation.
* (For example, perhaps the schema compiler has generated some vendor extension in the code.)
*
* <li>
* If the system property {@link #JAXB_CONTEXT_FACTORY} exists, then its value is assumed to be the provider
* factory class. This phase of the look up enables per-JVM override of the JAXB implementation.
*
* <li>
! * Look for <tt>/META-INF/services/javax.xml.bind.JAXBContext</tt> file in the associated classloader.
! * This file follows the standard service descriptor convention, and if such a file exists, its content
! * is assumed to be the provider factory class. This phase of the look up is for automatic discovery.
! * It allows users to just put a JAXB implementation in a classpath and use it without any furhter configuration.
*
* <li>
* Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
* the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
* This phase of the look up is so that JavaSE can have its own JAXB implementation as the last resort.
* </ol>
*
* <p>
! * Once the provider factory class is discovered, its
! * <tt>public static JAXBContext createContext(String,ClassLoader,Map)</tt> method
! * (see {@link #newInstance(String, ClassLoader, Map)} for the parameter semantics.)
! * or <tt>public static JAXBContext createContet(Class[],Map)</tt> method
! * (see {@link #newInstance(Class[], Map)} for the parameter semantics) are invoked
* to create a {@link JAXBContext}.
*
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
* @see Marshaller
* @see Unmarshaller
* @see <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.4.1">S 7.4.1 "Named Packages" in Java Language Specification</a>
* @since 1.6, JAXB 1.0
--- 203,304 ----
* <p>
* When one of the <tt>newInstance</tt> methods is called, a JAXB implementation is discovered
* by the following steps.
*
* <ol>
+ *
* <li>
* For each package/class explicitly passed in to the {@link #newInstance} method, in the order they are specified,
* <tt>jaxb.properties</tt> file is looked up in its package, by using the associated classloader —
* this is {@link Class#getClassLoader() the owner class loader} for a {@link Class} argument, and for a package
* the specified {@link ClassLoader}.
*
* <p>
* If such a file is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
* the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class.
! * This class is then loaded by the associated class loader discussed above.
*
* <p>
* This phase of the look up allows some packages to force the use of a certain JAXB implementation.
* (For example, perhaps the schema compiler has generated some vendor extension in the code.)
*
* <li>
* If the system property {@link #JAXB_CONTEXT_FACTORY} exists, then its value is assumed to be the provider
* factory class. This phase of the look up enables per-JVM override of the JAXB implementation.
*
* <li>
! * Provider of {@link javax.xml.bind.JAXBContextFactory} is loaded using the service-provider loading
! * facilities, defined by the {@link java.util.ServiceLoader} class, to attempt
! * to locate and load an implementation of the service using the {@linkplain
! * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: the service-provider loading facility
! * will use the {@linkplain java.lang.Thread#getContextClassLoader() current thread's context class loader}
! * to attempt to load the context factory. If the context class loader is null, the
! * {@linkplain ClassLoader#getSystemClassLoader() system class loader} will be used.
! * <br>
! * In case of {@link java.util.ServiceConfigurationError service
! * configuration error} a {@link javax.xml.bind.JAXBException} will be thrown.
! * </li>
! *
! * <li>
! * Look for resource <tt>/META-INF/services/javax.xml.bind.JAXBContext</tt> using provided class loader.
! * Methods without class loader parameter use {@code Thread.currentThread().getContextClassLoader()}.
! * If such a resource exists, its content is assumed to be the provider factory class and must supply
! * an implementation class containing the following method signatures:
! *
! * <pre>
! *
! * public static JAXBContext createContext(
! * String contextPath,
! * ClassLoader classLoader,
! * Map<String,Object> properties throws JAXBException
! *
! * public static JAXBContext createContext(
! * Class[] classes,
! * Map<String,Object> properties ) throws JAXBException
! * </pre>
! *
! * <li>
! * Look for resource <tt>/META-INF/services/javax.xml.bind.JAXBContext</tt> using provided class loader.
! * Methods without class loader parameter use {@code Thread.currentThread().getContextClassLoader()}.
! * If such a resource exists, its content is assumed to be the provider factory class and must supply
! * an implementation class containing the following method signatures:
! *
! * <pre>
! *
! * public static JAXBContext createContext(
! * String contextPath,
! * ClassLoader classLoader,
! * Map<String,Object> properties throws JAXBException
! *
! * public static JAXBContext createContext(
! * Class[] classes,
! * Map<String,Object> properties ) throws JAXBException
! * </pre>
! * </li>
*
* <li>
* Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
* the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
* This phase of the look up is so that JavaSE can have its own JAXB implementation as the last resort.
* </ol>
*
* <p>
! * Once the provider factory class {@link javax.xml.bind.JAXBContextFactory} is discovered, one of its methods
! * {@link javax.xml.bind.JAXBContextFactory#createContext(String, ClassLoader, java.util.Map)} or
! * {@link javax.xml.bind.JAXBContextFactory#createContext(Class[], java.util.Map)} is invoked
* to create a {@link JAXBContext}.
*
+ * <p/>
+ *
+ * @apiNote
+ * <p>Service discovery method using file /META-INF/services/javax.xml.bind.JAXBContext (described in step 4)
+ * and leveraging provider's static methods is supported only to allow backwards compatibility, but it is strongly
+ * recommended to migrate to standard ServiceLoader mechanism (described in step 3).
+ *
+ * @implNote
+ * Within the last step, if Glassfish AS environment detected, its specific service loader is used to find factory class.
+ *
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
* @see Marshaller
* @see Unmarshaller
* @see <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.4.1">S 7.4.1 "Named Packages" in Java Language Specification</a>
* @since 1.6, JAXB 1.0
*** 263,283 ****
/**
* The name of the property that contains the name of the class capable
* of creating new <tt>JAXBContext</tt> objects.
*/
! public static final String JAXB_CONTEXT_FACTORY =
! "javax.xml.bind.context.factory";
!
protected JAXBContext() {
}
/**
* <p>
! * Obtain a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* This is a convenience method to invoke the
* {@link #newInstance(String,ClassLoader)} method with
* the context class loader of the current thread.
--- 307,325 ----
/**
* The name of the property that contains the name of the class capable
* of creating new <tt>JAXBContext</tt> objects.
*/
! public static final String JAXB_CONTEXT_FACTORY = "javax.xml.bind.JAXBContextFactory";
protected JAXBContext() {
}
/**
* <p>
! * Create a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* This is a convenience method to invoke the
* {@link #newInstance(String,ClassLoader)} method with
* the context class loader of the current thread.
*** 298,308 ****
return newInstance( contextPath, getContextClassLoader());
}
/**
* <p>
! * Obtain a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* The client application must supply a context path which is a list of
* colon (':', \u005Cu003A) separated java package names that contain
* schema-derived classes and/or fully qualified JAXB-annotated classes.
--- 340,350 ----
return newInstance( contextPath, getContextClassLoader());
}
/**
* <p>
! * Create a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* The client application must supply a context path which is a list of
* colon (':', \u005Cu003A) separated java package names that contain
* schema-derived classes and/or fully qualified JAXB-annotated classes.
*** 394,404 ****
return newInstance(contextPath,classLoader,Collections.<String,Object>emptyMap());
}
/**
* <p>
! * Obtain a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* This is mostly the same as {@link JAXBContext#newInstance(String, ClassLoader)},
* but this version allows you to pass in provider-specific properties to configure
* the instantiation of {@link JAXBContext}.
--- 436,446 ----
return newInstance(contextPath,classLoader,Collections.<String,Object>emptyMap());
}
/**
* <p>
! * Create a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* This is mostly the same as {@link JAXBContext#newInstance(String, ClassLoader)},
* but this version allows you to pass in provider-specific properties to configure
* the instantiation of {@link JAXBContext}.
*** 441,451 ****
}
// TODO: resurrect this once we introduce external annotations
// /**
// * <p>
! // * Obtain a new instance of a <tt>JAXBContext</tt> class.
// *
// * <p>
// * The client application must supply a list of classes that the new
// * context object needs to recognize.
// *
--- 483,493 ----
}
// TODO: resurrect this once we introduce external annotations
// /**
// * <p>
! // * Create a new instance of a <tt>JAXBContext</tt> class.
// *
// * <p>
// * The client application must supply a list of classes that the new
// * context object needs to recognize.
// *
*** 477,487 ****
// * list of java classes to be recognized by the new {@link JAXBContext}.
// * Can be empty, in which case a {@link JAXBContext} that only knows about
// * spec-defined classes will be returned.
// *
// * @return
! // * A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
// *
// * @throws JAXBException
// * if an error was encountered while creating the
// * <tt>JAXBContext</tt>, such as (but not limited to):
// * <ol>
--- 519,529 ----
// * list of java classes to be recognized by the new {@link JAXBContext}.
// * Can be empty, in which case a {@link JAXBContext} that only knows about
// * spec-defined classes will be returned.
// *
// * @return
! // * A new instance of a <tt>JAXBContext</tt>.
// *
// * @throws JAXBException
// * if an error was encountered while creating the
// * <tt>JAXBContext</tt>, such as (but not limited to):
// * <ol>
*** 515,525 ****
// return ContextFinder.find(externalBindings,classesToBeBound);
// }
/**
* <p>
! * Obtain a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* The client application must supply a list of classes that the new
* context object needs to recognize.
*
--- 557,567 ----
// return ContextFinder.find(externalBindings,classesToBeBound);
// }
/**
* <p>
! * Create a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* The client application must supply a list of classes that the new
* context object needs to recognize.
*
*** 557,567 ****
* list of java classes to be recognized by the new {@link JAXBContext}.
* Can be empty, in which case a {@link JAXBContext} that only knows about
* spec-defined classes will be returned.
*
* @return
! * A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
*
* @throws JAXBException
* if an error was encountered while creating the
* <tt>JAXBContext</tt>, such as (but not limited to):
* <ol>
--- 599,609 ----
* list of java classes to be recognized by the new {@link JAXBContext}.
* Can be empty, in which case a {@link JAXBContext} that only knows about
* spec-defined classes will be returned.
*
* @return
! * A new instance of a <tt>JAXBContext</tt>.
*
* @throws JAXBException
* if an error was encountered while creating the
* <tt>JAXBContext</tt>, such as (but not limited to):
* <ol>
*** 576,594 ****
* @throws IllegalArgumentException
* if the parameter contains {@code null} (i.e., {@code newInstance(null);})
*
* @since 1.6, JAXB 2.0
*/
! public static JAXBContext newInstance( Class... classesToBeBound )
throws JAXBException {
return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
}
/**
* <p>
! * Obtain a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* An overloading of {@link JAXBContext#newInstance(Class...)}
* to configure 'properties' for this instantiation of {@link JAXBContext}.
*
--- 618,636 ----
* @throws IllegalArgumentException
* if the parameter contains {@code null} (i.e., {@code newInstance(null);})
*
* @since 1.6, JAXB 2.0
*/
! public static JAXBContext newInstance( Class<?> ... classesToBeBound )
throws JAXBException {
return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
}
/**
* <p>
! * Create a new instance of a <tt>JAXBContext</tt> class.
*
* <p>
* An overloading of {@link JAXBContext#newInstance(Class...)}
* to configure 'properties' for this instantiation of {@link JAXBContext}.
*
*** 603,613 ****
* @param properties
* provider-specific properties. Can be null, which means the same thing as passing
* in an empty map.
*
* @return
! * A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
*
* @throws JAXBException
* if an error was encountered while creating the
* <tt>JAXBContext</tt>, such as (but not limited to):
* <ol>
--- 645,655 ----
* @param properties
* provider-specific properties. Can be null, which means the same thing as passing
* in an empty map.
*
* @return
! * A new instance of a <tt>JAXBContext</tt>.
*
* @throws JAXBException
* if an error was encountered while creating the
* <tt>JAXBContext</tt>, such as (but not limited to):
* <ol>
*** 622,632 ****
* @throws IllegalArgumentException
* if the parameter contains {@code null} (i.e., {@code newInstance(null,someMap);})
*
* @since 1.6, JAXB 2.0
*/
! public static JAXBContext newInstance( Class[] classesToBeBound, Map<String,?> properties )
throws JAXBException {
if (classesToBeBound == null) {
throw new IllegalArgumentException();
}
--- 664,674 ----
* @throws IllegalArgumentException
* if the parameter contains {@code null} (i.e., {@code newInstance(null,someMap);})
*
* @since 1.6, JAXB 2.0
*/
! public static JAXBContext newInstance( Class<?>[] classesToBeBound, Map<String,?> properties )
throws JAXBException {
if (classesToBeBound == null) {
throw new IllegalArgumentException();
}
*** 754,766 ****
private static ClassLoader getContextClassLoader() {
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
} else {
! return (ClassLoader) java.security.AccessController.doPrivileged(
! new java.security.PrivilegedAction() {
! public java.lang.Object run() {
return Thread.currentThread().getContextClassLoader();
}
});
}
}
--- 796,808 ----
private static ClassLoader getContextClassLoader() {
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
} else {
! return java.security.AccessController.doPrivileged(
! new java.security.PrivilegedAction<ClassLoader>() {
! public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
});
}
}
< prev index next >