akarasulu 2003/11/02 08:57:00
Added: merlin/util/env .cvsignore project.xml
merlin/util/env/src/java/org/apache/avalon/util/env Env.java
EnvAccessException.java
merlin/util/env/src/test/org/apache/avalon/util/env
EnvTest.java
Log:
Moving shell environment variable access code to util package.
Revision Changes Path
1.1 avalon/merlin/util/env/.cvsignore
Index: .cvsignore
===================================================================
.classpath
.project
target
1.1 avalon/merlin/util/env/project.xml
Index: project.xml
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<extend>${basedir}/../../project.xml</extend>
<groupId>avalon-util</groupId>
<id>avalon-util-env</id>
<name>Avalon Environment Utilities</name>
<currentVersion>1.0</currentVersion>
<package>org.apache.avalon.util.env</package>
<inceptionYear>2003</inceptionYear>
<shortDescription>Avalon utilities used to access shell environment
variables.</shortDescription>
<build>
<sourceDirectory>${basedir}/src/java/</sourceDirectory>
<unitTestSourceDirectory>${basedir}/src/test/</unitTestSourceDirectory>
<unitTest>
<includes>
<include>**/*Test.*</include>
<include>**/*TestCase.*</include>
</includes>
<excludes>
<exclude>**/Abstract*.*</exclude>
</excludes>
<resources>
<resource>
<directory>${basedir}/src/test</directory>
<includes>
<include>**/*.x*</include>
</includes>
</resource>
</resources>
</unitTest>
<resources>
<resource>
<directory>${basedir}/src/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}</directory>
<includes>
<include>snapshot.properties</include>
</includes>
</resource>
</resources>
<jars></jars>
</build>
</project>
1.1
avalon/merlin/util/env/src/java/org/apache/avalon/util/env/Env.java
Index: Env.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
"Apache Software Foundation" must not be used to endorse or promote
products derived from this software without prior written
permission. For written permission, please contact [EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.avalon.util.env ;
import java.io.File ;
import java.io.FileReader ;
import java.io.IOException ;
import java.io.PrintWriter ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
import java.util.Properties ;
import java.util.Enumeration ;
/**
* Encapsulates operating system and shell specific access to environment
* variables.
*
* @todo cleanup exception handling which is now in an odd state (constructors)
* need to be added and or changed.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a>
* @author $Author: akarasulu $
* @version $Revision: 1.1 $
*/
public class Env extends Properties
{
/** os.name System property */
public static final String OSNAME = System.getProperty( "os.name" ) ;
/** user.name System property */
public static final String USERNAME = System.getProperty( "user.name" ) ;
/** the user's platform specific shell executable */
private static String s_shell = null ;
/**
* Creates a snapshot of the current shell environment variables for a user.
*
* @throws EnvAccessException if there is an error accessing the environment
*/
public Env() throws EnvAccessException
{
Properties l_props = getEnvVariables() ;
Enumeration l_list = l_props.propertyNames() ;
while ( l_list.hasMoreElements() )
{
String l_key = ( String ) l_list.nextElement() ;
setProperty( l_key, l_props.getProperty( l_key ) ) ;
}
}
/**
* Gets the value of a shell environment variable.
*
* @param a_name the name of variable
* @return the String representation of an environment variable value
* @throws Exception if there is a problem accessing the environment
*/
public static String getEnvVariable( String a_name )
throws EnvAccessException
{
String l_osName = System.getProperty( "os.name" ) ;
if ( isUnix() )
{
return getUnixShellVariable( a_name ) ;
}
else if ( -1 != l_osName.indexOf( "Windows" ) )
{
if ( null == s_shell )
{
if ( -1 != l_osName.indexOf( "98" ) ||
-1 != l_osName.indexOf( "95" ) )
{
s_shell = "command.exe" ;
}
else
{
s_shell = "cmd.exe" ;
}
}
return getWindowsShellVariable( a_name ) ;
}
throw new EnvAccessException( a_name,
"Unrecognized operating system: " + l_osName ) ;
}
/**
* Checks to see if the operating system is a UNIX variant.
*
* @return true of the OS is a UNIX variant, false otherwise
*/
public static boolean isUnix()
{
if ( -1 != OSNAME.indexOf( "Linux" ) ||
-1 != OSNAME.indexOf( "SunOS" ) ||
-1 != OSNAME.indexOf( "Solaris" ) ||
-1 != OSNAME.indexOf( "MPE/iX" ) ||
-1 != OSNAME.indexOf( "AIX" ) ||
-1 != OSNAME.indexOf( "FreeBSD" ) ||
-1 != OSNAME.indexOf( "Irix" ) ||
-1 != OSNAME.indexOf( "Digital Unix" ) ||
-1 != OSNAME.indexOf( "HP-UX" ) )
{
return true ;
}
return false ;
}
/**
* Checks to see if the operating system is a Windows variant.
*
* @return true of the OS is a Windows variant, false otherwise
*/
public static boolean isWindows()
{
if ( -1 != OSNAME.indexOf( "Windows" ) )
{
return true ;
}
return false ;
}
/**
* Gets all environment variables within a Properties instance where the
* key is the environment variable name and value is the value of the
* property.
*
* @return the environment variables and values as Properties
*/
public static Properties getEnvVariables() throws EnvAccessException
{
if ( isUnix() )
{
return getUnixShellVariables() ;
}
if ( isWindows() )
{
return getWindowsShellVariables() ;
}
throw new EnvAccessException( "NA",
new UnsupportedOperationException( "Environment operations not "
+ "supported on unrecognized operatings system" ) ) ;
}
/**
* Gets the user's shell executable.
*
* @return the shell executable for the user
* @throws EnvAccessException the there is a problem accessing shell
* information
*/
public static String getUserShell() throws EnvAccessException
{
if ( isUnix() )
{
return getUnixUserShell() ;
}
if ( isWindows() )
{
return getWindowsUserShell() ;
}
throw new EnvAccessException( "NA",
new UnsupportedOperationException( "Environment operations not "
+ "supported on unrecognized operatings system" ) ) ;
}
// ------------------------------------------------------------------------
// Private UNIX Shell Operations
// ------------------------------------------------------------------------
/**
* Gets the default login shell used by a unix user.
*
* @return the UNIX user's default shell as referenced in /etc/passwd
*/
private static String getUnixUserShell()
throws EnvAccessException
{
if ( null != s_shell )
{
return s_shell ;
}
File l_etcpasswd = new File( "/etc/passwd" ) ;
if ( l_etcpasswd.exists() && l_etcpasswd.canRead() )
{
BufferedReader l_in = null ;
try
{
String l_entry = null ;
l_in = new BufferedReader( new FileReader( l_etcpasswd ) ) ;
while ( null != ( l_entry = l_in.readLine() ) )
{
// Skip entries other than the one for this username
if ( ! l_entry.startsWith( USERNAME ) )
{
continue ;
}
// Get the shell part of the passwd entry
int l_index = l_entry.lastIndexOf( ':' ) ;
if ( l_index == -1 )
{
throw new EnvAccessException( "NA",
"/etc/passwd contains malformed user entry for "
+ USERNAME ) ;
}
s_shell = l_entry.substring( l_index + 1 ) ;
return s_shell ;
}
}
catch ( IOException e )
{
throw new EnvAccessException( "NA", e ) ;
}
throw new EnvAccessException( "NA", "User " + USERNAME
+ " does not seem to exist in /etc/passwd" ) ;
}
throw new EnvAccessException( "NA", "Don't know what to do with"
+ " a UNIX system without a readable /etc/passwd file" ) ;
}
/**
* Gets a UNIX shell environment parameter by forking the user's default
* shell based on /etc/passwd and running echo on the environment variable
* within the shell. This should work on all UNIX shells like sh, ksh, csh,
* zsh and bash accross all the supported platforms.
*
* @param a_name the name of the variable accessed
* @return the value of the variable
* @throws EnvAccessException if there is an error accessing the value
*/
private static String getUnixShellVariable( String a_name )
throws EnvAccessException
{
String l_value = null ;
Process l_proc = null ;
// Read from process here
BufferedReader l_in = null ;
// Write to process here
PrintWriter l_out = null ;
StringBuffer l_cmd = new StringBuffer() ;
String l_osName = System.getProperty( "os.name" ) ;
l_cmd.append( "echo $" ) ;
l_cmd.append( a_name ) ;
// fire up the shell and get echo'd results on stdout
try
{
String [] l_args = { getUnixUserShell(), "-t" } ;
l_proc = Runtime.getRuntime().exec( l_args ) ;
l_out = new PrintWriter( l_proc.getOutputStream() ) ;
l_out.println( l_cmd.toString() ) ;
l_out.flush() ;
l_proc.waitFor() ;
l_in = new BufferedReader(
new InputStreamReader( l_proc.getInputStream() ) ) ;
l_value = l_in.readLine() ;
l_in.close() ;
l_out.close() ;
}
catch( Throwable t )
{
t.printStackTrace() ;
throw new EnvAccessException( a_name, t ) ;
}
finally
{
l_proc.destroy() ;
try
{
if ( null != l_in )
{
l_in.close() ;
}
if ( null != l_out )
{
l_out.close() ;
}
}
catch( IOException e )
{
}
}
// Check that we exited normally before returning an invalid output
if ( 0 == l_proc.exitValue() )
{
// Handle situations where the env property does not exist.
if ( l_value.trim().equals( "" ) )
{
return null ;
}
return l_value ;
}
throw new EnvAccessException( a_name, "Environment process failed "
+ " with non-zero exit code of " + l_proc.exitValue() ) ;
}
/**
* Adds a set of Windows variables to a set of properties.
*/
private static Properties getUnixShellVariables()
throws EnvAccessException
{
Process l_proc = null ;
Properties l_props = new Properties() ;
// Read from process here
BufferedReader l_in = null ;
// Write to process here
PrintWriter l_out = null ;
// fire up the shell and get echo'd results on stdout
try
{
String [] l_args = { getUnixUserShell(), "-t" } ;
l_proc = Runtime.getRuntime().exec( l_args ) ;
l_out = new PrintWriter( l_proc.getOutputStream() ) ;
l_out.println( "env" ) ;
l_out.flush() ;
l_in = new BufferedReader(
new InputStreamReader( l_proc.getInputStream() ) ) ;
String l_line = null ;
while ( null != ( l_line = l_in.readLine() ) )
{
int l_idx = l_line.indexOf( '=') ;
String l_name = l_line.substring( 0, l_idx ) ;
String l_value = l_line.substring( l_idx + 1, l_line.length() );
l_props.setProperty( l_name, l_value ) ;
}
l_proc.waitFor() ;
l_in.close() ;
l_out.close() ;
}
catch( Throwable t )
{
t.printStackTrace() ;
throw new EnvAccessException( "NA", t ) ;
}
finally
{
l_proc.destroy() ;
try
{
if ( null != l_in )
{
l_in.close() ;
}
if ( null != l_out )
{
l_out.close() ;
}
}
catch( IOException e )
{
}
}
// Check that we exited normally before returning an invalid output
if ( 0 != l_proc.exitValue() )
{
throw new EnvAccessException( "NA", "Environment process failed "
+ " with non-zero exit code of " + l_proc.exitValue() ) ;
}
return l_props ;
}
// ------------------------------------------------------------------------
// Private Windows Shell Operations
// ------------------------------------------------------------------------
/**
* Gets the shell used by the Windows user.
*
* @return the shell: cmd.exe or command.exe.
*/
private static String getWindowsUserShell()
{
if ( null != s_shell )
{
return s_shell ;
}
if ( -1 != OSNAME.indexOf( "98" ) || -1 != OSNAME.indexOf( "95" ) )
{
s_shell = "command.exe" ;
return s_shell ;
}
s_shell = "cmd.exe" ;
return s_shell ;
}
/**
* Adds a set of Windows variables to a set of properties.
*/
private static Properties getWindowsShellVariables()
throws EnvAccessException
{
String l_line = null ;
Process l_proc = null ;
BufferedReader l_in = null ;
Properties l_props = new Properties() ;
StringBuffer l_cmd = new StringBuffer() ;
// build the command based on the shell used: cmd.exe or command.exe
if ( -1 != OSNAME.indexOf( "98" ) || -1 != OSNAME.indexOf( "95" ) )
{
l_cmd.append( "command.exe /C SET" ) ;
}
else
{
l_cmd.append( "cmd.exe /C SET" ) ;
}
// fire up the shell and get echo'd results on stdout
try
{
l_proc = Runtime.getRuntime().exec( l_cmd.toString() ) ;
l_in = new BufferedReader(
new InputStreamReader( l_proc.getInputStream() ) ) ;
while ( null != ( l_line = l_in.readLine() ) )
{
int l_idx = l_line.indexOf( '=') ;
String l_name = l_line.substring( 0, l_idx ) ;
String l_value = l_line.substring( l_idx + 1, l_line.length() );
l_props.setProperty( l_name, l_value ) ;
}
l_proc.waitFor() ;
l_in.close() ;
}
catch( Throwable t )
{
throw new EnvAccessException( "ALL", t ) ;
}
finally
{
l_proc.destroy() ;
try
{
if ( null != l_in )
{
l_in.close() ;
}
}
catch( IOException e )
{
}
}
if ( 0 != l_proc.exitValue() )
{
throw new EnvAccessException( "ALL", "Environment process failed"
+ " with non-zero exit code of " + l_proc.exitValue() ) ;
}
return l_props ;
}
/**
* Gets the value for a windows command shell environment variable.
*
* @param a_name the name of the variable
* @return the value of the variable
* @throws EnvAccessException if there is an error accessing the value
*/
private static String getWindowsShellVariable( String a_name )
throws EnvAccessException
{
String l_value = null ;
Process l_proc = null ;
BufferedReader l_in = null ;
StringBuffer l_cmd = new StringBuffer( getWindowsUserShell() ) ;
l_cmd.append( " /C echo %" ) ;
l_cmd.append( a_name ) ;
l_cmd.append( '%' ) ;
// fire up the shell and get echo'd results on stdout
try
{
l_proc = Runtime.getRuntime().exec( l_cmd.toString() ) ;
l_in = new BufferedReader(
new InputStreamReader( l_proc.getInputStream() ) ) ;
l_value = l_in.readLine() ;
l_proc.waitFor() ;
l_in.close() ;
}
catch( Throwable t )
{
throw new EnvAccessException( a_name, t ) ;
}
finally
{
l_proc.destroy() ;
try
{
if ( null != l_in )
{
l_in.close() ;
}
}
catch( IOException e )
{
}
}
if ( 0 == l_proc.exitValue() )
{
// Handle situations where the env property does not exist.
if ( l_value.startsWith( "%") && l_value.endsWith( "%" ) )
{
return null ;
}
return l_value ;
}
throw new EnvAccessException( a_name, "Environment process failed"
+ " with non-zero exit code of " + l_proc.exitValue() ) ;
}
}
1.1
avalon/merlin/util/env/src/java/org/apache/avalon/util/env/EnvAccessException.java
Index: EnvAccessException.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
"Apache Software Foundation" must not be used to endorse or promote
products derived from this software without prior written
permission. For written permission, please contact [EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.avalon.util.env;
/**
* A simple wrapper exception around exceptions that could occur while accessing
* environment parameters.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
* @version $Revision: 1.1 $
*/
public class EnvAccessException extends Exception
{
/** the environment variable name if available */
public final String m_variable ;
public final Throwable m_cause;
/**
* Creates an exception denoting a failure while attempting to access an
* environment variable within an operating system and shell specific
* environment that is caused by another exception.
*
* @param a_variable the variable whose value was to be accessed
* @param a_message the reason for the access failure
* @param a_cause the underlying exception that caused the failure
*/
EnvAccessException( final String a_variable, final Throwable a_cause )
{
super() ;
m_variable = a_variable ;
m_cause = a_cause;
}
/**
* Creates an exception denoting a failure while attempting to access an
* environment variable within an operating system and shell specific
* environment.
*
* @param a_variable the variable whose value was to be accessed
* @param a_message the reason for the access failure
*/
EnvAccessException( final String a_variable, final String a_message )
{
super( a_message ) ;
m_variable = a_variable ;
m_cause = null;
}
/**
* Gets the variable that was to be accessed.
*
* @return the value of the variable
*/
public String getVariable()
{
return m_variable ;
}
/**
* Return the causal exception.
*
* @return the exception that caused this exception (possibly null)
*/
public Throwable getCause()
{
return m_cause;
}
/**
* Prepends variable name to the base message.
*
* @see java.lang.Throwable#getMessage()
*/
public String getMessage()
{
String l_base = super.getMessage() ;
if ( null == l_base )
{
return "Failed to access " + m_variable + " environment variable" ;
}
return "Failed to access " + m_variable
+ " environment variable - " + l_base ;
}
}
1.1
avalon/merlin/util/env/src/test/org/apache/avalon/util/env/EnvTest.java
Index: EnvTest.java
===================================================================
package org.apache.avalon.util.env;
import junit.framework.TestCase ;
/**
* Env tests.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a>
* @author $Author: akarasulu $
* @version $Revision: 1.1 $
*/
public class EnvTest extends TestCase
{
public static void main(String[] args)
{
junit.textui.TestRunner.run(EnvTest.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception
{
super.tearDown();
}
/**
* Constructor for EnvTest.
* @param arg0
*/
public EnvTest(String arg0)
{
super(arg0);
}
final public void testEnvVariable()
throws Exception
{
if ( Env.isUnix() )
{
assertNotNull( Env.getEnvVariable( "PATH" ) ) ;
}
if ( Env.isWindows() )
{
assertNotNull( Env.getEnvVariable( "Path" ) ) ;
}
assertNull( Env.getEnvVariable( "PAT" ) ) ;
EnvAccessException l_error = null ;
try
{
// All shells and their echo's will freak out when they see this
Env.getEnvVariable( "--*&^%^%$" ) ;
}
catch( EnvAccessException e )
{
l_error = e ;
}
assertNotNull( l_error ) ;
}
public void testEnv()
throws EnvAccessException
{
Env l_env = new Env() ;
assertNull( l_env.getProperty( "PAT" ) ) ;
if ( Env.isUnix() )
{
assertNotNull( l_env.getProperty( "PATH" ) ) ;
}
if ( Env.isWindows() )
{
assertNotNull( l_env.getProperty( "Path" ) ) ;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]