Browse Source

Add a task to load the native environment into some properties with a specified prefix

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270524 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 23 years ago
parent
commit
3bea6099f8
2 changed files with 105 additions and 0 deletions
  1. +102
    -0
      proposal/myrmidon/src/java/org/apache/antlib/nativelib/LoadEnvironment.java
  2. +3
    -0
      proposal/myrmidon/src/java/org/apache/antlib/nativelib/Resources.properties

+ 102
- 0
proposal/myrmidon/src/java/org/apache/antlib/nativelib/LoadEnvironment.java View File

@@ -0,0 +1,102 @@
/*
* 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.nativelib;

import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.api.AbstractTask;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.framework.exec.Environment;
import org.apache.myrmidon.framework.exec.ExecException;

/**
* This task is responsible for loading that OS-specific environment
* variables and adding them as propertys to the context with a specified
* prefix.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
*/
public class LoadEnvironment
extends AbstractTask
{
private static final Resources REZ =
ResourceManager.getPackageResources( LoadEnvironment.class );

private String m_prefix;

public void setPrefix( final String prefix )
{
m_prefix = prefix;
}

public void execute()
throws TaskException
{
if( null == m_prefix )
{
final String message = REZ.getString( "loadenv.no-prefix.error" );
throw new TaskException( message );
}

//Make sure prefix ends with a '.'
if( !m_prefix.endsWith( "." ) )
{
m_prefix += ".";
}

if( getLogger().isDebugEnabled() )
{
final String displayPrefix =
m_prefix.substring( 0, m_prefix.length() - 1 );
final String message =
REZ.getString( "loadenv.prefix.notice", displayPrefix );
getLogger().debug( message );
}

final Properties environment = loadNativeEnvironment();
final Iterator keys = environment.keySet().iterator();
while( keys.hasNext() )
{
final String key = (String)keys.next();
final String value = environment.getProperty( key );

if( value.equals( "" ) )
{
final String message = REZ.getString( "loadenv.ignoring-empty.warn", key );
getLogger().warn( message );
}
else
{
getContext().setProperty( m_prefix + key, value );
}
}
}

/**
* Utility method to load the native environment variables.
*/
private Properties loadNativeEnvironment()
throws TaskException
{
try
{
return Environment.getNativeEnvironment();
}
catch( final ExecException ee )
{
throw new TaskException( ee.getMessage(), ee );
}
catch( final IOException ioe )
{
throw new TaskException( ioe.getMessage(), ioe );
}
}
}

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

@@ -0,0 +1,3 @@
loadenv.no-prefix.error=No prefix specified for environment data.
loadenv.prefix.notice=Loading Environment with prefix {0}.
loadenv.ignoring-empty.warn=Key {0} in native OS environment is empty - ignoring key.

Loading…
Cancel
Save