1package org.apache.archiva.repository.maven2;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.dependency.tree.maven2.ArchivaRepositoryConnectorFactory;
23import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
24import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
25import org.apache.maven.repository.internal.DefaultVersionResolver;
26import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
27import org.eclipse.aether.DefaultRepositorySystemSession;
28import org.eclipse.aether.RepositorySystem;
29import org.eclipse.aether.RepositorySystemSession;
30import org.eclipse.aether.collection.DependencySelector;
31import org.eclipse.aether.impl.ArtifactDescriptorReader;
32import org.eclipse.aether.impl.DefaultServiceLocator;
33import org.eclipse.aether.impl.VersionRangeResolver;
34import org.eclipse.aether.impl.VersionResolver;
35import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
36import org.eclipse.aether.repository.LocalRepository;
37import org.eclipse.aether.repository.LocalRepositoryManager;
38import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
39import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
40import org.eclipse.aether.util.graph.selector.AndDependencySelector;
41import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44import org.springframework.stereotype.Service;
4546import javax.annotation.PostConstruct;
4748/**49 * Some static utility methods that are used by different classes.50 */51 @Service("mavenSystemManager")
52publicclassMavenSystemManager {
5354static Logger log = LoggerFactory.getLogger(MavenSystemManager.class);
5556private DefaultServiceLocator locator;
57private RepositorySystem system;
5859 @PostConstruct
60privatesynchronizedvoid init() {
61 locator = newLocator();
62 system = newRepositorySystem(locator);
6364 }
6566/**67 * Creates a new aether repository system session for the given directory and assigns the68 * repository to this session.69 *70 * @param localRepoDir The repository directory71 * @return The newly created session object.72 */73publicstatic RepositorySystemSession newRepositorySystemSession(String localRepoDir) {
74 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
7576 LocalRepository repo = new LocalRepository(localRepoDir);
7778 DependencySelector depFilter = new AndDependencySelector(new ExclusionDependencySelector());
79 session.setDependencySelector(depFilter);
80 SimpleLocalRepositoryManagerFactory repFactory = new SimpleLocalRepositoryManagerFactory();
81try {
82 LocalRepositoryManager manager = repFactory.newInstance(session, repo);
83 session.setLocalRepositoryManager(manager);
84 } catch (NoLocalRepositoryManagerException e) {
85 log.error("Could not assign the repository manager to the session: {}", e.getMessage(), e);
86 }
8788return session;
89 }
9091public RepositorySystem getRepositorySystem() {
92return system;
93 }
9495public DefaultServiceLocator getLocator() {
96return locator;
97 }
9899/**100 * Finds the101 *102 * @return103 */104publicstatic RepositorySystem newRepositorySystem(DefaultServiceLocator locator) {
105return locator.getService(RepositorySystem.class);
106 }
107108publicstatic DefaultServiceLocator newLocator() {
109 DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
110111 locator.addService(RepositoryConnectorFactory.class,
112 ArchivaRepositoryConnectorFactory.class);// FileRepositoryConnectorFactory.class );113 locator.addService(VersionResolver.class, DefaultVersionResolver.class);
114 locator.addService(VersionRangeResolver.class, DefaultVersionRangeResolver.class);
115 locator.addService(ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class);
116117return locator;
118 }
119 }