This project has retired. For details please refer to its Attic page.
AbstractRepositoryGroup xref
View Javadoc
1   package org.apache.archiva.repository.base;
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.archiva.repository.EditableRepositoryGroup;
23  import org.apache.archiva.repository.ManagedRepository;
24  import org.apache.archiva.repository.RepositoryCapabilities;
25  import org.apache.archiva.repository.RepositoryType;
26  import org.apache.archiva.repository.storage.RepositoryStorage;
27  import org.apache.commons.collections4.map.ListOrderedMap;
28  
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.concurrent.locks.ReadWriteLock;
32  import java.util.concurrent.locks.ReentrantReadWriteLock;
33  
34  /**
35   * Abstract repository group implementation.
36   *
37   */
38  public class AbstractRepositoryGroup extends AbstractRepository implements EditableRepositoryGroup
39  {
40  
41      private ListOrderedMap<String, ManagedRepository> repositories = new ListOrderedMap<>();
42  
43      private int mergedIndexTTL;
44  
45      private final ReadWriteLock rwl = new ReentrantReadWriteLock();
46  
47      private RepositoryCapabilities capabilities;
48  
49      public AbstractRepositoryGroup( RepositoryType type, String id, String name, RepositoryStorage storage) {
50          super(type, id, name, storage);
51      }
52  
53      public AbstractRepositoryGroup(Locale primaryLocale, RepositoryType type, String id, String name, RepositoryStorage storage) {
54          super(primaryLocale, type, id, name, storage);
55      }
56  
57      @Override
58      public boolean hasIndex() {
59          return true;
60      }
61  
62      @Override
63      public RepositoryCapabilities getCapabilities() {
64          return capabilities;
65      }
66  
67  
68      @Override
69      public void clearRepositories() {
70          rwl.writeLock().lock();
71          try {
72              repositories.clear();
73          } finally {
74              rwl.writeLock().unlock();
75          }
76      }
77  
78      @Override
79      public void setRepositories(List<ManagedRepository> newRepositories) {
80          rwl.writeLock().lock();
81          try {
82              repositories.clear();
83              for(ManagedRepository repo : newRepositories) {
84                  if (repo!=null)
85                      repositories.put(repo.getId(), repo);
86              }
87          } finally {
88              rwl.writeLock().unlock();
89          }
90      }
91  
92      @Override
93      public void addRepository(ManagedRepository repository) {
94          rwl.writeLock().lock();
95          try {
96              if (repository!=null)
97                  repositories.put(repository.getId(), repository);
98          } finally {
99              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 }