You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRepository.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package org.apache.archiva.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import com.cronutils.model.CronType;
  21. import com.cronutils.model.definition.CronDefinition;
  22. import com.cronutils.model.definition.CronDefinitionBuilder;
  23. import com.cronutils.parser.CronParser;
  24. import org.apache.archiva.common.utils.PathUtil;
  25. import org.apache.archiva.indexer.ArchivaIndexingContext;
  26. import org.apache.archiva.repository.features.RepositoryFeature;
  27. import org.apache.archiva.repository.features.StagingRepositoryFeature;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import java.io.IOException;
  32. import java.net.URI;
  33. import java.nio.file.Path;
  34. import java.util.ArrayList;
  35. import java.util.Collections;
  36. import java.util.HashMap;
  37. import java.util.HashSet;
  38. import java.util.List;
  39. import java.util.Locale;
  40. import java.util.Map;
  41. import java.util.Set;
  42. /**
  43. * Implementation of a repository with the necessary fields for a bare repository.
  44. * No features are provided. Capabilities and features must be implemented by concrete classes.
  45. *
  46. */
  47. public abstract class AbstractRepository implements EditableRepository, RepositoryEventListener
  48. {
  49. Logger log = LoggerFactory.getLogger(AbstractRepository.class);
  50. private final RepositoryType type;
  51. private final String id;
  52. private Map<Locale, String> names = new HashMap<>( );
  53. private Map<Locale, String> descriptions = new HashMap<>( );
  54. private Locale primaryLocale = new Locale("en_US");
  55. private URI location;
  56. private URI baseUri;
  57. private Set<URI> failoverLocations = new HashSet<>( );
  58. private Set<URI> uFailoverLocations = Collections.unmodifiableSet( failoverLocations );
  59. private boolean scanned = true;
  60. String schedulingDefinition = "0 0 02 * * ?";
  61. private String layout = "default";
  62. public static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
  63. private List<RepositoryEventListener> listeners = new ArrayList<>();
  64. Map<Class<? extends RepositoryFeature<?>>, RepositoryFeature<?>> featureMap = new HashMap<>( );
  65. protected Path repositoryBase;
  66. private ArchivaIndexingContext indexingContext;
  67. public AbstractRepository(RepositoryType type, String id, String name, Path repositoryBase) {
  68. this.id = id;
  69. this.names.put( primaryLocale, name);
  70. this.type = type;
  71. this.repositoryBase=repositoryBase;
  72. }
  73. public AbstractRepository(Locale primaryLocale, RepositoryType type, String id, String name, Path repositoryBase) {
  74. setPrimaryLocale( primaryLocale );
  75. this.id = id;
  76. this.names.put( primaryLocale, name);
  77. this.type = type;
  78. this.repositoryBase=repositoryBase;
  79. }
  80. protected void setPrimaryLocale(Locale locale) {
  81. this.primaryLocale = locale;
  82. }
  83. @Override
  84. public String getId( )
  85. {
  86. return id;
  87. }
  88. @Override
  89. public String getName( )
  90. {
  91. return getName( primaryLocale );
  92. }
  93. @Override
  94. public String getName( Locale locale )
  95. {
  96. return names.get(locale);
  97. }
  98. @Override
  99. public String getDescription( )
  100. {
  101. return getDescription( primaryLocale );
  102. }
  103. @Override
  104. public String getDescription( Locale locale )
  105. {
  106. return descriptions.get(primaryLocale);
  107. }
  108. @Override
  109. public RepositoryType getType( )
  110. {
  111. return type;
  112. }
  113. @Override
  114. public URI getLocation( )
  115. {
  116. return location;
  117. }
  118. @Override
  119. public Path getLocalPath() {
  120. Path localPath;
  121. if (StringUtils.isEmpty(getLocation().getScheme()) || "file".equals(getLocation().getScheme()) ) {
  122. localPath = PathUtil.getPathFromUri(getLocation());
  123. if (localPath.isAbsolute()) {
  124. return localPath;
  125. } else {
  126. return repositoryBase.resolve(localPath);
  127. }
  128. } else {
  129. return repositoryBase.resolve(getId());
  130. }
  131. }
  132. @Override
  133. public Set<URI> getFailoverLocations( )
  134. {
  135. return uFailoverLocations;
  136. }
  137. @Override
  138. public boolean isScanned( )
  139. {
  140. return scanned;
  141. }
  142. @Override
  143. public String getSchedulingDefinition( )
  144. {
  145. return schedulingDefinition;
  146. }
  147. @Override
  148. public abstract boolean hasIndex( );
  149. @Override
  150. public String getLayout( )
  151. {
  152. return layout;
  153. }
  154. @Override
  155. public abstract RepositoryCapabilities getCapabilities( );
  156. @Override
  157. public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature( Class<T> clazz ) throws UnsupportedFeatureException
  158. {
  159. if (featureMap.containsKey( clazz )) {
  160. return (RepositoryFeature<T>) featureMap.get(clazz);
  161. } else
  162. {
  163. throw new UnsupportedFeatureException( "Feature " + clazz + " not supported" );
  164. }
  165. }
  166. @Override
  167. public <T extends RepositoryFeature<T>> boolean supportsFeature( Class<T> clazz )
  168. {
  169. return featureMap.containsKey( clazz );
  170. }
  171. @Override
  172. public Locale getPrimaryLocale( )
  173. {
  174. return primaryLocale;
  175. }
  176. @Override
  177. public void setName( Locale locale, String name )
  178. {
  179. names.put(locale, name);
  180. }
  181. @Override
  182. public void setDescription( Locale locale, String description )
  183. {
  184. descriptions.put(locale, description);
  185. }
  186. @Override
  187. public void setLocation( URI location )
  188. {
  189. this.location = location;
  190. }
  191. @Override
  192. public void addFailoverLocation( URI location )
  193. {
  194. this.failoverLocations.add(location);
  195. }
  196. @Override
  197. public void removeFailoverLocation( URI location )
  198. {
  199. this.failoverLocations.remove( location );
  200. }
  201. @Override
  202. public void clearFailoverLocations( )
  203. {
  204. this.failoverLocations.clear();
  205. }
  206. @Override
  207. public void setScanned( boolean scanned )
  208. {
  209. this.scanned = scanned;
  210. }
  211. @Override
  212. public void setLayout( String layout )
  213. {
  214. this.layout = layout;
  215. }
  216. @Override
  217. public void setBaseUri(URI baseUri) {
  218. this.baseUri = baseUri;
  219. }
  220. @Override
  221. public void setSchedulingDefinition(String cronExpression) {
  222. CronParser parser = new CronParser(CRON_DEFINITION);
  223. parser.parse(cronExpression).validate();
  224. this.schedulingDefinition = cronExpression;
  225. }
  226. protected <T extends RepositoryFeature<T>> void addFeature(RepositoryFeature<T> feature) {
  227. featureMap.put( (Class<? extends RepositoryFeature<?>>) feature.getClass(), feature);
  228. }
  229. @Override
  230. public void setIndexingContext(ArchivaIndexingContext context) {
  231. this.indexingContext = context;
  232. }
  233. @Override
  234. public ArchivaIndexingContext getIndexingContext() {
  235. return indexingContext;
  236. }
  237. @Override
  238. public void close() {
  239. ArchivaIndexingContext ctx = getIndexingContext();
  240. if (ctx!=null) {
  241. try {
  242. ctx.close();
  243. } catch (IOException e) {
  244. log.warn("Error during index context close.",e);
  245. }
  246. }
  247. if (supportsFeature(StagingRepositoryFeature.class)) {
  248. StagingRepositoryFeature sf = getFeature(StagingRepositoryFeature.class).get();
  249. if (sf.getStagingRepository()!=null) {
  250. sf.getStagingRepository().close();
  251. }
  252. }
  253. clearListeners();
  254. }
  255. @Override
  256. public <T> void raise(RepositoryEvent<T> event) {
  257. for(RepositoryEventListener listener : listeners) {
  258. listener.raise(event);
  259. }
  260. }
  261. public void addListener(RepositoryEventListener listener) {
  262. if (!this.listeners.contains(listener)) {
  263. this.listeners.add(listener);
  264. }
  265. }
  266. public void removeListener(RepositoryEventListener listener) {
  267. this.removeListener(listener);
  268. }
  269. public void clearListeners() {
  270. this.listeners.clear();
  271. }
  272. }