svn-gvsig-desktop / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / panels / JDKPathPanel.java @ 21757
History | View | Annotate | Download (8.99 KB)
1 |
/*
|
---|---|
2 |
* $Id: JDKPathPanel.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2004 Klaus Bartz
|
5 |
*
|
6 |
* File : JDKPathPanel.java
|
7 |
* Description : A panel to selct the JDK path.
|
8 |
* Author's email : bartzkau@users.berlios.de
|
9 |
*
|
10 |
* This program is free software; you can redistribute it and/or
|
11 |
* modify it under the terms of the GNU General Public License
|
12 |
* as published by the Free Software Foundation; either version 2
|
13 |
* of the License, or any later version.
|
14 |
*
|
15 |
* This program is distributed in the hope that it will be useful,
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
* GNU General Public License for more details.
|
19 |
*
|
20 |
* You should have received a copy of the GNU General Public License
|
21 |
* along with this program; if not, write to the Free Software
|
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
23 |
*/
|
24 |
package com.izforge.izpack.panels; |
25 |
|
26 |
import java.io.File; |
27 |
import java.util.StringTokenizer; |
28 |
|
29 |
import com.izforge.izpack.installer.InstallData; |
30 |
import com.izforge.izpack.installer.InstallerFrame; |
31 |
import com.izforge.izpack.util.AbstractUIHandler; |
32 |
import com.izforge.izpack.util.FileExecutor; |
33 |
|
34 |
/**
|
35 |
* Panel which asks for the JDK path.
|
36 |
*
|
37 |
* @author Klaus Bartz
|
38 |
*
|
39 |
*/
|
40 |
public class JDKPathPanel extends PathInputPanel |
41 |
{ |
42 |
private static final String[] testFiles = new String[] |
43 |
{"lib" + File.separator + "tools.jar"}; |
44 |
|
45 |
private String detectedVersion; |
46 |
private String minVersion = null; |
47 |
private String maxVersion = null; |
48 |
private String variableName; |
49 |
/**
|
50 |
* The constructor.
|
51 |
*
|
52 |
* @param parent The parent window.
|
53 |
* @param idata The installation data.
|
54 |
*/
|
55 |
public JDKPathPanel(InstallerFrame parent, InstallData idata)
|
56 |
{ |
57 |
super(parent, idata);
|
58 |
setMustExist(true);
|
59 |
setExistFiles(JDKPathPanel.testFiles); |
60 |
setMinVersion(idata.getVariable("JDKPathPanel.minVersion"));
|
61 |
setMaxVersion(idata.getVariable("JDKPathPanel.maxVersion"));
|
62 |
setVariableName("JDKPath");
|
63 |
} |
64 |
/**
|
65 |
* Indicates wether the panel has been validated or not.
|
66 |
*
|
67 |
* @return Wether the panel has been validated or not.
|
68 |
*/
|
69 |
public boolean isValidated() |
70 |
{ |
71 |
if( super.isValidated()) |
72 |
{ |
73 |
if( verifyVersion() )
|
74 |
{ |
75 |
idata.setVariable(getVariableName(),pathSelectionPanel.getPath()); |
76 |
return( true ); |
77 |
} |
78 |
// Bad version detected.
|
79 |
String min = getMinVersion();
|
80 |
String max = getMaxVersion();
|
81 |
StringBuffer message = new StringBuffer(); |
82 |
message.append(parent.langpack.getString("JDKPathPanel.badVersion1")).
|
83 |
append(getDetectedVersion()). |
84 |
append(parent.langpack.getString("JDKPathPanel.badVersion2"));
|
85 |
if( min != null&& max != null ) |
86 |
message.append( min ).append(" - ").append( max );
|
87 |
else if( min != null ) |
88 |
message.append( " >= " ).append(min);
|
89 |
else if( max != null ) |
90 |
message.append( " <= " ).append(max);
|
91 |
|
92 |
message.append(parent.langpack.getString("JDKPathPanel.badVersion3"));
|
93 |
if( askQuestion(parent.langpack.getString("installer.warning"),message.toString(), |
94 |
AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_NO ) |
95 |
== AbstractUIHandler.ANSWER_YES) |
96 |
return( true); |
97 |
} |
98 |
return( false ); |
99 |
} |
100 |
/** Called when the panel becomes active. */
|
101 |
public void panelActivate() |
102 |
{ |
103 |
// Resolve the default for chosenPath
|
104 |
super.panelActivate();
|
105 |
String chosenPath;
|
106 |
// The variable will be exist if we enter this panel
|
107 |
// second time. We would maintain the previos
|
108 |
// selected path.
|
109 |
if( idata.getVariable(getVariableName()) != null ) |
110 |
chosenPath = idata.getVariable(getVariableName()); |
111 |
else
|
112 |
// Try the JAVA_HOME as child dir of the jdk path
|
113 |
chosenPath = (new File(idata.getVariable("JAVA_HOME"))).getParent(); |
114 |
// Set the path for method pathIsValid ...
|
115 |
pathSelectionPanel.setPath(chosenPath); |
116 |
|
117 |
if( ! pathIsValid() || ! verifyVersion())
|
118 |
chosenPath = "";
|
119 |
// Set the default to the path selection panel.
|
120 |
pathSelectionPanel.setPath(chosenPath); |
121 |
String var = idata.getVariable("JDKPathPanel.skipIfValid"); |
122 |
// Should we skip this panel?
|
123 |
if( chosenPath.length() > 0 && |
124 |
var != null && var.equalsIgnoreCase("yes") ) |
125 |
{ |
126 |
idata.setVariable(getVariableName(),chosenPath); |
127 |
parent.skipPanel(); |
128 |
} |
129 |
|
130 |
} |
131 |
|
132 |
private final boolean verifyVersion() |
133 |
{ |
134 |
String min = getMinVersion();
|
135 |
String max = getMaxVersion();
|
136 |
// No min and max, version always ok.
|
137 |
if( min == null && max == null) |
138 |
return( true ); |
139 |
|
140 |
if( ! pathIsValid())
|
141 |
return( false ); |
142 |
// No get the version ...
|
143 |
// We cannot look to the version of this vm because we should
|
144 |
// test the given JDK VM.
|
145 |
String[] params = {pathSelectionPanel.getPath() + File.separator + "bin" |
146 |
+ File.separator + "java", "-version"}; |
147 |
String[] output = new String[2]; |
148 |
FileExecutor fe = new FileExecutor();
|
149 |
fe.executeCommand(params, output); |
150 |
// "My" VM writes the version on stderr :-(
|
151 |
String vs = (output[0].length() > 0 ) ? output[0] : output[1]; |
152 |
if( min != null ) |
153 |
{ |
154 |
if( ! compareVersions( vs, min, true, 3, 3, "__NO_NOT_IDENTIFIER_")) |
155 |
return( false ); |
156 |
} |
157 |
if( max != null ) |
158 |
if( ! compareVersions( vs, max, false, 3, 3, "__NO_NOT_IDENTIFIER_")) |
159 |
return( false ); |
160 |
return( true ); |
161 |
} |
162 |
|
163 |
private final boolean compareVersions(String in, String template, |
164 |
boolean isMin, int assumedPlace, int halfRange, String useNotIdentifier) |
165 |
{ |
166 |
StringTokenizer st = new StringTokenizer(in, " \t\n\r\f\"" ); |
167 |
int length = st.countTokens();
|
168 |
int i;
|
169 |
int currentRange = 0; |
170 |
String[] interestedEntries = new String[halfRange + halfRange]; |
171 |
int praeScan = 0; |
172 |
praeScan = assumedPlace - halfRange; |
173 |
for( i = 0; i < assumedPlace - halfRange; ++i) |
174 |
if( st.hasMoreTokens())
|
175 |
st.nextToken(); // Forget this entries.
|
176 |
|
177 |
for( i = 0; i < halfRange + halfRange; ++i) |
178 |
{ // Put the interesting Strings into an intermediaer array.
|
179 |
if( st.hasMoreTokens())
|
180 |
{ |
181 |
interestedEntries[i] = st.nextToken(); |
182 |
currentRange++; |
183 |
} |
184 |
} |
185 |
|
186 |
for( i = 0; i < currentRange; ++i) |
187 |
{ |
188 |
if( useNotIdentifier != null && interestedEntries[i].indexOf (useNotIdentifier) > -1) |
189 |
continue;
|
190 |
if( Character.getType(interestedEntries[i].charAt(0)) != Character.DECIMAL_DIGIT_NUMBER) |
191 |
continue;
|
192 |
break;
|
193 |
} |
194 |
if( i == currentRange)
|
195 |
{ |
196 |
detectedVersion = "<not found>";
|
197 |
return(false); |
198 |
} |
199 |
detectedVersion = interestedEntries[i]; |
200 |
StringTokenizer current = new StringTokenizer(interestedEntries[i], "._-" ); |
201 |
StringTokenizer needed = new StringTokenizer(template, "._-" ); |
202 |
while( needed.hasMoreTokens() && current.hasMoreTokens())
|
203 |
{ |
204 |
String cur = current.nextToken();
|
205 |
String nee = needed.nextToken();
|
206 |
if( Integer.parseInt(cur) < Integer.parseInt(nee)) |
207 |
if(isMin )
|
208 |
return( false ); |
209 |
else
|
210 |
return( true ); |
211 |
if( Integer.parseInt(cur) > Integer.parseInt(nee)) |
212 |
if(isMin )
|
213 |
return( true ); |
214 |
else
|
215 |
return( false ); |
216 |
} |
217 |
return(true); |
218 |
} |
219 |
|
220 |
/**
|
221 |
* Returns the current detected version.
|
222 |
* @return the current detected version
|
223 |
*/
|
224 |
public String getDetectedVersion() |
225 |
{ |
226 |
return detectedVersion;
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Returns the current used maximum version.
|
231 |
* @return the current used maximum version
|
232 |
*/
|
233 |
public String getMaxVersion() |
234 |
{ |
235 |
return maxVersion;
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Returns the current used minimum version.
|
240 |
* @return the current used minimum version
|
241 |
*/
|
242 |
public String getMinVersion() |
243 |
{ |
244 |
return minVersion;
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* Sets the given value as current detected version.
|
249 |
* @param string version string to be used as detected version
|
250 |
*/
|
251 |
protected void setDetectedVersion(String string) |
252 |
{ |
253 |
detectedVersion = string; |
254 |
} |
255 |
|
256 |
/**
|
257 |
* Sets the given value as maximum for version control.
|
258 |
* @param string version string to be used as maximum
|
259 |
*/
|
260 |
protected void setMaxVersion(String string) |
261 |
{ |
262 |
maxVersion = string; |
263 |
} |
264 |
|
265 |
/**
|
266 |
* Sets the given value as minimum for version control.
|
267 |
* @param string version string to be used as minimum
|
268 |
*/
|
269 |
protected void setMinVersion(String string) |
270 |
{ |
271 |
minVersion = string; |
272 |
} |
273 |
|
274 |
/**
|
275 |
* Returns the name of the variable which should be used for the path.
|
276 |
* @return the name of the variable which should be used for the path
|
277 |
*/
|
278 |
public String getVariableName() |
279 |
{ |
280 |
return variableName;
|
281 |
} |
282 |
|
283 |
/**
|
284 |
* Sets the name for the variable which should be set with the path.
|
285 |
* @param string variable name to be used
|
286 |
*/
|
287 |
public void setVariableName(String string) |
288 |
{ |
289 |
variableName = string; |
290 |
} |
291 |
|
292 |
} |