1 21 22 package com.izforge.izpack.panels; 23 24 import java.io.File ; 25 import java.util.StringTokenizer ; 26 27 import com.izforge.izpack.installer.InstallData; 28 import com.izforge.izpack.installer.InstallerFrame; 29 import com.izforge.izpack.util.AbstractUIHandler; 30 import com.izforge.izpack.util.FileExecutor; 31 import com.izforge.izpack.util.OsVersion; 32 33 39 public class JDKPathPanel extends PathInputPanel 40 { 41 42 private static final long serialVersionUID = 3257006553327810104L; 43 44 private static final String [] testFiles = new String [] { "lib" + File.separator + "tools.jar"}; 45 46 private String detectedVersion; 47 48 private String minVersion = null; 49 50 private String maxVersion = null; 51 52 private String variableName; 53 54 60 public JDKPathPanel(InstallerFrame parent, InstallData idata) 61 { 62 super(parent, idata); 63 setMustExist(true); 64 if(!OsVersion.IS_OSX) 65 setExistFiles(JDKPathPanel.testFiles); 66 setMinVersion(idata.getVariable("JDKPathPanel.minVersion")); 67 setMaxVersion(idata.getVariable("JDKPathPanel.maxVersion")); 68 setVariableName("JDKPath"); 69 } 70 71 76 public boolean isValidated() 77 { 78 if(idata.getVariable("PANEL_LAYOUT_TEST") != null) 79 return(true); 80 if (super.isValidated()) 81 { 82 if (verifyVersion()) 83 { 84 idata.setVariable(getVariableName(), pathSelectionPanel.getPath()); 85 return (true); 86 } 87 String min = getMinVersion(); 89 String max = getMaxVersion(); 90 StringBuffer message = new StringBuffer (); 91 message.append(parent.langpack.getString("JDKPathPanel.badVersion1")).append( 92 getDetectedVersion()).append( 93 parent.langpack.getString("JDKPathPanel.badVersion2")); 94 if (min != null && max != null) 95 message.append(min).append(" - ").append(max); 96 else if (min != null) 97 message.append(" >= ").append(min); 98 else if (max != null) message.append(" <= ").append(max); 99 100 message.append(parent.langpack.getString("JDKPathPanel.badVersion3")); 101 if (askQuestion(parent.langpack.getString("installer.warning"), message.toString(), 102 AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_NO) == AbstractUIHandler.ANSWER_YES) 103 { 104 idata.setVariable(getVariableName(), pathSelectionPanel.getPath()); 105 return (true); 106 } 107 } 108 return (false); 109 } 110 111 112 public void panelActivate() 113 { 114 super.panelActivate(); 116 String chosenPath; 117 if (idata.getVariable(getVariableName()) != null) 121 chosenPath = idata.getVariable(getVariableName()); 122 else 123 chosenPath = (new File (idata.getVariable("JAVA_HOME"))).getParent(); 125 pathSelectionPanel.setPath(chosenPath); 127 128 if (!pathIsValid() || !verifyVersion()) chosenPath = ""; 129 pathSelectionPanel.setPath(chosenPath); 131 String var = idata.getVariable("JDKPathPanel.skipIfValid"); 132 if (chosenPath.length() > 0 && var != null && "yes".equalsIgnoreCase(var)) 134 { 135 idata.setVariable(getVariableName(), chosenPath); 136 parent.skipPanel(); 137 } 138 139 } 140 141 private boolean verifyVersion() 142 { 143 String min = getMinVersion(); 144 String max = getMaxVersion(); 145 if (min == null && max == null) return (true); 147 148 if (!pathIsValid()) return (false); 149 String [] params = { 153 pathSelectionPanel.getPath() + File.separator + "bin" + File.separator + "java", 154 "-version"}; 155 String [] output = new String [2]; 156 FileExecutor fe = new FileExecutor(); 157 fe.executeCommand(params, output); 158 String vs = (output[0].length() > 0) ? output[0] : output[1]; 160 if (min != null) 161 { 162 if (!compareVersions(vs, min, true, 4, 4, "__NO_NOT_IDENTIFIER_")) return (false); 163 } 164 if (max != null) 165 if (!compareVersions(vs, max, false, 4, 4, "__NO_NOT_IDENTIFIER_")) return (false); 166 return (true); 167 } 168 169 private boolean compareVersions(String in, String template, boolean isMin, 170 int assumedPlace, int halfRange, String useNotIdentifier) 171 { 172 StringTokenizer st = new StringTokenizer (in, " \t\n\r\f\""); 173 int i; 174 int currentRange = 0; 175 String [] interestedEntries = new String [halfRange + halfRange]; 176 for (i = 0; i < assumedPlace - halfRange; ++i) 177 if (st.hasMoreTokens()) st.nextToken(); 179 for (i = 0; i < halfRange + halfRange; ++i) 180 { if (st.hasMoreTokens()) 182 { 183 interestedEntries[i] = st.nextToken(); 184 currentRange++; 185 } 186 } 187 188 for (i = 0; i < currentRange; ++i) 189 { 190 if (useNotIdentifier != null && interestedEntries[i].indexOf(useNotIdentifier) > -1) 191 continue; 192 if (Character.getType(interestedEntries[i].charAt(0)) != Character.DECIMAL_DIGIT_NUMBER) 193 continue; 194 break; 195 } 196 if (i == currentRange) 197 { 198 detectedVersion = "<not found>"; 199 return (false); 200 } 201 detectedVersion = interestedEntries[i]; 202 StringTokenizer current = new StringTokenizer (interestedEntries[i], "._-"); 203 StringTokenizer needed = new StringTokenizer (template, "._-"); 204 while (needed.hasMoreTokens()) 205 { 206 if (!current.hasMoreTokens()) return (false); 213 String cur = current.nextToken(); 214 String nee = needed.nextToken(); 215 int curVal = 0; 216 int neededVal = 0; 217 try 218 { 219 curVal = Integer.parseInt(cur); 220 neededVal = Integer.parseInt(nee); 221 } 222 catch (NumberFormatException nfe) 223 { return (false); 231 } 232 if (curVal < neededVal) if (isMin) 233 return (false); 234 else 235 return (true); 236 if (Integer.parseInt(cur) > Integer.parseInt(nee)) if (isMin) 237 return (true); 238 else 239 return (false); 240 } 241 return (true); 242 } 243 244 249 public String getDetectedVersion() 250 { 251 return detectedVersion; 252 } 253 254 259 public String getMaxVersion() 260 { 261 return maxVersion; 262 } 263 264 269 public String getMinVersion() 270 { 271 return minVersion; 272 } 273 274 279 protected void setDetectedVersion(String string) 280 { 281 detectedVersion = string; 282 } 283 284 289 protected void setMaxVersion(String string) 290 { 291 maxVersion = string; 292 } 293 294 299 protected void setMinVersion(String string) 300 { 301 minVersion = string; 302 } 303 304 309 public String getVariableName() 310 { 311 return variableName; 312 } 313 314 319 public void setVariableName(String string) 320 { 321 variableName = string; 322 } 323 324 329 public String getSummaryBody() 330 { 331 return (idata.getVariable(getVariableName())); 332 } 333 } 334 | Popular Tags |