Debian Codegraph @

src/main/java/net/sf/antcontrib/platform/Platform.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 package net.sf.antcontrib.platform;
17
18 import java.util.Enumeration;
19 import java.util.Hashtable;
20 import java.util.Properties;
21 import java.util.Vector;
22
23 import org.apache.tools.ant.taskdefs.Execute;
24
25 /***
26 *
27 * </pre>
28 * @author <a href="mailto:[email protected]">Matthew Inger</a>
29 */
30 public class Platform
31 {
32 public static final int FAMILY_NONE = 0;
33 public static final int FAMILY_UNIX = 1;
34 public static final int FAMILY_WINDOWS = 2;
35 public static final int FAMILY_OS2 = 3;
36 public static final int FAMILY_ZOS = 4;
37 public static final int FAMILY_OS400 = 5;
38 public static final int FAMILY_DOS = 6;
39 public static final int FAMILY_MAC = 7;
40 public static final int FAMILY_MACOSX = 8;
41 public static final int FAMILY_TANDEM = 9;
42 public static final int FAMILY_OPENVMS = 10;
43
44 public static final String FAMILY_NAME_UNIX = "unix";
45 public static final String FAMILY_NAME_WINDOWS = "windows";
46 public static final String FAMILY_NAME_OS2 = "os/2";
47 public static final String FAMILY_NAME_ZOS = "z/os";
48 public static final String FAMILY_NAME_OS400 = "os/400";
49 public static final String FAMILY_NAME_DOS = "dos";
50 public static final String FAMILY_NAME_MAC = "mac";
51 public static final String FAMILY_NAME_TANDEM = "tandem";
52 public static final String FAMILY_NAME_OPENVMS = "openvms";
53
54 private static final Hashtable familyNames;
55
56 static
57 {
58 familyNames = new Hashtable();
59 familyNames.put(new Integer(FAMILY_WINDOWS), FAMILY_NAME_WINDOWS);
60 familyNames.put(new Integer(FAMILY_OS2), FAMILY_NAME_OS2);
61 familyNames.put(new Integer(FAMILY_ZOS), FAMILY_NAME_ZOS);
62 familyNames.put(new Integer(FAMILY_OS400), FAMILY_NAME_OS400);
63 familyNames.put(new Integer(FAMILY_DOS), FAMILY_NAME_DOS);
64 familyNames.put(new Integer(FAMILY_MAC), FAMILY_NAME_MAC);
65 familyNames.put(new Integer(FAMILY_MACOSX), FAMILY_NAME_UNIX);
66 familyNames.put(new Integer(FAMILY_TANDEM), FAMILY_NAME_TANDEM);
67 familyNames.put(new Integer(FAMILY_UNIX), FAMILY_NAME_UNIX);
68 familyNames.put(new Integer(FAMILY_OPENVMS), FAMILY_NAME_OPENVMS);
69 }
70
71 public static final int getOsFamily()
72 {
73 String osName = System.getProperty("os.name").toLowerCase();
74 String pathSep = System.getProperty("path.separator");
75 int family = FAMILY_NONE;
76
77 if (osName.indexOf("windows") != -1)
78 {
79 family = FAMILY_WINDOWS;
80 }
81 else if (osName.indexOf("os/2") != -1)
82 {
83 family = FAMILY_OS2;
84 }
85 else if (osName.indexOf("z/os") != -1
86 || osName.indexOf("os/390") != -1)
87 {
88 family = FAMILY_ZOS;
89 }
90 else if (osName.indexOf("os/400") != -1)
91 {
92 family = FAMILY_OS400;
93 }
94 else if (pathSep.equals(";"))
95 {
96 family = FAMILY_DOS;
97 }
98 else if (osName.indexOf("mac") != -1)
99 {
100 if (osName.endsWith("x"))
101 family = FAMILY_UNIX; // MACOSX
102 else
103 family = FAMILY_MAC;
104 }
105 else if (osName.indexOf("nonstop_kernel") != -1)
106 {
107 family = FAMILY_TANDEM;
108 }
109 else if (osName.indexOf("openvms") != -1)
110 {
111 family = FAMILY_OPENVMS;
112 }
113 else if (pathSep.equals(":"))
114 {
115 family = FAMILY_UNIX;
116 }
117
118 return family;
119 }
120
121 public static final String getOsFamilyName()
122 {
123 int family = getOsFamily();
124 return (String)(familyNames.get(new Integer(family)));
125 }
126
127 public static final Properties getEnv()
128 {
129 Properties env = new Properties();
130 Vector osEnv = Execute.getProcEnvironment();
131 for (Enumeration e = osEnv.elements(); e.hasMoreElements();) {
132 String entry = (String) e.nextElement();
133 int pos = entry.indexOf('=');
134 if (pos != -1) {
135 env.setProperty(entry.substring(0, pos),
136 entry.substring(pos + 1));
137 }
138 }
139 return env;
140 }
141
142 public static final String getDefaultShell()
143 {
144 String shell = getEnv().getProperty("SHELL");
145
146 if (shell == null)
147 {
148 int family = getOsFamily();
149 switch (family)
150 {
151 case FAMILY_DOS:
152 case FAMILY_WINDOWS:
153 {
154 shell = "CMD.EXE";
155 break;
156 }
157
158 default:
159 {
160 shell = "bash";
161 break;
162 }
163 }
164 }
165 return shell;
166 }
167
168 public static final String getDefaultScriptSuffix()
169 {
170 int family = getOsFamily();
171 String suffix = null;
172
173 switch (family)
174 {
175 case FAMILY_DOS:
176 case FAMILY_WINDOWS:
177 {
178 suffix = ".bat";
179 break;
180 }
181
182 default:
183 {
184 suffix = null;
185 break;
186 }
187 }
188
189 return suffix;
190 }
191
192
193 public static final String[] getDefaultShellArguments()
194 {
195 int family = getOsFamily();
196 String args[] = null;
197
198 switch (family)
199 {
200 case FAMILY_DOS:
201 case FAMILY_WINDOWS:
202 {
203 args = new String[] { "/c" , "call" };
204 break;
205 }
206
207 default:
208 {
209 args = new String[0];
210 break;
211 }
212 }
213
214 return args;
215 }
216
217 }