root / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / util / OsConstraint.java @ 21757
History | View | Annotate | Download (7.03 KB)
1 |
/*
|
---|---|
2 |
* $Id: OsConstraint.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2002 Olexij Tkatchenko
|
5 |
*
|
6 |
* File : OsConstraint.java
|
7 |
* Description : A constraint on the OS to perform some action on.
|
8 |
* Author's email : ot@parcs.de
|
9 |
* Website : http://www.izforge.com
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
24 |
*/
|
25 |
package com.izforge.izpack.util; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import net.n3.nanoxml.XMLElement; |
32 |
|
33 |
/**
|
34 |
* Encapsulates OS constraints specified on creation time and allows
|
35 |
* to check them against the current OS.
|
36 |
*
|
37 |
* For example, this is used for <executable>s to check whether
|
38 |
* the executable is suitable for the current OS.
|
39 |
*
|
40 |
* @author Olexij Tkatchenko <ot@parcs.de>
|
41 |
*/
|
42 |
public class OsConstraint implements java.io.Serializable |
43 |
{ |
44 |
/** The OS family */
|
45 |
private String family; |
46 |
/** OS name from java system properties */
|
47 |
private String name; |
48 |
/** OS version from java system properties */
|
49 |
private String version; |
50 |
/** OS architecture from java system properties */
|
51 |
private String arch; |
52 |
|
53 |
|
54 |
/**
|
55 |
* Constructs a new instance. Please remember, MacOSX belongs to Unix family.
|
56 |
*
|
57 |
* @param family The OS family (unix, windows or mac).
|
58 |
* @param name The exact OS name.
|
59 |
* @param version The exact OS version (check property <code>os.version</code> for values).
|
60 |
* @param arch The machine architecture (check property <code>os.arch</code> for values).
|
61 |
*/
|
62 |
public OsConstraint(String family, String name, String version, String arch) |
63 |
{ |
64 |
this.family = family != null ? family.toLowerCase() : null; |
65 |
this.name = name != null ? name.toLowerCase() : null; |
66 |
this.version = version != null ? version.toLowerCase() : null; |
67 |
this.arch = arch != null ? arch.toLowerCase() : null; |
68 |
} |
69 |
|
70 |
|
71 |
/**
|
72 |
* Matches OS specification in this class against current system properties.
|
73 |
*
|
74 |
* @return Description of the Return Value
|
75 |
*/
|
76 |
public boolean matchCurrentSystem() |
77 |
{ |
78 |
boolean match = true; |
79 |
String osName = System.getProperty("os.name").toLowerCase(); |
80 |
|
81 |
if (arch != null && arch.length() != 0) |
82 |
{ |
83 |
match = System.getProperty("os.arch").toLowerCase().equals(arch); |
84 |
} |
85 |
if (match && version != null && version.length() != 0) |
86 |
{ |
87 |
match = System.getProperty("os.version").toLowerCase().equals(version); |
88 |
} |
89 |
if (match && name != null && name.length() != 0) |
90 |
{ |
91 |
match = osName.equals(name); |
92 |
} |
93 |
if (match && family != null) |
94 |
{ |
95 |
if (family.equals("windows")) |
96 |
{ |
97 |
match = OsVersion.IS_WINDOWS; |
98 |
} |
99 |
else if (family.equals("mac") || family.equals("osx")) |
100 |
{ |
101 |
match = OsVersion.IS_OSX; |
102 |
} |
103 |
else if (family.equals("unix")) |
104 |
{ |
105 |
match = OsVersion.IS_UNIX; |
106 |
} |
107 |
} |
108 |
|
109 |
return match && (family != null || name != null || version != null || arch != null); |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Extract a list of OS constraints from given element.
|
114 |
*
|
115 |
* @param element parent XMLElement
|
116 |
* @return List of OsConstraint (or empty List if no constraints found)
|
117 |
*/
|
118 |
static public List getOsList(XMLElement element) |
119 |
{ |
120 |
// get os info on this executable
|
121 |
ArrayList osList = new ArrayList(); |
122 |
Iterator osIterator = element.getChildrenNamed("os").iterator(); |
123 |
while (osIterator.hasNext())
|
124 |
{ |
125 |
XMLElement os = (XMLElement) osIterator.next(); |
126 |
osList.add (new OsConstraint (
|
127 |
os.getAttribute("family", null), |
128 |
os.getAttribute("name", null), |
129 |
os.getAttribute("version", null), |
130 |
os.getAttribute("arch", null) |
131 |
) |
132 |
); |
133 |
} |
134 |
|
135 |
// backward compatibility: still support os attribute
|
136 |
String osattr = element.getAttribute ("os"); |
137 |
if (osattr != null && osattr.length() > 0) |
138 |
{ |
139 |
// add the "os" attribute as a family constraint
|
140 |
osList.add (new OsConstraint (osattr, null, null, null)); |
141 |
} |
142 |
|
143 |
return osList;
|
144 |
} |
145 |
|
146 |
|
147 |
/**
|
148 |
* Helper function: Scan a list of OsConstraints for a match.
|
149 |
*
|
150 |
* @param constraint_list List of OsConstraint to check
|
151 |
*
|
152 |
* @return true if one of the OsConstraints matched the current system or
|
153 |
* constraint_list is null (no constraints), false if none of the OsConstraints matched
|
154 |
*/
|
155 |
public static boolean oneMatchesCurrentSystem (List constraint_list) |
156 |
{ |
157 |
if (constraint_list == null) |
158 |
return true; |
159 |
|
160 |
Iterator constraint_it = constraint_list.iterator ();
|
161 |
|
162 |
// no constraints at all - matches!
|
163 |
if (! constraint_it.hasNext ())
|
164 |
return true; |
165 |
|
166 |
while (constraint_it.hasNext ())
|
167 |
{ |
168 |
OsConstraint osc = (OsConstraint)constraint_it.next(); |
169 |
|
170 |
Debug.trace ("checking if os constraints "+osc+" match current OS"); |
171 |
|
172 |
// check for match
|
173 |
if (osc.matchCurrentSystem ())
|
174 |
{ |
175 |
Debug.trace ("matched current OS.");
|
176 |
return true; // bail out on first match |
177 |
} |
178 |
|
179 |
} |
180 |
|
181 |
Debug.trace ("no match with current OS!");
|
182 |
// no match found
|
183 |
return false; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Helper function: Check whether the given XMLElement is "suitable" for the
|
188 |
* current OS.
|
189 |
*
|
190 |
* @param el The XMLElement to check for OS constraints.
|
191 |
*
|
192 |
* @return true if there were no OS constraints or the constraints matched the current OS.
|
193 |
*
|
194 |
*/
|
195 |
public static boolean oneMatchesCurrentSystem (XMLElement el) |
196 |
{ |
197 |
return oneMatchesCurrentSystem(getOsList(el));
|
198 |
} |
199 |
|
200 |
public void setFamily(String f) |
201 |
{ |
202 |
family = f.toLowerCase(); |
203 |
} |
204 |
|
205 |
|
206 |
public String getFamily() |
207 |
{ |
208 |
return family;
|
209 |
} |
210 |
|
211 |
|
212 |
public void setName(String n) |
213 |
{ |
214 |
name = n.toLowerCase(); |
215 |
} |
216 |
|
217 |
|
218 |
public String getName() |
219 |
{ |
220 |
return name;
|
221 |
} |
222 |
|
223 |
|
224 |
public void setVersion(String v) |
225 |
{ |
226 |
version = v.toLowerCase(); |
227 |
} |
228 |
|
229 |
|
230 |
public String getVersion() |
231 |
{ |
232 |
return version;
|
233 |
} |
234 |
|
235 |
|
236 |
public void setArch(String a) |
237 |
{ |
238 |
arch = a.toLowerCase(); |
239 |
} |
240 |
|
241 |
|
242 |
public String getArch() |
243 |
{ |
244 |
return arch;
|
245 |
} |
246 |
|
247 |
public String toString() |
248 |
{ |
249 |
StringBuffer retval = new StringBuffer(); |
250 |
retval.append("[Os ");
|
251 |
retval.append(" family "+family);
|
252 |
retval.append(" name "+name);
|
253 |
retval.append(" version "+version);
|
254 |
retval.append(" arch "+arch);
|
255 |
retval.append(" ]");
|
256 |
return retval.toString();
|
257 |
} |
258 |
|
259 |
} |
260 |
|