Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHRINKWRAP-288 - added loading file from stream in jar #5

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public interface MavenDependencyResolver
*/
MavenDependencyResolver configureFrom(String path);

/**
* Configures Maven from a settings.xml file
*
* @param path A path to a settings.xml configuration file
* @return A dependency builder with a configuration from given file
*/
MavenDependencyResolver configureFromFileInClassPath(String path);

/**
* Loads remote repositories for a POM file. If repositories are defined in
* the parent of the POM file and there are accessible via local file
Expand Down Expand Up @@ -162,16 +154,7 @@ public interface MavenDependencyResolver
* @throws ResolutionException
*/
MavenDependencyResolver includeDependenciesFromPom(final String path) throws ResolutionException;

/**
* Resolves based upon dependencies declared in the POM at the classpath
*
* @param path
* @return
* @throws ResolutionException
*/
MavenDependencyResolver includeDependenciesFromPomInClassPath(final String path) throws ResolutionException;


/**
* Resolves based upon dependencies declared in the POM at the specified path
*
Expand Down
32 changes: 32 additions & 0 deletions impl-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<version.org.sonatype.aether>1.8</version.org.sonatype.aether>
<version.org.apache.maven>3.0.1</version.org.apache.maven>
<version.org.apache.maven.wagon>1.0-beta-7</version.org.apache.maven.wagon>
<appended.jar.for.test>${basedir}/src/test/resources/artifacts/test-pom-in.jar</appended.jar.for.test>

</properties>

Expand Down Expand Up @@ -145,6 +146,37 @@
<build>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${appended.jar.for.test}</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${appended.jar.for.test}</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package org.jboss.shrinkwrap.resolver.impl.maven;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -76,6 +78,9 @@ public class MavenBuilderImpl implements MavenDependencyResolverInternal

private static final Logger log = Logger.getLogger(MavenArtifactBuilderImpl.class.getName());

private static final String CLASSPATH_QUALIFIER = "classpath:";
private static final String FILE_QUALIFIER = "file:";

private static final File[] FILE_CAST = new File[0];

private final MavenRepositorySystem system;
Expand Down Expand Up @@ -125,23 +130,16 @@ public MavenBuilderImpl()
@Override
public MavenDependencyResolver configureFrom(String path)
{
path = resolvePathByQualifier(path);
Validate.isReadable(path, "Path to the settings.xml ('" + path + "')must be defined and accessible");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clutters the real path user specified, making the exception message unclear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you're right, specially if the path is resolved for "classpath".
we can attribute the resolvedPath to a new String. wdyt?


system.loadSettings(new File(path), settings);
// regenerate session
this.session = system.getSession(settings);
return this;
}

/**
* Configures Maven from a settings.xml file in classpath
*
* @param path A path to a settings.xml configuration file
* @return A dependency builder with a configuration from given file
*/
@Override
public MavenDependencyResolver configureFromFileInClassPath(String path) {
return configureFrom(getResourcePathFromResourceName(path));
}



/**
* Loads remote repositories for a POM file. If repositories are defined in
Expand Down Expand Up @@ -202,33 +200,21 @@ public MavenDependencyResolver loadReposFromPom(final String path) throws Resolu
*/

@Override
public MavenDependencyResolver includeDependenciesFromPom(final String path) throws ResolutionException
public MavenDependencyResolver includeDependenciesFromPom(String path) throws ResolutionException
{
path = resolvePathByQualifier(path);
Validate.isReadable(path, "Path to the pom.xml file must be defined and accessible");

Model model = system.loadPom(new File(path), settings, session);

ArtifactTypeRegistry stereotypes = system.getArtifactTypeRegistry(session);

for (org.apache.maven.model.Dependency dependency : model.getDependencies())
{
dependencies.push(MavenConverter.fromDependency(dependency, stereotypes));
dependencies.push(MavenConverter.fromDependency(dependency, stereotypes));
}
return this;
}

/**
* Loads dependencies from the specified pom located in classpath and applies the specified <tt>MavenResolutionFilter</tt>.
* Adds the Maven central repository by default.
*
* @param path path to file which contains the desired dependencies
* @param filter the filter to apply
* @return a corresponding <tt>MavenDependencyResolver</tt>
* @throws ResolutionException if any resolution related exceptions occur
*/
@Override
public MavenDependencyResolver includeDependenciesFromPomInClassPath(String path) throws ResolutionException {
return includeDependenciesFromPom(getResourcePathFromResourceName(path));
}

/**
* @deprecated please use {@link #includeDependenciesFromPom(String)} instead
Expand Down Expand Up @@ -570,11 +556,6 @@ public MavenDependencyResolver configureFrom(String path)
return delegate.configureFrom(path);
}

@Override
public MavenDependencyResolver configureFromFileInClassPath(String path) {
return delegate.configureFromFileInClassPath(path);
}

@Override
public File[] resolveAsFiles(MavenResolutionFilter filter) throws ResolutionException
{
Expand Down Expand Up @@ -645,11 +626,6 @@ public MavenDependencyResolver includeDependenciesFromPom(String path) throws Re
return delegate.includeDependenciesFromPom(path);
}

@Override
public MavenDependencyResolver includeDependenciesFromPomInClassPath(String path) throws ResolutionException {
return delegate.includeDependenciesFromPomInClassPath(path);
}

/**
* @deprecated please use {@link #includeDependenciesFromPom(String)} instead
*/
Expand Down Expand Up @@ -856,11 +832,6 @@ public MavenDependencyResolver configureFrom(String path)
return delegate.configureFrom(path);
}

@Override
public MavenDependencyResolver configureFromFileInClassPath(String path) {
return delegate.configureFromFileInClassPath(path);
}

@Override
public MavenDependencyResolver loadMetadataFromPom(String path) throws ResolutionException
{
Expand Down Expand Up @@ -938,11 +909,6 @@ public MavenDependencyResolver includeDependenciesFromPom(String path) throws Re
{
return delegate.includeDependenciesFromPom(path);
}

@Override
public MavenDependencyResolver includeDependenciesFromPomInClassPath(String path) throws ResolutionException {
return delegate.includeDependenciesFromPomInClassPath(path);
}

/**
* @deprecated please use {@link #includeDependenciesFromPom(String)} instead
Expand Down Expand Up @@ -986,39 +952,75 @@ public MavenDependencyResolver goOffline()
return this;
}

/**
* Gets a resource from the TCCL and returns its name As resource in classpath.
*
* @param resourceName is the name of the resource in the classpath
* @return the file path for resourceName @see {@link java.net.URL#getFile()}
* @throws IllegalArgumentException if resourceName doesn't exist in the classpath or privileges are not granted
*/
private String getResourcePathFromResourceName(final String resourceName) throws IllegalArgumentException {
final URL resourceUrl = AccessController.doPrivileged(GetTcclAction.INSTANCE).getResource(resourceName);
Validate.notNull(resourceUrl, resourceName + " doesn't exist or can't be accessed");

String resourcePath = AccessController.doPrivileged(GetTcclAction.INSTANCE).getResource(resourceName).getFile();
try {
// Have to URL decode the string as the ClassLoader.getResource(String) returns an URL encoded URL
resourcePath = URLDecoder.decode(resourcePath, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(uee);
}
return resourcePath;
}

/**
* Obtains the {@link Thread} Context {@link ClassLoader}
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
INSTANCE;

@Override
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}

}
}
/**
* Gets a resource from the TCCL and returns its name As resource in classpath.
*
* @param resourceName is the name of the resource in the classpath
* @return the file path for resourceName @see {@link java.net.URL#getFile()}
* @throws IllegalArgumentException if resourceName doesn't exist in the classpath or privileges are not granted
*/
private String getLocalResourcePathFromResourceName(final String resourceName) throws IllegalArgumentException
{
final URL resourceUrl = AccessController.doPrivileged(SecurityActions.GetTcclAction.INSTANCE).getResource(resourceName);
Validate.notNull(resourceUrl, resourceName + " doesn't exist or can't be accessed");

String resourcePath = resourceUrl.getFile();
resourcePath = this.decodeToUTF8(resourcePath);

if (!this.isResourcePathForAReadableFile(resourcePath)) {
resourcePath = this.createTmpPomFile(resourceName);
}
return resourcePath;
}

private boolean isResourcePathForAReadableFile(String resourcePath) {
File file = new File(resourcePath);
return file.exists();
}

private String createTmpPomFile(final String resourceName) {
File tmp = null;
try {
String tmpFileName = resourceName.replaceAll("/", ".").replaceAll(File.pathSeparator, ".");
System.out.println(tmpFileName);
tmp = File.createTempFile("sw_" + tmpFileName, null);

InputStream inputStream = AccessController.doPrivileged(SecurityActions.GetTcclAction.INSTANCE).getResourceAsStream(resourceName);
OutputStream out;
out = new FileOutputStream(tmp);

byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
inputStream.close();

} catch (IOException e) {
throw new IllegalArgumentException("Could not create a temp pom file with the resource name " + resourceName);
}

return tmp.getPath();

}

private String decodeToUTF8(String resourcePath) {
try {
// Have to URL decode the string as the ClassLoader.getResource(String) returns an URL encoded URL
resourcePath = URLDecoder.decode(resourcePath, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(uee);
}
return resourcePath;
}

private String resolvePathByQualifier(String path) {
if (path.startsWith(CLASSPATH_QUALIFIER)) {
path = this.getLocalResourcePathFromResourceName(path.replace(CLASSPATH_QUALIFIER, ""));
} else if (path.startsWith(FILE_QUALIFIER)) {
path = path.replace(FILE_QUALIFIER, "");
}
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jboss.shrinkwrap.resolver.impl.maven;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;

Expand Down Expand Up @@ -97,4 +98,19 @@ else if (t instanceof IllegalArgumentException)
}
}
}

/**
* Obtains the {@link Thread} Context {@link ClassLoader}
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
static enum GetTcclAction implements PrivilegedAction<ClassLoader>
{
INSTANCE;

@Override
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
}
}