KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > os > RegistryHandler


1 /*
2  * $Id: RegistryHandler.java 1708 2007-01-13 18:31:26Z jponge $
3  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
4  *
5  * http://www.izforge.com/izpack/
6  * http://developer.berlios.de/projects/izpack/
7  *
8  * Copyright 2005 Klaus Bartz
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.util.os;
24
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import com.coi.tools.os.win.MSWinConstants;
33 import com.coi.tools.os.win.NativeLibException;
34 import com.coi.tools.os.win.RegDataContainer;
35 import com.izforge.izpack.installer.AutomatedInstallData;
36 import com.izforge.izpack.installer.ResourceManager;
37 import com.izforge.izpack.util.Debug;
38 import com.izforge.izpack.util.OSClassHelper;
39
40 /**
41  * This class represents a registry handler in a operating system independent way. OS specific
42  * subclasses are used to implement the necessary mapping from this generic API to the classes that
43  * reflect the system dependent AIP.
44  *
45  * @author Klaus Bartz
46  *
47  */

48 public class RegistryHandler extends OSClassHelper implements MSWinConstants
49 {
50
51     public static final String JavaDoc UNINSTALL_ROOT = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
52
53     public static final Map JavaDoc ROOT_KEY_MAP = new HashMap JavaDoc();
54
55     protected String JavaDoc uninstallName = null;
56
57     private static final String JavaDoc UNINSTALLER_ICON = "UninstallerIcon";
58
59     private static RegistryHandler defaultHandler = null;
60     static
61     {
62         ROOT_KEY_MAP.put("HKCR", new Integer JavaDoc(HKEY_CLASSES_ROOT));
63         ROOT_KEY_MAP.put("HKEY_CLASSES_ROOT", new Integer JavaDoc(HKEY_CLASSES_ROOT));
64         ROOT_KEY_MAP.put("HKCU", new Integer JavaDoc(HKEY_CURRENT_USER));
65         ROOT_KEY_MAP.put("HKEY_CURRENT_USER", new Integer JavaDoc(HKEY_CURRENT_USER));
66         ROOT_KEY_MAP.put("HKLM", new Integer JavaDoc(HKEY_LOCAL_MACHINE));
67         ROOT_KEY_MAP.put("HKEY_LOCAL_MACHINE", new Integer JavaDoc(HKEY_LOCAL_MACHINE));
68         ROOT_KEY_MAP.put("HKU", new Integer JavaDoc(HKEY_USERS));
69         ROOT_KEY_MAP.put("HKEY_USERS", new Integer JavaDoc(HKEY_USERS));
70         ROOT_KEY_MAP.put("HKPD", new Integer JavaDoc(HKEY_PERFORMANCE_DATA));
71         ROOT_KEY_MAP.put("HKEY_PERFORMANCE_DATA", new Integer JavaDoc(HKEY_PERFORMANCE_DATA));
72         ROOT_KEY_MAP.put("HKCC", new Integer JavaDoc(HKEY_CURRENT_CONFIG));
73         ROOT_KEY_MAP.put("HKEY_CURRENT_CONFIG", new Integer JavaDoc(HKEY_CURRENT_CONFIG));
74         ROOT_KEY_MAP.put("HKDDS", new Integer JavaDoc(HKEY_DYN_DATA));
75         ROOT_KEY_MAP.put("HKEY_DYN_DATA", new Integer JavaDoc(HKEY_DYN_DATA));
76
77     }
78
79     /**
80      * Default constructor.
81      */

82     public RegistryHandler()
83     {
84         super();
85     }
86
87     /**
88      * Creates an registry handler which uses an oblect of the given class as worker.
89      *
90      * @param className full qualified class name of the class which should be used as worker
91      */

92     public RegistryHandler(String JavaDoc className)
93     {
94         super(className);
95         setDefault();
96     }
97
98     /**
99      * Set this object as default handler if it is not done earlier.
100      */

101     private synchronized void setDefault()
102     {
103         if (defaultHandler == null) defaultHandler = this;
104     }
105
106     /**
107      * Sets the given contents to the given registry value. If a sub key or the registry value does
108      * not exist, it will be created. The return value is a String array which contains the names of
109      * the keys and values which are created. REG_SZ is used as registry value type.
110      *
111      * @param key the registry key which should be used or created
112      * @param value the registry value into which the contents should be set
113      * @param contents the contents for the value
114      * @throws NativeLibException
115      */

116     public void setValue(String JavaDoc key, String JavaDoc value, String JavaDoc contents) throws NativeLibException
117     {
118     }
119
120     public void setValue(String JavaDoc key, String JavaDoc value, String JavaDoc[] contents) throws NativeLibException
121     {
122     }
123
124     /**
125      * Sets the given contents to the given registry value. If a sub key or the registry value does
126      * not exist, it will be created. The return value is a String array which contains the names of
127      * the keys and values which are created. REG_BINARY is used as registry value type.
128      *
129      * @param key the registry key which should be used or created
130      * @param value the registry value into which the contents should be set
131      * @param contents the contents for the value
132      * @throws NativeLibException
133      */

134     public void setValue(String JavaDoc key, String JavaDoc value, byte[] contents) throws NativeLibException
135     {
136     }
137
138     /**
139      * Sets the given contents to the given registry value. If a sub key or the registry value does
140      * not exist, it will be created. The return value is a String array which contains the names of
141      * the keys and values which are created. REG_DWORD is used as registry value type.
142      *
143      * @param key the registry key which should be used or created
144      * @param value the registry value into which the contents should be set
145      * @param contents the contents for the value
146      * @throws NativeLibException
147      */

148     public void setValue(String JavaDoc key, String JavaDoc value, long contents) throws NativeLibException
149     {
150     }
151
152     /**
153      * Returns the contents of the key/value pair if value exist, else the given default value.
154      *
155      * @param key the registry key which should be used
156      * @param value the registry value from which the contents should be requested
157      * @param defaultVal value to be used if no value exist in the registry
158      * @return requested value if exist, else the default value
159      * @throws NativeLibException
160      */

161     public RegDataContainer getValue(String JavaDoc key, String JavaDoc value, RegDataContainer defaultVal) throws NativeLibException
162     {
163         return (null);
164     }
165
166     /**
167      * Returns whether a key exist or not.
168      *
169      * @param key key to be evaluated
170      * @return whether a key exist or not
171      * @throws NativeLibException
172      */

173     public boolean keyExist(String JavaDoc key) throws NativeLibException
174     {
175         return (false);
176     }
177
178     /**
179      * Returns whether a the given value under the given key exist or not.
180      *
181      * @param key key to be used as path for the value
182      * @param value value name to be evaluated
183      * @return whether a the given value under the given key exist or not
184      * @throws NativeLibException
185      */

186     public boolean valueExist(String JavaDoc key, String JavaDoc value) throws NativeLibException
187     {
188         return (false);
189     }
190
191     /**
192      * Returns all keys which are defined under the given key.
193      *
194      * @param key key to be used as path for the sub keys
195      * @return all keys which are defined under the given key
196      * @throws NativeLibException
197      */

198     public String JavaDoc[] getSubkeys(String JavaDoc key) throws NativeLibException
199     {
200         return (null);
201     }
202
203     /**
204      * Returns all value names which are defined under the given key.
205      *
206      * @param key key to be used as path for the value names
207      * @return all value names which are defined under the given key
208      * @throws NativeLibException
209      */

210     public String JavaDoc[] getValueNames(String JavaDoc key) throws NativeLibException
211     {
212         return (null);
213     }
214
215     /**
216      * Returns the contents of the key/value pair if value exist, else an exception is raised.
217      *
218      * @param key the registry key which should be used
219      * @param value the registry value from which the contents should be requested
220      * @return requested value if exist, else an exception
221      * @throws NativeLibException
222      */

223     public RegDataContainer getValue(String JavaDoc key, String JavaDoc value) throws NativeLibException
224     {
225         return (null);
226     }
227
228     /**
229      * Creates the given key in the registry.
230      *
231      * @param key key to be created
232      * @throws NativeLibException
233      */

234     public void createKey(String JavaDoc key) throws NativeLibException
235     {
236     }
237     
238     /**
239      * Deletes the given key if exist, else throws an exception.
240      * @param key key to be deleted
241      * @throws NativeLibException
242      */

243     public void deleteKey( String JavaDoc key) throws NativeLibException
244     {
245     }
246
247     /**
248      * Deletes a key under the current root if it is empty, else do nothing.
249      *
250      * @param key key to be deleted
251      * @throws NativeLibException
252      */

253     public void deleteKeyIfEmpty(String JavaDoc key) throws NativeLibException
254     {
255     }
256     
257     /**
258      * Deletes a value.
259      *
260      * @param key key of the value which should be deleted
261      * @param value value name to be deleted
262      * @throws NativeLibException
263      */

264     public void deleteValue(String JavaDoc key, String JavaDoc value) throws NativeLibException
265     {
266     }
267
268     /**
269      * Sets the root for the next registry access.
270      *
271      * @param i an integer which refers to a HKEY
272      * @throws NativeLibException
273      */

274     public void setRoot(int i) throws NativeLibException
275     {
276     }
277
278     /**
279      * Return the root as integer (HKEY_xxx).
280      *
281      * @return the root as integer
282      * @throws NativeLibException
283      */

284     public int getRoot() throws NativeLibException
285     {
286         return (0);
287     }
288
289     /**
290      * Activates logging of registry changes.
291      *
292      * @throws NativeLibException
293      */

294     public void activateLogging() throws NativeLibException
295     {
296     }
297
298     /**
299      * Suspends logging of registry changes.
300      *
301      * @throws NativeLibException
302      */

303     public void suspendLogging() throws NativeLibException
304     {
305     }
306
307     /**
308      * Resets logging of registry changes.
309      *
310      * @throws NativeLibException
311      */

312     public void resetLogging() throws NativeLibException
313     {
314     }
315
316     public List JavaDoc getLoggingInfo() throws NativeLibException
317     {
318         return (null);
319     }
320
321     public void setLoggingInfo(List JavaDoc info) throws NativeLibException
322     {
323     }
324
325     public void addLoggingInfo(List JavaDoc info) throws NativeLibException
326     {
327     }
328
329     public void rewind() throws NativeLibException
330     {
331     }
332
333     public String JavaDoc getUninstallName()
334     {
335         if (uninstallName != null) return (uninstallName);
336         if (installdata == null) return (null);
337         return (installdata.getVariable("APP_NAME") + " " + installdata.getVariable("APP_VER"));
338     }
339
340     public boolean isProductRegistered() throws NativeLibException
341     {
342         String JavaDoc uninstallName = getUninstallName();
343         if (uninstallName == null) return (false);
344         String JavaDoc keyName = UNINSTALL_ROOT + uninstallName;
345         int oldVal = getRoot();
346         setRoot(HKEY_LOCAL_MACHINE);
347         boolean retval = keyExist(keyName);
348         setRoot(oldVal);
349         return (retval);
350     }
351
352     public void setUninstallName(String JavaDoc name)
353     {
354         uninstallName = name;
355     }
356
357     public void registerUninstallKey() throws NativeLibException
358     {
359         String JavaDoc uninstallName = getUninstallName();
360         if (uninstallName == null) return;
361         String JavaDoc keyName = UNINSTALL_ROOT + uninstallName;
362         String JavaDoc cmd = "\"" + installdata.getVariable("JAVA_HOME") + "\\bin\\javaw.exe\" -jar \""
363                 + installdata.getVariable("INSTALL_PATH") + "\\uninstaller\\uninstaller.jar\"";
364
365         int oldVal = getRoot();
366         setRoot(HKEY_LOCAL_MACHINE);
367         setValue(keyName, "DisplayName", uninstallName);
368         setValue(keyName, "UninstallString", cmd);
369         // Try to write the uninstaller icon out.
370
try
371         {
372             InputStream JavaDoc input = ResourceManager.getInstance().getInputStream(UNINSTALLER_ICON);
373             String JavaDoc iconPath = installdata.getVariable("INSTALL_PATH") + File.separator
374                     + "Uninstaller" + File.separator + "UninstallerIcon.ico";
375             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(iconPath);
376             byte[] buffer = new byte[5120];
377             long bytesCopied = 0;
378             int bytesInBuffer;
379             while ((bytesInBuffer = input.read(buffer)) != -1)
380             {
381                 out.write(buffer, 0, bytesInBuffer);
382                 bytesCopied += bytesInBuffer;
383             }
384             input.close();
385             out.close();
386             setValue(keyName, "DisplayIcon", iconPath);
387         }
388         catch (Exception JavaDoc exception)
389         { // May be no icon resource defined; ignore it
390
Debug.trace(exception);
391         }
392         setRoot(oldVal);
393     }
394
395     /**
396      * @param idata
397      */

398     public boolean verify(AutomatedInstallData idata) throws Exception JavaDoc
399     {
400         super.verify(idata);
401         return (true);
402
403     }
404
405     /**
406      * Returns whether an action with this handler should be performed or not.
407      *
408      * @return always true
409      */

410     public boolean doPerform()
411     {
412         return true;
413     }
414
415     /**
416      * Returns the default handler which is the first created registry handler.
417      *
418      * @return Returns the default handler.
419      */

420     public RegistryHandler getDefaultHandler()
421     {
422         return defaultHandler;
423     }
424
425 }
426
Popular Tags