1 | /*
|
---|
2 | * Copyright 2001-2005 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 |
|
---|
18 | package org.apache.tools.ant.taskdefs.condition;
|
---|
19 |
|
---|
20 | import java.util.Locale;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Condition that tests the OS type.
|
---|
25 | *
|
---|
26 | * @since Ant 1.4
|
---|
27 | */
|
---|
28 | public class Os implements Condition {
|
---|
29 | private static final String OS_NAME =
|
---|
30 | System.getProperty("os.name").toLowerCase(Locale.US);
|
---|
31 | private static final String OS_ARCH =
|
---|
32 | System.getProperty("os.arch").toLowerCase(Locale.US);
|
---|
33 | private static final String OS_VERSION =
|
---|
34 | System.getProperty("os.version").toLowerCase(Locale.US);
|
---|
35 | private static final String PATH_SEP =
|
---|
36 | System.getProperty("path.separator");
|
---|
37 |
|
---|
38 | private String family;
|
---|
39 | private String name;
|
---|
40 | private String version;
|
---|
41 | private String arch;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Default constructor
|
---|
45 | *
|
---|
46 | */
|
---|
47 | public Os() {
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Constructor that sets the family attribute
|
---|
52 | *
|
---|
53 | * @param family a String value
|
---|
54 | */
|
---|
55 | public Os(String family) {
|
---|
56 | setFamily(family);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Sets the desired OS family type
|
---|
61 | *
|
---|
62 | * @param f The OS family type desired<br />
|
---|
63 | * Possible values:<br />
|
---|
64 | * <ul>
|
---|
65 | * <li>dos</li>
|
---|
66 | * <li>mac</li>
|
---|
67 | * <li>netware</li>
|
---|
68 | * <li>os/2</li>
|
---|
69 | * <li>tandem</li>
|
---|
70 | * <li>unix</li>
|
---|
71 | * <li>windows</li>
|
---|
72 | * <li>win9x</li>
|
---|
73 | * <li>z/os</li>
|
---|
74 | * <li>os/400</li>
|
---|
75 | * </ul>
|
---|
76 | */
|
---|
77 | public void setFamily(String f) {
|
---|
78 | family = f.toLowerCase(Locale.US);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Sets the desired OS name
|
---|
83 | *
|
---|
84 | * @param name The OS name
|
---|
85 | */
|
---|
86 | public void setName(String name) {
|
---|
87 | this.name = name.toLowerCase(Locale.US);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Sets the desired OS architecture
|
---|
92 | *
|
---|
93 | * @param arch The OS architecture
|
---|
94 | */
|
---|
95 | public void setArch(String arch) {
|
---|
96 | this.arch = arch.toLowerCase(Locale.US);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Sets the desired OS version
|
---|
101 | *
|
---|
102 | * @param version The OS version
|
---|
103 | */
|
---|
104 | public void setVersion(String version) {
|
---|
105 | this.version = version.toLowerCase(Locale.US);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Determines if the OS on which Ant is executing matches the type of
|
---|
110 | * that set in setFamily.
|
---|
111 | * @see Os#setFamily(String)
|
---|
112 | */
|
---|
113 | public boolean eval() throws BuildException {
|
---|
114 | return isOs(family, name, arch, version);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Determines if the OS on which Ant is executing matches the
|
---|
119 | * given OS family.
|
---|
120 | * @param family the family to check for
|
---|
121 | * @return true if the OS matches
|
---|
122 | * @since 1.5
|
---|
123 | */
|
---|
124 | public static boolean isFamily(String family) {
|
---|
125 | return isOs(family, null, null, null);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Determines if the OS on which Ant is executing matches the
|
---|
130 | * given OS name.
|
---|
131 | *
|
---|
132 | * @param name the OS name to check for
|
---|
133 | * @return true if the OS matches
|
---|
134 | * @since 1.7
|
---|
135 | */
|
---|
136 | public static boolean isName(String name) {
|
---|
137 | return isOs(null, name, null, null);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Determines if the OS on which Ant is executing matches the
|
---|
142 | * given OS architecture.
|
---|
143 | *
|
---|
144 | * @param arch the OS architecture to check for
|
---|
145 | * @return true if the OS matches
|
---|
146 | * @since 1.7
|
---|
147 | */
|
---|
148 | public static boolean isArch(String arch) {
|
---|
149 | return isOs(null, null, arch, null);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Determines if the OS on which Ant is executing matches the
|
---|
154 | * given OS version.
|
---|
155 | *
|
---|
156 | * @param version the OS version to check for
|
---|
157 | * @return true if the OS matches
|
---|
158 | * @since 1.7
|
---|
159 | */
|
---|
160 | public static boolean isVersion(String version) {
|
---|
161 | return isOs(null, null, null, version);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Determines if the OS on which Ant is executing matches the
|
---|
166 | * given OS family, name, architecture and version
|
---|
167 | *
|
---|
168 | * @param family The OS family
|
---|
169 | * @param name The OS name
|
---|
170 | * @param arch The OS architecture
|
---|
171 | * @param version The OS version
|
---|
172 | * @return true if the OS matches
|
---|
173 | * @since 1.7
|
---|
174 | */
|
---|
175 | public static boolean isOs(String family, String name, String arch,
|
---|
176 | String version) {
|
---|
177 | boolean retValue = false;
|
---|
178 |
|
---|
179 | if (family != null || name != null || arch != null
|
---|
180 | || version != null) {
|
---|
181 |
|
---|
182 | boolean isFamily = true;
|
---|
183 | boolean isName = true;
|
---|
184 | boolean isArch = true;
|
---|
185 | boolean isVersion = true;
|
---|
186 |
|
---|
187 | if (family != null) {
|
---|
188 | if (family.equals("windows")) {
|
---|
189 | isFamily = OS_NAME.indexOf("windows") > -1;
|
---|
190 | } else if (family.equals("os/2")) {
|
---|
191 | isFamily = OS_NAME.indexOf("os/2") > -1;
|
---|
192 | } else if (family.equals("netware")) {
|
---|
193 | isFamily = OS_NAME.indexOf("netware") > -1;
|
---|
194 | } else if (family.equals("dos")) {
|
---|
195 | isFamily = PATH_SEP.equals(";") && !isFamily("netware");
|
---|
196 | } else if (family.equals("mac")) {
|
---|
197 | isFamily = OS_NAME.indexOf("mac") > -1;
|
---|
198 | } else if (family.equals("tandem")) {
|
---|
199 | isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
|
---|
200 | } else if (family.equals("unix")) {
|
---|
201 | isFamily = PATH_SEP.equals(":")
|
---|
202 | && !isFamily("openvms")
|
---|
203 | && (!isFamily("mac") || OS_NAME.endsWith("x"));
|
---|
204 | } else if (family.equals("win9x")) {
|
---|
205 | isFamily = isFamily("windows")
|
---|
206 | && (OS_NAME.indexOf("95") >= 0
|
---|
207 | || OS_NAME.indexOf("98") >= 0
|
---|
208 | || OS_NAME.indexOf("me") >= 0
|
---|
209 | || OS_NAME.indexOf("ce") >= 0);
|
---|
210 | } else if (family.equals("z/os")) {
|
---|
211 | isFamily = OS_NAME.indexOf("z/os") > -1
|
---|
212 | || OS_NAME.indexOf("os/390") > -1;
|
---|
213 | } else if (family.equals("os/400")) {
|
---|
214 | isFamily = OS_NAME.indexOf("os/400") > -1;
|
---|
215 | } else if (family.equals("openvms")) {
|
---|
216 | isFamily = OS_NAME.indexOf("openvms") > -1;
|
---|
217 | } else {
|
---|
218 | throw new BuildException(
|
---|
219 | "Don\'t know how to detect os family \""
|
---|
220 | + family + "\"");
|
---|
221 | }
|
---|
222 | }
|
---|
223 | if (name != null) {
|
---|
224 | isName = name.equals(OS_NAME);
|
---|
225 | }
|
---|
226 | if (arch != null) {
|
---|
227 | isArch = arch.equals(OS_ARCH);
|
---|
228 | }
|
---|
229 | if (version != null) {
|
---|
230 | isVersion = version.equals(OS_VERSION);
|
---|
231 | }
|
---|
232 | retValue = isFamily && isName && isArch && isVersion;
|
---|
233 | }
|
---|
234 | return retValue;
|
---|
235 | }
|
---|
236 | }
|
---|