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