Browse Source

Add a task to load properties from a property file - optionally with a specified prefix

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270525 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 23 years ago
parent
commit
babc775431
3 changed files with 163 additions and 0 deletions
  1. +124
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/LoadProperties.java
  2. +34
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/PropertyLoader.java
  3. +5
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties

+ 124
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/LoadProperties.java View File

@@ -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 <a href="mailto:peter@apache.org">Peter Donald</a>
*/
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 );
}
}
}

+ 34
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/PropertyLoader.java View File

@@ -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;
}
}

+ 5
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties View File

@@ -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.


Loading…
Cancel
Save