< prev index next >

src/java.xml.bind/share/classes/javax/xml/bind/JAXBContext.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  77  * {@link DatatypeConverter#setDatatypeConverter(DatatypeConverterInterface)
  78  * DatatypeConverter.setDatatypeConverter} api prior to any client
  79  * invocations of the marshal and unmarshal methods.  This is necessary to
  80  * configure the datatype converter that will be used during these operations.</i>
  81  *
  82  * <a name="Unmarshalling"></a>
  83  * <h3>Unmarshalling</h3>
  84  * <p>
  85  * The {@link Unmarshaller} class provides the client application the ability
  86  * to convert XML data into a tree of Java content objects.
  87  * The unmarshal method allows for
  88  * any global XML element declared in the schema to be unmarshalled as
  89  * the root of an instance document.
  90  * Additionally, the unmarshal method allows for an unrecognized root element that
  91  * has  an xsi:type attribute's value that references a type definition declared in
  92  * the schema  to be unmarshalled as the root of an instance document.
  93  * The {@code JAXBContext} object
  94  * allows the merging of global elements and type definitions across a set of schemas (listed
  95  * in the {@code contextPath}). Since each schema in the schema set can belong
  96  * to distinct namespaces, the unification of schemas to an unmarshalling
  97  * context should be namespace independent.  This means that a client
  98  * application is able to unmarshal XML documents that are instances of
  99  * any of the schemas listed in the {@code contextPath}.  For example:
 100  *
 101  * <pre>
 102  *      JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );
 103  *      Unmarshaller u = jc.createUnmarshaller();
 104  *      FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok
 105  *      BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok
 106  *      BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath
 107  * </pre>
 108  *
 109  * <p>
 110  * The client application may also generate Java content trees explicitly rather
 111  * than unmarshalling existing XML data.  For all JAXB-annotated value classes,
 112  * an application can create content using constructors.
 113  * For schema-derived interface/implementation classes and for the
 114  * creation of elements that are not bound to a JAXB-annotated
 115  * class, an application needs to have access and knowledge about each of
 116  * the schema derived {@code ObjectFactory} classes that exist in each of
 117  * java packages contained in the {@code contextPath}.  For each schema


 183  * In JAXB 2.0, the {@link Unmarshaller} has included convenince methods that expose
 184  * the JAXP 1.3 {@link javax.xml.validation} framework.  Please refer to the
 185  * {@link Unmarshaller#setSchema(javax.xml.validation.Schema)} API for more
 186  * information.
 187  *
 188  *
 189  * <h3>JAXB Runtime Binding Framework Compatibility</h3>
 190  * <p>
 191  * The following JAXB 1.0 restriction only applies to binding schema to
 192  * interfaces/implementation classes.
 193  * Since this binding does not require a common runtime system, a JAXB
 194  * client application must not attempt to mix runtime objects ({@code JAXBContext,
 195  * Marshaller}, etc. ) from different providers.  This does not
 196  * mean that the client application isn't portable, it simply means that a
 197  * client has to use a runtime system provided by the same provider that was
 198  * used to compile the schema.
 199  *
 200  *
 201  * <h3>Discovery of JAXB implementation</h3>
 202  * <p>
 203  * When one of the {@code newInstance} methods is called, a JAXB implementation is discovered
 204  * by the following steps.






 205  *
 206  * <ol>
 207  *
 208  * <li>
 209  * For each package/class explicitly passed in to the {@link #newInstance} method, in the order they are specified,
 210  * {@code jaxb.properties} file is looked up in its package, by using the associated classloader &mdash;
 211  * this is {@link Class#getClassLoader() the owner class loader} for a {@link Class} argument, and for a package
 212  * the specified {@link ClassLoader}.
 213  *
 214  * <p>
 215  * If such a file is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
 216  * the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class.
 217  * This class is then loaded by the associated class loader discussed above.

 218  *
 219  * <p>
 220  * This phase of the look up allows some packages to force the use of a certain JAXB implementation.
 221  * (For example, perhaps the schema compiler has generated some vendor extension in the code.)
 222  *
 223  * <li>
 224  * If the system property {@link #JAXB_CONTEXT_FACTORY} exists, then its value is assumed to be the provider
 225  * factory class. This phase of the look up enables per-JVM override of the JAXB implementation.


 226  *
 227  * <li>
 228  * Provider of {@link javax.xml.bind.JAXBContextFactory} is loaded using the service-provider loading
 229  * facilities, defined by the {@link java.util.ServiceLoader} class, to attempt
 230  * to locate and load an implementation of the service using the {@linkplain
 231  * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: the service-provider loading facility
 232  * will use the {@linkplain java.lang.Thread#getContextClassLoader() current thread's context class loader}
 233  * to attempt to load the context factory. If the context class loader is null, the
 234  * {@linkplain ClassLoader#getSystemClassLoader() system class loader} will be used.
 235  * <br>
 236  * In case of {@link java.util.ServiceConfigurationError service
 237  * configuration error} a {@link javax.xml.bind.JAXBException} will be thrown.
 238  * </li>
 239  *
 240  * <li>
 241  * Look for resource {@code /META-INF/services/javax.xml.bind.JAXBContext} using provided class loader.
 242  * Methods without class loader parameter use {@code Thread.currentThread().getContextClassLoader()}.
 243  * If such a resource exists, its content is assumed to be the provider factory class and must supply
 244  * an implementation class containing the following method signatures:











 245  *







 246  * <pre>
 247  *
 248  * public static JAXBContext createContext(
 249  *                                      String contextPath,
 250  *                                      ClassLoader classLoader,
 251  *                                      Map&lt;String,Object&gt; properties throws JAXBException
 252  *
 253  * public static JAXBContext createContext(
 254  *                                      Class[] classes,
 255  *                                      Map&lt;String,Object&gt; properties ) throws JAXBException
 256  * </pre>
 257  * This configuration method is deprecated.
 258  *
 259  * <li>
 260  * Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
 261  * the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
 262  * This phase of the look up is so that JavaSE can have its own JAXB implementation as the last resort.
 263  * </ol>
 264  *
 265  * <p>
 266  * Once the provider factory class {@link javax.xml.bind.JAXBContextFactory} is discovered, one of its methods
 267  * {@link javax.xml.bind.JAXBContextFactory#createContext(String, ClassLoader, java.util.Map)} or
 268  * {@link javax.xml.bind.JAXBContextFactory#createContext(Class[], java.util.Map)} is invoked
 269  * to create a {@link JAXBContext}.
 270  *
 271  * @apiNote
 272  * <p>Service discovery method using file /META-INF/services/javax.xml.bind.JAXBContext (described in step 4)
 273  * and leveraging provider's static methods is supported only to allow backwards compatibility, but it is strongly
 274  * recommended to migrate to standard ServiceLoader mechanism (described in step 3).





 275  *
 276  * @implNote
 277  * Within the last step, if Glassfish AS environment detected, its specific service loader is used to find factory class.
 278  *
 279  * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li>
 280  *             <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li>
 281  *             <li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
 282  *
 283  * @see Marshaller
 284  * @see Unmarshaller
 285  * @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"
 286  *      in Java Language Specification</a>
 287  *
 288  * @since 1.6, JAXB 1.0
 289  */
 290 public abstract class JAXBContext {
 291 
 292     /**
 293      * The name of the property that contains the name of the class capable
 294      * of creating new {@code JAXBContext} objects.
 295      */
 296     public static final String JAXB_CONTEXT_FACTORY = "javax.xml.bind.JAXBContextFactory";
 297 
 298     protected JAXBContext() {
 299     }
 300 
 301 
 302     /**
 303      * Create a new instance of a {@code JAXBContext} class.
 304      *
 305      * <p>
 306      * This is a convenience method to invoke the
 307      * {@link #newInstance(String,ClassLoader)} method with
 308      * the context class loader of the current thread.
 309      *
 310      * @throws JAXBException if an error was encountered while creating the
 311      *                       {@code JAXBContext} such as
 312      * <ol>
 313      *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
 314      *   <li>an ambiguity among global elements contained in the contextPath</li>
 315      *   <li>failure to locate a value for the context factory provider property</li>
 316      *   <li>mixing schema derived packages from different providers on the same contextPath</li>
 317      * </ol>
 318      */
 319     public static JAXBContext newInstance( String contextPath )
 320         throws JAXBException {
 321 
 322         //return newInstance( contextPath, JAXBContext.class.getClassLoader() );
 323         return newInstance( contextPath, getContextClassLoader());
 324     }
 325 
 326     /**
 327      * Create a new instance of a {@code JAXBContext} class.
 328      *
 329      * <p>
 330      * The client application must supply a context path which is a list of
 331      * colon (':', \u005Cu003A) separated java package names that contain
 332      * schema-derived classes and/or fully qualified JAXB-annotated classes.
 333      * Schema-derived
 334      * code is registered with the JAXBContext by the
 335      * ObjectFactory.class generated per package.
 336      * Alternatively than being listed in the context path, programmer
 337      * annotated JAXB mapped classes can be listed in a


 388      * packages listed on the {@code contextPath}, a {@code JAXBException}
 389      * will be thrown.
 390      *
 391      * <p>
 392      * Mixing generated interface/impl bindings from multiple JAXB Providers
 393      * in the same context path may result in a {@code JAXBException}
 394      * being thrown.
 395      *
 396      * <p>
 397      * The steps involved in discovering the JAXB implementation is discussed in the class javadoc.
 398      *
 399      * @param contextPath list of java package names that contain schema
 400      *                    derived class and/or java to schema (JAXB-annotated)
 401      *                    mapped classes
 402      * @param classLoader
 403      *      This class loader will be used to locate the implementation
 404      *      classes.
 405      *
 406      * @return a new instance of a {@code JAXBContext}
 407      * @throws JAXBException if an error was encountered while creating the
 408      *                       {@code JAXBContext} such as
 409      * <ol>
 410      *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
 411      *   <li>an ambiguity among global elements contained in the contextPath</li>
 412      *   <li>failure to locate a value for the context factory provider property</li>
 413      *   <li>mixing schema derived packages from different providers on the same contextPath</li>
 414      * </ol>
 415      */
 416     public static JAXBContext newInstance( String contextPath, ClassLoader classLoader ) throws JAXBException {
 417 
 418         return newInstance(contextPath,classLoader,Collections.<String,Object>emptyMap());
 419     }
 420 
 421     /**
 422      * Create a new instance of a {@code JAXBContext} class.
 423      *
 424      * <p>
 425      * This is mostly the same as {@link JAXBContext#newInstance(String, ClassLoader)},
 426      * but this version allows you to pass in provider-specific properties to configure
 427      * the instantiation of {@link JAXBContext}.
 428      *
 429      * <p>
 430      * The interpretation of properties is up to implementations. Implementations should
 431      * throw {@code JAXBException} if it finds properties that it doesn't understand.
 432      *
 433      * @param contextPath list of java package names that contain schema derived classes
 434      * @param classLoader
 435      *      This class loader will be used to locate the implementation classes.
 436      * @param properties
 437      *      provider-specific properties. Can be null, which means the same thing as passing
 438      *      in an empty map.
 439      *
 440      * @return a new instance of a {@code JAXBContext}
 441      * @throws JAXBException if an error was encountered while creating the
 442      *                       {@code JAXBContext} such as
 443      * <ol>
 444      *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
 445      *   <li>an ambiguity among global elements contained in the contextPath</li>
 446      *   <li>failure to locate a value for the context factory provider property</li>
 447      *   <li>mixing schema derived packages from different providers on the same contextPath</li>
 448      * </ol>
 449      * @since 1.6, JAXB 2.0
 450      */
 451     public static JAXBContext newInstance( String contextPath,
 452                                            ClassLoader classLoader,
 453                                            Map<String,?>  properties  ) throws JAXBException {
 454 
 455         return ContextFinder.find(
 456                         /* The default property name according to the JAXB spec */
 457                         JAXB_CONTEXT_FACTORY,
 458 
 459                         /* the context path supplied by the client app */
 460                         contextPath,
 461 
 462                         /* class loader to be used */
 463                         classLoader,
 464                         properties );
 465     }
 466 
 467 // TODO: resurrect this once we introduce external annotations
 468 //    /**


 566      *
 567      * Therefore, a typical client application only needs to specify the
 568      * top-level classes, but it needs to be careful.
 569      *
 570      * <p>
 571      * Note that for each java package registered with JAXBContext,
 572      * when the optional package annotations exist, they must be processed.
 573      * (see JLS, Section 7.4.1 "Named Packages").
 574      *
 575      * <p>
 576      * The steps involved in discovering the JAXB implementation is discussed in the class javadoc.
 577      *
 578      * @param classesToBeBound
 579      *      list of java classes to be recognized by the new {@link JAXBContext}.
 580      *      Can be empty, in which case a {@link JAXBContext} that only knows about
 581      *      spec-defined classes will be returned.
 582      *
 583      * @return
 584      *      A new instance of a {@code JAXBContext}.
 585      *
 586      * @throws JAXBException
 587      *      if an error was encountered while creating the
 588      *      {@code JAXBContext}, such as (but not limited to):
 589      * <ol>
 590      *  <li>No JAXB implementation was discovered
 591      *  <li>Classes use JAXB annotations incorrectly
 592      *  <li>Classes have colliding annotations (i.e., two classes with the same type name)
 593      *  <li>The JAXB implementation was unable to locate
 594      *      provider-specific out-of-band information (such as additional
 595      *      files generated at the development time.)
 596      * </ol>
 597      *
 598      * @throws IllegalArgumentException
 599      *      if the parameter contains {@code null} (i.e., {@code newInstance(null);})
 600      *
 601      * @since 1.6, JAXB 2.0
 602      */
 603     public static JAXBContext newInstance( Class<?> ... classesToBeBound )
 604         throws JAXBException {
 605 
 606         return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
 607     }
 608 
 609     /**
 610      * Create a new instance of a {@code JAXBContext} class.
 611      *
 612      * <p>
 613      * An overloading of {@link JAXBContext#newInstance(Class...)}
 614      * to configure 'properties' for this instantiation of {@link JAXBContext}.
 615      *
 616      * <p>
 617      * The interpretation of properties is up to implementations. Implementations should
 618      * throw {@code JAXBException} if it finds properties that it doesn't understand.
 619      *
 620      * @param classesToBeBound
 621      *      list of java classes to be recognized by the new {@link JAXBContext}.
 622      *      Can be empty, in which case a {@link JAXBContext} that only knows about
 623      *      spec-defined classes will be returned.
 624      * @param properties
 625      *      provider-specific properties. Can be null, which means the same thing as passing
 626      *      in an empty map.
 627      *
 628      * @return
 629      *      A new instance of a {@code JAXBContext}.
 630      *
 631      * @throws JAXBException
 632      *      if an error was encountered while creating the
 633      *      {@code JAXBContext}, such as (but not limited to):
 634      * <ol>
 635      *  <li>No JAXB implementation was discovered
 636      *  <li>Classes use JAXB annotations incorrectly
 637      *  <li>Classes have colliding annotations (i.e., two classes with the same type name)


   1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  77  * {@link DatatypeConverter#setDatatypeConverter(DatatypeConverterInterface)
  78  * DatatypeConverter.setDatatypeConverter} api prior to any client
  79  * invocations of the marshal and unmarshal methods.  This is necessary to
  80  * configure the datatype converter that will be used during these operations.</i>
  81  *
  82  * <a name="Unmarshalling"></a>
  83  * <h3>Unmarshalling</h3>
  84  * <p>
  85  * The {@link Unmarshaller} class provides the client application the ability
  86  * to convert XML data into a tree of Java content objects.
  87  * The unmarshal method allows for
  88  * any global XML element declared in the schema to be unmarshalled as
  89  * the root of an instance document.
  90  * Additionally, the unmarshal method allows for an unrecognized root element that
  91  * has  an xsi:type attribute's value that references a type definition declared in
  92  * the schema  to be unmarshalled as the root of an instance document.
  93  * The {@code JAXBContext} object
  94  * allows the merging of global elements and type definitions across a set of schemas (listed
  95  * in the {@code contextPath}). Since each schema in the schema set can belong
  96  * to distinct namespaces, the unification of schemas to an unmarshalling
  97  * context must be namespace independent.  This means that a client
  98  * application is able to unmarshal XML documents that are instances of
  99  * any of the schemas listed in the {@code contextPath}.  For example:
 100  *
 101  * <pre>
 102  *      JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );
 103  *      Unmarshaller u = jc.createUnmarshaller();
 104  *      FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok
 105  *      BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok
 106  *      BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath
 107  * </pre>
 108  *
 109  * <p>
 110  * The client application may also generate Java content trees explicitly rather
 111  * than unmarshalling existing XML data.  For all JAXB-annotated value classes,
 112  * an application can create content using constructors.
 113  * For schema-derived interface/implementation classes and for the
 114  * creation of elements that are not bound to a JAXB-annotated
 115  * class, an application needs to have access and knowledge about each of
 116  * the schema derived {@code ObjectFactory} classes that exist in each of
 117  * java packages contained in the {@code contextPath}.  For each schema


 183  * In JAXB 2.0, the {@link Unmarshaller} has included convenince methods that expose
 184  * the JAXP 1.3 {@link javax.xml.validation} framework.  Please refer to the
 185  * {@link Unmarshaller#setSchema(javax.xml.validation.Schema)} API for more
 186  * information.
 187  *
 188  *
 189  * <h3>JAXB Runtime Binding Framework Compatibility</h3>
 190  * <p>
 191  * The following JAXB 1.0 restriction only applies to binding schema to
 192  * interfaces/implementation classes.
 193  * Since this binding does not require a common runtime system, a JAXB
 194  * client application must not attempt to mix runtime objects ({@code JAXBContext,
 195  * Marshaller}, etc. ) from different providers.  This does not
 196  * mean that the client application isn't portable, it simply means that a
 197  * client has to use a runtime system provided by the same provider that was
 198  * used to compile the schema.
 199  *
 200  *
 201  * <h3>Discovery of JAXB implementation</h3>
 202  * <p>
 203  * To create an instance of {@link JAXBContext}, one of {@code JAXBContext.newInstance(...)} methods is invoked. After
 204  * JAX-B implementation is discovered, call is delegated to appropriate provider's method {@code createContext(...)}
 205  * passing parameters from the original call.
 206  * <p>
 207  * JAX-B implementation discovery happens each time {@code JAXBContext.newInstance} is invoked. If there is no user
 208  * specific configuration provided, default JAX-B provider must be returned.
 209  * <p>
 210  * Implementation discovery consists of following steps:
 211  *
 212  * <ol>
 213  *
 214  * <li>
 215  * Packages/classes explicitly passed in to the {@link #newInstance} method are processed in the order they are
 216  * specified, until {@code jaxb.properties} file is looked up in its package, by using the associated classloader &mdash;
 217  * this is {@link Class#getClassLoader() the owner class loader} for a {@link Class} argument, and for a package
 218  * the specified {@link ClassLoader}.
 219  *
 220  * <p>
 221  * If such a resource is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
 222  * the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class. If no value
 223  * found, {@code "javax.xml.bind.context.factory"} is used as a key for backwards compatibility reasons. This class is
 224  * then loaded by the associated class loader discussed above.
 225  *
 226  * <p>
 227  * This phase of the look up allows some packages to force the use of a certain JAXB implementation.
 228  * (For example, perhaps the schema compiler has generated some vendor extension in the code.)
 229  *
 230  * <li>
 231  * If the system property {@link #JAXB_CONTEXT_FACTORY} exists, then its value is assumed to be the provider
 232  * factory class. If no such property exists, properties {@code "javax.xml.bind.context.factory"} and
 233  * {@code "javax.xml.bind.JAXBContext"} are checked too (in this order), for backwards compatibility reasons. This phase
 234  * of the look up enables per-JVM override of the JAXB implementation.
 235  *
 236  * <li>
 237  * Provider of {@link javax.xml.bind.JAXBContextFactory} is loaded using the service-provider loading
 238  * facilities, defined by the {@link java.util.ServiceLoader} class, to attempt
 239  * to locate and load an implementation of the service using the {@linkplain
 240  * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: the service-provider loading facility
 241  * will use the {@linkplain java.lang.Thread#getContextClassLoader() current thread's context class loader}
 242  * to attempt to load the context factory. If the context class loader is null, the
 243  * {@linkplain ClassLoader#getSystemClassLoader() system class loader} will be used.
 244  * <br>
 245  * In case of {@link java.util.ServiceConfigurationError service
 246  * configuration error} a {@link javax.xml.bind.JAXBException} will be thrown.

 247  *
 248  * <li>
 249  * Look for resource {@code /META-INF/services/javax.xml.bind.JAXBContext} using provided class loader.
 250  * Methods without class loader parameter use {@code Thread.currentThread().getContextClassLoader()}.
 251  * If such a resource exists, its content is assumed to be the provider factory class.
 252  *
 253  * This configuration method is deprecated.
 254  *
 255  * <li>
 256  * Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
 257  * the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
 258  * This phase of the look up is so that JavaSE can have its own JAXB implementation as the last resort.
 259  * </ol>
 260  *
 261  * <p>
 262  * Once the provider factory class is discovered, context creation is delegated to one of its
 263  * {@code createContext(...)} methods.
 264  *
 265  * For backward compatibility reasons, there are two ways how to implement provider factory class:
 266  * <ol>
 267  *     <li>the class is implementation of {@link javax.xml.bind.JAXBContextFactory}. It must also implement no-arg
 268  *     constructor. If discovered in other step then 3, new instance using no-arg constructor is created first.
 269  *     After that, appropriate instance method is invoked on this instance.
 270  *     <li>the class is not implementation of interface above and then it is mandated to implement the following
 271  *     static method signatures:
 272  * <pre>
 273  *
 274  * public static JAXBContext createContext(
 275  *                                      String contextPath,
 276  *                                      ClassLoader classLoader,
 277  *                                      Map&lt;String,Object&gt; properties ) throws JAXBException
 278  *
 279  * public static JAXBContext createContext(
 280  *                                      Class[] classes,
 281  *                                      Map&lt;String,Object&gt; properties ) throws JAXBException
 282  * </pre>
 283  *      In this scenario, appropriate static method is used instead of instance method. This approach is incompatible
 284  *      with {@link java.util.ServiceLoader} so it can't be used with step 3.




 285  * </ol>

 286  * <p>
 287  * There is no difference in behavior of given method {@code createContext(...)} regardless of whether it uses approach
 288  * 1 (JAXBContextFactory) or 2 (no interface, static methods).


 289  *
 290  * @apiNote
 291  * Service discovery method using resource {@code /META-INF/services/javax.xml.bind.JAXBContext} (described in step 4)
 292  * is supported only to allow backwards compatibility, it is strongly recommended to migrate to standard
 293  * {@link java.util.ServiceLoader} mechanism (described in step 3). The difference here is the resource name, which
 294  * doesn't match service's type name.
 295  * <p>
 296  * Also using providers implementing interface {@link JAXBContextFactory} is preferred over using ones defining
 297  * static methods, same as {@link JAXBContext#JAXB_CONTEXT_FACTORY} property is preferred over property
 298  * {@code "javax.xml.bind.context.factory"}
 299  *
 300  * @implNote
 301  * Within the last step, if Glassfish AS environment detected, its specific service loader is used to find factory class.
 302  *
 303  * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li>
 304  *             <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li>
 305  *             <li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
 306  *
 307  * @see Marshaller
 308  * @see Unmarshaller
 309  * @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"
 310  *      in Java Language Specification</a>
 311  *
 312  * @since 1.6, JAXB 1.0
 313  */
 314 public abstract class JAXBContext {
 315 
 316     /**
 317      * The name of the property that contains the name of the class capable
 318      * of creating new {@code JAXBContext} objects.
 319      */
 320     public static final String JAXB_CONTEXT_FACTORY = "javax.xml.bind.JAXBContextFactory";
 321 
 322     protected JAXBContext() {
 323     }
 324 
 325 
 326     /**
 327      * Create a new instance of a {@code JAXBContext} class.
 328      *
 329      * <p>
 330      * This is a convenience method to invoke the
 331      * {@link #newInstance(String,ClassLoader)} method with
 332      * the context class loader of the current thread.
 333      *
 334      * @throws JAXBException if an error was encountered while creating the
 335      *               {@code JAXBContext}. See {@link JAXBContext#newInstance(String, ClassLoader, Map)} for details.






 336      */
 337     public static JAXBContext newInstance( String contextPath )
 338             throws JAXBException {
 339 
 340         //return newInstance( contextPath, JAXBContext.class.getClassLoader() );
 341         return newInstance( contextPath, getContextClassLoader());
 342     }
 343 
 344     /**
 345      * Create a new instance of a {@code JAXBContext} class.
 346      *
 347      * <p>
 348      * The client application must supply a context path which is a list of
 349      * colon (':', \u005Cu003A) separated java package names that contain
 350      * schema-derived classes and/or fully qualified JAXB-annotated classes.
 351      * Schema-derived
 352      * code is registered with the JAXBContext by the
 353      * ObjectFactory.class generated per package.
 354      * Alternatively than being listed in the context path, programmer
 355      * annotated JAXB mapped classes can be listed in a


 406      * packages listed on the {@code contextPath}, a {@code JAXBException}
 407      * will be thrown.
 408      *
 409      * <p>
 410      * Mixing generated interface/impl bindings from multiple JAXB Providers
 411      * in the same context path may result in a {@code JAXBException}
 412      * being thrown.
 413      *
 414      * <p>
 415      * The steps involved in discovering the JAXB implementation is discussed in the class javadoc.
 416      *
 417      * @param contextPath list of java package names that contain schema
 418      *                    derived class and/or java to schema (JAXB-annotated)
 419      *                    mapped classes
 420      * @param classLoader
 421      *      This class loader will be used to locate the implementation
 422      *      classes.
 423      *
 424      * @return a new instance of a {@code JAXBContext}
 425      * @throws JAXBException if an error was encountered while creating the
 426      *               {@code JAXBContext}. See {@link JAXBContext#newInstance(String, ClassLoader, Map)} for details.






 427      */
 428     public static JAXBContext newInstance( String contextPath, ClassLoader classLoader ) throws JAXBException {
 429 
 430         return newInstance(contextPath,classLoader,Collections.<String,Object>emptyMap());
 431     }
 432 
 433     /**
 434      * Create a new instance of a {@code JAXBContext} class.
 435      *
 436      * <p>
 437      * This is mostly the same as {@link JAXBContext#newInstance(String, ClassLoader)},
 438      * but this version allows you to pass in provider-specific properties to configure
 439      * the instantiation of {@link JAXBContext}.
 440      *
 441      * <p>
 442      * The interpretation of properties is up to implementations. Implementations must
 443      * throw {@code JAXBException} if it finds properties that it doesn't understand.
 444      *
 445      * @param contextPath list of java package names that contain schema derived classes
 446      * @param classLoader
 447      *      This class loader will be used to locate the implementation classes.
 448      * @param properties
 449      *      provider-specific properties. Can be null, which means the same thing as passing
 450      *      in an empty map.
 451      *
 452      * @return a new instance of a {@code JAXBContext}
 453      * @throws JAXBException if an error was encountered while creating the
 454      *                       {@code JAXBContext}. See {@link #newInstance(String, ClassLoader)} for details.






 455      * @since 1.6, JAXB 2.0
 456      */
 457     public static JAXBContext newInstance( String contextPath,
 458                                            ClassLoader classLoader,
 459                                            Map<String,?>  properties  ) throws JAXBException {
 460 
 461         return ContextFinder.find(
 462                         /* The default property name according to the JAXB spec */
 463                 JAXB_CONTEXT_FACTORY,
 464 
 465                         /* the context path supplied by the client app */
 466                 contextPath,
 467 
 468                         /* class loader to be used */
 469                 classLoader,
 470                 properties );
 471     }
 472 
 473 // TODO: resurrect this once we introduce external annotations
 474 //    /**


 572      *
 573      * Therefore, a typical client application only needs to specify the
 574      * top-level classes, but it needs to be careful.
 575      *
 576      * <p>
 577      * Note that for each java package registered with JAXBContext,
 578      * when the optional package annotations exist, they must be processed.
 579      * (see JLS, Section 7.4.1 "Named Packages").
 580      *
 581      * <p>
 582      * The steps involved in discovering the JAXB implementation is discussed in the class javadoc.
 583      *
 584      * @param classesToBeBound
 585      *      list of java classes to be recognized by the new {@link JAXBContext}.
 586      *      Can be empty, in which case a {@link JAXBContext} that only knows about
 587      *      spec-defined classes will be returned.
 588      *
 589      * @return
 590      *      A new instance of a {@code JAXBContext}.
 591      *
 592      * @throws JAXBException if an error was encountered while creating the
 593      *               {@code JAXBContext}. See {@link JAXBContext#newInstance(Class[], Map)} for details.









 594      *
 595      * @throws IllegalArgumentException
 596      *      if the parameter contains {@code null} (i.e., {@code newInstance(null);})
 597      *
 598      * @since 1.6, JAXB 2.0
 599      */
 600     public static JAXBContext newInstance( Class<?> ... classesToBeBound )
 601             throws JAXBException {
 602 
 603         return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
 604     }
 605 
 606     /**
 607      * Create a new instance of a {@code JAXBContext} class.
 608      *
 609      * <p>
 610      * An overloading of {@link JAXBContext#newInstance(Class...)}
 611      * to configure 'properties' for this instantiation of {@link JAXBContext}.
 612      *
 613      * <p>
 614      * The interpretation of properties is up to implementations. Implementations must
 615      * throw {@code JAXBException} if it finds properties that it doesn't understand.
 616      *
 617      * @param classesToBeBound
 618      *      list of java classes to be recognized by the new {@link JAXBContext}.
 619      *      Can be empty, in which case a {@link JAXBContext} that only knows about
 620      *      spec-defined classes will be returned.
 621      * @param properties
 622      *      provider-specific properties. Can be null, which means the same thing as passing
 623      *      in an empty map.
 624      *
 625      * @return
 626      *      A new instance of a {@code JAXBContext}.
 627      *
 628      * @throws JAXBException
 629      *      if an error was encountered while creating the
 630      *      {@code JAXBContext}, such as (but not limited to):
 631      * <ol>
 632      *  <li>No JAXB implementation was discovered
 633      *  <li>Classes use JAXB annotations incorrectly
 634      *  <li>Classes have colliding annotations (i.e., two classes with the same type name)


< prev index next >