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  import org.apache.maven.plugin.logging.Log;
23  import org.codehaus.mojo.jaxb2.shared.Validate;
24  import org.codehaus.mojo.jaxb2.shared.environment.AbstractLogAwareFacet;
25  
26  /**
27   * Adapter converting a ThreadContextClassLoaderHolder to the standard lifecycle
28   * defined within the EnvironmentFacet.
29   *
30   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
31   */
32  public class ContextClassLoaderEnvironmentFacet extends AbstractLogAwareFacet {
33  
34      // Internal state
35      private ThreadContextClassLoaderBuilder builder;
36      private ThreadContextClassLoaderHolder holder;
37  
38      /**
39       * Compound constructor creating a ContextClassLoaderEnvironmentFacet wrapping the supplied data.
40       *
41       * @param log     The active Maven Log.
42       * @param builder A fully set up ThreadContextClassLoaderBuilder, where the {@code buildAndSet()} method
43       *                should <strong>not</strong> be invoked yet.
44       * @see ThreadContextClassLoaderBuilder
45       */
46      public ContextClassLoaderEnvironmentFacet(final Log log,
47                                                final ThreadContextClassLoaderBuilder builder) {
48          super(log);
49  
50          // Check sanity
51          Validate.notNull(builder, "builder");
52  
53          // Assign internal state
54          this.builder = builder;
55      }
56  
57      /**
58       * Delegate method retrieving the classpath as argument from the underlying ThreadContextClassLoaderHolder.
59       * Note that the setup method must be invoked before this one is.
60       *
61       * @return the ClassPath as an argument to external processes such as XJC.
62       */
63      public String getClassPathAsArgument() {
64  
65          // Check sanity
66          Validate.notNull(holder, "holder");
67  
68          // Delegate and return
69          return holder.getClassPathAsArgument();
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void setup() {
77          this.holder = builder.buildAndSet();
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public void restore() {
85          holder.restoreClassLoaderAndReleaseThread();
86      }
87  }