001/*
002 * $Id: Win_RegistryHandler.java,v 1.6 2005/09/23 15:24:42 bartzkau Exp $
003 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
004 * 
005 * http://www.izforge.com/izpack/
006 * http://developer.berlios.de/projects/izpack/
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
023package com.izforge.izpack.util.os;
024
025import java.util.List;
026
027import com.coi.tools.os.izpack.Registry;
028import com.coi.tools.os.win.NativeLibException;
029import com.coi.tools.os.win.RegDataContainer;
030
031/**
032 * This is the Microsoft Windows specific implementation of <code>RegistryHandler</code>.
033 * 
034 * @author bartzkau
035 * 
036 */
037public class Win_RegistryHandler extends RegistryHandler
038{
039
040    Registry regWorker = null;
041
042    /**
043     * Default constructor.
044     */
045    public Win_RegistryHandler()
046    {
047        super("com.coi.tools.os.izpack.Registry");
048        if (good()) regWorker = (Registry) worker;
049    }
050
051    /**
052     * Sets the given contents to the given registry value. If a sub key or the registry value does
053     * not exist, it will be created. The return value is a String array which contains the names of
054     * the keys and values which are created. REG_SZ is used as registry value type.
055     * 
056     * @param key the registry key which should be used or created
057     * @param value the registry value into which the contents should be set
058     * @param contents the contents for the value
059     * @throws NativeLibException
060     * @throws NativeLibException
061     */
062    public void setValue(String key, String value, String contents) throws NativeLibException
063    {
064        if (!good()) return;
065        regWorker.setValue(key, value, contents);
066    }
067
068    /**
069     * Sets the given contents to the given registry value. If a sub key or the registry value does
070     * not exist, it will be created. The return value is a String array which contains the names of
071     * the keys and values which are created. REG_MULTI_SZ is used as registry value type.
072     * 
073     * @param key the registry key which should be used or created
074     * @param value the registry value into which the contents should be set
075     * @param contents the contents for the value
076     * @throws NativeLibException
077     */
078    public void setValue(String key, String value, String[] contents) throws NativeLibException
079    {
080        if (!good()) return;
081        regWorker.setValue(key, value, contents);
082    }
083
084    /**
085     * Sets the given contents to the given registry value. If a sub key or the registry value does
086     * not exist, it will be created. The return value is a String array which contains the names of
087     * the keys and values which are created. REG_BINARY is used as registry value type.
088     * 
089     * @param key the registry key which should be used or created
090     * @param value the registry value into which the contents should be set
091     * @param contents the contents for the value
092     * @throws NativeLibException
093     */
094    public void setValue(String key, String value, byte[] contents) throws NativeLibException
095    {
096        if (!good()) return;
097        regWorker.setValue(key, value, contents);
098    }
099
100    /**
101     * Sets the given contents to the given registry value. If a sub key or the registry value does
102     * not exist, it will be created. The return value is a String array which contains the names of
103     * the keys and values which are created. REG_DWORD is used as registry value type.
104     * 
105     * @param key the registry key which should be used or created
106     * @param value the registry value into which the contents should be set
107     * @param contents the contents for the value
108     * @throws NativeLibException
109     */
110    public void setValue(String key, String value, long contents) throws NativeLibException
111    {
112        if (!good()) return;
113        regWorker.setValue(key, value, contents);
114    }
115
116    /**
117     * Returns the contents of the key/value pair if value exist, else the given default value.
118     * 
119     * @param key the registry key which should be used
120     * @param value the registry value from which the contents should be requested
121     * @param defaultVal value to be used if no value exist in the registry
122     * @return requested value if exist, else the default value
123     * @throws Exception
124     */
125    public RegDataContainer getValue(String key, String value, RegDataContainer defaultVal) throws NativeLibException
126    {
127        if (!good()) return (null);
128        if (valueExist(key, value)) return (getValue(key, value));
129        return (defaultVal);
130    }
131
132    /**
133     * Returns whether a key exist or not.
134     * 
135     * @param key key to be evaluated
136     * @return whether a key exist or not
137     * @throws Exception
138     */
139    public boolean keyExist(String key) throws NativeLibException
140    {
141        if (!good()) return (false);
142        return (regWorker.keyExist(key));
143    }
144
145    /**
146     * Returns whether a the given value under the given key exist or not.
147     * 
148     * @param key key to be used as path for the value
149     * @param value value name to be evaluated
150     * @return whether a the given value under the given key exist or not
151     * @throws Exception
152     */
153    public boolean valueExist(String key, String value) throws NativeLibException
154    {
155        if (!good()) return (false);
156        return (regWorker.valueExist(key, value));
157    }
158
159    /**
160     * Returns all keys which are defined under the given key.
161     * 
162     * @param key key to be used as path for the sub keys
163     * @return all keys which are defined under the given key
164     * @throws Exception
165     */
166    public String[] getSubkeys(String key) throws NativeLibException
167    {
168        if (!good()) return (null);
169        return (regWorker.getSubkeys(key));
170    }
171
172    /**
173     * Returns all value names which are defined under the given key.
174     * 
175     * @param key key to be used as path for the value names
176     * @return all value names which are defined under the given key
177     * @throws Exception
178     */
179    public String[] getValueNames(String key) throws NativeLibException
180    {
181        if (!good()) return (null);
182        return (regWorker.getValueNames(key));
183    }
184
185    /**
186     * Returns the contents of the key/value pair if value exist, else an exception is raised.
187     * 
188     * @param key the registry key which should be used
189     * @param value the registry value from which the contents should be requested
190     * @return requested value if exist, else an exception
191     * @throws Exception
192     */
193    public RegDataContainer getValue(String key, String value) throws NativeLibException
194    {
195        if (!good()) return (null);
196        return (regWorker.getValue(key, value));
197    }
198
199    /**
200     * Creates the given key in the registry.
201     * 
202     * @param key key to be created
203     * @throws Exception
204     */
205    public void createKey(String key) throws NativeLibException
206    {
207        if (!good()) return;
208        regWorker.createKey(key);
209    }
210
211    /**
212     * Deletes the given key if exist, else throws an exception.
213     * @param key key to be deleted
214     * @throws NativeLibException
215     */
216    public void deleteKey( String key) throws NativeLibException
217    {
218        if (!good()) return;
219        regWorker.deleteKey(key);
220    }
221    
222    /**
223     * Deletes a key under the current root if it is empty, else do nothing.
224     * 
225     * @param key key to be deleted
226     * @throws NativeLibException
227     */
228    public void deleteKeyIfEmpty(String key) throws NativeLibException
229    {
230        if (!good()) return;
231        regWorker.deleteKeyIfEmpty(key);
232    }
233    
234    /**
235     * Deletes a value.
236     * 
237     * @param key key of the value which should be deleted
238     * @param value value name to be deleted
239     * @throws NativeLibException
240     */
241    public void deleteValue(String key, String value) throws NativeLibException
242    {
243        if (!good()) return;
244        regWorker.deleteValue(key, value);
245    }
246
247    /**
248     * Sets the root for the next registry access.
249     * 
250     * @param i an integer which refers to a HKEY
251     * @throws Exception
252     */
253    public void setRoot(int i) throws NativeLibException
254    {
255        if (!good()) return;
256        regWorker.setRoot(i);
257    }
258
259    /**
260     * Return the root as integer (HKEY_xxx).
261     * 
262     * @return the root as integer
263     * @throws Exception
264     */
265    public int getRoot() throws NativeLibException
266    {
267        if (!good()) return (0);
268        return (regWorker.getRoot());
269    }
270
271    /**
272     * Activates logging of registry changes.
273     * 
274     * @throws Exception
275     */
276    public void activateLogging() throws NativeLibException
277    {
278        if (!good()) return;
279        regWorker.activateLogging();
280    }
281
282    /**
283     * Suspends logging of registry changes.
284     * 
285     * @throws Exception
286     */
287    public void suspendLogging() throws NativeLibException
288    {
289        if (!good()) return;
290        regWorker.suspendLogging();
291    }
292
293    /**
294     * Resets logging of registry changes.
295     * 
296     * @throws Exception
297     */
298    public void resetLogging() throws NativeLibException
299    {
300        if (!good()) return;
301        regWorker.resetLogging();
302    }
303
304    public List getLoggingInfo() throws NativeLibException
305    {
306        if (!good()) return (null);
307        return (regWorker.getLoggingInfo());
308    }
309
310    public void setLoggingInfo(List info) throws NativeLibException
311    {
312        if (!good()) return;
313        regWorker.setLoggingInfo(info);
314    }
315
316    public void addLoggingInfo(List info) throws NativeLibException
317    {
318        if (!good()) return;
319        regWorker.addLoggingInfo(info);
320    }
321
322    public void rewind() throws NativeLibException
323    {
324        if (!good()) return;
325        regWorker.rewind();
326    }
327
328}