]> source.dussan.org Git - archiva.git/blob
636b8c9887b1ec782b319a7a23c6b0fd8514b4d9
[archiva.git] /
1 package org.apache.archiva.maven.repository;
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  * 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
21 import org.apache.archiva.common.filelock.FileLockManager;
22 import org.apache.archiva.configuration.AbstractRepositoryConfiguration;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
27 import org.apache.archiva.event.Event;
28 import org.apache.archiva.event.EventHandler;
29 import org.apache.archiva.repository.EditableManagedRepository;
30 import org.apache.archiva.repository.EditableRemoteRepository;
31 import org.apache.archiva.repository.EditableRepository;
32 import org.apache.archiva.repository.EditableRepositoryGroup;
33 import org.apache.archiva.repository.ManagedRepository;
34 import org.apache.archiva.repository.ReleaseScheme;
35 import org.apache.archiva.repository.RemoteRepository;
36 import org.apache.archiva.repository.Repository;
37 import org.apache.archiva.repository.RepositoryCredentials;
38 import org.apache.archiva.repository.RepositoryException;
39 import org.apache.archiva.repository.RepositoryGroup;
40 import org.apache.archiva.repository.RepositoryProvider;
41 import org.apache.archiva.repository.RepositoryType;
42 import org.apache.archiva.repository.UnsupportedURIException;
43 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
44 import org.apache.archiva.repository.base.PasswordCredentials;
45 import org.apache.archiva.repository.event.RepositoryEvent;
46 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
47 import org.apache.archiva.repository.features.IndexCreationFeature;
48 import org.apache.archiva.repository.features.RemoteIndexFeature;
49 import org.apache.archiva.repository.features.StagingRepositoryFeature;
50 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
51 import org.apache.commons.lang3.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.stereotype.Service;
55
56 import javax.inject.Inject;
57 import java.io.IOException;
58 import java.net.URI;
59 import java.net.URISyntaxException;
60 import java.nio.file.Files;
61 import java.nio.file.Path;
62 import java.nio.file.Paths;
63 import java.time.Duration;
64 import java.time.Period;
65 import java.time.temporal.ChronoUnit;
66 import java.util.ArrayList;
67 import java.util.HashSet;
68 import java.util.List;
69 import java.util.Set;
70 import java.util.stream.Collectors;
71
72 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
73 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_PACKED_INDEX_PATH;
74
75 /**
76  * Provider for the maven2 repository implementation
77  */
78 @Service("mavenRepositoryProvider")
79 public class MavenRepositoryProvider implements RepositoryProvider {
80
81
82     @Inject
83     private ArchivaConfiguration archivaConfiguration;
84
85     @Inject
86     private FileLockManager fileLockManager;
87
88     private final List<EventHandler<? super RepositoryEvent>> repositoryEventHandlers = new ArrayList<>( );
89
90     private static final Logger log = LoggerFactory.getLogger(MavenRepositoryProvider.class);
91
92     static final Set<RepositoryType> TYPES = new HashSet<>();
93
94     static {
95         TYPES.add(RepositoryType.MAVEN);
96     }
97
98     @Override
99     public Set<RepositoryType> provides() {
100         return TYPES;
101     }
102
103     @Override
104     public MavenManagedRepository createManagedInstance(String id, String name) {
105         return createManagedInstance(id, name, archivaConfiguration.getRemoteRepositoryBaseDir());
106     }
107
108     public MavenManagedRepository createManagedInstance(String id, String name, Path baseDir) {
109         FilesystemStorage storage;
110         try {
111             storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
112         } catch (IOException e) {
113             log.error("Could not initialize fileystem for repository {}", id);
114             throw new RuntimeException(e);
115         }
116         MavenManagedRepository repo = new MavenManagedRepository( id, name, storage );
117         registerEventHandler( repo );
118         return repo;
119     }
120
121     @Override
122     public MavenRemoteRepository createRemoteInstance(String id, String name) {
123         return createRemoteInstance(id, name, archivaConfiguration.getRemoteRepositoryBaseDir());
124     }
125
126     public MavenRemoteRepository createRemoteInstance(String id, String name, Path baseDir) {
127         FilesystemStorage storage;
128         try {
129             storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
130         } catch (IOException e) {
131             log.error("Could not initialize filesystem for repository {}: '{}'", id, e.getMessage());
132             throw new RuntimeException(e);
133         }
134         MavenRemoteRepository repo = new MavenRemoteRepository( id, name, storage );
135         registerEventHandler( repo );
136         return repo;
137     }
138
139     @Override
140     public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
141         return createRepositoryGroup(id, name, archivaConfiguration.getRepositoryGroupBaseDir().resolve( id ));
142     }
143
144     public MavenRepositoryGroup createRepositoryGroup(String id, String name, Path repositoryPath) {
145         FilesystemStorage storage;
146         try {
147             storage = new FilesystemStorage(repositoryPath, fileLockManager);
148         } catch (IOException e) {
149             log.error("Could not initialize filesystem for repository {}: '{}'", id, e.getMessage());
150             throw new RuntimeException(e);
151         }
152         MavenRepositoryGroup group = new MavenRepositoryGroup( id, name, storage );
153         registerEventHandler( group );
154         return group;
155     }
156
157     private void registerEventHandler( Repository repo ) {
158         for (EventHandler<? super RepositoryEvent> eventHandler : repositoryEventHandlers) {
159             repo.registerEventHandler( RepositoryEvent.ANY, eventHandler );
160         }
161     }
162
163     private URI getURIFromString(String uriStr) throws RepositoryException {
164         URI uri;
165         try {
166             if (StringUtils.isEmpty(uriStr)) {
167                 return new URI("");
168             }
169             if (uriStr.startsWith("/")) {
170                 // only absolute paths are prepended with file scheme
171                 uri = new URI("file://" + uriStr);
172             } else if (uriStr.contains(":\\")) {
173                 //windows absolute path drive 
174                 uri = new URI("file:///" + uriStr.replaceAll("\\\\", "/"));
175             } else {
176                 uri = new URI(uriStr);
177             }
178             if (uri.getScheme() != null && !"file".equals(uri.getScheme())) {
179                 log.error("Bad URI scheme found: {}, URI={}", uri.getScheme(), uri);
180                 throw new RepositoryException("The uri " + uriStr + " is not valid. Only file:// URI is allowed for maven.");
181             }
182         } catch (URISyntaxException e) {
183             String newCfg = "file://" + uriStr;
184             try {
185                 uri = new URI(newCfg);
186             } catch (URISyntaxException e1) {
187                 log.error("Could not create URI from {} -> {}", uriStr, newCfg);
188                 throw new RepositoryException("The config entry " + uriStr + " cannot be converted to URI.");
189             }
190         }
191         log.debug("Setting location uri: {}", uri);
192         return uri;
193     }
194
195     @Override
196     public ManagedRepository createManagedInstance(ManagedRepositoryConfiguration cfg) throws RepositoryException {
197         MavenManagedRepository repo = createManagedInstance(cfg.getId(), cfg.getName(), Paths.get(cfg.getLocation()).getParent());
198         updateManagedInstance(repo, cfg);
199         return repo;
200     }
201
202     @Override
203     public void updateManagedInstance(EditableManagedRepository repo, ManagedRepositoryConfiguration cfg) throws RepositoryException {
204         try {
205             repo.setLocation(getURIFromString(cfg.getLocation()));
206         } catch (UnsupportedURIException e) {
207             throw new RepositoryException("The location entry is not a valid uri: " + cfg.getLocation());
208         }
209         setBaseConfig(repo, cfg);
210         Path repoDir = repo.getRoot().getFilePath();
211         if (!Files.exists(repoDir)) {
212             log.debug("Creating repo directory {}", repoDir);
213             try {
214                 Files.createDirectories(repoDir);
215             } catch (IOException e) {
216                 log.error("Could not create directory {} for repository {}", repoDir, repo.getId(), e);
217                 throw new RepositoryException("Could not create directory for repository " + repoDir);
218             }
219         }
220         repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
221         repo.setBlocksRedeployment(cfg.isBlockRedeployments());
222         repo.setScanned(cfg.isScanned());
223         if (cfg.isReleases()) {
224             repo.addActiveReleaseScheme(ReleaseScheme.RELEASE);
225         } else {
226             repo.removeActiveReleaseScheme(ReleaseScheme.RELEASE);
227         }
228         if (cfg.isSnapshots()) {
229             repo.addActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
230         } else {
231             repo.removeActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
232         }
233
234         StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature(StagingRepositoryFeature.class).get();
235         stagingRepositoryFeature.setStageRepoNeeded(cfg.isStageRepoNeeded());
236
237         IndexCreationFeature indexCreationFeature = repo.getFeature(IndexCreationFeature.class).get();
238         indexCreationFeature.setSkipPackedIndexCreation(cfg.isSkipPackedIndexCreation());
239         String indexDir = StringUtils.isEmpty( cfg.getIndexDir() ) ? DEFAULT_INDEX_PATH : cfg.getIndexDir();
240         String packedIndexDir = StringUtils.isEmpty( cfg.getPackedIndexDir() ) ? DEFAULT_PACKED_INDEX_PATH : cfg.getPackedIndexDir();
241         indexCreationFeature.setIndexPath(getURIFromString(indexDir));
242         indexCreationFeature.setPackedIndexPath(getURIFromString(packedIndexDir));
243
244         ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature(ArtifactCleanupFeature.class).get();
245
246         artifactCleanupFeature.setDeleteReleasedSnapshots(cfg.isDeleteReleasedSnapshots());
247         artifactCleanupFeature.setRetentionCount(cfg.getRetentionCount());
248         artifactCleanupFeature.setRetentionPeriod(Period.ofDays(cfg.getRetentionPeriod()));
249     }
250
251
252     @Override
253     public ManagedRepository createStagingInstance(ManagedRepositoryConfiguration baseConfiguration) throws RepositoryException {
254         log.debug("Creating staging instance for {}", baseConfiguration.getId());
255         return createManagedInstance(getStageRepoConfig(baseConfiguration));
256     }
257
258
259     @Override
260     public RemoteRepository createRemoteInstance(RemoteRepositoryConfiguration cfg) throws RepositoryException {
261         MavenRemoteRepository repo = createRemoteInstance(cfg.getId(), cfg.getName(), archivaConfiguration.getRemoteRepositoryBaseDir());
262         updateRemoteInstance(repo, cfg);
263         return repo;
264     }
265
266     private String convertUriToPath(URI uri) {
267         if (uri.getScheme() == null) {
268             return uri.getPath();
269         } else if ("file".equals(uri.getScheme())) {
270             return Paths.get(uri).toString();
271         } else {
272             return uri.toString();
273         }
274     }
275
276     @Override
277     public void updateRemoteInstance(EditableRemoteRepository repo, RemoteRepositoryConfiguration cfg) throws RepositoryException {
278         setBaseConfig(repo, cfg);
279         repo.setCheckPath(cfg.getCheckPath());
280         repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
281         try {
282             repo.setLocation(new URI(cfg.getUrl()));
283         } catch (UnsupportedURIException | URISyntaxException e) {
284             log.error("Could not set remote url " + cfg.getUrl());
285             throw new RepositoryException("The url config is not a valid uri: " + cfg.getUrl());
286         }
287         repo.setTimeout(Duration.ofSeconds(cfg.getTimeout()));
288         RemoteIndexFeature remoteIndexFeature = repo.getFeature(RemoteIndexFeature.class).get();
289         remoteIndexFeature.setDownloadRemoteIndex(cfg.isDownloadRemoteIndex());
290         remoteIndexFeature.setDownloadRemoteIndexOnStartup(cfg.isDownloadRemoteIndexOnStartup());
291         remoteIndexFeature.setDownloadTimeout(Duration.ofSeconds(cfg.getRemoteDownloadTimeout()));
292         remoteIndexFeature.setProxyId(cfg.getRemoteDownloadNetworkProxyId());
293         if (cfg.isDownloadRemoteIndex()) {
294             try {
295                 remoteIndexFeature.setIndexUri(new URI(cfg.getRemoteIndexUrl()));
296             } catch (URISyntaxException e) {
297                 log.error("Could not set remote index url " + cfg.getRemoteIndexUrl());
298                 remoteIndexFeature.setDownloadRemoteIndex(false);
299                 remoteIndexFeature.setDownloadRemoteIndexOnStartup(false);
300             }
301         }
302         for ( Object key : cfg.getExtraHeaders().keySet() ) {
303             repo.addExtraHeader( key.toString(), cfg.getExtraHeaders().get(key).toString() );
304         }
305         for ( Object key : cfg.getExtraParameters().keySet() ) {
306             repo.addExtraParameter( key.toString(), cfg.getExtraParameters().get(key).toString() );
307         }
308         PasswordCredentials credentials = new PasswordCredentials("", new char[0]);
309         if (cfg.getPassword() != null && cfg.getUsername() != null) {
310             credentials.setPassword(cfg.getPassword().toCharArray());
311             credentials.setUsername(cfg.getUsername());
312             repo.setCredentials(credentials);
313         } else {
314             credentials.setPassword(new char[0]);
315         }
316         IndexCreationFeature indexCreationFeature = repo.getFeature(IndexCreationFeature.class).get();
317         if ( !StringUtils.isEmpty( cfg.getIndexDir( ) ) )
318         {
319             indexCreationFeature.setIndexPath( getURIFromString( cfg.getIndexDir( ) ) );
320         }
321         if (!StringUtils.isEmpty( cfg.getPackedIndexDir() )) {
322             indexCreationFeature.setPackedIndexPath(getURIFromString(cfg.getPackedIndexDir()));
323         }
324         log.debug("Updated remote instance {}", repo);
325     }
326
327     @Override
328     public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
329         Path repositoryPath = getRepositoryGroupPath( configuration );
330         MavenRepositoryGroup newGrp = createRepositoryGroup(configuration.getId(), configuration.getName(),
331                 repositoryPath);
332         updateRepositoryGroupInstance(newGrp, configuration);
333         return newGrp;
334     }
335
336     private Path getRepositoryGroupPath(RepositoryGroupConfiguration configuration) {
337         if (StringUtils.isNotEmpty( configuration.getLocation() )) {
338             return Paths.get( configuration.getLocation( ) );
339         } else {
340             return getArchivaConfiguration( ).getRepositoryGroupBaseDir( ).resolve( configuration.getId( ) );
341         }
342     }
343
344     @Override
345     public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
346         repositoryGroup.setName(repositoryGroup.getPrimaryLocale(), configuration.getName());
347         repositoryGroup.setMergedIndexTTL(configuration.getMergedIndexTtl());
348         repositoryGroup.setSchedulingDefinition(configuration.getCronExpression());
349         if (repositoryGroup.supportsFeature( IndexCreationFeature.class )) {
350             IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
351             indexCreationFeature.setIndexPath( getURIFromString(configuration.getMergedIndexPath()) );
352             Path localPath = Paths.get(configuration.getMergedIndexPath());
353             Path repoGroupPath = repositoryGroup.getRoot().getFilePath().toAbsolutePath();
354             if (localPath.isAbsolute() && !localPath.startsWith(repoGroupPath)) {
355                 try {
356                     FilesystemStorage storage = new FilesystemStorage(localPath.getParent(), fileLockManager);
357                     indexCreationFeature.setLocalIndexPath(storage.getAsset(localPath.getFileName().toString()));
358                 } catch (IOException e) {
359                     throw new RepositoryException("Could not initialize storage for index path "+localPath);
360                 }
361             } else if (localPath.isAbsolute()) {
362                 indexCreationFeature.setLocalIndexPath(repositoryGroup.getAsset(repoGroupPath.relativize(localPath).toString()));
363             } else
364             {
365                 indexCreationFeature.setLocalIndexPath(repositoryGroup.getAsset(localPath.toString()));
366             }
367         }
368         // References to other repositories are set filled by the registry
369     }
370
371     @Override
372     public RemoteRepositoryConfiguration getRemoteConfiguration(RemoteRepository remoteRepository) throws RepositoryException {
373         if (!(remoteRepository instanceof MavenRemoteRepository)) {
374             log.error("Wrong remote repository type " + remoteRepository.getClass().getName());
375             throw new RepositoryException("The given repository type cannot be handled by the maven provider: " + remoteRepository.getClass().getName());
376         }
377         RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
378         cfg.setType(remoteRepository.getType().toString());
379         cfg.setId(remoteRepository.getId());
380         cfg.setName(remoteRepository.getName());
381         cfg.setDescription(remoteRepository.getDescription());
382         cfg.setUrl(remoteRepository.getLocation().toString());
383         cfg.setTimeout((int) remoteRepository.getTimeout().toMillis() / 1000);
384         cfg.setCheckPath(remoteRepository.getCheckPath());
385         RepositoryCredentials creds = remoteRepository.getLoginCredentials();
386         if (creds != null) {
387             if (creds instanceof PasswordCredentials) {
388                 PasswordCredentials pCreds = (PasswordCredentials) creds;
389                 cfg.setPassword(new String(pCreds.getPassword()));
390                 cfg.setUsername(pCreds.getUsername());
391             }
392         }
393         cfg.setLayout(remoteRepository.getLayout());
394         cfg.setExtraParameters(remoteRepository.getExtraParameters());
395         cfg.setExtraHeaders(remoteRepository.getExtraHeaders());
396         cfg.setRefreshCronExpression(remoteRepository.getSchedulingDefinition());
397
398         IndexCreationFeature indexCreationFeature = remoteRepository.getFeature(IndexCreationFeature.class).get();
399         cfg.setIndexDir(convertUriToPath(indexCreationFeature.getIndexPath()));
400         cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath()));
401
402         RemoteIndexFeature remoteIndexFeature = remoteRepository.getFeature(RemoteIndexFeature.class).get();
403         if ( remoteIndexFeature.getIndexUri( ) == null )
404         {
405             cfg.setRemoteIndexUrl( "" );
406         }
407         else
408         {
409             cfg.setRemoteIndexUrl(remoteIndexFeature.getIndexUri().toString());
410         }
411         cfg.setRemoteDownloadTimeout((int) remoteIndexFeature.getDownloadTimeout().get(ChronoUnit.SECONDS));
412         cfg.setDownloadRemoteIndexOnStartup(remoteIndexFeature.isDownloadRemoteIndexOnStartup());
413         cfg.setDownloadRemoteIndex(remoteIndexFeature.isDownloadRemoteIndex());
414         cfg.setRemoteDownloadNetworkProxyId(remoteIndexFeature.getProxyId());
415         if ( StringUtils.isEmpty( remoteIndexFeature.getProxyId( ) ) )
416         {
417             cfg.setRemoteDownloadNetworkProxyId("");
418         }
419         else
420         {
421             cfg.setRemoteDownloadNetworkProxyId(remoteIndexFeature.getProxyId());
422         }
423
424
425         return cfg;
426
427     }
428
429     @Override
430     public ManagedRepositoryConfiguration getManagedConfiguration(ManagedRepository managedRepository) throws RepositoryException {
431         if (!(managedRepository instanceof MavenManagedRepository || managedRepository instanceof BasicManagedRepository )) {
432             log.error("Wrong remote repository type " + managedRepository.getClass().getName());
433             throw new RepositoryException("The given repository type cannot be handled by the maven provider: " + managedRepository.getClass().getName());
434         }
435         ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
436         cfg.setType(managedRepository.getType().toString());
437         cfg.setId(managedRepository.getId());
438         cfg.setName(managedRepository.getName());
439         cfg.setDescription(managedRepository.getDescription());
440         cfg.setLocation(convertUriToPath(managedRepository.getLocation()));
441         cfg.setLayout(managedRepository.getLayout());
442         cfg.setRefreshCronExpression(managedRepository.getSchedulingDefinition());
443         cfg.setScanned(managedRepository.isScanned());
444         cfg.setBlockRedeployments(managedRepository.blocksRedeployments());
445         StagingRepositoryFeature stagingRepositoryFeature = managedRepository.getFeature(StagingRepositoryFeature.class).get();
446         cfg.setStageRepoNeeded(stagingRepositoryFeature.isStageRepoNeeded());
447         IndexCreationFeature indexCreationFeature = managedRepository.getFeature(IndexCreationFeature.class).get();
448         cfg.setIndexDir(convertUriToPath(indexCreationFeature.getIndexPath()));
449         cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath()));
450         cfg.setSkipPackedIndexCreation(indexCreationFeature.isSkipPackedIndexCreation());
451
452         ArtifactCleanupFeature artifactCleanupFeature = managedRepository.getFeature(ArtifactCleanupFeature.class).get();
453         cfg.setRetentionCount(artifactCleanupFeature.getRetentionCount());
454         cfg.setRetentionPeriod(artifactCleanupFeature.getRetentionPeriod().getDays());
455         cfg.setDeleteReleasedSnapshots(artifactCleanupFeature.isDeleteReleasedSnapshots());
456
457         cfg.setReleases( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.RELEASE ) );
458         cfg.setSnapshots( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
459         return cfg;
460
461     }
462
463     @Override
464     public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
465         if (repositoryGroup.getType() != RepositoryType.MAVEN) {
466             throw new RepositoryException("The given repository group is not of MAVEN type");
467         }
468         RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration();
469         cfg.setId(repositoryGroup.getId());
470         cfg.setName(repositoryGroup.getName());
471         if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
472         {
473             IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
474
475             cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() );
476         }
477         cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL());
478         cfg.setRepositories(repositoryGroup.getRepositories().stream().map( Repository::getId ).collect(Collectors.toList()));
479         cfg.setCronExpression(repositoryGroup.getSchedulingDefinition());
480         return cfg;
481     }
482
483     @Override
484     public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
485     {
486         this.repositoryEventHandlers.add( eventHandler );
487     }
488
489     private ManagedRepositoryConfiguration getStageRepoConfig(ManagedRepositoryConfiguration repository) {
490         ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration();
491         stagingRepository.setId(repository.getId() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
492         stagingRepository.setLayout(repository.getLayout());
493         stagingRepository.setName(repository.getName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
494         stagingRepository.setBlockRedeployments(repository.isBlockRedeployments());
495         stagingRepository.setRetentionPeriod(repository.getRetentionPeriod());
496         stagingRepository.setDeleteReleasedSnapshots(repository.isDeleteReleasedSnapshots());
497         stagingRepository.setStageRepoNeeded(false);
498
499         String path = repository.getLocation();
500         int lastIndex = path.replace('\\', '/').lastIndexOf('/');
501         stagingRepository.setLocation(path.substring(0, lastIndex) + "/" + stagingRepository.getId());
502
503         if (StringUtils.isNotBlank(repository.getIndexDir())) {
504             Path indexDir;
505             try {
506                 if (repository.getIndexDir().startsWith( "file:" )) {
507                     indexDir = Paths.get( new URI( repository.getIndexDir( ) ) );
508                 } else
509                 {
510                     indexDir = Paths.get( repository.getIndexDir( ) );
511                 }
512                 if (indexDir.isAbsolute()) {
513                     Path newDir = indexDir.getParent().resolve(indexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
514                     log.debug("Changing index directory {} -> {}", indexDir, newDir);
515                     stagingRepository.setIndexDir(newDir.toString());
516                 } else {
517                     log.debug("Keeping index directory {}", repository.getIndexDir());
518                     stagingRepository.setIndexDir(repository.getIndexDir());
519                 }
520             } catch (URISyntaxException e) {
521                 log.error("Could not parse index path as uri {}", repository.getIndexDir());
522                 stagingRepository.setIndexDir("");
523             }
524             // in case of absolute dir do not use the same
525         }
526         if (StringUtils.isNotBlank(repository.getPackedIndexDir())) {
527             Path packedIndexDir;
528             packedIndexDir = Paths.get(repository.getPackedIndexDir());
529             if (packedIndexDir.isAbsolute()) {
530                 Path newDir = packedIndexDir.getParent().resolve(packedIndexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
531                 log.debug("Changing index directory {} -> {}", packedIndexDir, newDir);
532                 stagingRepository.setPackedIndexDir(newDir.toString());
533             } else {
534                 log.debug("Keeping index directory {}", repository.getPackedIndexDir());
535                 stagingRepository.setPackedIndexDir(repository.getPackedIndexDir());
536             }
537             // in case of absolute dir do not use the same
538         }
539         stagingRepository.setRefreshCronExpression(repository.getRefreshCronExpression());
540         stagingRepository.setReleases(repository.isReleases());
541         stagingRepository.setRetentionCount(repository.getRetentionCount());
542         stagingRepository.setScanned(repository.isScanned());
543         stagingRepository.setSnapshots(repository.isSnapshots());
544         stagingRepository.setSkipPackedIndexCreation(repository.isSkipPackedIndexCreation());
545         // do not duplicate description
546         //stagingRepository.getDescription("")
547         return stagingRepository;
548     }
549
550     private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg)
551     {
552
553         URI baseUri = archivaConfiguration.getRepositoryBaseDir().toUri();
554         repo.setBaseUri(baseUri);
555
556         repo.setName(repo.getPrimaryLocale(), cfg.getName());
557         repo.setDescription(repo.getPrimaryLocale(), cfg.getDescription());
558         repo.setLayout(cfg.getLayout());
559     }
560
561     public ArchivaConfiguration getArchivaConfiguration() {
562         return archivaConfiguration;
563     }
564
565     public void setArchivaConfiguration(ArchivaConfiguration archivaConfiguration) {
566         this.archivaConfiguration = archivaConfiguration;
567     }
568
569     @Override
570     public void handle(Event event) {
571         //
572     }
573
574 }