1 package org.apache.archiva.repository.maven2;
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.common.filelock.DefaultFileLockManager;
23 import org.apache.archiva.common.filelock.FileLockManager;
24 import org.apache.archiva.common.utils.PathUtil;
25 import org.apache.archiva.repository.*;
26 import org.apache.archiva.repository.content.FilesystemStorage;
27 import org.apache.archiva.repository.content.RepositoryStorage;
28 import org.apache.archiva.repository.content.maven2.MavenRepositoryRequestInfo;
29 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
30 import org.apache.archiva.repository.features.IndexCreationFeature;
31 import org.apache.archiva.repository.features.RepositoryFeature;
32 import org.apache.archiva.repository.features.StagingRepositoryFeature;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.util.Locale;
41 import java.util.function.Function;
44 * Maven2 managed repository implementation.
46 public class MavenManagedRepository extends AbstractManagedRepository
49 private static final Logger log = LoggerFactory.getLogger( MavenManagedRepository.class );
51 public static final String DEFAULT_LAYOUT = "default";
52 public static final String LEGACY_LAYOUT = "legacy";
53 private ArtifactCleanupFeature artifactCleanupFeature = new ArtifactCleanupFeature( );
54 private IndexCreationFeature indexCreationFeature;
55 private StagingRepositoryFeature stagingRepositoryFeature = new StagingRepositoryFeature( );
59 private static final RepositoryCapabilities CAPABILITIES = new StandardCapabilities(
60 new ReleaseScheme[] { ReleaseScheme.RELEASE, ReleaseScheme.SNAPSHOT },
61 new String[] { DEFAULT_LAYOUT, LEGACY_LAYOUT},
63 new String[] {ArtifactCleanupFeature.class.getName(), IndexCreationFeature.class.getName(),
64 StagingRepositoryFeature.class.getName()},
72 public MavenManagedRepository(String id, String name, FilesystemStorage storage)
75 super( RepositoryType.MAVEN, id, name, storage);
76 this.indexCreationFeature = new IndexCreationFeature(this, this);
77 setLocation(storage.getAsset("").getFilePath().toUri());
80 public MavenManagedRepository( Locale primaryLocale, String id, String name, FilesystemStorage storage )
82 super( primaryLocale, RepositoryType.MAVEN, id, name, storage );
83 setLocation(storage.getAsset("").getFilePath().toUri());
87 public RepositoryCapabilities getCapabilities( )
93 @SuppressWarnings( "unchecked" )
95 public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature( Class<T> clazz ) throws UnsupportedFeatureException
97 if (ArtifactCleanupFeature.class.equals( clazz ))
99 return (RepositoryFeature<T>) artifactCleanupFeature;
100 } else if (IndexCreationFeature.class.equals(clazz)) {
101 return (RepositoryFeature<T>) indexCreationFeature;
102 } else if (StagingRepositoryFeature.class.equals(clazz)) {
103 return (RepositoryFeature<T>) stagingRepositoryFeature;
105 throw new UnsupportedFeatureException( );
110 public <T extends RepositoryFeature<T>> boolean supportsFeature( Class<T> clazz )
112 if (ArtifactCleanupFeature.class.equals(clazz) ||
113 IndexCreationFeature.class.equals(clazz) ||
114 StagingRepositoryFeature.class.equals(clazz)) {
121 public boolean hasIndex( )
123 return indexCreationFeature.hasIndex();
127 public void setLocation( URI location )
129 URI previousLocation = super.getLocation();
130 Path previousLoc = PathUtil.getPathFromUri(previousLocation);
131 Path newLoc = PathUtil.getPathFromUri( location );
132 if (!newLoc.toAbsolutePath().equals(previousLoc.toAbsolutePath())) {
133 super.setLocation(location);
134 if (!Files.exists(newLoc)) {
136 Files.createDirectories(newLoc);
137 } catch (IOException e) {
138 log.error("Could not create directory {}", location, e);
141 FilesystemStorage previous = (FilesystemStorage) getStorage();
143 FilesystemStorage fs = new FilesystemStorage(newLoc, previous.getFileLockManager());
145 } catch (IOException e) {
146 log.error("Could not create new filesystem storage at {}", newLoc);
148 Path tmpDir = Files.createTempDirectory("tmp-repo-"+getId());
149 FilesystemStorage fs = new FilesystemStorage(tmpDir, previous.getFileLockManager());
151 } catch (IOException ex) {
152 throw new RuntimeException("Could not setup storage for repository "+getId());
160 public RepositoryRequestInfo getRequestInfo() {
161 return new MavenRepositoryRequestInfo(this);
164 public static MavenManagedRepository newLocalInstance(String id, String name, Path basePath) throws IOException {
165 FileLockManager lockManager = new DefaultFileLockManager();
166 FilesystemStorage storage = new FilesystemStorage(basePath.resolve(id), lockManager);
167 return new MavenManagedRepository(id, name, storage);