Coverage Report - org.mule.util.SystemUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SystemUtils
0%
0/114
0%
0/66
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.util;
 8  
 
 9  
 import org.mule.api.DefaultMuleException;
 10  
 
 11  
 import java.io.BufferedReader;
 12  
 import java.io.InputStreamReader;
 13  
 import java.lang.reflect.Method;
 14  
 import java.util.Collections;
 15  
 import java.util.HashMap;
 16  
 import java.util.Map;
 17  
 
 18  
 import org.apache.commons.cli.BasicParser;
 19  
 import org.apache.commons.cli.CommandLine;
 20  
 import org.apache.commons.cli.Option;
 21  
 import org.apache.commons.cli.Options;
 22  
 import org.apache.commons.cli.ParseException;
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 // @ThreadSafe
 27  
 
 28  0
 public class SystemUtils extends org.apache.commons.lang.SystemUtils
 29  
 {
 30  
     // class logger
 31  0
     protected static final Log logger = LogFactory.getLog(SystemUtils.class);
 32  
 
 33  
     // bash prepends: declare -x
 34  
     // zsh prepends: typeset -x
 35  0
     private static final String[] UNIX_ENV_PREFIXES = new String[]{"declare -", "typeset -"};
 36  
 
 37  
     // the environment of the VM process
 38  0
     private static Map environment = null;
 39  
 
 40  
     /**
 41  
      * Get the operating system environment variables. This should work for Windows
 42  
      * and Linux.
 43  
      *
 44  
      * @return Map<String, String> or an empty map if there was an error.
 45  
      */
 46  
     public static synchronized Map getenv()
 47  
     {
 48  0
         if (environment == null)
 49  
         {
 50  
             try
 51  
             {
 52  0
                 if (SystemUtils.IS_JAVA_1_4)
 53  
                 {
 54  
                     // fallback to external process
 55  0
                     environment = Collections.unmodifiableMap(getenvJDK14());
 56  
                 }
 57  
                 else
 58  
                 {
 59  
                     // the following runaround is necessary since we still want to
 60  
                     // compile on JDK 1.4
 61  0
                     Class target = System.class;
 62  0
                     Method envMethod = target.getMethod("getenv", ArrayUtils.EMPTY_CLASS_ARRAY);
 63  0
                     environment = Collections.unmodifiableMap((Map) envMethod.invoke(target, (Object[]) null));
 64  
                 }
 65  
             }
 66  0
             catch (Exception ex)
 67  
             {
 68  0
                 logger.error("Could not access OS environment: ", ex);
 69  0
                 environment = Collections.EMPTY_MAP;
 70  0
             }
 71  
         }
 72  
 
 73  0
         return environment;
 74  
     }
 75  
 
 76  
     private static Map getenvJDK14() throws Exception
 77  
     {
 78  0
         Map env = new HashMap();
 79  0
         Process process = null;
 80  
 
 81  
         try
 82  
         {
 83  0
             boolean isUnix = true;
 84  
             String command;
 85  
 
 86  0
             if (SystemUtils.IS_OS_WINDOWS)
 87  
             {
 88  0
                 command = "cmd /c set";
 89  0
                 isUnix = false;
 90  
             }
 91  
             else
 92  
             {
 93  0
                 command = "env";
 94  
             }
 95  
 
 96  0
             process = Runtime.getRuntime().exec(command);
 97  0
             BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
 98  
 
 99  
             String line;
 100  0
             while ((line = br.readLine()) != null)
 101  
             {
 102  0
                 for (int prefix = 0; prefix < UNIX_ENV_PREFIXES.length; prefix++)
 103  
                 {
 104  0
                     if (line.startsWith(UNIX_ENV_PREFIXES[prefix]))
 105  
                     {
 106  0
                         line = line.substring(UNIX_ENV_PREFIXES[prefix].length());
 107  
                     }
 108  
                 }
 109  
 
 110  0
                 int index = -1;
 111  0
                 if ((index = line.indexOf('=')) > -1)
 112  
                 {
 113  0
                     String key = line.substring(0, index).trim();
 114  0
                     String value = line.substring(index + 1).trim();
 115  
                     // remove quotes, if any
 116  0
                     if (isUnix && value.length() > 1 && (value.startsWith("\"") || value.startsWith("'")))
 117  
                     {
 118  0
                         value = value.substring(1, value.length() - 1);
 119  
                     }
 120  0
                     env.put(key, value);
 121  0
                 }
 122  
                 else
 123  
                 {
 124  0
                     env.put(line, StringUtils.EMPTY);
 125  
                 }
 126  0
             }
 127  
         }
 128  0
         catch (Exception e)
 129  
         {
 130  0
             throw e; // bubble up
 131  
         }
 132  
         finally
 133  
         {
 134  0
             if (process != null)
 135  
             {
 136  0
                 process.destroy();
 137  
             }
 138  
         }
 139  
 
 140  0
         return env;
 141  
     }
 142  
 
 143  
     public static String getenv(String name)
 144  
     {
 145  0
         return (String) SystemUtils.getenv().get(name);
 146  
     }
 147  
 
 148  
     public static boolean isSunJDK()
 149  
     {
 150  0
         return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("SUN") != -1;
 151  
     }
 152  
 
 153  
     public static boolean isAppleJDK()
 154  
     {
 155  0
         return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("APPLE") != -1;
 156  
     }
 157  
 
 158  
     public static boolean isIbmJDK()
 159  
     {
 160  0
         return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("IBM") != -1;
 161  
     }
 162  
 
 163  
     // TODO MULE-1947 Command-line arguments should be handled exclusively by the bootloader
 164  
 
 165  
     private static CommandLine parseCommandLine(String args[], String opts[][]) throws DefaultMuleException
 166  
     {
 167  0
         Options options = new Options();
 168  0
         for (int i = 0; i < opts.length; i++)
 169  
         {
 170  0
             options.addOption(opts[i][0], opts[i][1].equals("true") ? true : false, opts[i][2]);
 171  
         }
 172  
 
 173  0
         BasicParser parser = new BasicParser();
 174  
 
 175  
         try
 176  
         {
 177  0
             CommandLine line = parser.parse(options, args, true);
 178  0
             if (line == null)
 179  
             {
 180  0
                 throw new DefaultMuleException("Unknown error parsing the Mule command line");
 181  
             }
 182  
 
 183  0
             return line;
 184  
         }
 185  0
         catch (ParseException p)
 186  
         {
 187  0
             throw new DefaultMuleException("Unable to parse the Mule command line because of: " + p.toString(), p);
 188  
         }
 189  
     }
 190  
 
 191  
     /**
 192  
      * Returns the value corresponding to the given option from the command line, for
 193  
      * example if the options are "-config mule-config.xml"
 194  
      * getCommandLineOption("config") would return "mule-config.xml"
 195  
      */
 196  
     // TODO MULE-1947 Command-line arguments should be handled exclusively by the bootloader
 197  
     public static String getCommandLineOption(String option, String args[], String opts[][])
 198  
             throws DefaultMuleException
 199  
     {
 200  0
         CommandLine line = parseCommandLine(args, opts);
 201  0
         return line.getOptionValue(option);
 202  
     }
 203  
 
 204  
     /**
 205  
      * Checks whether a command line option is set. This is useful for command line
 206  
      * options that don't have an argument, like "-cluster", which means that this
 207  
      * Mule instance is part of a cluster.
 208  
      */
 209  
     // TODO MULE-1947 Command-line arguments should be handled exclusively by the bootloader
 210  
     public static boolean hasCommandLineOption(String option, String args[], String opts[][])
 211  
             throws DefaultMuleException
 212  
     {
 213  0
         CommandLine line = parseCommandLine(args, opts);
 214  0
         return line.hasOption(option);
 215  
     }
 216  
 
 217  
     /**
 218  
      * Returns a Map of all options in the command line. The Map is keyed off the
 219  
      * option name. The value will be whatever is present on the command line.
 220  
      * Options that don't have an argument will have the String "true".
 221  
      */
 222  
     // TODO MULE-1947 Command-line arguments should be handled exclusively by the bootloader
 223  
     public static Map<String, Object> getCommandLineOptions(String args[], String opts[][]) throws DefaultMuleException
 224  
     {
 225  0
         CommandLine line = parseCommandLine(args, opts);
 226  0
         Map<String, Object> ret = new HashMap<String, Object>();
 227  0
         Option[] options = line.getOptions();
 228  
 
 229  0
         for (int i = 0; i < options.length; i++)
 230  
         {
 231  0
             Option option = options[i];
 232  0
             ret.put(option.getOpt(), option.getValue("true"));
 233  
         }
 234  
 
 235  0
         return ret;
 236  
     }
 237  
 
 238  
     /**
 239  
      * Returns a Map of all valid property definitions in <code>-Dkey=value</code>
 240  
      * format. <code>-Dkey</code> is interpreted as <code>-Dkey=true</code>,
 241  
      * everything else is ignored. Whitespace in values is properly handled but needs
 242  
      * to be quoted properly: <code>-Dkey="some value"</code>.
 243  
      *
 244  
      * @param input String with property definitionn
 245  
      * @return a {@link Map} of property String keys with their defined values
 246  
      *         (Strings). If no valid key-value pairs can be parsed, the map is
 247  
      *         empty.
 248  
      */
 249  
     public static Map<String, String> parsePropertyDefinitions(String input)
 250  
     {
 251  0
         if (StringUtils.isEmpty(input))
 252  
         {
 253  0
             return Collections.emptyMap();
 254  
         }
 255  
 
 256  
         // the result map of property key/value pairs
 257  0
         final Map<String, String> result = new HashMap<String, String>();
 258  
 
 259  
         // where to begin looking for key/value tokens
 260  0
         int tokenStart = 0;
 261  
 
 262  
         // this is the main loop that scans for all tokens
 263  
         findtoken:
 264  0
         while (tokenStart < input.length())
 265  
         {
 266  
             // find first definition or bail
 267  0
             tokenStart = StringUtils.indexOf(input, "-D", tokenStart);
 268  0
             if (tokenStart == StringUtils.INDEX_NOT_FOUND)
 269  
             {
 270  0
                 break findtoken;
 271  
             }
 272  
             else
 273  
             {
 274  
                 // skip leading -D
 275  0
                 tokenStart += 2;
 276  
             }
 277  
 
 278  
             // find key
 279  0
             int keyStart = tokenStart;
 280  0
             int keyEnd = keyStart;
 281  
 
 282  0
             if (keyStart == input.length())
 283  
             {
 284  
                 // short input: '-D' only
 285  0
                 break;
 286  
             }
 287  
 
 288  
             // let's check out what we have next
 289  0
             char cursor = input.charAt(keyStart);
 290  
 
 291  
             // '-D xxx'
 292  0
             if (cursor == ' ')
 293  
             {
 294  0
                 continue findtoken;
 295  
             }
 296  
 
 297  
             // '-D='
 298  0
             if (cursor == '=')
 299  
             {
 300  
                 // skip over garbage to next potential definition
 301  0
                 tokenStart = StringUtils.indexOf(input, ' ', tokenStart);
 302  0
                 if (tokenStart != StringUtils.INDEX_NOT_FOUND)
 303  
                 {
 304  
                     // '-D= ..' - continue with next token
 305  0
                     continue findtoken;
 306  
                 }
 307  
                 else
 308  
                 {
 309  
                     // '-D=' - get out of here
 310  
                     break findtoken;
 311  
                 }
 312  
             }
 313  
 
 314  
             // apparently there's a key, so find the end
 315  
             findkey:
 316  0
             while (keyEnd < input.length())
 317  
             {
 318  0
                 cursor = input.charAt(keyEnd);
 319  
 
 320  
                 // '-Dkey ..'
 321  0
                 if (cursor == ' ')
 322  
                 {
 323  0
                     tokenStart = keyEnd;
 324  0
                     break findkey;
 325  
                 }
 326  
 
 327  
                 // '-Dkey=..'
 328  0
                 if (cursor == '=')
 329  
                 {
 330  0
                     break findkey;
 331  
                 }
 332  
 
 333  
                 // keep looking
 334  0
                 keyEnd++;
 335  
             }
 336  
 
 337  
             // yay, finally a key
 338  0
             String key = StringUtils.substring(input, keyStart, keyEnd);
 339  
 
 340  
             // assume that there is no value following
 341  0
             int valueStart = keyEnd;
 342  0
             int valueEnd = keyEnd;
 343  
 
 344  
             // default value
 345  0
             String value = "true";
 346  
 
 347  
             // now find the value, but only if the current cursor is not a space
 348  0
             if (keyEnd < input.length() && cursor != ' ')
 349  
             {
 350  
                 // bump value start/end
 351  0
                 valueStart = keyEnd + 1;
 352  0
                 valueEnd = valueStart;
 353  
 
 354  
                 // '-Dkey="..'
 355  0
                 cursor = input.charAt(valueStart);
 356  0
                 if (cursor == '"')
 357  
                 {
 358  
                     // opening "
 359  0
                     valueEnd = StringUtils.indexOf(input, '"', ++valueStart);
 360  
                 }
 361  
                 else
 362  
                 {
 363  
                     // unquoted value
 364  0
                     valueEnd = StringUtils.indexOf(input, ' ', valueStart);
 365  
                 }
 366  
 
 367  
                 // no '"' or ' ' delimiter found - use the rest of the string
 368  0
                 if (valueEnd == StringUtils.INDEX_NOT_FOUND)
 369  
                 {
 370  0
                     valueEnd = input.length();
 371  
                 }
 372  
 
 373  
                 // create value
 374  0
                 value = StringUtils.substring(input, valueStart, valueEnd);
 375  
             }
 376  
 
 377  
             // finally create key and value && loop again for next token
 378  0
             result.put(key, value);
 379  
 
 380  
             // start next search at end of value
 381  0
             tokenStart = valueEnd;
 382  0
         }
 383  
 
 384  0
         return result;
 385  
     }
 386  
 
 387  
 }