From 46e8d5e583cce93428bb4ded03054a610eb1dea6 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sun, 16 Dec 2001 12:10:29 +0000 Subject: [PATCH] Start testign integration of the framework across. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270231 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/myrmidon/framework/Os.java | 235 ++++++++++++++++++ .../myrmidon/framework/Resources.properties | 2 + 2 files changed, 237 insertions(+) create mode 100644 proposal/myrmidon/src/java/org/apache/myrmidon/framework/Os.java diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Os.java b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Os.java new file mode 100644 index 000000000..c8e3c01a9 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Os.java @@ -0,0 +1,235 @@ +/* + * 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 file. + */ +package org.apache.myrmidon.framework; + +import java.util.Locale; +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.myrmidon.api.TaskException; + +/** + * Class to help determining the OS. + * + * @author Stefan Bodewig + * @author Magesh Umasankar + * @author Peter Donald + */ +public class Os +{ + private static final Resources REZ = + ResourceManager.getPackageResources( Os.class ); + + private final static String m_osName = + System.getProperty( "os.name" ).toLowerCase( Locale.US ); + private final static String m_osArch = + System.getProperty( "os.arch" ).toLowerCase( Locale.US ); + private final static String m_osVersion = + System.getProperty( "os.version" ).toLowerCase( Locale.US ); + private final static String m_pathSep = + System.getProperty( "path.separator" ); + + private String m_arch; + private String m_family; + private String m_name; + private String m_version; + + public Os() + { + } + + public Os( final String family ) + { + setFamily( family ); + } + + /** + * Determines if the OS on which Ant is executing matches the given OS + * architecture. + * + * @param arch Description of Parameter + * @return The Arch value + */ + public static boolean isArch( final String arch ) + { + return isOs( null, null, arch, null ); + } + + /** + * Determines if the OS on which Ant is executing matches the given OS + * family. + * + * @param family Description of Parameter + * @return The Family value + * @since 1.5 + */ + public static boolean isFamily( final String family ) + { + return isOs( family, null, null, null ); + } + + /** + * Determines if the OS on which Ant is executing matches the given OS name. + * + * @param name Description of Parameter + * @return The Name value + * @since 1.7 + */ + public static boolean isName( final String name ) + { + return isOs( null, name, null, null ); + } + + /** + * Determines if the OS on which Ant is executing matches the given OS + * family, name, architecture and version + * + * @param family The OS family + * @param name The OS name + * @param arch The OS architecture + * @param version The OS version + * @return The Os value + */ + private static boolean isOs( final String family, + final String name, + final String arch, + final String version ) + { + boolean retValue = false; + + if( family != null || name != null || arch != null || version != null ) + { + boolean isFamily = true; + boolean isName = true; + boolean isArch = true; + boolean isVersion = true; + + if( family != null ) + { + if( family.equals( "windows" ) ) + { + isFamily = m_osName.indexOf( "windows" ) > -1; + } + else if( family.equals( "os/2" ) ) + { + isFamily = m_osName.indexOf( "os/2" ) > -1; + } + else if( family.equals( "netware" ) ) + { + isFamily = m_osName.indexOf( "netware" ) > -1; + } + else if( family.equals( "dos" ) ) + { + isFamily = m_pathSep.equals( ";" ) && !isFamily( "netware" ); + } + else if( family.equals( "mac" ) ) + { + isFamily = m_osName.indexOf( "mac" ) > -1; + } + else if( family.equals( "unix" ) ) + { + isFamily = m_pathSep.equals( ":" ) && + ( !isFamily( "mac" ) || m_osName.endsWith( "x" ) ); + } + else + { + final String message = REZ.getString( "unknown-os-family", family ); + throw new IllegalArgumentException( message ); + } + } + + if( name != null ) + { + isName = name.equals( m_osName ); + } + + if( arch != null ) + { + isArch = arch.equals( m_osArch ); + } + + if( version != null ) + { + isVersion = version.equals( m_osVersion ); + } + + retValue = isFamily && isName && isArch && isVersion; + } + return retValue; + } + + /** + * Determines if the OS on which Ant is executing matches the given OS + * version. + */ + public static boolean isVersion( final String version ) + { + return isOs( null, null, null, version ); + } + + /** + * Sets the desired OS architecture + * + * @param arch The OS architecture + */ + public void setArch( final String arch ) + { + m_arch = arch.toLowerCase( Locale.US ); + } + + /** + * Sets the desired OS family type + * + * @param f The OS family type desired
+ * Possible values:
+ * + * + */ + public void setFamily( String f ) + { + m_family = f.toLowerCase( Locale.US ); + } + + /** + * Sets the desired OS name + * + * @param name The OS name + */ + public void setName( String name ) + { + m_name = name.toLowerCase( Locale.US ); + } + + /** + * Sets the desired OS version + * + * @param version The OS version + */ + public void setVersion( String version ) + { + m_version = version.toLowerCase( Locale.US ); + } + + /** + * Determines if the OS on which Ant is executing matches the type of that + * set in setFamily. + * + * @see Os#setFamily(String) + */ + public boolean eval() + throws TaskException + { + return isOs( m_family, m_name, m_arch, m_version ); + } +} diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Resources.properties b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Resources.properties index 9d0791058..b7da51461 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Resources.properties +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/Resources.properties @@ -14,3 +14,5 @@ pattern.ifelse-duplicate.error=Can only set one of if/else for pattern data type type.no-factory.error=Unable to retrieve DataType factory from TypeManager. type.no-create.error=Unable to create datatype. type.no-id.error=Id must be specified. + +unknown-family=Don't know how to detect os family "{0}" \ No newline at end of file