diff options
author | Joakim Erdfelt <joakime@apache.org> | 2007-10-12 01:26:06 +0000 |
---|---|---|
committer | Joakim Erdfelt <joakime@apache.org> | 2007-10-12 01:26:06 +0000 |
commit | 69cdf2b90262fb5e2e22a75ab53ed7b7b6cfed74 (patch) | |
tree | 35674c4fffbdb8a83dfd26006c791176b53dc323 /archiva-base | |
parent | ae567b9b6bbfb4e78375bec0bfec8b4f8333dd6d (diff) | |
download | archiva-69cdf2b90262fb5e2e22a75ab53ed7b7b6cfed74.tar.gz archiva-69cdf2b90262fb5e2e22a75ab53ed7b7b6cfed74.zip |
[MRM-501] Saving configuration fails through the webapp.
Provided an alternative configuration location.
Added unit tests for this condition.
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@584021 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-base')
4 files changed, 167 insertions, 18 deletions
diff --git a/archiva-base/archiva-configuration/pom.xml b/archiva-base/archiva-configuration/pom.xml index c0486a8a6..4cb8378fd 100644 --- a/archiva-base/archiva-configuration/pom.xml +++ b/archiva-base/archiva-configuration/pom.xml @@ -70,6 +70,10 @@ </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-expression-evaluator</artifactId> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-container-default</artifactId> </dependency> <dependency> diff --git a/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java index a663dcdb5..b26174e6d 100644 --- a/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java @@ -23,6 +23,10 @@ import org.apache.commons.io.FileUtils; import org.apache.maven.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator; import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader; import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryWriter; +import org.codehaus.plexus.evaluator.DefaultExpressionEvaluator; +import org.codehaus.plexus.evaluator.EvaluatorException; +import org.codehaus.plexus.evaluator.ExpressionEvaluator; +import org.codehaus.plexus.evaluator.sources.SystemPropertyExpressionSource; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; @@ -85,6 +89,11 @@ public class DefaultArchivaConfiguration private String userConfigFilename; /** + * @plexus.configuration default-value="${appserver.base}/conf/archiva.xml" + */ + private String altConfigFilename; + + /** * Configuration Listeners we've registered. */ private Set<ConfigurationListener> listeners = new HashSet<ConfigurationListener>(); @@ -94,11 +103,6 @@ public class DefaultArchivaConfiguration */ private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>(); - public String getFilteredUserConfigFilename() - { - return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" ) ); - } - public synchronized Configuration getConfiguration() { if ( configuration == null ) @@ -268,14 +272,17 @@ public class DefaultArchivaConfiguration throws RegistryException { // TODO: may not be needed under commons-configuration 1.4 - check - File file = new File( getFilteredUserConfigFilename() ); - try - { - FileUtils.writeStringToFile( file, "<configuration/>", "UTF-8" ); - } - catch ( IOException e ) + // UPDATE: Upgrading to commons-configuration 1.4 breaks half the unit tests. 10/11/2007 (joakime) + + String contents = "<configuration />"; + if ( !writeFile( "user configuration", userConfigFilename, contents ) ) { - throw new RegistryException( "Unable to create configuration file: " + e.getMessage(), e ); + if ( !writeFile( "alternative configuration", altConfigFilename, contents ) ) + { + throw new RegistryException( "Unable to create configuration file in either user [" + + userConfigFilename + "] or alternative [" + altConfigFilename + + "] locations on disk, usually happens when not allowed to write to those locations." ); + } } try @@ -297,6 +304,30 @@ public class DefaultArchivaConfiguration return registry.getSection( KEY + ".user" ); } + /** + * Attempts to write the contents to a file, if an IOException occurs, return false. + * + * @param filetype the filetype (freeform text) to use in logging messages when failure to write. + * @param path the path to write to. + * @param contents the contents to write. + * @return true if write successful. + */ + private boolean writeFile( String filetype, String path, String contents ) + { + File file = new File( path ); + + try + { + FileUtils.writeStringToFile( file, contents, "UTF-8" ); + return true; + } + catch ( IOException e ) + { + getLogger().error( "Unable to create " + filetype + " file: " + e.getMessage(), e ); + return false; + } + } + private void triggerEvent( int type ) { ConfigurationEvent evt = new ConfigurationEvent( type ); @@ -358,6 +389,20 @@ public class DefaultArchivaConfiguration public void initialize() throws InitializationException { + // Resolve expressions in the userConfigFilename and altConfigFilename + try + { + ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator(); + expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() ); + userConfigFilename = expressionEvaluator.expand( userConfigFilename ); + altConfigFilename = expressionEvaluator.expand( altConfigFilename ); + } + catch ( EvaluatorException e ) + { + throw new InitializationException( "Unable to evaluate expressions found in " + + "userConfigFilename or altConfigFilename." ); + } + registry.addChangeListener( this ); } @@ -409,4 +454,14 @@ public class DefaultArchivaConfiguration return config; } + + public String getUserConfigFilename() + { + return userConfigFilename; + } + + public String getAltConfigFilename() + { + return altConfigFilename; + } } diff --git a/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java b/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java index c0da5e897..b92ce3390 100644 --- a/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java +++ b/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java @@ -19,11 +19,10 @@ package org.apache.maven.archiva.configuration; * under the License. */ -import org.apache.commons.io.IOUtils; import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.registry.Registry; +import org.codehaus.plexus.registry.RegistryException; import org.codehaus.plexus.util.FileUtils; -import org.custommonkey.xmlunit.XMLAssert; import org.easymock.MockControl; import java.io.File; @@ -40,12 +39,12 @@ public class ArchivaConfigurationTest { @SuppressWarnings("unused") private Registry registry; - + protected void setUp() throws Exception { super.setUp(); - + registry = (Registry) lookup( Registry.ROLE, "commons-configuration" ); } @@ -304,9 +303,11 @@ public class ArchivaConfigurationTest { DefaultArchivaConfiguration archivaConfiguration = (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName() ); - + assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml", - archivaConfiguration.getFilteredUserConfigFilename() ); + archivaConfiguration.getUserConfigFilename() ); + assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml", + archivaConfiguration.getAltConfigFilename() ); } public void testStoreConfigurationFallback() @@ -456,6 +457,36 @@ public class ArchivaConfigurationTest assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() ); } } + + public void testLoadConfigurationFromInvalidBothLocationsOnDisk() throws Exception + { + ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), + "test-not-allowed-to-write-to-both" ); + Configuration config = archivaConfiguration.getConfiguration(); + + try + { + archivaConfiguration.save( config ); + fail( "Should have thrown a RegistryException because the configuration can't be saved." ); + } + catch ( RegistryException e ) + { + /* expected exception */ + } + } + + public void testLoadConfigurationFromInvalidUserLocationOnDisk() throws Exception + { + File testConfDir = getTestFile( "target/test-appserver-base/conf/" ); + testConfDir.mkdirs(); + + ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), + "test-not-allowed-to-write-to-user" ); + Configuration config = archivaConfiguration.getConfiguration(); + archivaConfiguration.save( config ); + // No Exception == test passes. + // Expected Path is: Should not have thrown an exception. + } public void testConfigurationUpgradeFrom09() throws Exception diff --git a/archiva-base/archiva-configuration/src/test/resources/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.xml b/archiva-base/archiva-configuration/src/test/resources/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.xml index 80ca2e847..b0ca9d638 100644 --- a/archiva-base/archiva-configuration/src/test/resources/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.xml +++ b/archiva-base/archiva-configuration/src/test/resources/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.xml @@ -262,5 +262,64 @@ </properties> </configuration> </component> + + <component> + <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role> + <role-hint>test-not-allowed-to-write-to-both</role-hint> + <implementation>org.apache.maven.archiva.configuration.DefaultArchivaConfiguration</implementation> + <requirements> + <requirement> + <role>org.codehaus.plexus.registry.Registry</role> + <role-hint>not-allowed-to-write-to-both</role-hint> + </requirement> + </requirements> + <configuration> + <user-config-filename>${basedir}/target/*intentionally:invalid*/.m2/archiva-user.xml</user-config-filename> + <alt-config-filename>${basedir}/target/*intentionally:invalid*/conf/archiva.xml</alt-config-filename> + </configuration> + </component> + <component> + <role>org.codehaus.plexus.registry.Registry</role> + <role-hint>not-allowed-to-write-to-both</role-hint> + <implementation>org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry</implementation> + <configuration> + <properties> + <xml fileName="${basedir}/target/*intentionally:invalid*/.m2/archiva-user.xml" config-optional="true" + config-name="org.apache.maven.archiva.user" config-at="org.apache.maven.archiva"/> + <xml fileName="${basedir}/target/*intentionally:invalid*/conf/archiva.xml" config-optional="true" + config-name="org.apache.maven.archiva.user" config-at="org.apache.maven.archiva"/> + </properties> + </configuration> + </component> + + <component> + <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role> + <role-hint>test-not-allowed-to-write-to-user</role-hint> + <implementation>org.apache.maven.archiva.configuration.DefaultArchivaConfiguration</implementation> + <requirements> + <requirement> + <role>org.codehaus.plexus.registry.Registry</role> + <role-hint>not-allowed-to-write-to-user</role-hint> + </requirement> + </requirements> + <configuration> + <user-config-filename>${basedir}/target/*intentionally:invalid*/.m2/archiva-user.xml</user-config-filename> + <alt-config-filename>${basedir}/target/test-appserver-base/conf/archiva.xml</alt-config-filename> + </configuration> + </component> + <component> + <role>org.codehaus.plexus.registry.Registry</role> + <role-hint>not-allowed-to-write-to-user</role-hint> + <implementation>org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry</implementation> + <configuration> + <properties> + <xml fileName="${basedir}/target/*intentionally:invalid*/.m2/archiva-user.xml" config-optional="true" + config-name="org.apache.maven.archiva.user" config-at="org.apache.maven.archiva"/> + <xml fileName="${basedir}/target/test-appserver-base/conf/archiva.xml" config-optional="true" + config-name="org.apache.maven.archiva.user" config-at="org.apache.maven.archiva"/> + </properties> + </configuration> + </component> + </components> </component-set> |