KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > JDKPathPanel


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 2004 Klaus Bartz
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.panels;
23
24 import java.io.File JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
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 /**
34  * Panel which asks for the JDK path.
35  *
36  * @author Klaus Bartz
37  *
38  */

39 public class JDKPathPanel extends PathInputPanel
40 {
41
42     private static final long serialVersionUID = 3257006553327810104L;
43
44     private static final String JavaDoc[] testFiles = new String JavaDoc[] { "lib" + File.separator + "tools.jar"};
45
46     private String JavaDoc detectedVersion;
47
48     private String JavaDoc minVersion = null;
49
50     private String JavaDoc maxVersion = null;
51
52     private String JavaDoc variableName;
53
54     /**
55      * The constructor.
56      *
57      * @param parent The parent window.
58      * @param idata The installation data.
59      */

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     /**
72      * Indicates wether the panel has been validated or not.
73      *
74      * @return Wether the panel has been validated or not.
75      */

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             // Bad version detected.
88
String JavaDoc min = getMinVersion();
89             String JavaDoc max = getMaxVersion();
90             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
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     /** Called when the panel becomes active. */
112     public void panelActivate()
113     {
114         // Resolve the default for chosenPath
115
super.panelActivate();
116         String JavaDoc chosenPath;
117         // The variable will be exist if we enter this panel
118
// second time. We would maintain the previos
119
// selected path.
120
if (idata.getVariable(getVariableName()) != null)
121             chosenPath = idata.getVariable(getVariableName());
122         else
123             // Try the JAVA_HOME as child dir of the jdk path
124
chosenPath = (new File JavaDoc(idata.getVariable("JAVA_HOME"))).getParent();
125         // Set the path for method pathIsValid ...
126
pathSelectionPanel.setPath(chosenPath);
127
128         if (!pathIsValid() || !verifyVersion()) chosenPath = "";
129         // Set the default to the path selection panel.
130
pathSelectionPanel.setPath(chosenPath);
131         String JavaDoc var = idata.getVariable("JDKPathPanel.skipIfValid");
132         // Should we skip this panel?
133
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 JavaDoc min = getMinVersion();
144         String JavaDoc max = getMaxVersion();
145         // No min and max, version always ok.
146
if (min == null && max == null) return (true);
147
148         if (!pathIsValid()) return (false);
149         // No get the version ...
150
// We cannot look to the version of this vm because we should
151
// test the given JDK VM.
152
String JavaDoc[] params = {
153                 pathSelectionPanel.getPath() + File.separator + "bin" + File.separator + "java",
154                 "-version"};
155         String JavaDoc[] output = new String JavaDoc[2];
156         FileExecutor fe = new FileExecutor();
157         fe.executeCommand(params, output);
158         // "My" VM writes the version on stderr :-(
159
String JavaDoc 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 JavaDoc in, String JavaDoc template, boolean isMin,
170             int assumedPlace, int halfRange, String JavaDoc useNotIdentifier)
171     {
172         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(in, " \t\n\r\f\"");
173         int i;
174         int currentRange = 0;
175         String JavaDoc[] interestedEntries = new String JavaDoc[halfRange + halfRange];
176         for (i = 0; i < assumedPlace - halfRange; ++i)
177             if (st.hasMoreTokens()) st.nextToken(); // Forget this entries.
178

179         for (i = 0; i < halfRange + halfRange; ++i)
180         { // Put the interesting Strings into an intermediaer array.
181
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 JavaDoc current = new StringTokenizer JavaDoc(interestedEntries[i], "._-");
203         StringTokenizer JavaDoc needed = new StringTokenizer JavaDoc(template, "._-");
204         while (needed.hasMoreTokens())
205         {
206             // Current can have no more tokens if needed has more
207
// and if a privious token was not accepted as good version.
208
// e.g. 1.4.2_02 needed, 1.4.2 current. The false return
209
// will be right here. Only if e.g. needed is 1.4.2_00 the
210
// return value will be false, but zero should not b e used
211
// at the last version part.
212
if (!current.hasMoreTokens()) return (false);
213             String JavaDoc cur = current.nextToken();
214             String JavaDoc 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 JavaDoc nfe)
223             { // A number format exception will be raised if
224
// there is a non numeric part in the version,
225
// e.g. 1.5.0_beta. The verification runs only into
226
// this deep area of version number (fourth sub place)
227
// if all other are equal to the given limit. Then
228
// it is right to return false because e.g.
229
// the minimal needed version will be 1.5.0.2.
230
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     /**
245      * Returns the current detected version.
246      *
247      * @return the current detected version
248      */

249     public String JavaDoc getDetectedVersion()
250     {
251         return detectedVersion;
252     }
253
254     /**
255      * Returns the current used maximum version.
256      *
257      * @return the current used maximum version
258      */

259     public String JavaDoc getMaxVersion()
260     {
261         return maxVersion;
262     }
263
264     /**
265      * Returns the current used minimum version.
266      *
267      * @return the current used minimum version
268      */

269     public String JavaDoc getMinVersion()
270     {
271         return minVersion;
272     }
273
274     /**
275      * Sets the given value as current detected version.
276      *
277      * @param string version string to be used as detected version
278      */

279     protected void setDetectedVersion(String JavaDoc string)
280     {
281         detectedVersion = string;
282     }
283
284     /**
285      * Sets the given value as maximum for version control.
286      *
287      * @param string version string to be used as maximum
288      */

289     protected void setMaxVersion(String JavaDoc string)
290     {
291         maxVersion = string;
292     }
293
294     /**
295      * Sets the given value as minimum for version control.
296      *
297      * @param string version string to be used as minimum
298      */

299     protected void setMinVersion(String JavaDoc string)
300     {
301         minVersion = string;
302     }
303
304     /**
305      * Returns the name of the variable which should be used for the path.
306      *
307      * @return the name of the variable which should be used for the path
308      */

309     public String JavaDoc getVariableName()
310     {
311         return variableName;
312     }
313
314     /**
315      * Sets the name for the variable which should be set with the path.
316      *
317      * @param string variable name to be used
318      */

319     public void setVariableName(String JavaDoc string)
320     {
321         variableName = string;
322     }
323
324     /*
325      * (non-Javadoc)
326      *
327      * @see com.izforge.izpack.installer.IzPanel#getSummaryBody()
328      */

329     public String JavaDoc getSummaryBody()
330     {
331         return (idata.getVariable(getVariableName()));
332     }
333 }
334
Popular Tags