/izpack-util/src/main/java/com/izforge/izpack/util/OsConstraintHelper.java

https://github.com/maslovalex/izpack · Java · 152 lines · 97 code · 16 blank · 39 comment · 46 complexity · cf0aeb5af6479219af401dde4a113b85 MD5 · raw file

  1. package com.izforge.izpack.util;
  2. import com.izforge.izpack.api.adaptator.IXMLElement;
  3. import com.izforge.izpack.api.data.binding.OsModel;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. /**
  7. * Encapsulates OS constraints specified on creation time and allows to check them against the
  8. * current OS.
  9. * <p/>
  10. * For example, this is used for &lt;executable&gt;s to check whether the executable is suitable for
  11. * the current OS.
  12. *
  13. * @author Olexij Tkatchenko <ot@parcs.de>
  14. */
  15. public class OsConstraintHelper
  16. {
  17. /**
  18. * Matches OS specification in this class against current system properties.
  19. *
  20. * @param osModel
  21. * @return Description of the Return Value
  22. */
  23. public static boolean matchCurrentSystem(OsModel osModel)
  24. {
  25. boolean match = true;
  26. String osName = System.getProperty("os.name").toLowerCase();
  27. if ((osModel.getArch() != null) && (osModel.getArch().length() != 0))
  28. {
  29. match = System.getProperty("os.arch").toLowerCase().equals(osModel.getArch());
  30. } // end if
  31. if (match && (osModel.getVersion() != null) && (osModel.getVersion().length() != 0))
  32. {
  33. match = System.getProperty("os.version").toLowerCase().equals(osModel.getVersion());
  34. } // end if
  35. if (match && (osModel.getName() != null) && (osModel.getName().length() != 0))
  36. {
  37. match = osName.equals(osModel.getName());
  38. } // end if
  39. if (match && (osModel.getFamily() != null))
  40. {
  41. if ("windows".equals(osModel.getFamily()))
  42. {
  43. match = OsVersion.IS_WINDOWS;
  44. } // end if
  45. else if ("mac".equals(osModel.getFamily()) || "osx".equals(osModel.getFamily()))
  46. {
  47. match = OsVersion.IS_OSX;
  48. } // end else if
  49. else if ("unix".equals(osModel.getFamily()))
  50. {
  51. match = OsVersion.IS_UNIX;
  52. } // end else if
  53. } // end if
  54. if (match && (osModel.getJre() != null) && (osModel.getJre().length() > 0))
  55. {
  56. match = System.getProperty("java.version").toLowerCase().startsWith(osModel.getJre());
  57. }
  58. return match
  59. && ((osModel.getFamily() != null) || (osModel.getName() != null) || (osModel.getVersion() != null) || (osModel.getArch() != null) || (osModel.getJre() != null));
  60. }
  61. /**
  62. * Extract a list of OS constraints from given element.
  63. *
  64. * @param element parent IXMLElement
  65. * @return List of OsModel (or empty List if no constraints found)
  66. */
  67. public static List<OsModel> getOsList(IXMLElement element)
  68. {
  69. // get os info on this executable
  70. ArrayList<OsModel> osList = new ArrayList<OsModel>();
  71. for (IXMLElement osElement : element.getChildrenNamed("os"))
  72. {
  73. osList.add(
  74. new OsModel(
  75. osElement.getAttribute("arch",
  76. null),
  77. osElement.getAttribute("family",
  78. null),
  79. osElement.getAttribute("jre",
  80. null),
  81. osElement.getAttribute("name",
  82. null),
  83. osElement.getAttribute("version",
  84. null))
  85. );
  86. }
  87. // backward compatibility: still support os attribute
  88. String osattr = element.getAttribute("os");
  89. if ((osattr != null) && (osattr.length() > 0))
  90. {
  91. // add the "os" attribute as a family constraint
  92. osList.add(
  93. new OsModel(osattr,
  94. null, null, null, null)
  95. );
  96. }
  97. return osList;
  98. }
  99. /**
  100. * Helper function: Scan a list of OsConstraints for a match.
  101. *
  102. * @param constraint_list List of OsModel to check
  103. * @return true if one of the OsConstraints matched the current system or constraint_list is
  104. * null (no constraints), false if none of the OsConstraints matched
  105. */
  106. public static boolean oneMatchesCurrentSystem(List<OsModel> constraint_list)
  107. {
  108. if (constraint_list == null || constraint_list.isEmpty())
  109. {
  110. return true;
  111. }
  112. for (OsModel osModel : constraint_list)
  113. {
  114. Debug.trace("checking if os constraints " + osModel + " match current OS");
  115. // check for match
  116. if (matchCurrentSystem(osModel))
  117. {
  118. Debug.trace("matched current OS.");
  119. return true; // bail out on first match
  120. } // end if
  121. } // end while
  122. Debug.trace("no match with current OS!");
  123. // no match found
  124. return false;
  125. }
  126. /**
  127. * Helper function: Check whether the given IXMLElement is "suitable" for the current OS.
  128. *
  129. * @param el The IXMLElement to check for OS constraints.
  130. * @return true if there were no OS constraints or the constraints matched the current OS.
  131. */
  132. public static boolean oneMatchesCurrentSystem(IXMLElement el)
  133. {
  134. return oneMatchesCurrentSystem(getOsList(el));
  135. }
  136. }