diff options
author | Olivier Lamy <olamy@apache.org> | 2012-04-06 09:58:14 +0000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2012-04-06 09:58:14 +0000 |
commit | 5b06b6673ee8eaed4b46ad8e847e98fe4c90319d (patch) | |
tree | e184a7512cd005f5baaf82a94e13fd67646cc2ea /redback-configuration/src | |
parent | be9e1800fdcb3c37c566220c1b2b79650d375000 (diff) | |
download | archiva-5b06b6673ee8eaed4b46ad8e847e98fe4c90319d.tar.gz archiva-5b06b6673ee8eaed4b46ad8e847e98fe4c90319d.zip |
import of redback core sources
http://svn.codehaus.org/redback/redback/trunk/ r1724
git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1310268 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'redback-configuration/src')
7 files changed, 536 insertions, 0 deletions
diff --git a/redback-configuration/src/main/java/org/codehaus/plexus/redback/configuration/UserConfiguration.java b/redback-configuration/src/main/java/org/codehaus/plexus/redback/configuration/UserConfiguration.java new file mode 100644 index 000000000..3e102a545 --- /dev/null +++ b/redback-configuration/src/main/java/org/codehaus/plexus/redback/configuration/UserConfiguration.java @@ -0,0 +1,221 @@ +package org.codehaus.plexus.redback.configuration; + +/* + * Copyright 2001-2006 The Codehaus. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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.registry.Registry; +import org.codehaus.plexus.registry.RegistryException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import javax.inject.Named; +import java.io.File; +import java.util.List; + +/** + * ConfigurationFactory + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +@Service( "userConfiguration" ) +public class UserConfiguration +{ + private static final String DEFAULT_CONFIG_RESOURCE = "org/codehaus/plexus/redback/config-defaults.properties"; + + protected Logger log = LoggerFactory.getLogger( getClass() ); + + /** + * + * + * @deprecated Please configure the Plexus registry instead + */ + private List<String> configs; + + private Registry lookupRegistry; + + private static final String PREFIX = "org.codehaus.plexus.redback"; + + @Inject + @Named( value = "commons-configuration" ) + private Registry registry; + + //TODO move this method call in the constructor + + @PostConstruct + public void initialize() + throws RegistryException + { + performLegacyInitialization(); + + try + { + registry.addConfigurationFromResource( DEFAULT_CONFIG_RESOURCE, PREFIX ); + } + catch ( RegistryException e ) + { + // Ok, not found in context classloader; try the one in this jar. + + ClassLoader prevCl = Thread.currentThread().getContextClassLoader(); + try + { + + Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ); + registry.addConfigurationFromResource( DEFAULT_CONFIG_RESOURCE, PREFIX ); + } + finally + { + Thread.currentThread().setContextClassLoader( prevCl ); + } + } + + lookupRegistry = registry.getSubset( PREFIX ); + + if ( log.isDebugEnabled() ) + { + log.debug( lookupRegistry.dump() ); + } + } + + private void performLegacyInitialization() + throws RegistryException + { + ExpressionEvaluator evaluator = new DefaultExpressionEvaluator(); + evaluator.addExpressionSource( new SystemPropertyExpressionSource() ); + + if ( configs != null ) + { + if ( !configs.isEmpty() ) + { + // TODO: plexus should be able to do this on it's own. + log.warn( + "DEPRECATED: the <configs> elements is deprecated. Please configure the Plexus registry instead" ); + } + + for ( String configName : configs ) + { + try + { + configName = evaluator.expand( configName ); + } + catch ( EvaluatorException e ) + { + log.warn( "Unable to resolve configuration name: " + e.getMessage(), e ); + } + log.info( "Attempting to find configuration [{}] (resolved to [{}])", configName, configName ); + + registry.addConfigurationFromFile( new File( configName ), PREFIX ); + } + } + } + + public String getString( String key ) + { + return lookupRegistry.getString( key ); + } + + public String getString( String key, String defaultValue ) + { + String value = lookupRegistry.getString( key, defaultValue ); + return value; + } + + public int getInt( String key ) + { + return lookupRegistry.getInt( key ); + } + + public int getInt( String key, int defaultValue ) + { + return lookupRegistry.getInt( key, defaultValue ); + } + + public boolean getBoolean( String key ) + { + return lookupRegistry.getBoolean( key ); + } + + public boolean getBoolean( String key, boolean defaultValue ) + { + return lookupRegistry.getBoolean( key, defaultValue ); + } + + @SuppressWarnings( "unchecked" ) + public List<String> getList( String key ) + { + return lookupRegistry.getList( key ); + } + + public String getConcatenatedList( String key, String defaultValue ) + { + String concatenatedList; + List<String> list = getList( key ); + if ( !list.isEmpty() ) + { + StringBuilder s = new StringBuilder(); + for ( String value : list ) + { + if ( s.length() > 0 ) + { + s.append( "," ); + } + s.append( value ); + } + concatenatedList = s.toString(); + } + else + { + concatenatedList = defaultValue; + } + + return concatenatedList; + } + + /** + * @return + * @deprecated + */ + public List<String> getConfigs() + { + return configs; + } + + /** + * @param configs + * @deprecated + */ + public void setConfigs( List<String> configs ) + { + this.configs = configs; + } + + public Registry getRegistry() + { + return registry; + } + + public void setRegistry( Registry registry ) + { + this.registry = registry; + } +} diff --git a/redback-configuration/src/main/resources/META-INF/spring-context.xml b/redback-configuration/src/main/resources/META-INF/spring-context.xml new file mode 100644 index 000000000..dc655aa4b --- /dev/null +++ b/redback-configuration/src/main/resources/META-INF/spring-context.xml @@ -0,0 +1,33 @@ +<?xml version="1.0"?> + +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd" + default-lazy-init="true"> + + <context:annotation-config /> + <context:component-scan base-package="org.codehaus.plexus.redback.configuration"/> + +</beans>
\ No newline at end of file diff --git a/redback-configuration/src/main/resources/org/codehaus/plexus/redback/config-defaults.properties b/redback-configuration/src/main/resources/org/codehaus/plexus/redback/config-defaults.properties new file mode 100644 index 000000000..2f39eeaab --- /dev/null +++ b/redback-configuration/src/main/resources/org/codehaus/plexus/redback/config-defaults.properties @@ -0,0 +1,127 @@ +# +# Copyright 2006 The Codehaus. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -------------------------------------------------------------------- +# Application Configuration + +application.timestamp=EEE d MMM yyyy HH:mm:ss Z + +# -------------------------------------------------------------------- +# JDBC Setup + +jdbc.driver.name=org.apache.derby.jdbc.EmbeddedDriver +jdbc.url=jdbc:derby:${plexus.home}/database;create=true +jdbc.username=sa +jdbc.password= + +# -------------------------------------------------------------------- +# Email Settings + +email.jndiSessionName=java:comp/env/mail/Session +email.smtp.host=localhost +email.smtp.port=25 +email.smtp.ssl.enabled=false +email.smtp.tls.enabled=false +email.smtp.username= +email.smtp.password= + +#TODO: move description elsewhere, remove bad default +# All emails sent by the system will be from the following address +#email.from.address=${user.name}@localhost +# All emails sent by the system will be from the following user name (used in conjunction with address) +#email.from.name=Unconfigured Username + +# If all email addresses (from new user registration) require an account validation email. +email.validation.required=true +# Timeout (in minutes) for the key generated for an email validation to remain valid. +# 2880 minutes = 48 hours +email.validation.timeout=2880 +# The subject line for the email message. +email.validation.subject=Welcome + +#TODO: move description elsewhere, remove bad default +# Get the Feedback to use for any outgoing emails. +# NOTE: if feedback.path starts with a "/" it is appended to the end of the value provided in application.url +# This value can be in the format/syntax of "/feedback.action" or even "mailto:feedback@application.com" +#email.feedback.path=/feedback.action + +#Set the application base URL. The default is to derive it from the HTTP request +#application.url=http://myurl.mycompany.com + +# -------------------------------------------------------------------- +# Auto Login Settings + +security.rememberme.enabled=true +# Timeout in minutes ( 525600 minutes = 1 year ) +security.rememberme.timeout=525600 + +# Single Sign On +# Timeout in minutes +security.signon.timeout=30 + +# -------------------------------------------------------------------- +# Default Username Values +redback.default.admin=admin + +# -------------------------------------------------------------------- +# Security Policies + +#security.policy.password.encoder= +security.policy.password.previous.count=6 +security.policy.password.expiration.enabled=true +security.policy.password.expiration.days=90 +security.policy.password.expiration.notify.days=10 +security.policy.allowed.login.attempt=10 + +# turn off the perclick enforcement of various security policies, slightly +# more heavyweight since it will ensure that the User object on each click +# is up to date +security.policy.strict.enforcement.enabled=true +security.policy.strict.force.password.change.enabled=true + +# -------------------------------------------------------------------- +# Password Rules +security.policy.password.rule.alphanumeric.enabled=false +security.policy.password.rule.alphacount.enabled=true +security.policy.password.rule.alphacount.minimum=1 +security.policy.password.rule.characterlength.enabled=true +security.policy.password.rule.characterlength.minimum=1 +security.policy.password.rule.characterlength.maximum=24 +security.policy.password.rule.musthave.enabled=true +security.policy.password.rule.numericalcount.enabled=true +security.policy.password.rule.numericalcount.minimum=1 +security.policy.password.rule.reuse.enabled=true +security.policy.password.rule.nowhitespace.enabled=true + +# -------------------------------------------------------------------- +# ldap settings +# +ldap.bind.authenticator.enabled=false + +# ldap options for configuration via properties file +#ldap.config.hostname= +#ldap.config.port= +#ldap.config.base.dn= +#ldap.config.context.factory= +#ldap.config.bind.dn= +#ldap.config.password= +#ldap.config.authentication.method= + +# config parameter for the ConfigurableUserManager +user.manager.impl=cached + + + diff --git a/redback-configuration/src/test/java/org/codehaus/plexus/redback/configuration/UserConfigurationTest.java b/redback-configuration/src/test/java/org/codehaus/plexus/redback/configuration/UserConfigurationTest.java new file mode 100644 index 000000000..9f8529990 --- /dev/null +++ b/redback-configuration/src/test/java/org/codehaus/plexus/redback/configuration/UserConfigurationTest.java @@ -0,0 +1,105 @@ +package org.codehaus.plexus.redback.configuration; + +/* + * Copyright 2001-2006 The Codehaus. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import junit.framework.TestCase; +import org.codehaus.plexus.util.StringUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * UserConfigurationTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +@RunWith( SpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +public class UserConfigurationTest + extends TestCase +{ + + @Inject @Named(value = "test") + UserConfiguration config; + + private void assertEmpty( String str ) + { + if ( StringUtils.isNotEmpty( str ) ) + { + fail( "Expected String to be empty." ); + } + } + + @Test + public void testLoad() + throws Exception + { + assertNotNull( config ); + // check that the configuration loaded correctly - if this fails, maybe you aren't in the right basedir + assertNotNull( config.getString( "test.value" ) ); + } + + @Test + public void testGetString() + throws Exception + { + // Test default configuration entry + assertEquals( "25", config.getString( "email.smtp.port" ) ); + // Test overlaid configuration entry + assertEquals( "127.0.2.2", config.getString( "email.smtp.host" ) ); + // Test default value + assertEquals( "127.0.0.1", config.getString( "email.smtp.host.bad", "127.0.0.1" ) ); +/* Requires commons-configuration 1.4 + // Test expressions + assertEquals( "jdbc:derby:" + System.getProperty( "plexus.home" ) + "/database;create=true", + config.getString( "jdbc.url" ) ); + assertEquals( "foo/bar", config.getString( "test.expression" ) ); +*/ + + assertEmpty( config.getString( "email.smtp.foo.foo" ) ); + } + + @Test + public void testGetBoolean() + throws Exception + { + assertFalse( config.getBoolean( "email.smtp.ssl.enabled" ) ); + assertFalse( config.getBoolean( "email.smtp.tls.enabled" ) ); + } + + @Test + public void testGetInt() + throws Exception + { + assertEquals( 25, config.getInt( "email.smtp.port" ) ); + assertEquals( 8080, config.getInt( "email.smtp.port.bad", 8080 ) ); + } + + @Test + public void testConcatenatedList() + { + assertEquals( "uid=brett,dc=codehaus,dc=org", config.getConcatenatedList( "ldap.bind.dn", null ) ); + assertEquals( "dc=codehaus,dc=org", config.getConcatenatedList( "ldap.base.dn", null ) ); + assertEquals( "foo", config.getConcatenatedList( "short.list", null ) ); + assertEquals( "bar,baz", config.getConcatenatedList( "no.list", "bar,baz" ) ); + } +} diff --git a/redback-configuration/src/test/resources/META-INF/spring-context.xml b/redback-configuration/src/test/resources/META-INF/spring-context.xml new file mode 100644 index 000000000..51d20fe68 --- /dev/null +++ b/redback-configuration/src/test/resources/META-INF/spring-context.xml @@ -0,0 +1,40 @@ +<?xml version="1.0"?> + +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd" + default-lazy-init="true"> + + + <bean name="test" class="org.codehaus.plexus.redback.configuration.UserConfiguration"> + <property name="configs"> + <list> + <value>src/test/resources/props/test-another.properties</value> + <value>src/test/resources/props/test-overlaid.properties</value> + </list> + </property> + </bean> + +</beans>
\ No newline at end of file diff --git a/redback-configuration/src/test/resources/props/test-another.properties b/redback-configuration/src/test/resources/props/test-another.properties new file mode 100644 index 000000000..a6da534c2 --- /dev/null +++ b/redback-configuration/src/test/resources/props/test-another.properties @@ -0,0 +1,4 @@ +email.smtp.host=127.0.2.2 +test.value=foo +test.expression=${org.codehaus.plexus.security.test.value}/bar +ldap.bind.dn=uid=brett,dc=codehaus,dc=org diff --git a/redback-configuration/src/test/resources/props/test-overlaid.properties b/redback-configuration/src/test/resources/props/test-overlaid.properties new file mode 100644 index 000000000..7ec32de46 --- /dev/null +++ b/redback-configuration/src/test/resources/props/test-overlaid.properties @@ -0,0 +1,6 @@ +email.smtp.host=127.0.1.1 +email.smtp.description=Testable Description +#can't do this, it's appended +#ldap.bind.dn=uid=user,dc=codehaus,dc=org +ldap.base.dn=dc=codehaus,dc=org +short.list=foo
\ No newline at end of file |