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   * @svnLink $Revision: 3872 $;$Date: 2009-08-04 13:41:09 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/maven/plugins/maven-jaxws-plugin/src/main/java/org/codehaus/mojo/jaxws/AbstractJaxwsMojo.java $
36   *
37   * @author dantran <dantran@apache.org>
38   * @version $Id: AbstractJaxwsMojo.java 3872 2009-08-04 11:41:09Z swismer $ *
39   */
40  abstract class AbstractJaxwsMojo extends AbstractMojo {
41  
42  	/**
43  	 * @parameter expression="${project}"
44  	 * @readonly
45  	 */
46  	protected MavenProject project;
47  
48  	/**
49  	 * Output messages about what the tool is doing
50  	 *
51  	 * @parameter default-value="false"
52  	 */
53  	protected boolean verbose;
54  
55  	/**
56  	 * Keep generated files.
57  	 *
58  	 * @parameter default-value="false"
59  	 */
60  	protected boolean keep;
61  
62  	/**
63  	 * Allow to use the JAXWS Vendor Extensions.
64  	 *
65  	 * @parameter default-value="false"
66  	 */
67  	protected boolean extension;
68  
69  
70  	/**
71  	 * Map of of plugin artifacts.
72  	 *
73  	 * @parameter expression="${plugin.artifactMap}"
74  	 * @readonly
75  	 */
76  	private Map pluginArtifactMap;
77  
78  	/**
79  	 * Either ${build.outputDirectory} or ${build.testOutputDirectory}.
80  	 */
81  	protected abstract File getDestDir();
82  
83  	/**
84  	 * Need to build a URLClassloader since Maven removed it form the chain
85  	 * @param parent
86  	 * @return
87  	 */
88  	protected String initClassLoader( ClassLoader parent )
89  		throws MojoExecutionException
90  	{
91  
92  		try
93  		{
94  			List classpathFiles = project.getCompileClasspathElements();
95  			
96  			URL[] urls = new URL[classpathFiles.size() + 3];
97  			
98  			StringBuffer classPath = new StringBuffer();
99  			
100 			for ( int i = 0; i < classpathFiles.size(); ++i )
101 			{
102 				getLog().debug( (String) classpathFiles.get( i ) );
103 				urls[i] = new File( (String) classpathFiles.get( i ) ).toURL();
104 				classPath.append( (String) classpathFiles.get( i ) );
105 				classPath.append( File.pathSeparatorChar );
106 			}
107 
108 			
109 			urls[classpathFiles.size()] = new File( project.getBuild().getOutputDirectory() ).toURL();
110 
111 			Artifact jaxwsToolsArtifact = (Artifact) pluginArtifactMap.get( "com.sun.xml.ws:jaxws-tools" );
112 			urls[classpathFiles.size() + 1] = jaxwsToolsArtifact.getFile().toURL();
113 			
114 			File toolsJar = new File( System.getProperty( "java.home"), "../lib/tools.jar" );
115 			if ( ! toolsJar.exists() )
116 			{
117 				//
118 				toolsJar = new File( System.getProperty( "java.home"), "lib/tools.jar" );
119 			}
120 			urls[classpathFiles.size() + 2] = toolsJar.toURL();
121 			
122 			URLClassLoader cl = new URLClassLoader( urls, parent );
123 
124 			// Set the new classloader
125 			Thread.currentThread().setContextClassLoader( cl );
126 
127 			System.setProperty( "java.class.path", classPath.toString() );
128 
129 			String sysCp = System.getProperty( "java.class.path" );
130 
131 			return sysCp;
132 		}
133 		catch ( MalformedURLException e )
134 		{
135 			throw new MojoExecutionException( e.getMessage(), e );
136 		}
137 		catch ( DependencyResolutionRequiredException e )
138 		{
139 			throw new MojoExecutionException( e.getMessage(), e );
140 		}
141 
142 	}
143 }