1 package org.apache.archiva.repository;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
25 import org.apache.archiva.repository.features.StagingRepositoryFeature;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Service;
30 import javax.annotation.PostConstruct;
31 import javax.inject.Inject;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.LinkedHashMap;
36 import java.util.List;
40 * Registry for repositories
42 @Service("repositoryRegistry")
43 public class RepositoryRegistry
46 private static final Logger log = LoggerFactory.getLogger( RepositoryRegistry.class );
49 * We inject all repository providers
52 List<RepositoryProvider> repositoryProviders;
55 ArchivaConfiguration archivaConfiguration;
58 RepositoryContentFactory repositoryContentFactory;
60 private Map<String, ManagedRepository> managedRepositories = new HashMap<>( );
61 private Map<String, RemoteRepository> remoteRepositories = new HashMap<>( );
64 private void initialize() {
65 managedRepositories = getManagedRepositoriesFromConfig();
66 remoteRepositories = getRemoteRepositoriesFromConfig();
69 private Map<RepositoryType, RepositoryProvider> getProviderMap() {
70 Map<RepositoryType, RepositoryProvider> map = new HashMap<>( );
71 if (repositoryProviders!=null) {
72 for(RepositoryProvider provider : repositoryProviders) {
73 for (RepositoryType type : provider.provides()) {
74 map.put(type, provider);
81 private Map<String,ManagedRepository> getManagedRepositoriesFromConfig()
83 List<ManagedRepositoryConfiguration> managedRepoConfigs =
84 getArchivaConfiguration().getConfiguration().getManagedRepositories();
86 if ( managedRepoConfigs == null )
88 return Collections.emptyMap();
91 Map<String,ManagedRepository> managedRepos = new LinkedHashMap<>( managedRepoConfigs.size() );
93 Map<RepositoryType, RepositoryProvider> providerMap = getProviderMap( );
94 for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
96 RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
97 if (providerMap.containsKey( repositoryType )) {
100 ManagedRepository repo = createNewManagedRepository( providerMap.get( repositoryType ), repoConfig );
101 managedRepos.put(repo.getId(), repo);
102 } catch (Exception e) {
103 log.error("Could not create managed repository "+repoConfig.getId(), e);
111 private ManagedRepository createNewManagedRepository(RepositoryProvider provider, ManagedRepositoryConfiguration cfg) throws RepositoryException
113 ManagedRepository repo = provider.createManagedInstance( cfg );
114 if (repo.supportsFeature( StagingRepositoryFeature.class )) {
115 StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get();
116 if (feature.isStageRepoNeeded()) {
117 ManagedRepository stageRepo = getManagedRepository( repo.getId()+StagingRepositoryFeature.STAGING_REPO_POSTFIX );
118 feature.setStagingRepository( stageRepo );
121 if (repo instanceof EditableManagedRepository)
123 ((EditableManagedRepository)repo).setContent( repositoryContentFactory.getManagedRepositoryContent( repo.getId( ) ) );
129 private Map<String,RemoteRepository> getRemoteRepositoriesFromConfig() {
130 List<RemoteRepositoryConfiguration> remoteRepoConfigs =
131 getArchivaConfiguration().getConfiguration().getRemoteRepositories();
133 if ( remoteRepoConfigs == null )
135 return Collections.emptyMap();
138 Map<String,RemoteRepository> remoteRepos = new LinkedHashMap<>( remoteRepoConfigs.size() );
140 Map<RepositoryType, RepositoryProvider> providerMap = getProviderMap( );
141 for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
143 RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
144 if (providerMap.containsKey( repositoryType )) {
145 remoteRepos.put(repoConfig.getId(), providerMap.get(repositoryType).createRemoteInstance( repoConfig ));
152 private ArchivaConfiguration getArchivaConfiguration() {
153 return this.archivaConfiguration;
156 public List<Repository> getRepositories() {
157 ArrayList<Repository> li = new ArrayList<>( );
158 li.addAll(managedRepositories.values());
159 li.addAll(remoteRepositories.values());
160 return Collections.unmodifiableList( li );
163 public List<ManagedRepository> getManagedRepositories() {
164 return Collections.unmodifiableList( new ArrayList(managedRepositories.values()) );
167 public List<RemoteRepository> getRemoteRepositories() {
168 return Collections.unmodifiableList( new ArrayList(remoteRepositories.values()) );
171 public Repository getRepository(String repoId) {
175 public ManagedRepository getManagedRepository(String repoId) {
179 public RemoteRepository getRemoteRepository(String repoId) {