/plugins/JavaSideKick/tags/javasidekick-3-1-1/src/sidekick/java/util/Os.java

# · Java · 425 lines · 207 code · 30 blank · 188 comment · 84 complexity · 9e596c1ac45715a364db2664ebabd87f MD5 · raw file

  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package sidekick.java.util;
  55. import java.io.*;
  56. import java.util.Hashtable;
  57. import java.util.Locale;
  58. import java.util.Vector;
  59. /**
  60. * ***** Dale Anson: Modified to not be Ant specific by removing "extends Condition"
  61. * and replacing BuildExceptions. Added getEnvironmentValue and associated methods.
  62. * *****
  63. * Condition that tests the OS type.
  64. *
  65. * @author Stefan Bodewig
  66. * @author Magesh Umasankar
  67. * @since Ant 1.4
  68. * @version $Revision: 13411 $
  69. */
  70. public class Os {
  71. private static final String OS_NAME =
  72. System.getProperty( "os.name" ).toLowerCase( Locale.US );
  73. private static final String OS_ARCH =
  74. System.getProperty( "os.arch" ).toLowerCase( Locale.US );
  75. private static final String OS_VERSION =
  76. System.getProperty( "os.version" ).toLowerCase( Locale.US );
  77. private static final String PATH_SEP =
  78. System.getProperty( "path.separator" );
  79. private String family;
  80. private String name;
  81. private String version;
  82. private String arch;
  83. private static Hashtable<String, String> environment;
  84. /**
  85. * Default constructor
  86. *
  87. */
  88. public Os() {}
  89. /**
  90. * Constructor that sets the family attribute
  91. *
  92. * @param family a String value
  93. */
  94. public Os( String family ) {
  95. setFamily( family );
  96. }
  97. /**
  98. * Sets the desired OS family type
  99. *
  100. * @param f The OS family type desired<br />
  101. * Possible values:<br />
  102. * <ul>
  103. * <li>dos</li>
  104. * <li>mac</li>
  105. * <li>netware</li>
  106. * <li>os/2</li>
  107. * <li>tandem</li>
  108. * <li>unix</li>
  109. * <li>windows</li>
  110. * <li>win9x</li>
  111. * <li>z/os</li>
  112. * <li>os/400</li>
  113. * </ul>
  114. */
  115. public void setFamily( String f ) {
  116. family = f.toLowerCase( Locale.US );
  117. }
  118. /**
  119. * Sets the desired OS name
  120. *
  121. * @param name The OS name
  122. */
  123. public void setName( String name ) {
  124. this.name = name.toLowerCase( Locale.US );
  125. }
  126. /**
  127. * Sets the desired OS architecture
  128. *
  129. * @param arch The OS architecture
  130. */
  131. public void setArch( String arch ) {
  132. this.arch = arch.toLowerCase( Locale.US );
  133. }
  134. /**
  135. * Sets the desired OS version
  136. *
  137. * @param version The OS version
  138. */
  139. public void setVersion( String version ) {
  140. this.version = version.toLowerCase( Locale.US );
  141. }
  142. /**
  143. * Determines if the OS on which Ant is executing matches the type of
  144. * that set in setFamily.
  145. * @see Os#setFamily(String)
  146. */
  147. public boolean eval() {
  148. return isOs( family, name, arch, version );
  149. }
  150. /**
  151. * Determines if the OS on which Ant is executing matches the
  152. * given OS family.
  153. * @param family the family to check for
  154. * @return true if the OS matches
  155. * @since 1.5
  156. */
  157. public static boolean isFamily( String family ) {
  158. return isOs( family, null, null, null );
  159. }
  160. /**
  161. * Determines if the OS on which Ant is executing matches the
  162. * given OS name.
  163. *
  164. * @param name the OS name to check for
  165. * @return true if the OS matches
  166. * @since 1.7
  167. */
  168. public static boolean isName( String name ) {
  169. return isOs( null, name, null, null );
  170. }
  171. /**
  172. * Determines if the OS on which Ant is executing matches the
  173. * given OS architecture.
  174. *
  175. * @param arch the OS architecture to check for
  176. * @return true if the OS matches
  177. * @since 1.7
  178. */
  179. public static boolean isArch( String arch ) {
  180. return isOs( null, null, arch, null );
  181. }
  182. /**
  183. * Determines if the OS on which Ant is executing matches the
  184. * given OS version.
  185. *
  186. * @param version the OS version to check for
  187. * @return true if the OS matches
  188. * @since 1.7
  189. */
  190. public static boolean isVersion( String version ) {
  191. return isOs( null, null, null, version );
  192. }
  193. /**
  194. * Determines if the OS on which Ant is executing matches the
  195. * given OS family, name, architecture and version
  196. *
  197. * @param family The OS family
  198. * @param name The OS name
  199. * @param arch The OS architecture
  200. * @param version The OS version
  201. * @return true if the OS matches
  202. * @since 1.7
  203. */
  204. public static boolean isOs( String family, String name, String arch,
  205. String version ) {
  206. boolean retValue = false;
  207. if ( family != null || name != null || arch != null
  208. || version != null ) {
  209. boolean isFamily = true;
  210. boolean isName = true;
  211. boolean isArch = true;
  212. boolean isVersion = true;
  213. if ( family != null ) {
  214. if ( family.equals( "windows" ) ) {
  215. isFamily = OS_NAME.indexOf( "windows" ) > -1;
  216. }
  217. else if ( family.equals( "os/2" ) ) {
  218. isFamily = OS_NAME.indexOf( "os/2" ) > -1;
  219. }
  220. else if ( family.equals( "netware" ) ) {
  221. isFamily = OS_NAME.indexOf( "netware" ) > -1;
  222. }
  223. else if ( family.equals( "dos" ) ) {
  224. isFamily = PATH_SEP.equals( ";" ) && !isFamily( "netware" );
  225. }
  226. else if ( family.equals( "mac" ) ) {
  227. isFamily = OS_NAME.indexOf( "mac" ) > -1;
  228. }
  229. else if ( family.equals( "tandem" ) ) {
  230. isFamily = OS_NAME.indexOf( "nonstop_kernel" ) > -1;
  231. }
  232. else if ( family.equals( "unix" ) ) {
  233. isFamily = PATH_SEP.equals( ":" )
  234. && !isFamily( "openvms" )
  235. && ( !isFamily( "mac" ) || OS_NAME.endsWith( "x" ) );
  236. }
  237. else if ( family.equals( "win9x" ) ) {
  238. isFamily = isFamily( "windows" )
  239. && ( OS_NAME.indexOf( "95" ) >= 0
  240. || OS_NAME.indexOf( "98" ) >= 0
  241. || OS_NAME.indexOf( "me" ) >= 0
  242. || OS_NAME.indexOf( "ce" ) >= 0 );
  243. }
  244. else if ( family.equals( "z/os" ) ) {
  245. isFamily = OS_NAME.indexOf( "z/os" ) > -1
  246. || OS_NAME.indexOf( "os/390" ) > -1;
  247. }
  248. else if ( family.equals( "os/400" ) ) {
  249. isFamily = OS_NAME.indexOf( "os/400" ) > -1;
  250. }
  251. else if ( family.equals( "openvms" ) ) {
  252. isFamily = OS_NAME.indexOf( "openvms" ) > -1;
  253. }
  254. else {
  255. /*
  256. throw new BuildException(
  257. "Don\'t know how to detect os family \""
  258. + family + "\"");
  259. */
  260. return false;
  261. }
  262. }
  263. if ( name != null ) {
  264. isName = name.equals( OS_NAME );
  265. }
  266. if ( arch != null ) {
  267. isArch = arch.equals( OS_ARCH );
  268. }
  269. if ( version != null ) {
  270. isVersion = version.equals( OS_VERSION );
  271. }
  272. retValue = isFamily && isName && isArch && isVersion;
  273. }
  274. return retValue;
  275. }
  276. /**
  277. * Dale Anson: One of the main reasons I borrowed this class from Ant was to
  278. * be able to read environment variables. It makes sense to add a method to
  279. * easily fetch the value of an environment variable here.
  280. * @param name the name of an environment variable. Much of this code was
  281. * copied from org.apache.tools.ant.taskdefs.Execute.
  282. * @return the value of the environment variable, or null if there is no value
  283. * for the given name
  284. */
  285. public static String getEnvironmentValue( String name ) {
  286. if ( environment != null ) {
  287. return ( String ) environment.get( name );
  288. }
  289. environment = new Hashtable<String, String>();
  290. try {
  291. String[] env_cmd = getProcEnvCommand();
  292. Process process = Runtime.getRuntime().exec( env_cmd );
  293. InputStream is = new BufferedInputStream( process.getInputStream() );
  294. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  295. CopyUtils.copy( is, baos );
  296. BufferedReader in = new BufferedReader( new StringReader( baos.toString() ) );
  297. // this portion copied from org.apache.tools.ant.taskdefs.Execute //
  298. Vector<String> procEnvironment = new Vector<String>();
  299. String var = null;
  300. String line, lineSep = System.getProperty( "line.separator" );
  301. while ( ( line = in.readLine() ) != null ) {
  302. if ( line.indexOf( '=' ) == -1 ) {
  303. // Chunk part of previous env var (UNIX env vars can
  304. // contain embedded new lines).
  305. if ( var == null ) {
  306. var = lineSep + line;
  307. }
  308. else {
  309. var += lineSep + line;
  310. }
  311. }
  312. else {
  313. // New env var...append the previous one if we have it.
  314. if ( var != null ) {
  315. procEnvironment.addElement( var );
  316. }
  317. var = line;
  318. }
  319. }
  320. // Since we "look ahead" before adding, there's one last env var.
  321. if ( var != null ) {
  322. procEnvironment.addElement( var );
  323. }
  324. // end copy from Execute //
  325. // now split out the names from the values and populate a Hashtable
  326. if ( procEnvironment.size() > 0 ) {
  327. java.util.Iterator it = procEnvironment.iterator();
  328. while ( it.hasNext() ) {
  329. var = ( String ) it.next();
  330. int index = var.indexOf( "=" );
  331. String key = var.substring( 0, index );
  332. String value = var.substring( index + 1 );
  333. environment.put( key, value );
  334. }
  335. }
  336. }
  337. catch ( Exception ignored ) {}
  338. return getEnvironmentValue( name );
  339. }
  340. /**
  341. * Dale Anson: Copied from org.apache.tools.ant.taskdefs.Execute. It's a private method in
  342. * that class, so I can't access it directly.
  343. */
  344. private static String[] getProcEnvCommand() {
  345. if ( Os.isFamily( "os/2" ) ) {
  346. // OS/2 - use same mechanism as Windows 2000
  347. String[] cmd = {"cmd", "/c", "set" };
  348. return cmd;
  349. }
  350. else if ( Os.isFamily( "windows" ) ) {
  351. // Determine if we're running under XP/2000/NT or 98/95
  352. if ( !Os.isFamily( "win9x" ) ) {
  353. // Windows XP/2000/NT
  354. String[] cmd = {"cmd", "/c", "set" };
  355. return cmd;
  356. }
  357. else {
  358. // Windows 98/95
  359. String[] cmd = {"command.com", "/c", "set" };
  360. return cmd;
  361. }
  362. }
  363. else if ( Os.isFamily( "z/os" ) || Os.isFamily( "unix" ) ) {
  364. // On most systems one could use: /bin/sh -c env
  365. // Some systems have /bin/env, others /usr/bin/env, just try
  366. String[] cmd = new String[ 1 ];
  367. if ( new File( "/bin/env" ).canRead() ) {
  368. cmd[ 0 ] = "/bin/env";
  369. }
  370. else if ( new File( "/usr/bin/env" ).canRead() ) {
  371. cmd[ 0 ] = "/usr/bin/env";
  372. }
  373. else {
  374. // rely on PATH
  375. cmd[ 0 ] = "env";
  376. }
  377. return cmd;
  378. }
  379. else if ( Os.isFamily( "netware" ) || Os.isFamily( "os/400" ) ) {
  380. // rely on PATH
  381. String[] cmd = {"env"};
  382. return cmd;
  383. }
  384. else if ( Os.isFamily( "openvms" ) ) {
  385. String[] cmd = {"show", "logical"};
  386. return cmd;
  387. }
  388. else {
  389. // MAC OS 9 and previous
  390. //TODO: I have no idea how to get it, someone must fix it
  391. String[] cmd = null;
  392. return cmd;
  393. }
  394. }
  395. }