View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.environment.classloading;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * <p>Specification for how to restore the original ThreadContext ClassLoader to a Thread.
24   * When we support JDK 1.7, this should really be an extension of AutoCloseable instead,
25   * to support the try-with-resources pattern. Typical use:</p>
26   * <pre>
27   *     <code>
28   *         // Create and set the ThreadContext ClassLoader
29   *         ThreadContextClassLoaderHolder holder = null;
30   *
31   *         try {
32   *
33   *          holder = ThreadContextClassLoaderBuilder.createFor(getClass())
34   *              .addPath("some/path")
35   *              .addURL(someURL)
36   *              .addPaths(aPathList)
37   *              .buildAndSet();
38   *
39   *          // ... perform operations using the newly set ThreadContext ClassLoader...
40   *
41   *         } finally {
42   *          // Restore the original ClassLoader
43   *          holder.restoreClassLoaderAndReleaseThread();
44   *         }
45   *     </code>
46   * </pre>
47   *
48   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
49   * @since 2.0
50   */
51  public interface ThreadContextClassLoaderHolder {
52  
53      /**
54       * Restores the original ThreadContext ClassLoader, and nullifies
55       * any references to the Thread which had its ThreadContext
56       * ClassLoader altered.
57       */
58      void restoreClassLoaderAndReleaseThread();
59  
60      /**
61       * Retrieves the ClassPath held by this ThreadContextClassLoaderHolder as a
62       * {@code File.pathSeparatorChar}-separated string. This is directly usable as a String argument by
63       * any external process.
64       *
65       * @return the ClassPath as an argument to external processes such as XJC.
66       */
67      String getClassPathAsArgument();
68  }