From 2fe8227634acfeaf7837e953e0f8fd2a44dae3a5 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Thu, 16 Nov 2017 19:54:13 +0100 Subject: [PATCH] Resolving base paths in archiva configuration Adding methods for retrieving base path information to ArchivaConfiguration resolving paths according to the configuration entries. --- .../configuration/ArchivaConfiguration.java | 12 ++++ .../DefaultArchivaConfiguration.java | 62 +++++++++++++++++-- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/ArchivaConfiguration.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/ArchivaConfiguration.java index acda2b710..e656b7e9d 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/ArchivaConfiguration.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/ArchivaConfiguration.java @@ -95,5 +95,17 @@ public interface ArchivaConfiguration public List getLanguagePriorities(); public Path getAppServerBaseDir(); + + /** + * Returns the base directory for repositories that have a relative location path set. + * @return + */ + public Path getRepositoryBaseDir(); + + /** + * Returns the data directory where repositories and metadata reside + * @return + */ + public Path getDataDirectory(); } diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java index 0222a426a..b28bed201 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java @@ -174,11 +174,16 @@ public class DefaultArchivaConfiguration private List languagePriorities = new ArrayList<>( ); + private volatile Path dataDirectory; + private volatile Path repositoryBaseDirectory; + @PostConstruct private void init() { languagePriorities = Locale.LanguageRange.parse( "en,fr,de" ); } + + @Override public Configuration getConfiguration() { @@ -223,15 +228,41 @@ public class DefaultArchivaConfiguration } Configuration config = new ConfigurationRegistryReader().read( subset ); - if (StringUtils.isEmpty( config.getArchivaRuntimeConfiguration().getDataDirectory() )) { - Path appserverBaseDir = Paths.get(registry.getString("appserver.base", "")); - config.getArchivaRuntimeConfiguration().setDataDirectory( appserverBaseDir.normalize().toString() ); + + // Resolving data and repositories directories + // If the config entries are absolute, the path is used as it is + // if the config entries are empty, they are resolved: + // dataDirectory = ${appserver.base}/data + // repositoryDirectory = ${dataDirectory}/repositories + // If the entries are relative they are resolved + // relative to the appserver.base, for dataDirectory + // relative to dataDirectory for repositoryBase + String dataDir = config.getArchivaRuntimeConfiguration().getDataDirectory(); + if (StringUtils.isEmpty( dataDir )) { + dataDirectory = getAppServerBaseDir().resolve( "data"); + } else { + Path tmpDataDir = Paths.get(dataDir); + if (tmpDataDir.isAbsolute()) { + dataDirectory = tmpDataDir; + } else { + dataDirectory = getAppServerBaseDir().resolve(tmpDataDir); + } } - if (StringUtils.isEmpty( config.getArchivaRuntimeConfiguration().getRepositoryBaseDirectory())) { - Path baseDir = Paths.get(config.getArchivaRuntimeConfiguration().getDataDirectory()); - config.getArchivaRuntimeConfiguration().setRepositoryBaseDirectory( baseDir.resolve("repositories").toString() ); + config.getArchivaRuntimeConfiguration().setDataDirectory( dataDirectory.normalize().toString() ); + String repoBaseDir = config.getArchivaRuntimeConfiguration().getRepositoryBaseDirectory(); + if (StringUtils.isEmpty( repoBaseDir )) { + repositoryBaseDirectory = dataDirectory.resolve("repositories"); + + } else { + Path tmpRepoBaseDir = Paths.get(repoBaseDir); + if (tmpRepoBaseDir.isAbsolute()) { + repositoryBaseDirectory = tmpRepoBaseDir; + } else { + dataDirectory.resolve(tmpRepoBaseDir); + } } + config.getRepositoryGroups(); config.getRepositoryGroupsAsMap(); if ( !CollectionUtils.isEmpty( config.getRemoteRepositories() ) ) @@ -901,6 +932,23 @@ public class DefaultArchivaConfiguration } } + @Override + public Path getRepositoryBaseDir() { + if (repositoryBaseDirectory==null) { + getConfiguration(); + } + return repositoryBaseDirectory; + + } + + @Override + public Path getDataDirectory() { + if (dataDirectory==null) { + getConfiguration(); + } + return dataDirectory; + } + @Override public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { @@ -911,6 +959,8 @@ public class DefaultArchivaConfiguration public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { configuration = null; + this.dataDirectory=null; + this.repositoryBaseDirectory=null; } private String removeExpressions( String directory ) -- 2.39.5