001package org.apache.archiva.repository.base; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.repository.EditableRepositoryGroup; 023import org.apache.archiva.repository.ManagedRepository; 024import org.apache.archiva.repository.RepositoryCapabilities; 025import org.apache.archiva.repository.RepositoryType; 026import org.apache.archiva.repository.storage.RepositoryStorage; 027import org.apache.commons.collections4.map.ListOrderedMap; 028 029import java.util.List; 030import java.util.Locale; 031import java.util.concurrent.locks.ReadWriteLock; 032import java.util.concurrent.locks.ReentrantReadWriteLock; 033 034/** 035 * Abstract repository group implementation. 036 * 037 */ 038public class AbstractRepositoryGroup extends AbstractRepository implements EditableRepositoryGroup 039{ 040 041 private ListOrderedMap<String, ManagedRepository> repositories = new ListOrderedMap<>(); 042 043 private int mergedIndexTTL; 044 045 private final ReadWriteLock rwl = new ReentrantReadWriteLock(); 046 047 private RepositoryCapabilities capabilities; 048 049 public AbstractRepositoryGroup( RepositoryType type, String id, String name, RepositoryStorage storage) { 050 super(type, id, name, storage); 051 } 052 053 public AbstractRepositoryGroup(Locale primaryLocale, RepositoryType type, String id, String name, RepositoryStorage storage) { 054 super(primaryLocale, type, id, name, storage); 055 } 056 057 @Override 058 public boolean hasIndex() { 059 return true; 060 } 061 062 @Override 063 public RepositoryCapabilities getCapabilities() { 064 return capabilities; 065 } 066 067 068 @Override 069 public void clearRepositories() { 070 rwl.writeLock().lock(); 071 try { 072 repositories.clear(); 073 } finally { 074 rwl.writeLock().unlock(); 075 } 076 } 077 078 @Override 079 public void setRepositories(List<ManagedRepository> newRepositories) { 080 rwl.writeLock().lock(); 081 try { 082 repositories.clear(); 083 for(ManagedRepository repo : newRepositories) { 084 if (repo!=null) 085 repositories.put(repo.getId(), repo); 086 } 087 } finally { 088 rwl.writeLock().unlock(); 089 } 090 } 091 092 @Override 093 public void addRepository(ManagedRepository repository) { 094 rwl.writeLock().lock(); 095 try { 096 if (repository!=null) 097 repositories.put(repository.getId(), repository); 098 } finally { 099 rwl.writeLock().unlock(); 100 } 101 } 102 103 @Override 104 public void addRepository(int index, ManagedRepository repository) { 105 rwl.writeLock().lock(); 106 try { 107 if (repository!=null) 108 repositories.put(index, repository.getId(), repository); 109 } finally { 110 rwl.writeLock().unlock(); 111 } 112 } 113 114 @Override 115 public boolean removeRepository(ManagedRepository repository) { 116 rwl.writeLock().lock(); 117 try { 118 return repositories.remove(repository.getId(), repository); 119 } finally { 120 rwl.writeLock().unlock(); 121 } 122 } 123 124 @Override 125 public ManagedRepository removeRepository(String repoId) { 126 rwl.writeLock().lock(); 127 try { 128 return repositories.remove(repoId); 129 } finally { 130 rwl.writeLock().unlock(); 131 } 132 } 133 134 @Override 135 public void setMergedIndexTTL(int timeInSeconds) { 136 this.mergedIndexTTL = timeInSeconds; 137 } 138 139 @Override 140 public List<ManagedRepository> getRepositories() { 141 rwl.readLock().lock(); 142 try { 143 return repositories.valueList(); 144 } finally { 145 rwl.readLock().unlock(); 146 } 147 } 148 149 @Override 150 public boolean contains(ManagedRepository repository) { 151 rwl.readLock().lock(); 152 try { 153 return repositories.containsValue(repository); 154 } finally { 155 rwl.readLock().unlock(); 156 } 157 } 158 159 @Override 160 public boolean contains(String id) { 161 rwl.readLock().lock(); 162 try { 163 return repositories.containsKey(id); 164 } finally { 165 rwl.readLock().unlock(); 166 } 167 } 168 169 @Override 170 public int getMergedIndexTTL() { 171 return mergedIndexTTL; 172 } 173 174 protected void setCapabilities(RepositoryCapabilities capabilities) { 175 this.capabilities = capabilities; 176 } 177}