PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/izpack-src/branches/branch-3-6/src/lib/com/izforge/izpack/util/IoHelper.java

https://github.com/jponge/izpack-full-svn-history-copy
Java | 130 lines | 79 code | 8 blank | 43 comment | 16 complexity | 1444cf2fe19e2ee2e3103f518c1bea6e MD5 | raw file
  1. /*
  2. * $Id$
  3. * IzPack
  4. * Copyright (C) 2004 Klaus Bartz
  5. *
  6. * File : IoHelper.java
  7. * Description : Helper for IO related stuff.
  8. * Author's email : bartzkau@users.berlios.de
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. package com.izforge.izpack.util;
  25. import java.util.Properties;
  26. import java.util.StringTokenizer;
  27. /**
  28. * <p>Class with some IO related helper.</p>
  29. *
  30. */
  31. public class IoHelper
  32. {
  33. private static Properties envVars = null;
  34. /**
  35. * Default constructor
  36. */
  37. private IoHelper()
  38. {
  39. }
  40. /**
  41. * Returns the value of the environment variable given
  42. * by key. This method is a work around for VM versions which
  43. * do not support getenv in an other way.
  44. * At the first call all environment variables will be loaded via
  45. * an exec.
  46. * @param key variable name for which the value should be resolved
  47. * @return the value of the environment variable given
  48. * by key
  49. */
  50. public static String getenv(String key)
  51. {
  52. if( envVars == null)
  53. loadEnv();
  54. return(String) ( envVars.get(key));
  55. }
  56. /**
  57. * Loads all environment variables via an exec.
  58. */
  59. private static void loadEnv()
  60. {
  61. envVars = new Properties();
  62. String[] output = new String[2];
  63. String[] params = null;
  64. String osname = System.getProperty("os.name").toLowerCase();
  65. if (osname.indexOf("windows 9") > -1)
  66. {
  67. params = new String[] { "command.com", "/c", "set" };
  68. }
  69. else
  70. if (osname.indexOf ("windows") > -1)
  71. {
  72. params = new String[] { "cmd.exe", "/c", "set" };
  73. }
  74. else
  75. {
  76. OsConstraint unixOs = new OsConstraint ("unix", null, null, null);
  77. if (unixOs.matchCurrentSystem())
  78. {
  79. params = new String[] { "env" };
  80. }
  81. }
  82. if (params == null)
  83. return;
  84. FileExecutor fe = new FileExecutor();
  85. fe.executeCommand(params, output);
  86. if( output[0].length() <= 0 )
  87. return;
  88. String lineSep = System.getProperty("line.separator");
  89. StringTokenizer st = new StringTokenizer(output[0], lineSep);
  90. String var = null;
  91. int index = 0;
  92. while(st.hasMoreTokens())
  93. {
  94. String line = st.nextToken();
  95. if( line.indexOf('=') == -1)
  96. { // May be a env var with a new line in it.
  97. if( var == null )
  98. {
  99. var = lineSep + line;
  100. }
  101. else
  102. {
  103. var += lineSep + line;
  104. }
  105. }
  106. else
  107. { // New var, perform the previous one.
  108. if( var != null )
  109. {
  110. index = var.indexOf('=');
  111. envVars.setProperty(var.substring(0, index), var.substring(index + 1));
  112. }
  113. var = line;
  114. }
  115. }
  116. if( var != null )
  117. { // Add last env var.
  118. index = var.indexOf('=');
  119. envVars.setProperty(var.substring(0, index), var.substring(index + 1));
  120. }
  121. }
  122. }