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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.indexer.ArchivaIndexingContext;
  25. import org.apache.archiva.repository.storage.RepositoryStorage;
  26. import org.apache.archiva.repository.storage.StorageAsset;
  27. import org.apache.archiva.repository.features.RepositoryFeature;
  28. import org.apache.archiva.repository.features.StagingRepositoryFeature;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.io.OutputStream;
  35. import java.net.URI;
  36. import java.nio.channels.ReadableByteChannel;
  37. import java.nio.channels.WritableByteChannel;
  38. import java.nio.file.CopyOption;
  39. import java.nio.file.Path;
  40. import java.util.ArrayList;
  41. import java.util.Collections;
  42. import java.util.HashMap;
  43. import java.util.HashSet;
  44. import java.util.List;
  45. import java.util.Locale;
  46. import java.util.Map;
  47. import java.util.Set;
  48. import java.util.function.Consumer;
  49. /**
  50. * Implementation of a repository with the necessary fields for a bare repository.
  51. * No features are provided. Capabilities and features must be implemented by concrete classes.
  52. *
  53. */
  54. public abstract class AbstractRepository implements EditableRepository, RepositoryEventListener
  55. {
  56. Logger log = LoggerFactory.getLogger(AbstractRepository.class);
  57. private final RepositoryType type;
  58. private final String id;
  59. private Map<Locale, String> names = new HashMap<>( );
  60. private Map<Locale, String> descriptions = new HashMap<>( );
  61. private Locale primaryLocale = new Locale("en_US");
  62. protected URI location;
  63. private URI baseUri;
  64. private Set<URI> failoverLocations = new HashSet<>( );
  65. private Set<URI> uFailoverLocations = Collections.unmodifiableSet( failoverLocations );
  66. private boolean scanned = true;
  67. String schedulingDefinition = "0 0 02 * * ?";
  68. private String layout = "default";
  69. public static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
  70. private List<RepositoryEventListener> listeners = new ArrayList<>();
  71. Map<Class<? extends RepositoryFeature<?>>, RepositoryFeature<?>> featureMap = new HashMap<>( );
  72. private ArchivaIndexingContext indexingContext;
  73. private RepositoryStorage storage;
  74. public AbstractRepository(RepositoryType type, String id, String name, RepositoryStorage repositoryStorage) {
  75. this.id = id;
  76. this.names.put( primaryLocale, name);
  77. this.type = type;
  78. this.storage = repositoryStorage;
  79. this.location = repositoryStorage.getLocation();
  80. }
  81. public AbstractRepository(Locale primaryLocale, RepositoryType type, String id, String name, RepositoryStorage repositoryStorage) {
  82. setPrimaryLocale( primaryLocale );
  83. this.id = id;
  84. this.names.put( primaryLocale, name);
  85. this.type = type;
  86. this.storage = repositoryStorage;
  87. this.location = repositoryStorage.getLocation();
  88. }
  89. protected void setPrimaryLocale(Locale locale) {
  90. this.primaryLocale = locale;
  91. }
  92. @Override
  93. public String getId( )
  94. {
  95. return id;
  96. }
  97. @Override
  98. public String getName( )
  99. {
  100. return getName( primaryLocale );
  101. }
  102. @Override
  103. public String getName( Locale locale )
  104. {
  105. return names.get(locale);
  106. }
  107. @Override
  108. public String getDescription( )
  109. {
  110. return getDescription( primaryLocale );
  111. }
  112. @Override
  113. public String getDescription( Locale locale )
  114. {
  115. return descriptions.get(primaryLocale);
  116. }
  117. @Override
  118. public RepositoryType getType( )
  119. {
  120. return type;
  121. }
  122. @Override
  123. public URI getLocation( )
  124. {
  125. return location;
  126. }
  127. @Override
  128. public Path getLocalPath() {
  129. return storage.getAsset("").getFilePath();
  130. // Path localPath;
  131. // if (StringUtils.isEmpty(getLocation().getScheme()) || "file".equals(getLocation().getScheme()) ) {
  132. // localPath = PathUtil.getPathFromUri(getLocation());
  133. // if (localPath.isAbsolute()) {
  134. // return localPath;
  135. // } else {
  136. // return repositoryBase.resolve(localPath);
  137. // }
  138. // } else {
  139. // return repositoryBase.resolve(getId());
  140. // }
  141. }
  142. @Override
  143. public Set<URI> getFailoverLocations( )
  144. {
  145. return uFailoverLocations;
  146. }
  147. @Override
  148. public boolean isScanned( )
  149. {
  150. return scanned;
  151. }
  152. @Override
  153. public String getSchedulingDefinition( )
  154. {
  155. return schedulingDefinition;
  156. }
  157. @Override
  158. public abstract boolean hasIndex( );
  159. @Override
  160. public String getLayout( )
  161. {
  162. return layout;
  163. }
  164. @Override
  165. public abstract RepositoryCapabilities getCapabilities( );
  166. @SuppressWarnings( "unchecked" )
  167. @Override
  168. public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature( Class<T> clazz ) throws UnsupportedFeatureException
  169. {
  170. if (featureMap.containsKey( clazz )) {
  171. return (RepositoryFeature<T>) featureMap.get(clazz);
  172. } else
  173. {
  174. throw new UnsupportedFeatureException( "Feature " + clazz + " not supported" );
  175. }
  176. }
  177. @Override
  178. public <T extends RepositoryFeature<T>> boolean supportsFeature( Class<T> clazz )
  179. {
  180. return featureMap.containsKey( clazz );
  181. }
  182. @Override
  183. public Locale getPrimaryLocale( )
  184. {
  185. return primaryLocale;
  186. }
  187. @Override
  188. public void setName( Locale locale, String name )
  189. {
  190. names.put(locale, name);
  191. }
  192. @Override
  193. public void setDescription( Locale locale, String description )
  194. {
  195. descriptions.put(locale, description);
  196. }
  197. @Override
  198. public void setLocation( final URI location )
  199. {
  200. if (location!=null && ( this.location == null || !this.location.equals(location))) {
  201. try {
  202. updateLocation(location);
  203. } catch (IOException e) {
  204. log.error("Could not update location of repository {} to {}", getId(), location, e);
  205. }
  206. }
  207. }
  208. @Override
  209. public void updateLocation(URI newLocation) throws IOException {
  210. storage.updateLocation(newLocation);
  211. this.location = newLocation;
  212. }
  213. @Override
  214. public void addFailoverLocation( URI location )
  215. {
  216. this.failoverLocations.add(location);
  217. }
  218. @Override
  219. public void removeFailoverLocation( URI location )
  220. {
  221. this.failoverLocations.remove( location );
  222. }
  223. @Override
  224. public void clearFailoverLocations( )
  225. {
  226. this.failoverLocations.clear();
  227. }
  228. @Override
  229. public void setScanned( boolean scanned )
  230. {
  231. this.scanned = scanned;
  232. }
  233. @Override
  234. public void setLayout( String layout )
  235. {
  236. this.layout = layout;
  237. }
  238. @Override
  239. public void setBaseUri(URI baseUri) {
  240. this.baseUri = baseUri;
  241. }
  242. @Override
  243. public void setSchedulingDefinition(String cronExpression) {
  244. if (StringUtils.isNotEmpty( cronExpression ))
  245. {
  246. CronParser parser = new CronParser( CRON_DEFINITION );
  247. parser.parse( cronExpression ).validate( );
  248. }
  249. this.schedulingDefinition = cronExpression;
  250. }
  251. @SuppressWarnings( "unchecked" )
  252. protected <T extends RepositoryFeature<T>> void addFeature(RepositoryFeature<T> feature) {
  253. featureMap.put( (Class<? extends RepositoryFeature<?>>) feature.getClass(), feature);
  254. }
  255. @Override
  256. public void setIndexingContext(ArchivaIndexingContext context) {
  257. this.indexingContext = context;
  258. }
  259. @Override
  260. public ArchivaIndexingContext getIndexingContext() {
  261. return indexingContext;
  262. }
  263. @Override
  264. public void close() {
  265. ArchivaIndexingContext ctx = getIndexingContext();
  266. if (ctx!=null) {
  267. try {
  268. ctx.close();
  269. } catch (IOException e) {
  270. log.warn("Error during index context close.",e);
  271. }
  272. }
  273. if (supportsFeature(StagingRepositoryFeature.class)) {
  274. StagingRepositoryFeature sf = getFeature(StagingRepositoryFeature.class).get();
  275. if (sf.getStagingRepository()!=null) {
  276. sf.getStagingRepository().close();
  277. }
  278. }
  279. clearListeners();
  280. }
  281. @Override
  282. public <T> void raise(RepositoryEvent<T> event) {
  283. for(RepositoryEventListener listener : listeners) {
  284. listener.raise(event);
  285. }
  286. }
  287. public void addListener(RepositoryEventListener listener) {
  288. if (!this.listeners.contains(listener)) {
  289. this.listeners.add(listener);
  290. }
  291. }
  292. public void removeListener(RepositoryEventListener listener) {
  293. this.removeListener(listener);
  294. }
  295. public void clearListeners() {
  296. this.listeners.clear();
  297. }
  298. @Override
  299. public StorageAsset getAsset(String path )
  300. {
  301. return storage.getAsset(path);
  302. }
  303. @Override
  304. public StorageAsset addAsset( String path, boolean container )
  305. {
  306. return storage.addAsset(path, container);
  307. }
  308. @Override
  309. public void removeAsset( StorageAsset asset ) throws IOException
  310. {
  311. storage.removeAsset(asset);
  312. }
  313. @Override
  314. public StorageAsset moveAsset( StorageAsset origin, String destination, CopyOption... copyOptions ) throws IOException
  315. {
  316. return storage.moveAsset(origin, destination);
  317. }
  318. @Override
  319. public void moveAsset( StorageAsset origin, StorageAsset destination, CopyOption... copyOptions ) throws IOException
  320. {
  321. storage.moveAsset( origin, destination, copyOptions );
  322. }
  323. @Override
  324. public StorageAsset copyAsset( StorageAsset origin, String destination, CopyOption... copyOptions ) throws IOException
  325. {
  326. return storage.copyAsset(origin, destination);
  327. }
  328. @Override
  329. public void copyAsset( StorageAsset origin, StorageAsset destination, CopyOption... copyOptions ) throws IOException
  330. {
  331. storage.copyAsset( origin, destination, copyOptions);
  332. }
  333. @Override
  334. public void consumeData(StorageAsset asset, Consumer<InputStream> consumerFunction, boolean readLock ) throws IOException
  335. {
  336. storage.consumeData(asset, consumerFunction, readLock);
  337. }
  338. @Override
  339. public void consumeDataFromChannel( StorageAsset asset, Consumer<ReadableByteChannel> consumerFunction, boolean readLock ) throws IOException
  340. {
  341. storage.consumeDataFromChannel( asset, consumerFunction, readLock );
  342. }
  343. @Override
  344. public void writeData( StorageAsset asset, Consumer<OutputStream> consumerFunction, boolean writeLock ) throws IOException
  345. {
  346. storage.writeData( asset, consumerFunction, writeLock );
  347. }
  348. @Override
  349. public void writeDataToChannel( StorageAsset asset, Consumer<WritableByteChannel> consumerFunction, boolean writeLock ) throws IOException
  350. {
  351. storage.writeDataToChannel( asset, consumerFunction, writeLock );
  352. }
  353. protected void setStorage( RepositoryStorage storage) {
  354. this.storage = storage;
  355. }
  356. protected RepositoryStorage getStorage() {
  357. return storage;
  358. }
  359. }