001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2002 Olexij Tkatchenko
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package com.izforge.izpack.util;
022:
023: import net.n3.nanoxml.XMLElement;
024:
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: /**
030: * Encapsulates OS constraints specified on creation time and allows to check them against the
031: * current OS.
032: * <p/>
033: * For example, this is used for <executable>s to check whether the executable is suitable for
034: * the current OS.
035: *
036: * @author Olexij Tkatchenko <ot@parcs.de>
037: */
038: public class OsConstraint implements java.io.Serializable {
039: //~ Static variables/initializers
040:
041: /**
042: *
043: */
044: private static final long serialVersionUID = 3762248660406450488L;
045:
046: //~ Instance variables
047:
048: /**
049: * OS architecture from java system properties
050: */
051: private String arch;
052:
053: /**
054: * The OS family
055: */
056: private String family;
057:
058: /**
059: * JRE version used for installation
060: */
061: private String jre;
062:
063: /**
064: * OS name from java system properties
065: */
066: private String name;
067:
068: /**
069: * OS version from java system properties
070: */
071: private String version;
072:
073: //~ Constructors
074:
075: /**
076: * Constructs a new instance. Please remember, MacOSX belongs to Unix family.
077: *
078: * @param family The OS family (unix, windows or mac).
079: * @param name The exact OS name.
080: * @param version The exact OS version (check property <code>os.version</code> for values).
081: * @param arch The machine architecture (check property <code>os.arch</code> for values).
082: * @param jre The Java version used for installation (check property <code>java.version</code> for values).
083: */
084: public OsConstraint(String family, String name, String version,
085: String arch, String jre) {
086: this .family = (family != null) ? family.toLowerCase() : null;
087: this .name = (name != null) ? name.toLowerCase() : null;
088: this .version = (version != null) ? version.toLowerCase() : null;
089: this .arch = (arch != null) ? arch.toLowerCase() : null;
090: this .jre = (jre != null) ? jre.toLowerCase() : null;
091: } // end OsConstraint()
092:
093: /**
094: * Creates a new instance. Please remember, MacOSX belongs to Unix family.
095: *
096: * @param family The OS family (unix, windows or mac).
097: * @param name The exact OS name.
098: * @param version The exact OS version (check property <code>os.version</code> for values).
099: * @param arch The machine architecture (check property <code>os.arch</code> for values).
100: */
101: public OsConstraint(String family, String name, String version,
102: String arch) {
103: this (family, name, version, arch, null);
104: } // end OsConstraint()
105:
106: //~ Methods
107:
108: /**
109: * Matches OS specification in this class against current system properties.
110: *
111: * @return Description of the Return Value
112: */
113: public boolean matchCurrentSystem() {
114: boolean match = true;
115: String osName = System.getProperty("os.name").toLowerCase();
116:
117: if ((arch != null) && (arch.length() != 0)) {
118: match = System.getProperty("os.arch").toLowerCase().equals(
119: arch);
120: } // end if
121:
122: if (match && (version != null) && (version.length() != 0)) {
123: match = System.getProperty("os.version").toLowerCase()
124: .equals(version);
125: } // end if
126:
127: if (match && (name != null) && (name.length() != 0)) {
128: match = osName.equals(name);
129: } // end if
130:
131: if (match && (family != null)) {
132: if ("windows".equals(family)) {
133: match = OsVersion.IS_WINDOWS;
134: } // end if
135: else if ("mac".equals(family) || "osx".equals(family)) {
136: match = OsVersion.IS_OSX;
137: } // end else if
138: else if ("unix".equals(family)) {
139: match = OsVersion.IS_UNIX;
140: } // end else if
141: } // end if
142:
143: if (match && (jre != null) && (jre.length() > 0)) {
144: match = System.getProperty("java.version").toLowerCase()
145: .startsWith(jre);
146: } // end if
147:
148: return match
149: && ((family != null) || (name != null)
150: || (version != null) || (arch != null) || (jre != null));
151: } // end matchCurrentSystem()
152:
153: /**
154: * Extract a list of OS constraints from given element.
155: *
156: * @param element parent XMLElement
157: * @return List of OsConstraint (or empty List if no constraints found)
158: */
159: public static List<OsConstraint> getOsList(XMLElement element) {
160: // get os info on this executable
161: ArrayList<OsConstraint> osList = new ArrayList<OsConstraint>();
162: Iterator<XMLElement> osIterator = element
163: .getChildrenNamed("os").iterator();
164:
165: while (osIterator.hasNext()) {
166: XMLElement os = osIterator.next();
167:
168: osList.add(new OsConstraint(
169: os.getAttribute("family", null), os.getAttribute(
170: "name", null), os.getAttribute("version",
171: null), os.getAttribute("arch", null), os
172: .getAttribute("jre", null)));
173: } // end while
174:
175: // backward compatibility: still support os attribute
176: String osattr = element.getAttribute("os");
177:
178: if ((osattr != null) && (osattr.length() > 0)) {
179: // add the "os" attribute as a family constraint
180: osList
181: .add(new OsConstraint(osattr, null, null, null,
182: null));
183: } // end if
184:
185: return osList;
186: } // end getOsList()
187:
188: /**
189: * Helper function: Scan a list of OsConstraints for a match.
190: *
191: * @param constraint_list List of OsConstraint to check
192: * @return true if one of the OsConstraints matched the current system or constraint_list is
193: * null (no constraints), false if none of the OsConstraints matched
194: */
195: public static boolean oneMatchesCurrentSystem(
196: List<OsConstraint> constraint_list) {
197: if (constraint_list == null) {
198: return true;
199: } // end if
200:
201: Iterator<OsConstraint> constraint_it = constraint_list
202: .iterator();
203:
204: // no constraints at all - matches!
205: if (!constraint_it.hasNext()) {
206: return true;
207: } // end if
208:
209: while (constraint_it.hasNext()) {
210: OsConstraint osc = constraint_it.next();
211:
212: Debug.trace("checking if os constraints " + osc
213: + " match current OS");
214:
215: // check for match
216: if (osc.matchCurrentSystem()) {
217: Debug.trace("matched current OS.");
218:
219: return true; // bail out on first match
220: } // end if
221: } // end while
222:
223: Debug.trace("no match with current OS!");
224:
225: // no match found
226: return false;
227: } // end oneMatchesCurrentSystem()
228:
229: /**
230: * Helper function: Check whether the given XMLElement is "suitable" for the current OS.
231: *
232: * @param el The XMLElement to check for OS constraints.
233: * @return true if there were no OS constraints or the constraints matched the current OS.
234: */
235: public static boolean oneMatchesCurrentSystem(XMLElement el) {
236: return oneMatchesCurrentSystem(getOsList(el));
237: } // end oneMatchesCurrentSystem()
238:
239: public void setFamily(String f) {
240: family = f.toLowerCase();
241: } // end setFamily()
242:
243: public String getFamily() {
244: return family;
245: } // end getFamily()
246:
247: public void setName(String n) {
248: name = n.toLowerCase();
249: } // end setName()
250:
251: public String getName() {
252: return name;
253: } // end getName()
254:
255: public void setVersion(String v) {
256: version = v.toLowerCase();
257: } // end setVersion()
258:
259: public String getVersion() {
260: return version;
261: } // end getVersion()
262:
263: public void setArch(String a) {
264: arch = a.toLowerCase();
265: } // end setArch()
266:
267: public String getArch() {
268: return arch;
269: } // end getArch()
270:
271: public String toString() {
272: StringBuffer retval = new StringBuffer();
273:
274: retval.append("[Os ");
275: retval.append(" family ").append(family);
276: retval.append(" name ").append(name);
277: retval.append(" version ").append(version);
278: retval.append(" arch ").append(arch);
279: retval.append(" jre ").append(jre);
280: retval.append(" ]");
281:
282: return retval.toString();
283: } // end toString()
284: } // end OsConstraint
|