]> source.dussan.org Git - archiva.git/commitdiff
Resolving base paths in archiva configuration
authorMartin Stockhammer <martin.stockhammer@ars.de>
Thu, 16 Nov 2017 18:54:13 +0000 (19:54 +0100)
committerMartin Stockhammer <martin.stockhammer@ars.de>
Thu, 16 Nov 2017 19:01:32 +0000 (20:01 +0100)
Adding methods for retrieving base path information to ArchivaConfiguration
resolving paths according to the configuration entries.

archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/ArchivaConfiguration.java
archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java

index acda2b710d31e1b9af87535e5b2306fe4f0d943d..e656b7e9d598a25e9f8553579d93116626cad75f 100644 (file)
@@ -95,5 +95,17 @@ public interface ArchivaConfiguration
     public List<Locale.LanguageRange> 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();
 }
 
index 0222a426a2d7ae6e10362cc03a35a0bad7f92b0e..b28bed201933eb37297f2a46483a7a4cc5cb6d4b 100644 (file)
@@ -174,11 +174,16 @@ public class DefaultArchivaConfiguration
 
     private List<Locale.LanguageRange> 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 )