View Javadoc

1   /*
2    * Copyright 2006 Codehaus
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.codehaus.mojo.jaxws;
19  
20  import java.io.File;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.net.URLClassLoader;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.DependencyResolutionRequiredException;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.project.MavenProject;
32  
33  /**
34   * 
35   * @author dantran <dantran@apache.org>
36   * @version $Id: AbstractJaxwsMojo.java 3240 2007-02-04 07:13:21Z dantran $ *
37   */
38  
39  public abstract class AbstractJaxwsMojo
40      extends AbstractMojo
41  {
42  
43      /**
44       * @parameter expression="${project}"
45       * @readonly
46       */
47      protected MavenProject project;
48  
49      /**
50       * Specify where to place output generated classes 
51       * Set to "" to turn it off
52       * @parameter default-value="${project.build.outputDirectory}"
53       */
54      protected File destDir;
55  
56      /**
57       * Output messages about what the tool is doing
58       * 
59       * @parameter default-value="false"
60       */
61      protected boolean verbose;
62  
63      /**
64       * Keep generated files.
65       * 
66       * @parameter default-value="false"
67       */
68      protected boolean keep;
69  
70      /**
71       * Allow to use the JAXWS Vendor Extensions.
72       * 
73       * @parameter default-value="false"
74       */
75      protected boolean extension;
76  
77  
78      /**
79       * Map of of plugin artifacts.
80       *
81       * @parameter expression="${plugin.artifactMap}"
82       * @readonly
83       */
84      private Map pluginArtifactMap;
85  
86      /**
87       * Need to build a URLClassloader since Maven removed it form the chain
88       * @param parent
89       * @return
90       */
91      protected String initClassLoader( ClassLoader parent )
92          throws MojoExecutionException
93      {
94  
95          try
96          {
97              List classpathFiles = project.getCompileClasspathElements();
98              
99              URL[] urls = new URL[classpathFiles.size() + 3];
100             
101             StringBuffer classPath = new StringBuffer();
102             
103             for ( int i = 0; i < classpathFiles.size(); ++i )
104             {
105                 getLog().debug( (String) classpathFiles.get( i ) );
106                 urls[i] = new File( (String) classpathFiles.get( i ) ).toURL();
107                 classPath.append( (String) classpathFiles.get( i ) );
108                 classPath.append( File.pathSeparatorChar );
109             }
110 
111             
112             urls[classpathFiles.size()] = new File( project.getBuild().getOutputDirectory() ).toURL();
113 
114             Artifact jaxwsToolsArtifact = (Artifact) pluginArtifactMap.get( "com.sun.xml.ws:jaxws-tools" );
115             urls[classpathFiles.size() + 1] = jaxwsToolsArtifact.getFile().toURL();
116             
117             File toolsJar = new File( System.getProperty( "java.home"), "../lib/tools.jar" );
118             if ( ! toolsJar.exists() ) 
119             {
120             	//
121             	toolsJar = new File( System.getProperty( "java.home"), "lib/tools.jar" );
122             }
123             urls[classpathFiles.size() + 2] = toolsJar.toURL();
124             
125             URLClassLoader cl = new URLClassLoader( urls, parent );
126 
127             // Set the new classloader
128             Thread.currentThread().setContextClassLoader( cl );
129 
130             System.setProperty( "java.class.path", classPath.toString() );
131 
132             String sysCp = System.getProperty( "java.class.path" );
133 
134             return sysCp;            
135         }
136         catch ( MalformedURLException e )
137         {
138             throw new MojoExecutionException( e.getMessage(), e );
139         }
140         catch ( DependencyResolutionRequiredException e )
141         {
142             throw new MojoExecutionException( e.getMessage(), e );
143         }
144 
145     }
146 }