001: /*
002: * $Id: RegistryHandler.java 2062 2008-02-25 20:22:45Z jponge $
003: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
004: *
005: * http://izpack.org/
006: * http://izpack.codehaus.org/
007: *
008: * Copyright 2005 Klaus Bartz
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: */
022:
023: package com.izforge.izpack.util.os;
024:
025: import java.io.File;
026: import java.io.FileOutputStream;
027: import java.io.InputStream;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.Map;
031:
032: import com.coi.tools.os.win.MSWinConstants;
033: import com.coi.tools.os.win.NativeLibException;
034: import com.coi.tools.os.win.RegDataContainer;
035: import com.izforge.izpack.installer.AutomatedInstallData;
036: import com.izforge.izpack.installer.ResourceManager;
037: import com.izforge.izpack.util.Debug;
038: import com.izforge.izpack.util.OSClassHelper;
039:
040: /**
041: * This class represents a registry handler in a operating system independent way. OS specific
042: * subclasses are used to implement the necessary mapping from this generic API to the classes that
043: * reflect the system dependent AIP.
044: *
045: * @author Klaus Bartz
046: *
047: */
048: public class RegistryHandler extends OSClassHelper implements
049: MSWinConstants {
050:
051: public static final String UNINSTALL_ROOT = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
052:
053: public static final Map<String, Integer> ROOT_KEY_MAP = new HashMap<String, Integer>();
054:
055: protected String uninstallName = null;
056:
057: private static final String UNINSTALLER_ICON = "UninstallerIcon";
058:
059: private static RegistryHandler defaultHandler = null;
060: static {
061: ROOT_KEY_MAP.put("HKCR", HKEY_CLASSES_ROOT);
062: ROOT_KEY_MAP.put("HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT);
063: ROOT_KEY_MAP.put("HKCU", HKEY_CURRENT_USER);
064: ROOT_KEY_MAP.put("HKEY_CURRENT_USER", HKEY_CURRENT_USER);
065: ROOT_KEY_MAP.put("HKLM", HKEY_LOCAL_MACHINE);
066: ROOT_KEY_MAP.put("HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE);
067: ROOT_KEY_MAP.put("HKU", HKEY_USERS);
068: ROOT_KEY_MAP.put("HKEY_USERS", HKEY_USERS);
069: ROOT_KEY_MAP.put("HKPD", HKEY_PERFORMANCE_DATA);
070: ROOT_KEY_MAP
071: .put("HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA);
072: ROOT_KEY_MAP.put("HKCC", HKEY_CURRENT_CONFIG);
073: ROOT_KEY_MAP.put("HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG);
074: ROOT_KEY_MAP.put("HKDDS", HKEY_DYN_DATA);
075: ROOT_KEY_MAP.put("HKEY_DYN_DATA", HKEY_DYN_DATA);
076:
077: }
078:
079: /**
080: * Default constructor.
081: */
082: public RegistryHandler() {
083: super ();
084: }
085:
086: /**
087: * Creates an registry handler which uses an oblect of the given class as worker.
088: *
089: * @param className full qualified class name of the class which should be used as worker
090: */
091: public RegistryHandler(String className) {
092: super (className);
093: setDefault();
094: }
095:
096: /**
097: * Set this object as default handler if it is not done earlier.
098: */
099: private synchronized void setDefault() {
100: if (defaultHandler == null)
101: defaultHandler = this ;
102: }
103:
104: /**
105: * Sets the given contents to the given registry value. If a sub key or the registry value does
106: * not exist, it will be created. The return value is a String array which contains the names of
107: * the keys and values which are created. REG_SZ is used as registry value type.
108: *
109: * @param key the registry key which should be used or created
110: * @param value the registry value into which the contents should be set
111: * @param contents the contents for the value
112: * @throws NativeLibException
113: */
114: public void setValue(String key, String value, String contents)
115: throws NativeLibException {
116: }
117:
118: public void setValue(String key, String value, String[] contents)
119: throws NativeLibException {
120: }
121:
122: /**
123: * Sets the given contents to the given registry value. If a sub key or the registry value does
124: * not exist, it will be created. The return value is a String array which contains the names of
125: * the keys and values which are created. REG_BINARY is used as registry value type.
126: *
127: * @param key the registry key which should be used or created
128: * @param value the registry value into which the contents should be set
129: * @param contents the contents for the value
130: * @throws NativeLibException
131: */
132: public void setValue(String key, String value, byte[] contents)
133: throws NativeLibException {
134: }
135:
136: /**
137: * Sets the given contents to the given registry value. If a sub key or the registry value does
138: * not exist, it will be created. The return value is a String array which contains the names of
139: * the keys and values which are created. REG_DWORD is used as registry value type.
140: *
141: * @param key the registry key which should be used or created
142: * @param value the registry value into which the contents should be set
143: * @param contents the contents for the value
144: * @throws NativeLibException
145: */
146: public void setValue(String key, String value, long contents)
147: throws NativeLibException {
148: }
149:
150: /**
151: * Returns the contents of the key/value pair if value exist, else the given default value.
152: *
153: * @param key the registry key which should be used
154: * @param value the registry value from which the contents should be requested
155: * @param defaultVal value to be used if no value exist in the registry
156: * @return requested value if exist, else the default value
157: * @throws NativeLibException
158: */
159: public RegDataContainer getValue(String key, String value,
160: RegDataContainer defaultVal) throws NativeLibException {
161: return (null);
162: }
163:
164: /**
165: * Returns whether a key exist or not.
166: *
167: * @param key key to be evaluated
168: * @return whether a key exist or not
169: * @throws NativeLibException
170: */
171: public boolean keyExist(String key) throws NativeLibException {
172: return (false);
173: }
174:
175: /**
176: * Returns whether a the given value under the given key exist or not.
177: *
178: * @param key key to be used as path for the value
179: * @param value value name to be evaluated
180: * @return whether a the given value under the given key exist or not
181: * @throws NativeLibException
182: */
183: public boolean valueExist(String key, String value)
184: throws NativeLibException {
185: return (false);
186: }
187:
188: /**
189: * Returns all keys which are defined under the given key.
190: *
191: * @param key key to be used as path for the sub keys
192: * @return all keys which are defined under the given key
193: * @throws NativeLibException
194: */
195: public String[] getSubkeys(String key) throws NativeLibException {
196: return (null);
197: }
198:
199: /**
200: * Returns all value names which are defined under the given key.
201: *
202: * @param key key to be used as path for the value names
203: * @return all value names which are defined under the given key
204: * @throws NativeLibException
205: */
206: public String[] getValueNames(String key) throws NativeLibException {
207: return (null);
208: }
209:
210: /**
211: * Returns the contents of the key/value pair if value exist, else an exception is raised.
212: *
213: * @param key the registry key which should be used
214: * @param value the registry value from which the contents should be requested
215: * @return requested value if exist, else an exception
216: * @throws NativeLibException
217: */
218: public RegDataContainer getValue(String key, String value)
219: throws NativeLibException {
220: return (null);
221: }
222:
223: /**
224: * Creates the given key in the registry.
225: *
226: * @param key key to be created
227: * @throws NativeLibException
228: */
229: public void createKey(String key) throws NativeLibException {
230: }
231:
232: /**
233: * Deletes the given key if exist, else throws an exception.
234: * @param key key to be deleted
235: * @throws NativeLibException
236: */
237: public void deleteKey(String key) throws NativeLibException {
238: }
239:
240: /**
241: * Deletes a key under the current root if it is empty, else do nothing.
242: *
243: * @param key key to be deleted
244: * @throws NativeLibException
245: */
246: public void deleteKeyIfEmpty(String key) throws NativeLibException {
247: }
248:
249: /**
250: * Deletes a value.
251: *
252: * @param key key of the value which should be deleted
253: * @param value value name to be deleted
254: * @throws NativeLibException
255: */
256: public void deleteValue(String key, String value)
257: throws NativeLibException {
258: }
259:
260: /**
261: * Sets the root for the next registry access.
262: *
263: * @param i an integer which refers to a HKEY
264: * @throws NativeLibException
265: */
266: public void setRoot(int i) throws NativeLibException {
267: }
268:
269: /**
270: * Return the root as integer (HKEY_xxx).
271: *
272: * @return the root as integer
273: * @throws NativeLibException
274: */
275: public int getRoot() throws NativeLibException {
276: return (0);
277: }
278:
279: /**
280: * Activates logging of registry changes.
281: *
282: * @throws NativeLibException
283: */
284: public void activateLogging() throws NativeLibException {
285: }
286:
287: /**
288: * Suspends logging of registry changes.
289: *
290: * @throws NativeLibException
291: */
292: public void suspendLogging() throws NativeLibException {
293: }
294:
295: /**
296: * Resets logging of registry changes.
297: *
298: * @throws NativeLibException
299: */
300: public void resetLogging() throws NativeLibException {
301: }
302:
303: public List<Object> getLoggingInfo() throws NativeLibException {
304: return (null);
305: }
306:
307: public void setLoggingInfo(List info) throws NativeLibException {
308: }
309:
310: public void addLoggingInfo(List info) throws NativeLibException {
311: }
312:
313: public void rewind() throws NativeLibException {
314: }
315:
316: public String getUninstallName() {
317: if (uninstallName != null)
318: return (uninstallName);
319: if (installdata == null)
320: return (null);
321: return (installdata.getVariable("APP_NAME") + " " + installdata
322: .getVariable("APP_VER"));
323: }
324:
325: public boolean isProductRegistered() throws NativeLibException {
326: String uninstallName = getUninstallName();
327: if (uninstallName == null)
328: return (false);
329: String keyName = UNINSTALL_ROOT + uninstallName;
330: int oldVal = getRoot();
331: setRoot(HKEY_LOCAL_MACHINE);
332: boolean retval = keyExist(keyName);
333: setRoot(oldVal);
334: return (retval);
335: }
336:
337: public void setUninstallName(String name) {
338: uninstallName = name;
339: }
340:
341: public void registerUninstallKey() throws NativeLibException {
342: String uninstallName = getUninstallName();
343: if (uninstallName == null)
344: return;
345: String keyName = UNINSTALL_ROOT + uninstallName;
346: String cmd = "\"" + installdata.getVariable("JAVA_HOME")
347: + "\\bin\\javaw.exe\" -jar \""
348: + installdata.getVariable("INSTALL_PATH")
349: + "\\uninstaller\\uninstaller.jar\"";
350:
351: int oldVal = getRoot();
352: setRoot(HKEY_LOCAL_MACHINE);
353: setValue(keyName, "DisplayName", uninstallName);
354: setValue(keyName, "UninstallString", cmd);
355: // Try to write the uninstaller icon out.
356: try {
357: InputStream input = ResourceManager.getInstance()
358: .getInputStream(UNINSTALLER_ICON);
359: String iconPath = installdata.getVariable("INSTALL_PATH")
360: + File.separator + "Uninstaller" + File.separator
361: + "UninstallerIcon.ico";
362: FileOutputStream out = new FileOutputStream(iconPath);
363: byte[] buffer = new byte[5120];
364: long bytesCopied = 0;
365: int bytesInBuffer;
366: while ((bytesInBuffer = input.read(buffer)) != -1) {
367: out.write(buffer, 0, bytesInBuffer);
368: bytesCopied += bytesInBuffer;
369: }
370: input.close();
371: out.close();
372: setValue(keyName, "DisplayIcon", iconPath);
373: } catch (Exception exception) { // May be no icon resource defined; ignore it
374: Debug.trace(exception);
375: }
376: setRoot(oldVal);
377: }
378:
379: /**
380: * @param idata
381: */
382: public boolean verify(AutomatedInstallData idata) throws Exception {
383: super .verify(idata);
384: return (true);
385:
386: }
387:
388: /**
389: * Returns whether an action with this handler should be performed or not.
390: *
391: * @return always true
392: */
393: public boolean doPerform() {
394: return true;
395: }
396:
397: /**
398: * Returns the default handler which is the first created registry handler.
399: *
400: * @return Returns the default handler.
401: */
402: public RegistryHandler getDefaultHandler() {
403: return defaultHandler;
404: }
405:
406: }
|