1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.condition;
  18. import java.util.Locale;
  19. import org.apache.tools.ant.BuildException;
  20. /**
  21. * Condition that tests the OS type.
  22. *
  23. * @since Ant 1.4
  24. * @version $Revision: 1.30.2.4 $
  25. */
  26. public class Os implements Condition {
  27. private static final String OS_NAME =
  28. System.getProperty("os.name").toLowerCase(Locale.US);
  29. private static final String OS_ARCH =
  30. System.getProperty("os.arch").toLowerCase(Locale.US);
  31. private static final String OS_VERSION =
  32. System.getProperty("os.version").toLowerCase(Locale.US);
  33. private static final String PATH_SEP =
  34. System.getProperty("path.separator");
  35. private String family;
  36. private String name;
  37. private String version;
  38. private String arch;
  39. /**
  40. * Default constructor
  41. *
  42. */
  43. public Os() {
  44. }
  45. /**
  46. * Constructor that sets the family attribute
  47. *
  48. * @param family a String value
  49. */
  50. public Os(String family) {
  51. setFamily(family);
  52. }
  53. /**
  54. * Sets the desired OS family type
  55. *
  56. * @param f The OS family type desired<br />
  57. * Possible values:<br />
  58. * <ul>
  59. * <li>dos</li>
  60. * <li>mac</li>
  61. * <li>netware</li>
  62. * <li>os/2</li>
  63. * <li>tandem</li>
  64. * <li>unix</li>
  65. * <li>windows</li>
  66. * <li>win9x</li>
  67. * <li>z/os</li>
  68. * <li>os/400</li>
  69. * </ul>
  70. */
  71. public void setFamily(String f) {
  72. family = f.toLowerCase(Locale.US);
  73. }
  74. /**
  75. * Sets the desired OS name
  76. *
  77. * @param name The OS name
  78. */
  79. public void setName(String name) {
  80. this.name = name.toLowerCase(Locale.US);
  81. }
  82. /**
  83. * Sets the desired OS architecture
  84. *
  85. * @param arch The OS architecture
  86. */
  87. public void setArch(String arch) {
  88. this.arch = arch.toLowerCase(Locale.US);
  89. }
  90. /**
  91. * Sets the desired OS version
  92. *
  93. * @param version The OS version
  94. */
  95. public void setVersion(String version) {
  96. this.version = version.toLowerCase(Locale.US);
  97. }
  98. /**
  99. * Determines if the OS on which Ant is executing matches the type of
  100. * that set in setFamily.
  101. * @see Os#setFamily(String)
  102. */
  103. public boolean eval() throws BuildException {
  104. return isOs(family, name, arch, version);
  105. }
  106. /**
  107. * Determines if the OS on which Ant is executing matches the
  108. * given OS family.
  109. * @param family the family to check for
  110. * @return true if the OS matches
  111. * @since 1.5
  112. */
  113. public static boolean isFamily(String family) {
  114. return isOs(family, null, null, null);
  115. }
  116. /**
  117. * Determines if the OS on which Ant is executing matches the
  118. * given OS name.
  119. *
  120. * @param name the OS name to check for
  121. * @return true if the OS matches
  122. * @since 1.7
  123. */
  124. public static boolean isName(String name) {
  125. return isOs(null, name, null, null);
  126. }
  127. /**
  128. * Determines if the OS on which Ant is executing matches the
  129. * given OS architecture.
  130. *
  131. * @param arch the OS architecture to check for
  132. * @return true if the OS matches
  133. * @since 1.7
  134. */
  135. public static boolean isArch(String arch) {
  136. return isOs(null, null, arch, null);
  137. }
  138. /**
  139. * Determines if the OS on which Ant is executing matches the
  140. * given OS version.
  141. *
  142. * @param version the OS version to check for
  143. * @return true if the OS matches
  144. * @since 1.7
  145. */
  146. public static boolean isVersion(String version) {
  147. return isOs(null, null, null, version);
  148. }
  149. /**
  150. * Determines if the OS on which Ant is executing matches the
  151. * given OS family, name, architecture and version
  152. *
  153. * @param family The OS family
  154. * @param name The OS name
  155. * @param arch The OS architecture
  156. * @param version The OS version
  157. * @return true if the OS matches
  158. * @since 1.7
  159. */
  160. public static boolean isOs(String family, String name, String arch,
  161. String version) {
  162. boolean retValue = false;
  163. if (family != null || name != null || arch != null
  164. || version != null) {
  165. boolean isFamily = true;
  166. boolean isName = true;
  167. boolean isArch = true;
  168. boolean isVersion = true;
  169. if (family != null) {
  170. if (family.equals("windows")) {
  171. isFamily = OS_NAME.indexOf("windows") > -1;
  172. } else if (family.equals("os/2")) {
  173. isFamily = OS_NAME.indexOf("os/2") > -1;
  174. } else if (family.equals("netware")) {
  175. isFamily = OS_NAME.indexOf("netware") > -1;
  176. } else if (family.equals("dos")) {
  177. isFamily = PATH_SEP.equals(";") && !isFamily("netware");
  178. } else if (family.equals("mac")) {
  179. isFamily = OS_NAME.indexOf("mac") > -1;
  180. } else if (family.equals("tandem")) {
  181. isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
  182. } else if (family.equals("unix")) {
  183. isFamily = PATH_SEP.equals(":")
  184. && !isFamily("openvms")
  185. && (!isFamily("mac") || OS_NAME.endsWith("x"));
  186. } else if (family.equals("win9x")) {
  187. isFamily = isFamily("windows")
  188. && (OS_NAME.indexOf("95") >= 0
  189. || OS_NAME.indexOf("98") >= 0
  190. || OS_NAME.indexOf("me") >= 0
  191. || OS_NAME.indexOf("ce") >= 0);
  192. } else if (family.equals("z/os")) {
  193. isFamily = OS_NAME.indexOf("z/os") > -1
  194. || OS_NAME.indexOf("os/390") > -1;
  195. } else if (family.equals("os/400")) {
  196. isFamily = OS_NAME.indexOf("os/400") > -1;
  197. } else if (family.equals("openvms")) {
  198. isFamily = OS_NAME.indexOf("openvms") > -1;
  199. } else {
  200. throw new BuildException(
  201. "Don\'t know how to detect os family \""
  202. + family + "\"");
  203. }
  204. }
  205. if (name != null) {
  206. isName = name.equals(OS_NAME);
  207. }
  208. if (arch != null) {
  209. isArch = arch.equals(OS_ARCH);
  210. }
  211. if (version != null) {
  212. isVersion = version.equals(OS_VERSION);
  213. }
  214. retValue = isFamily && isName && isArch && isVersion;
  215. }
  216. return retValue;
  217. }
  218. }