diff --git a/gradle.properties b/gradle.properties index 89ebc51c18..d4ffd5d33d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 \ No newline at end of file +camelVersion=2.17.0 +aetherVersion=1.1.0 \ No newline at end of file diff --git a/stub-runner/stub-runner-boot/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/streamService/maven-metadata.xml b/stub-runner/stub-runner-boot/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/streamService/maven-metadata.xml new file mode 100644 index 0000000000..dc32790f53 --- /dev/null +++ b/stub-runner/stub-runner-boot/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/streamService/maven-metadata.xml @@ -0,0 +1,12 @@ + + + io.codearte.accurest.stubs + streamService + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062112 + + diff --git a/stub-runner/stub-runner-junit/README.adoc b/stub-runner/stub-runner-junit/README.adoc index 4b96939426..d55abedcef 100644 --- a/stub-runner/stub-runner-junit/README.adoc +++ b/stub-runner/stub-runner-junit/README.adoc @@ -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: diff --git a/stub-runner/stub-runner-junit/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/fraudDetectionServer/maven-metadata.xml b/stub-runner/stub-runner-junit/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/fraudDetectionServer/maven-metadata.xml new file mode 100644 index 0000000000..d4ab9afc83 --- /dev/null +++ b/stub-runner/stub-runner-junit/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/fraudDetectionServer/maven-metadata.xml @@ -0,0 +1,12 @@ + + + io.codearte.accurest.stubs + fraudDetectionServer + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062112 + + diff --git a/stub-runner/stub-runner-junit/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/loanIssuance/maven-metadata.xml b/stub-runner/stub-runner-junit/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/loanIssuance/maven-metadata.xml new file mode 100644 index 0000000000..bf14a1ad21 --- /dev/null +++ b/stub-runner/stub-runner-junit/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/loanIssuance/maven-metadata.xml @@ -0,0 +1,12 @@ + + + io.codearte.accurest.stubs + loanIssuance + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062111 + + diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/camelService/maven-metadata.xml b/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/camelService/maven-metadata.xml new file mode 100644 index 0000000000..e71db595a7 --- /dev/null +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/camelService/maven-metadata.xml @@ -0,0 +1,12 @@ + + + io.codearte.accurest.stubs + camelService + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062112 + + diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/integrationService/maven-metadata.xml b/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/integrationService/maven-metadata.xml new file mode 100644 index 0000000000..b93b655f1f --- /dev/null +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/integrationService/maven-metadata.xml @@ -0,0 +1,12 @@ + + + io.codearte.accurest.stubs + integrationService + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062112 + + diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/streamService/maven-metadata.xml b/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/streamService/maven-metadata.xml new file mode 100644 index 0000000000..dc32790f53 --- /dev/null +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/resources/m2repo/repository/io/codearte/accurest/stubs/streamService/maven-metadata.xml @@ -0,0 +1,12 @@ + + + io.codearte.accurest.stubs + streamService + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062112 + + diff --git a/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java b/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java index 44f959d210..973a4468ef 100644 --- a/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java +++ b/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java @@ -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 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(); diff --git a/stub-runner/stub-runner/build.gradle b/stub-runner/stub-runner/build.gradle index 158628c06b..6fa7593cd3 100644 --- a/stub-runner/stub-runner/build.gradle +++ b/stub-runner/stub-runner/build.gradle @@ -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' diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AetherStubDownloader.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AetherStubDownloader.groovy new file mode 100644 index 0000000000..03c2880ca2 --- /dev/null +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AetherStubDownloader.groovy @@ -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 remoteRepos + private final RepositorySystem repositorySystem + private final RepositorySystemSession session + + AetherStubDownloader(RepositorySystem repositorySystem, List 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 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 + } + +} \ No newline at end of file diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy index db17ee7baa..36251d4524 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy @@ -55,7 +55,7 @@ class AvailablePortScanner { try { socket = new ServerSocket(portToScan) } finally { - socket.close() + socket?.close() } } diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerFactory.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerFactory.groovy index 9b1a03929d..6563028f91 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerFactory.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerFactory.groovy @@ -19,23 +19,23 @@ class BatchStubRunnerFactory { private final AccurestMessaging accurestMessaging BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection dependencies) { - this(stubRunnerOptions, dependencies, new GrapeStubDownloader(), new NoOpAccurestMessaging()) + this(stubRunnerOptions, dependencies, new AetherStubDownloader(stubRunnerOptions), new NoOpAccurestMessaging()) } BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection dependencies, AccurestMessaging accurestMessaging) { - this(stubRunnerOptions, dependencies, new GrapeStubDownloader(), accurestMessaging) + this(stubRunnerOptions, dependencies, new AetherStubDownloader(stubRunnerOptions), accurestMessaging) } BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection dependencies, - StubDownloader stubDownloader) { + StubDownloader stubDownloader) { this(stubRunnerOptions, dependencies, stubDownloader, new NoOpAccurestMessaging()) } BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, - Collection dependencies, - StubDownloader stubDownloader, + Collection dependencies, + StubDownloader stubDownloader, AccurestMessaging accurestMessaging) { this.stubRunnerOptions = stubRunnerOptions this.dependencies = dependencies diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/GrapeStubDownloader.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/GrapeStubDownloader.groovy deleted file mode 100644 index 638cadef84..0000000000 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/GrapeStubDownloader.groovy +++ /dev/null @@ -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() - } - - } - -}