Skip to content

Commit

Permalink
Fixed mojohaus#15: corrected classloader classpath for looking up wsd…
Browse files Browse the repository at this point in the history
…l files
  • Loading branch information
andham committed Jul 6, 2017
1 parent 55877e2 commit c5d7b82
Show file tree
Hide file tree
Showing 21 changed files with 628 additions and 130 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/codehaus/mojo/jaxws/MainWsImportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
package org.codehaus.mojo.jaxws;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -99,4 +102,26 @@ protected File getImplDestDir()
{
return implDestDir;
}

@Override
protected List<String> getWSDLFileLookupClasspathElements()
{
List<String> list = new ArrayList<String>();

for ( Artifact a : project.getDependencyArtifacts() )
{
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() )
|| Artifact.SCOPE_PROVIDED.equals( a.getScope() )
|| Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
{
File file = a.getFile();
if ( file != null )
{
list.add( file.getPath() );
}
}
}

return list;
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/codehaus/mojo/jaxws/TestWsImportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
package org.codehaus.mojo.jaxws;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -129,6 +132,26 @@ protected File getImplDestDir()
return implDestDir;
}

@Override
protected List<String> getWSDLFileLookupClasspathElements()
{
List<String> list = new ArrayList<String>();

for ( Artifact a : project.getDependencyArtifacts() )
{
if ( !Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
{
File file = a.getFile();
if ( file != null )
{
list.add( file.getPath() );
}
}
}

return list;
}

@Override
public void executeJaxws()
throws MojoExecutionException
Expand Down
202 changes: 101 additions & 101 deletions src/main/java/org/codehaus/mojo/jaxws/WsImportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@
import java.util.Enumeration;
import java.util.Formatter;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.Proxy;
Expand Down Expand Up @@ -271,6 +269,12 @@ abstract class WsImportMojo

protected abstract File getImplDestDir();

/**
* Returns the dependencies that should be placed on the classpath for the classloader
* to lookup wsdl files.
*/
protected abstract List<String> getWSDLFileLookupClasspathElements();

@Override
public void executeJaxws()
throws MojoExecutionException
Expand Down Expand Up @@ -559,136 +563,132 @@ private URL[] getWSDLFiles()
throws MojoExecutionException
{
List<URL> files = new ArrayList<URL>();
Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
List<URL> urlCpath = new ArrayList<URL>( dependencyArtifacts.size() );
for ( Artifact a : dependencyArtifacts )
ClassLoader loader = null;

try
{
try
{
if ( a.getFile() != null )
{
URL u = a.getFile().toURI().toURL();
urlCpath.add( u );
}
else
{
getLog().warn( "cannot find file for " + a.getGroupId() + ":" + a.getArtifactId() + ":"
+ a.getVersion() );
}
}
catch ( MalformedURLException ex )
List<String> classpathElements = getWSDLFileLookupClasspathElements();
List<URL> urlCpath = new ArrayList<URL>( classpathElements.size() );
for ( String el : classpathElements )
{
getLog().error( ex );
URL u = new File( el ).toURI().toURL();
urlCpath.add( u );
}
}
ClassLoader loader = urlCpath.isEmpty() ? Thread.currentThread().getContextClassLoader()
: new URLClassLoader( urlCpath.toArray( new URL[0] ) );
if ( wsdlFiles != null )
{
for ( String wsdlFileName : wsdlFiles )

loader = new URLClassLoader( urlCpath.toArray( new URL[0] ) );

if ( wsdlFiles != null )
{
File wsdl = new File( wsdlFileName );
URL toAdd = null;
if ( !wsdl.isAbsolute() )
for ( String wsdlFileName : wsdlFiles )
{
wsdl = new File( wsdlDirectory, wsdlFileName );
}
if ( !wsdl.exists() )
{
toAdd = loader.getResource( wsdlFileName );
}
else
{
try
File wsdl = new File( wsdlFileName );
URL toAdd = null;
if ( !wsdl.isAbsolute() )
{
toAdd = wsdl.toURI().toURL();
wsdl = new File( wsdlDirectory, wsdlFileName );
}
catch ( MalformedURLException ex )
if ( !wsdl.exists() )
{
getLog().error( ex );
toAdd = loader.getResource( wsdlFileName );
}
}
getLog().debug( "The wsdl File is '" + wsdlFileName + "' from '" + toAdd + "'" );
if ( toAdd != null )
{
files.add( toAdd );
}
else
{
throw new MojoExecutionException( "'" + wsdlFileName + "' not found." );
}
}
}
else
{
getLog().debug( "The wsdl Directory is " + wsdlDirectory );
if ( wsdlDirectory.exists() )
{
File[] wsdls = wsdlDirectory.listFiles( new WSDLFile() );
for ( File wsdl : wsdls )
{
try
else
{
files.add( wsdl.toURI().toURL() );
try
{
toAdd = wsdl.toURI().toURL();
}
catch ( MalformedURLException ex )
{
getLog().error( ex );
}
}
catch ( MalformedURLException ex )
getLog().debug( "The wsdl File is '" + wsdlFileName + "' from '" + toAdd + "'" );
if ( toAdd != null )
{
getLog().error( ex );
files.add( toAdd );
}
else
{
throw new MojoExecutionException( "'" + wsdlFileName + "' not found." );
}
}
}
else
{
URI rel = project.getBasedir().toURI().relativize( wsdlDirectory.toURI() );
String dir = rel.getPath();
URL u = loader.getResource( dir );
if ( u == null )
{
dir = "WEB-INF/wsdl/";
u = loader.getResource( dir );
}
if ( u == null )
getLog().debug( "The wsdl Directory is " + wsdlDirectory );
if ( wsdlDirectory.exists() )
{
dir = "META-INF/wsdl/";
u = loader.getResource( dir );
File[] wsdls = wsdlDirectory.listFiles( new WSDLFile() );
for ( File wsdl : wsdls )
{
files.add( wsdl.toURI().toURL() );
}
}
if ( !( u == null || !"jar".equalsIgnoreCase( u.getProtocol() ) ) )
else
{
String path = u.getPath();
JarFile jarFile = null;
try
URI rel = project.getBasedir().toURI().relativize( wsdlDirectory.toURI() );
String dir = rel.getPath();
URL u = loader.getResource( dir );
if ( u == null )
{
Pattern p = Pattern.compile( dir.replace( File.separatorChar, '/' ) + PATTERN,
Pattern.CASE_INSENSITIVE );
jarFile = new JarFile( path.substring( 5, path.indexOf( "!/" ) ) );
Enumeration<JarEntry> jes = jarFile.entries();
while ( jes.hasMoreElements() )
{
JarEntry je = jes.nextElement();
Matcher m = p.matcher( je.getName() );
if ( m.matches() )
{
String s = "jar:" + path.substring( 0, path.indexOf( "!/" ) + 2 ) + je.getName();
files.add( new URL( s ) );
}
}
dir = "WEB-INF/wsdl/";
u = loader.getResource( dir );
}
catch ( IOException ex )
if ( u == null )
{
getLog().error( ex );
dir = "META-INF/wsdl/";
u = loader.getResource( dir );
}
finally
if ( !( u == null || !"jar".equalsIgnoreCase( u.getProtocol() ) ) )
{
closeQuietly( jarFile );
String path = u.getPath();
JarFile jarFile = null;
try
{
Pattern p = Pattern.compile( dir.replace( File.separatorChar, '/' ) + PATTERN,
Pattern.CASE_INSENSITIVE );
jarFile = new JarFile( path.substring( 5, path.indexOf( "!/" ) ) );
Enumeration<JarEntry> jes = jarFile.entries();
while ( jes.hasMoreElements() )
{
JarEntry je = jes.nextElement();
Matcher m = p.matcher( je.getName() );
if ( m.matches() )
{
String s = "jar:" + path.substring( 0, path.indexOf( "!/" ) + 2 ) + je.getName();
files.add( new URL( s ) );
}
}
}
catch ( IOException ex )
{
getLog().error( ex );
}
finally
{
closeQuietly( jarFile );
}
}
}
}
}
if ( !urlCpath.isEmpty() )
catch ( MojoExecutionException e )
{
// if we created a classloader, cleanup
closeQuietly( loader );
throw e;
}
catch ( Exception e )
{
throw new MojoExecutionException( "Error while retrieving list of WSDL files to process", e );
}
finally
{
if ( loader != null )
{
// if we created a classloader, cleanup
closeQuietly( loader );
}
}

return files.toArray( new URL[0] );
}

Expand Down
1 change: 1 addition & 0 deletions src/test/it/issue-12/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
19 changes: 19 additions & 0 deletions src/test/it/issue-15/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2017 MojoHaus
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

invoker.goals=clean generate-test-sources
Loading

0 comments on commit c5d7b82

Please sign in to comment.