View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.examples.manual;
20  
21  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
22  import org.eclipse.aether.RepositorySystem;
23  import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
24  import org.eclipse.aether.impl.DefaultServiceLocator;
25  import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
26  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
27  import org.eclipse.aether.transport.file.FileTransporterFactory;
28  import org.eclipse.aether.transport.http.HttpTransporterFactory;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * A factory for repository system instances that employs Maven Artifact Resolver's built-in service locator
34   * infrastructure to wire up the system's components.
35   */
36  public class ManualRepositorySystemFactory {
37      private static final Logger LOGGER = LoggerFactory.getLogger(ManualRepositorySystemFactory.class);
38  
39      public static RepositorySystem newRepositorySystem() {
40          /*
41           * Aether's components implement org.eclipse.aether.spi.locator.Service to ease manual wiring and using the
42           * prepopulated DefaultServiceLocator, we only need to register the repository connector and transporter
43           * factories.
44           */
45          DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
46          locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
47          locator.addService(TransporterFactory.class, FileTransporterFactory.class);
48          locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
49  
50          locator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
51              @Override
52              public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {
53                  LOGGER.error("Service creation failed for {} with implementation {}", type, impl, exception);
54              }
55          });
56  
57          return locator.getService(RepositorySystem.class);
58      }
59  }