KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > OsConstraint


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2002 Olexij Tkatchenko
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.util;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import net.n3.nanoxml.XMLElement;
29
30 /**
31  * Encapsulates OS constraints specified on creation time and allows to check them against the
32  * current OS.
33  *
34  * For example, this is used for <executable>s to check whether the executable is suitable for
35  * the current OS.
36  *
37  * @author Olexij Tkatchenko <ot@parcs.de>
38  */

39 public class OsConstraint implements java.io.Serializable JavaDoc
40 {
41
42     /**
43      *
44      */

45     private static final long serialVersionUID = 3762248660406450488L;
46
47     /** The OS family */
48     private String JavaDoc family;
49
50     /** OS name from java system properties */
51     private String JavaDoc name;
52
53     /** OS version from java system properties */
54     private String JavaDoc version;
55
56     /** OS architecture from java system properties */
57     private String JavaDoc arch;
58
59     /**
60      * Constructs a new instance. Please remember, MacOSX belongs to Unix family.
61      *
62      * @param family The OS family (unix, windows or mac).
63      * @param name The exact OS name.
64      * @param version The exact OS version (check property <code>os.version</code> for values).
65      * @param arch The machine architecture (check property <code>os.arch</code> for values).
66      */

67     public OsConstraint(String JavaDoc family, String JavaDoc name, String JavaDoc version, String JavaDoc arch)
68     {
69         this.family = family != null ? family.toLowerCase() : null;
70         this.name = name != null ? name.toLowerCase() : null;
71         this.version = version != null ? version.toLowerCase() : null;
72         this.arch = arch != null ? arch.toLowerCase() : null;
73     }
74
75     /**
76      * Matches OS specification in this class against current system properties.
77      *
78      * @return Description of the Return Value
79      */

80     public boolean matchCurrentSystem()
81     {
82         boolean match = true;
83         String JavaDoc osName = System.getProperty("os.name").toLowerCase();
84
85         if (arch != null && arch.length() != 0)
86         {
87             match = System.getProperty("os.arch").toLowerCase().equals(arch);
88         }
89         if (match && version != null && version.length() != 0)
90         {
91             match = System.getProperty("os.version").toLowerCase().equals(version);
92         }
93         if (match && name != null && name.length() != 0)
94         {
95             match = osName.equals(name);
96         }
97         if (match && family != null)
98         {
99             if ("windows".equals(family))
100             {
101                 match = OsVersion.IS_WINDOWS;
102             }
103             else if ("mac".equals(family) || "osx".equals(family))
104             {
105                 match = OsVersion.IS_OSX;
106             }
107             else if ("unix".equals(family))
108             {
109                 match = OsVersion.IS_UNIX;
110             }
111         }
112
113         return match && (family != null || name != null || version != null || arch != null);
114     }
115
116     /**
117      * Extract a list of OS constraints from given element.
118      *
119      * @param element parent XMLElement
120      * @return List of OsConstraint (or empty List if no constraints found)
121      */

122     static public List JavaDoc getOsList(XMLElement element)
123     {
124         // get os info on this executable
125
ArrayList JavaDoc osList = new ArrayList JavaDoc();
126         Iterator JavaDoc osIterator = element.getChildrenNamed("os").iterator();
127         while (osIterator.hasNext())
128         {
129             XMLElement os = (XMLElement) osIterator.next();
130             osList.add(new OsConstraint(os.getAttribute("family", null), os.getAttribute("name",
131                     null), os.getAttribute("version", null), os.getAttribute("arch", null)));
132         }
133
134         // backward compatibility: still support os attribute
135
String JavaDoc osattr = element.getAttribute("os");
136         if (osattr != null && osattr.length() > 0)
137         {
138             // add the "os" attribute as a family constraint
139
osList.add(new OsConstraint(osattr, null, null, null));
140         }
141
142         return osList;
143     }
144
145     /**
146      * Helper function: Scan a list of OsConstraints for a match.
147      *
148      * @param constraint_list List of OsConstraint to check
149      *
150      * @return true if one of the OsConstraints matched the current system or constraint_list is
151      * null (no constraints), false if none of the OsConstraints matched
152      */

153     public static boolean oneMatchesCurrentSystem(List JavaDoc constraint_list)
154     {
155         if (constraint_list == null) return true;
156
157         Iterator JavaDoc constraint_it = constraint_list.iterator();
158
159         // no constraints at all - matches!
160
if (!constraint_it.hasNext()) return true;
161
162         while (constraint_it.hasNext())
163         {
164             OsConstraint osc = (OsConstraint) constraint_it.next();
165
166             Debug.trace("checking if os constraints " + osc + " match current OS");
167
168             // check for match
169
if (osc.matchCurrentSystem())
170             {
171                 Debug.trace("matched current OS.");
172                 return true; // bail out on first match
173
}
174
175         }
176
177         Debug.trace("no match with current OS!");
178         // no match found
179
return false;
180     }
181
182     /**
183      * Helper function: Check whether the given XMLElement is "suitable" for the current OS.
184      *
185      * @param el The XMLElement to check for OS constraints.
186      *
187      * @return true if there were no OS constraints or the constraints matched the current OS.
188      *
189      */

190     public static boolean oneMatchesCurrentSystem(XMLElement el)
191     {
192         return oneMatchesCurrentSystem(getOsList(el));
193     }
194
195     public void setFamily(String JavaDoc f)
196     {
197         family = f.toLowerCase();
198     }
199
200     public String JavaDoc getFamily()
201     {
202         return family;
203     }
204
205     public void setName(String JavaDoc n)
206     {
207         name = n.toLowerCase();
208     }
209
210     public String JavaDoc getName()
211     {
212         return name;
213     }
214
215     public void setVersion(String JavaDoc v)
216     {
217         version = v.toLowerCase();
218     }
219
220     public String JavaDoc getVersion()
221     {
222         return version;
223     }
224
225     public void setArch(String JavaDoc a)
226     {
227         arch = a.toLowerCase();
228     }
229
230     public String JavaDoc getArch()
231     {
232         return arch;
233     }
234
235     public String JavaDoc toString()
236     {
237         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
238         retval.append("[Os ");
239         retval.append(" family ").append(family);
240         retval.append(" name ").append(name);
241         retval.append(" version ").append(version);
242         retval.append(" arch ").append(arch);
243         retval.append(" ]");
244         return retval.toString();
245     }
246
247 }
248
Popular Tags