diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/LoadProperties.java b/proposal/myrmidon/src/java/org/apache/antlib/core/LoadProperties.java new file mode 100644 index 000000000..63a8a78fc --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/LoadProperties.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.antlib.core; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.avalon.excalibur.io.IOUtil; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.AbstractContainerTask; + +/** + * This task loads properties from a property file and places them in the context. + * + * @author Peter Donald + */ +public class LoadProperties + extends AbstractContainerTask +{ + private static final Resources REZ = + ResourceManager.getPackageResources( LoadProperties.class ); + + private String m_prefix; + private File m_file; + + /** + * Specify the prefix to be placed before all properties (if any). + */ + public void setPrefix( final String prefix ) + { + m_prefix = prefix; + } + + public void setFile( final File file ) + { + m_file = file; + } + + public void execute() + throws TaskException + { + if( null == m_file ) + { + final String message = REZ.getString( "loadprop.no-file.error" ); + throw new TaskException( message ); + } + + //Make sure prefix ends with a '.' if specified + if( null == m_prefix ) + { + m_prefix = ""; + } + else if( !m_prefix.endsWith( "." ) ) + { + m_prefix += "."; + } + + loadFile( m_file ); + } + + /** + * Utility method to load properties file. + */ + private void loadFile( final File file ) + throws TaskException + { + if( getLogger().isDebugEnabled() ) + { + final String message = + REZ.getString( "loadprop.file.notice", file.getAbsolutePath() ); + getLogger().debug( message ); + } + + if( !file.exists() ) + { + final String message = + REZ.getString( "loadprop.missing-file.notice", file.getAbsolutePath() ); + getLogger().debug( message ); + } + else + { + FileInputStream input = null; + + try + { + input = new FileInputStream( file ); + final Properties properties = new PropertyLoader( this ); + properties.load( input ); + } + catch( final IOException ioe ) + { + throw new TaskException( ioe.getMessage(), ioe ); + } + + IOUtil.shutdownStream( input ); + } + } + + /** + * Utility method that will resolve and add specified proeprty. + * Used by external PropertyLoader class as a call back method. + */ + protected final void addUnresolvedValue( final String name, final String value ) + { + try + { + final Object objectValue = resolveValue( value.toString() ); + setProperty( m_prefix + name, objectValue ); + } + catch( final TaskException te ) + { + final String message = REZ.getString( "loadprop.bad-resolve.error", name, value ); + getLogger().info( message, te ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/PropertyLoader.java b/proposal/myrmidon/src/java/org/apache/antlib/core/PropertyLoader.java new file mode 100644 index 000000000..ebb742c13 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/PropertyLoader.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.antlib.core; + +import java.util.Properties; + +/** + * This class is an UGLY HACK utility class to enable us to reuse + * the property parsing and loading code from Properties object. + */ +class PropertyLoader + extends Properties +{ + private LoadProperties m_loadProperties; + + public PropertyLoader( LoadProperties loadProperties ) + { + m_loadProperties = loadProperties; + } + + /** + * Overidden put to add unresolved values. + */ + public synchronized Object put( Object key, Object value ) + { + m_loadProperties.addUnresolvedValue( key.toString(), value.toString() ); + return null; + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties b/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties index 6ece8908d..94fa15265 100644 --- a/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties @@ -4,6 +4,11 @@ property.multi-set.error=Value can not be set multiple times. property.no-name.error=Name must be specified. property.no-value.error=Value must be specified. +loadprop.no-file.error=No file specified to load properties from. +loadprop.file.notice=Loading proeprties from {0}. +loadprop.missing-file.notice=Unable to find property file: {0}. +loadprop.bad-resolve.error=Unable to resolve and set property named "{0}" to value "{1}". + convert.bad-byte.error=Error converting object ({0}) to Byte. convert.bad-class.error=Error converting object ({0}) to Class. convert.bad-double.error=Error converting object ({0}) to Double.