View Javadoc
1   /*
2    * Copyright 2013-2020 the original author or authors.
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    *      https://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  package org.springframework.cloud.contract.maven.verifier.stubrunner;
18  
19  import javax.inject.Inject;
20  import javax.inject.Named;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.eclipse.aether.RepositorySystemSession;
25  
26  import org.springframework.cloud.contract.stubrunner.BatchStubRunner;
27  import org.springframework.cloud.contract.stubrunner.BatchStubRunnerFactory;
28  import org.springframework.cloud.contract.stubrunner.RunningStubs;
29  import org.springframework.cloud.contract.stubrunner.StubDownloader;
30  import org.springframework.cloud.contract.stubrunner.StubRunnerOptions;
31  
32  /**
33   * Fetches stubs from a remote location.
34   *
35   * @author Mariusz Smykula
36   */
37  @Named
38  public class RemoteStubRunner {
39  
40  	private static final Log log = LogFactory.getLog(RemoteStubRunner.class);
41  
42  	private final AetherStubDownloaderFactory aetherStubDownloaderFactory;
43  
44  	@Inject
45  	public RemoteStubRunner(AetherStubDownloaderFactory aetherStubDownloaderFactory) {
46  		this.aetherStubDownloaderFactory = aetherStubDownloaderFactory;
47  	}
48  
49  	public BatchStubRunner run(StubRunnerOptions options, RepositorySystemSession repositorySystemSession) {
50  		StubDownloader stubDownloader = this.aetherStubDownloaderFactory.build(repositorySystemSession).build(options);
51  		try {
52  			if (log.isDebugEnabled()) {
53  				log.debug("Launching StubRunner with args: " + options);
54  			}
55  			BatchStubRunner stubRunner = new BatchStubRunnerFactory(options, stubDownloader).buildBatchStubRunner();
56  			RunningStubs runningCollaborators = stubRunner.runStubs();
57  			log.info(runningCollaborators.toString());
58  			return stubRunner;
59  		}
60  		catch (Exception e) {
61  			log.error("An exception occurred while trying to execute the stubs: " + e.getMessage());
62  			throw e;
63  		}
64  
65  	}
66  
67  }