Grab -> Aether (#255)

This commit is contained in:
Marcin Grzejszczak
2016-04-29 11:44:06 +02:00
parent 4d9efc2865
commit 5076429eab
14 changed files with 209 additions and 193 deletions

View File

@@ -10,4 +10,5 @@ springVersion=4.2.5.RELEASE
springIntegrationDslVersion=1.1.2.RELEASE
springStreamVersion=1.0.0.RC2
springCloudVersion=1.1.0.RC2
camelVersion=2.17.0
camelVersion=2.17.0
aetherVersion=1.1.0

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.codearte.accurest.stubs</groupId>
<artifactId>streamService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20160409062112</lastUpdated>
</versioning>
</metadata>

View File

@@ -15,8 +15,8 @@ After that rule gets executed Stub Runner connects to your Maven repository and
- start a WireMock server for each Maven dependency on a random port from the provided range of ports / provided port
- feed the WireMock server with all JSON files that are valid WireMock definitions
Stub Runner uses http://docs.groovy-lang.org/latest/html/documentation/grape.html[Groovy's Grape] mechanism to download the Maven dependencies.
Check their http://docs.groovy-lang.org/latest/html/documentation/grape.html[docs] for more information.
Stub Runner uses https://wiki.eclipse.org/Aether[Eclipse Aether] mechanism to download the Maven dependencies.
Check their https://wiki.eclipse.org/Aether[docs] for more information.
Since the `AccurestRule` implements the `StubFinder` it allows you to find the started stubs:

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.codearte.accurest.stubs</groupId>
<artifactId>fraudDetectionServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20160409062112</lastUpdated>
</versioning>
</metadata>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.codearte.accurest.stubs</groupId>
<artifactId>loanIssuance</artifactId>
<version>0.0.1-SNAPSHOT</version>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20160409062111</lastUpdated>
</versioning>
</metadata>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.codearte.accurest.stubs</groupId>
<artifactId>camelService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20160409062112</lastUpdated>
</versioning>
</metadata>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.codearte.accurest.stubs</groupId>
<artifactId>integrationService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20160409062112</lastUpdated>
</versioning>
</metadata>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.codearte.accurest.stubs</groupId>
<artifactId>streamService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20160409062112</lastUpdated>
</versioning>
</metadata>

View File

@@ -1,23 +1,24 @@
package io.codearte.accurest.stubrunner.spring;
import java.io.IOException;
import java.util.Set;
import io.codearte.accurest.messaging.AccurestMessaging;
import io.codearte.accurest.messaging.noop.NoOpAccurestMessaging;
import io.codearte.accurest.stubrunner.AetherStubDownloader;
import io.codearte.accurest.stubrunner.BatchStubRunner;
import io.codearte.accurest.stubrunner.BatchStubRunnerFactory;
import io.codearte.accurest.stubrunner.StubConfiguration;
import io.codearte.accurest.stubrunner.StubDownloader;
import io.codearte.accurest.stubrunner.StubRunner;
import io.codearte.accurest.stubrunner.StubRunnerOptions;
import io.codearte.accurest.stubrunner.StubRunning;
import io.codearte.accurest.stubrunner.util.StubsParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import io.codearte.accurest.messaging.AccurestMessaging;
import io.codearte.accurest.messaging.noop.NoOpAccurestMessaging;
import io.codearte.accurest.stubrunner.BatchStubRunner;
import io.codearte.accurest.stubrunner.BatchStubRunnerFactory;
import io.codearte.accurest.stubrunner.StubConfiguration;
import io.codearte.accurest.stubrunner.StubRunner;
import io.codearte.accurest.stubrunner.StubRunnerOptions;
import io.codearte.accurest.stubrunner.StubRunning;
import io.codearte.accurest.stubrunner.util.StubsParser;
import java.io.IOException;
import java.util.Set;
/**
* Configuration that initializes a {@link BatchStubRunner} that runs {@link StubRunner} instance for each stub
@@ -26,6 +27,7 @@ import io.codearte.accurest.stubrunner.util.StubsParser;
public class StubRunnerConfiguration {
@Autowired(required = false) AccurestMessaging accurestMessaging;
@Autowired(required = false) StubDownloader stubDownloader;
/**
* Bean that initializes stub runners, runs them and on shutdown closes them. Upon its instantiation
@@ -51,6 +53,7 @@ public class StubRunnerConfiguration {
stubRepositoryRoot == null || workOffline, stubsSuffix, stubs);
Set<StubConfiguration> dependencies = StubsParser.fromString(stubs, stubsSuffix);
BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions, dependencies,
stubDownloader != null ? stubDownloader : new AetherStubDownloader(stubRunnerOptions),
accurestMessaging != null ? accurestMessaging : new NoOpAccurestMessaging()).buildBatchStubRunner();
// TODO: Consider running it in a separate thread
batchStubRunner.runStubs();

View File

@@ -11,6 +11,12 @@ dependencies {
compile "com.github.tomakehurst:wiremock:$wiremockVersion"
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'args4j:args4j:2.32'
compile "org.eclipse.aether:aether-api:${aetherVersion}"
compile "org.eclipse.aether:aether-impl:${aetherVersion}"
compile "org.eclipse.aether:aether-transport-file:${aetherVersion}"
compile "org.eclipse.aether:aether-transport-http:${aetherVersion}"
compile "org.eclipse.aether:aether-connector-basic:${aetherVersion}"
compile 'org.apache.maven:maven-aether-provider:3.3.9'
if (rootProject.hasProperty("fatJar")) runtime 'ch.qos.logback:logback-classic:1.1.3'

View File

@@ -0,0 +1,106 @@
package io.codearte.accurest.stubrunner
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.apache.maven.repository.internal.MavenRepositorySystemUtils
import org.eclipse.aether.DefaultRepositorySystemSession
import org.eclipse.aether.RepositorySystem
import org.eclipse.aether.RepositorySystemSession
import org.eclipse.aether.artifact.Artifact
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory
import org.eclipse.aether.impl.DefaultServiceLocator
import org.eclipse.aether.repository.LocalRepository
import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.resolution.ArtifactRequest
import org.eclipse.aether.resolution.ArtifactResult
import org.eclipse.aether.resolution.VersionRangeRequest
import org.eclipse.aether.resolution.VersionRangeResult
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory
import org.eclipse.aether.spi.connector.transport.TransporterFactory
import org.eclipse.aether.transport.file.FileTransporterFactory
import org.eclipse.aether.transport.http.HttpTransporterFactory
import org.eclipse.aether.version.Version
import static io.codearte.accurest.stubrunner.util.ZipCategory.unzipTo
import static java.nio.file.Files.createTempDirectory
/**
* @author Mariusz Smykula
*/
@CompileStatic
@Slf4j
class AetherStubDownloader implements StubDownloader {
private static final String MAVEN_LOCAL_REPOSITORY_LOCATION = 'maven.repo.local'
private static final String ACCUREST_TEMP_DIR_PREFIX = 'accurest'
private static final String ARTIFACT_EXTENSION = 'jar'
private static final String ARTIFACT_VERSION = '(0,]'
private final List<RemoteRepository> remoteRepos
private final RepositorySystem repositorySystem
private final RepositorySystemSession session
AetherStubDownloader(RepositorySystem repositorySystem, List<RemoteRepository> repositories, RepositorySystemSession session) {
this.remoteRepos = repositories
this.repositorySystem = repositorySystem ?: newRepositorySystem()
this.session = session ?: newSession(this.repositorySystem)
}
AetherStubDownloader(StubRunnerOptions stubRunnerOptions) {
this.remoteRepos = remoteRepositories(stubRunnerOptions)
this.repositorySystem = newRepositorySystem()
this.session = newSession(this.repositorySystem)
}
private List<RemoteRepository> remoteRepositories(StubRunnerOptions stubRunnerOptions) {
return [new RemoteRepository.Builder("remote", "default", stubRunnerOptions.stubRepositoryRoot).build()]
}
private RepositorySystem newRepositorySystem() {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator()
locator.addService(RepositoryConnectorFactory, BasicRepositoryConnectorFactory)
locator.addService(TransporterFactory, FileTransporterFactory)
locator.addService(TransporterFactory, HttpTransporterFactory)
return locator.getService(RepositorySystem)
}
private RepositorySystemSession newSession(RepositorySystem system) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository(System.getProperty(MAVEN_LOCAL_REPOSITORY_LOCATION, "${System.getProperty("user.home")}/.m2/repository"));
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
return session;
}
@Override
public File downloadAndUnpackStubJar(boolean workOffline, String stubRepositoryRoot, String stubsGroup, String stubsModule, String classifier) {
Version highestVersion = resolveArtifactVersion(stubsGroup, stubsModule, classifier);
log.info("Resolved highest version is $highestVersion")
if (!highestVersion) {
log.warn("Stub for group [$stubsGroup] module [$stubsModule] and classifier [$classifier] not found in [$stubRepositoryRoot]")
return null
}
Artifact artifact = new DefaultArtifact(stubsGroup, stubsModule, classifier, ARTIFACT_EXTENSION, highestVersion.toString())
ArtifactRequest request = new ArtifactRequest(artifact: artifact, repositories: remoteRepos)
log.info("Resolving artifact $artifact from $remoteRepos")
ArtifactResult result = repositorySystem.resolveArtifact(session, request)
log.info("Resolved artifact $artifact to ${result.artifact.file} from ${result.repository}")
return unpackStubJarToATemporaryFolder(result.artifact.file.toURI())
}
private Version resolveArtifactVersion(String stubsGroup, String stubsModule, String classifier) {
Artifact artifact = new DefaultArtifact(stubsGroup, stubsModule, classifier, ARTIFACT_EXTENSION, ARTIFACT_VERSION)
VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, remoteRepos, null);
VersionRangeResult rangeResult = repositorySystem.resolveVersionRange(session, versionRangeRequest);
return rangeResult.highestVersion;
}
private static File unpackStubJarToATemporaryFolder(URI stubJarUri) {
File tmpDirWhereStubsWillBeUnzipped = createTempDirectory(ACCUREST_TEMP_DIR_PREFIX).toFile()
tmpDirWhereStubsWillBeUnzipped.deleteOnExit()
log.info("Unpacking stub from JAR [URI: ${stubJarUri}]")
unzipTo(new File(stubJarUri), tmpDirWhereStubsWillBeUnzipped)
return tmpDirWhereStubsWillBeUnzipped
}
}

View File

@@ -55,7 +55,7 @@ class AvailablePortScanner {
try {
socket = new ServerSocket(portToScan)
} finally {
socket.close()
socket?.close()
}
}

View File

@@ -19,23 +19,23 @@ class BatchStubRunnerFactory {
private final AccurestMessaging accurestMessaging
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection<StubConfiguration> dependencies) {
this(stubRunnerOptions, dependencies, new GrapeStubDownloader(), new NoOpAccurestMessaging())
this(stubRunnerOptions, dependencies, new AetherStubDownloader(stubRunnerOptions), new NoOpAccurestMessaging())
}
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection<StubConfiguration> dependencies,
AccurestMessaging accurestMessaging) {
this(stubRunnerOptions, dependencies, new GrapeStubDownloader(), accurestMessaging)
this(stubRunnerOptions, dependencies, new AetherStubDownloader(stubRunnerOptions), accurestMessaging)
}
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions,
Collection<StubConfiguration> dependencies,
StubDownloader stubDownloader) {
StubDownloader stubDownloader) {
this(stubRunnerOptions, dependencies, stubDownloader, new NoOpAccurestMessaging())
}
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions,
Collection<StubConfiguration> dependencies,
StubDownloader stubDownloader,
Collection<StubConfiguration> dependencies,
StubDownloader stubDownloader,
AccurestMessaging accurestMessaging) {
this.stubRunnerOptions = stubRunnerOptions
this.dependencies = dependencies

View File

@@ -1,172 +0,0 @@
package io.codearte.accurest.stubrunner
import groovy.grape.Grape
import groovy.util.logging.Slf4j
import io.codearte.accurest.stubrunner.util.StringUtils
import io.codearte.accurest.stubrunner.util.ZipCategory
import static java.nio.file.Files.createTempDirectory
/**
* Downloads stubs from an external repository and unpacks them locally
*/
@Slf4j
class GrapeStubDownloader implements StubDownloader {
private static final String LATEST_MODULE = '*'
private static final String REPOSITORY_NAME = 'dependency-repository'
private static final String STUB_RUNNER_TEMP_DIR_PREFIX = 'stub-runner'
private static final String GRAPE_CONFIG = 'grape.config'
private static final String STUB_RUNNER_GRAPE_CONFIG = "accurest.stubrunner.grape.config"
/**
* Downloads stubs from an external repository and unpacks them locally.
* Depending on the switch either uses only local repository to check for
* stub presence.
*
* @param workOffline -flag that defines whether only local cache should be used
* @param stubRepositoryRoot - address of the repo from which deps should be grabbed
* @param stubsGroup - group name of the jar containing stubs
* @param stubsModule - artifact id with a classifier name of the jar containing stubs
* @return file where the stubs where unpacked
*/
File downloadAndUnpackStubJar(boolean workOffline, String stubRepositoryRoot, String stubsGroup, String
stubsModule, String classifier) {
log.warn("Downloading stubs for group [$stubsGroup], module [$stubsModule], " +
"classifier [$classifier] from repository [$stubRepositoryRoot]")
URI stubJarUri = findGrabbedStubJars(workOffline, stubRepositoryRoot, stubsGroup, stubsModule, classifier)
if (!stubJarUri) {
log.warn("Failed to download stubs for group [$stubsGroup] artifactId [$stubsModule] and " +
"classifier [$classifier] from repository [$stubRepositoryRoot]")
return null
}
File unzippedStubsDir = unpackStubJarToATemporaryFolder(stubJarUri)
unzippedStubsDir.deleteOnExit()
Thread.addShutdownHook {
unzippedStubsDir.deleteDir()
}
return unzippedStubsDir
}
private File unpackStubJarToATemporaryFolder(URI stubJarUri) {
File tmpDirWhereStubsWillBeUnzipped = createTempDirectory(STUB_RUNNER_TEMP_DIR_PREFIX).toFile()
tmpDirWhereStubsWillBeUnzipped.deleteOnExit()
log.info("Unpacking stub from JAR [URI: ${stubJarUri}]")
use(ZipCategory) {
new File(stubJarUri).unzipTo(tmpDirWhereStubsWillBeUnzipped)
}
return tmpDirWhereStubsWillBeUnzipped
}
private URI findGrabbedStubJars(boolean workOffline, String stubRepositoryRoot, String stubsGroup, String stubsModule, String classifier) {
Map depToGrab = [group: stubsGroup, module: stubsModule, classifier: classifier, version: LATEST_MODULE, transitive: false]
String accurestStubrunnerGrapePath = System.getProperty(STUB_RUNNER_GRAPE_CONFIG, getDefaultAccurestGrapeConfigPath())
initializeAccurestGrapeIfAbsent(accurestStubrunnerGrapePath)
String oldGrapeConfig = System.getProperty(GRAPE_CONFIG)
try {
System.setProperty(GRAPE_CONFIG, accurestStubrunnerGrapePath)
log.info("Setting default grapes path to [$accurestStubrunnerGrapePath]")
if (StringUtils.hasText(stubRepositoryRoot)) {
return buildResolver(workOffline).resolveDependency(stubRepositoryRoot, depToGrab)
}
log.warn("No repository passed so will try to resolve dependencies from local Maven Repo")
return buildResolver(true).resolveDependency(stubRepositoryRoot, depToGrab)
} finally {
restoreOldGrapeConfigIfApplicable(oldGrapeConfig)
}
}
private DependencyResolver buildResolver(boolean workOffline) {
return workOffline ? new LocalOnlyDependencyResolver() : new RemoteDependencyResolver()
}
private void initializeAccurestGrapeIfAbsent(String accurestGrapePath) {
File accurestGrape = new File(accurestGrapePath)
if (!accurestGrape.exists()) {
accurestGrape.parentFile.mkdirs()
accurestGrape.createNewFile()
accurestGrape.text = GrapeStubDownloader.class.getResource('/accurestStubrunnerGrapeConfig.xml').text
}
}
private void restoreOldGrapeConfigIfApplicable(String oldGrapeConfig) {
if (oldGrapeConfig) {
System.setProperty(GRAPE_CONFIG, oldGrapeConfig)
}
}
private String getDefaultAccurestGrapeConfigPath() {
return "${System.getProperty('user.home')}/.accurest/accurestStubrunnerGrapeConfig.xml"
}
/**
* Dependency resolver providing {@link URI} to remote dependencies.
*/
@Slf4j
private class RemoteDependencyResolver extends DependencyResolver {
URI resolveDependency(String stubRepositoryRoot, Map depToGrab) {
try {
return doResolveRemoteDependency(stubRepositoryRoot, depToGrab)
} catch (UnknownHostException e) {
failureHandler(stubRepositoryRoot, "unknown host error -> ${e.message}", e)
} catch (Exception e) {
failureHandler(stubRepositoryRoot, "connection error -> ${e.message}", e)
}
}
private URI doResolveRemoteDependency(String stubRepositoryRoot, Map depToGrab) {
Grape.addResolver(name: REPOSITORY_NAME, root: stubRepositoryRoot)
log.info("Resolving dependency ${depToGrab} location in remote repository...")
return resolveDependencyLocation(depToGrab)
}
private void failureHandler(String stubRepository, String reason, Exception cause) {
log.warn("Unable to resolve dependency in stub repository [$stubRepository]. Reason: [$reason]", cause)
}
}
/**
* Dependency resolver that first checks if a dependency is available in the local repository.
* If not, it will try to provide {@link URI} from the remote repository.
*
* @see RemoteDependencyResolver
*/
@Slf4j
private class LocalOnlyDependencyResolver extends DependencyResolver {
URI resolveDependency(String stubRepositoryRoot, Map depToGrab) {
try {
log.info("Resolving dependency ${depToGrab} location in local repository...")
return resolveDependencyLocation(depToGrab)
} catch (Exception e) { //Grape throws ordinary RuntimeException
log.warn("Unable to find dependency $depToGrab in local repository, trying $stubRepositoryRoot")
log.debug("Unable to find dependency $depToGrab in local repository: ${e.getClass()}: ${e.message}")
return null
}
}
}
/**
* Base class of dependency resolvers providing {@link URI} to required dependency.
*/
abstract class DependencyResolver {
/**
* Returns {@link URI} to a dependency.
*
* @param stubRepositoryRoot root of the repository where the dependency should be found
* @param depToGrab parameters describing dependency to search for
*
* @return {@link URI} to dependency
*/
abstract URI resolveDependency(String stubRepositoryRoot, Map depToGrab)
URI resolveDependencyLocation(Map depToGrab) {
return Grape.resolve([classLoader: new GroovyClassLoader()], depToGrab).first()
}
}
}