0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU General
0007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
0008: * License("CDDL") (collectively, the "License"). You may not use this file except in
0009: * compliance with the License. You can obtain a copy of the License at
0010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
0011: * License for the specific language governing permissions and limitations under the
0012: * License. When distributing the software, include this License Header Notice in
0013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
0014: * designates this particular file as subject to the "Classpath" exception as
0015: * provided by Sun in the GPL Version 2 section of the License file that
0016: * accompanied this code. If applicable, add the following below the License Header,
0017: * with the fields enclosed by brackets [] replaced by your own identifying
0018: * information: "Portions Copyrighted [year] [name of copyright owner]"
0019: *
0020: * Contributor(s):
0021: *
0022: * The Original Software is NetBeans. The Initial Developer of the Original Software
0023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
0024: * Rights Reserved.
0025: *
0026: * If you wish your version of this file to be governed by only the CDDL or only the
0027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
0028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
0029: * you do not indicate a single choice of license, a recipient has the option to
0030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
0031: * to extend the choice of license to its licensees as provided above. However, if
0032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
0033: * the option applies only if the new code is made subject to such option by the
0034: * copyright holder.
0035: */
0036:
0037: package org.netbeans.installer.utils.system.windows;
0038:
0039: import java.util.Map;
0040: import java.util.Random;
0041: import org.netbeans.installer.utils.LogManager;
0042: import org.netbeans.installer.utils.ResourceUtils;
0043: import org.netbeans.installer.utils.StringUtils;
0044: import org.netbeans.installer.utils.exceptions.NativeException;
0045: import static org.netbeans.installer.utils.StringUtils.EMPTY_STRING;
0046:
0047: /**
0048: *
0049: * @author Dmitry Lipin
0050: * @author Kirill Sorokin
0051: */
0052: public class WindowsRegistry {
0053: /////////////////////////////////////////////////////////////////////////////
0054: // Instance
0055:
0056: // constructor //////////////////////////////////////////////////////////////
0057: /**
0058: *
0059: */
0060: public WindowsRegistry() {
0061: // does nothing
0062: }
0063:
0064: // queries //////////////////////////////////////////////////////////////////////
0065: /**
0066: * Checks whether the specified key exists in the registry (can be read).
0067: *
0068: * @param section The section of the registry
0069: * @param key The specified key
0070: * @return <i>true</i> if the specified key exists (can be read), <i>false</i> otherwise
0071: */
0072: public boolean keyExists(int section, String key)
0073: throws NativeException {
0074: validateSection(section);
0075: validateKey(key);
0076:
0077: try {
0078: return checkKeyAccess0(section, key, KEY_READ_LEVEL);
0079: } catch (UnsatisfiedLinkError e) {
0080: throw new NativeException(
0081: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0082: }
0083: }
0084:
0085: public boolean keyExists(int section, String parent, String child)
0086: throws NativeException {
0087: //validateSection(section);
0088: validateKey(parent);
0089: validateKeyName(child);
0090: validateParenthood(parent, child);
0091:
0092: try {
0093: return keyExists(section, parent + SEPARATOR + child);
0094: } catch (UnsatisfiedLinkError e) {
0095: throw new NativeException(
0096: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0097: }
0098: }
0099:
0100: /**
0101: * Checks whether the specified value exists in the registry.
0102: *
0103: * @param section The section of the registry
0104: * @param key The specified key
0105: * @param value The specified value
0106: * @return <i>true</i> if the specified value exists, <i>false</i> otherwise
0107: */
0108: public boolean valueExists(int section, String key, String name)
0109: throws NativeException {
0110: //validateSection(section);
0111: //validateKey(key);
0112: validateValueName(name);
0113:
0114: if (keyExists(section, key)) {
0115: try {
0116: return valueExists0(section, key, name);
0117: } catch (UnsatisfiedLinkError e) {
0118: throw new NativeException(
0119: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0120: }
0121: } else {
0122: throw new NativeException(
0123: "Cannot check for value existance - key does not exist");
0124: }
0125: }
0126:
0127: /**
0128: * Checks whether the specified value exists in the registry.
0129: *
0130: * @param section The section of the registry
0131: * @param key The specified key
0132: * @param value The specified value
0133: * @return <i>true</i> if the specified value exists, <i>false</i> otherwise
0134: */
0135: public boolean keyEmpty(int section, String key)
0136: throws NativeException {
0137: //validateSection(section);
0138: //validateKey(key);
0139:
0140: if (keyExists(section, key)) {
0141: try {
0142: return keyEmpty0(section, key);
0143: } catch (UnsatisfiedLinkError e) {
0144: throw new NativeException(
0145: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0146: }
0147: } else {
0148: throw new NativeException(
0149: "Cannot check -- key does not exist");
0150: }
0151: }
0152:
0153: /**
0154: * Get the number of the subkeys of the specified key.
0155: *
0156: * @param section The section of the registry
0157: * @param key The specified key
0158: * @return If the key doesn`t exist or can`t be accessed then return -1.
0159: * <br>Otherwise return the number of subkeys
0160: */
0161: public int countSubKeys(int section, String key)
0162: throws NativeException {
0163: //validateSection(section);
0164: //validateKey(key);
0165:
0166: if (keyExists(section, key)) {
0167: try {
0168: return countSubKeys0(section, key);
0169: } catch (UnsatisfiedLinkError e) {
0170: throw new NativeException(
0171: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0172: }
0173: } else {
0174: throw new NativeException(
0175: "Cannot count subkeys -- key does not exist");
0176: }
0177: }
0178:
0179: /** Get the number of the values of the specified key.
0180: * @param section The section of the registry
0181: * @param key The specified key
0182: * @return If the key doesn`t exist or can`t be accessed then return -1.
0183: * <br>Otherwise return the number of values
0184: */
0185: public int countValues(int section, String key)
0186: throws NativeException {
0187: //validateSection(section);
0188: //validateKey(key);
0189:
0190: if (keyExists(section, key)) {
0191: try {
0192: return countValues0(section, key);
0193: } catch (UnsatisfiedLinkError e) {
0194: throw new NativeException(
0195: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0196: }
0197:
0198: } else {
0199: throw new NativeException(
0200: "Cannot count values -- key does not exist");
0201: }
0202: }
0203:
0204: public String[] getSubKeys(int section, String key)
0205: throws NativeException {
0206: String[] names = getSubKeyNames(section, key);
0207: String[] subkeys = new String[names.length];
0208:
0209: for (int i = 0; i < names.length; i++) {
0210: subkeys[i] = constructKey(key, names[i]);
0211: }
0212:
0213: return subkeys;
0214: }
0215:
0216: /**
0217: * Get the array of subkey names of the specified key.
0218: *
0219: * @param section The section of the registry
0220: * @param key The specified key
0221: * @return If the key doesn`t exist or can`t be accessed then return <i>null</i>
0222: * <br>Otherwise return the array of subkey names
0223: */
0224: public String[] getSubKeyNames(int section, String key)
0225: throws NativeException {
0226: //validateSection(section);
0227: //validateKey(key);
0228:
0229: if (keyExists(section, key)) {
0230: try {
0231: return getSubkeyNames0(section, key);
0232: } catch (UnsatisfiedLinkError e) {
0233: throw new NativeException(
0234: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0235: }
0236: } else {
0237: throw new NativeException(
0238: "Cannot get subkey names -- key does not exist");
0239: }
0240: }
0241:
0242: /** Get the array of values names of the specified key.
0243: * @param section The section of the registry
0244: * @param key The specified key
0245: * @return If the key doesn`t exist or can`t be accessed then return <i>null</i>
0246: * <br>Otherwise return the array of value names
0247: */
0248: public String[] getValueNames(int section, String key)
0249: throws NativeException {
0250: //validateSection(section);
0251: //validateKey(key);
0252:
0253: if (keyExists(section, key)) {
0254: try {
0255: return getValueNames0(section, key);
0256: } catch (UnsatisfiedLinkError e) {
0257: throw new NativeException(
0258: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0259: }
0260: } else {
0261: throw new NativeException(
0262: "Cannot list value names -- key does not exist");
0263: }
0264: }
0265:
0266: /**
0267: * Returns the type of the value.
0268: *
0269: * @param section The section of the registry
0270: * @param key The specified key
0271: * @param value The specified value
0272: *
0273: * @return The possible values are:<br>
0274: *
0275: * <code>REG_NONE</code><br>
0276: * <code>REG_SZ</code><br>
0277: * <code>REG_EXPAND_SZ</code><br>
0278: * <code>REG_BINARY</code><br>
0279: * <code>REG_DWORD</code>=<code>REG_DWORD_LITTLE_ENDIAN</code><br>
0280: * <code>REG_DWORD_BIG_ENDIAN</code><br>
0281: * <code>REG_LINK</code><br>
0282: * <code>REG_MULTI_SZ</code><br>
0283: * <code>REG_RESOURCE_LIST</code><br>
0284: * <code>REG_FULL_RESOURCE_DESCRIPTOR</code><br>
0285: * <code>REG_RESOURCE_REQUIREMENTS_LIST</code><br>
0286: * <code>REG_QWORD</code>=<code>REG_QWORD_LITTLE_ENDIAN</code>
0287: */
0288: public int getValueType(int section, String key, String name)
0289: throws NativeException {
0290: //validateSection(section);
0291: //validateKey(key);
0292: //validateValueName(name);
0293:
0294: if (keyExists(section, key)) {
0295: if (valueExists(section, key, name)) {
0296: try {
0297: return getValueType0(section, key, name);
0298: } catch (UnsatisfiedLinkError e) {
0299: throw new NativeException(
0300: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0301: }
0302: } else {
0303: throw new NativeException(
0304: "Cannot get value type -- value does not exist");
0305: }
0306: } else {
0307: throw new NativeException(
0308: "Cannot get value type -- key does not exist");
0309: }
0310: }
0311:
0312: // key operations ///////////////////////////////////////////////////////////////
0313: /**
0314: * Create the new key in the registry.
0315: *
0316: * @param section The section of the registry
0317: * @param key The specified key
0318: * @return <i>true</i> if the key was successfully created,
0319: * <br> <i>false</i> otherwise
0320: */
0321: public void createKey(int section, String key)
0322: throws NativeException {
0323: createKey(section, getKeyParent(key), getKeyName(key));
0324: }
0325:
0326: /**
0327: * Create the new key in the registry.
0328: *
0329: * @param section The section of the registry
0330: * @param parent key The specified parent key
0331: * @param parent key The specified child key
0332: * @return <i>true</i> if the key was successfully created,
0333: * <br> <i>false</i> otherwise
0334: */
0335: public void createKey(int section, String parent, String child)
0336: throws NativeException {
0337: //validateSection(section);
0338: //validateKey(parent);
0339: //validateKeyName(child);
0340: //validateParenthood(parent, child);
0341:
0342: if (!keyExists(section, parent, child)) {
0343: if (!keyExists(section, parent)) {
0344: createKey(section, getKeyParent(parent),
0345: getKeyName(parent));
0346: }
0347:
0348: try {
0349: createKey0(section, parent, child);
0350: } catch (UnsatisfiedLinkError e) {
0351: throw new NativeException(
0352: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0353: }
0354: }
0355: }
0356:
0357: /**
0358: * Delete the specified key exists in the registry. Note that if the key
0359: * contains subkeys then it would not be deleted.
0360: *
0361: * @param section The section of the registry
0362: * @param key The specified key
0363: * @return <i>true</i> if the specified key was deleted, <i>false</i> otherwise
0364: */
0365: public void deleteKey(int section, String key)
0366: throws NativeException {
0367: deleteKey(section, getKeyParent(key), getKeyName(key));
0368: }
0369:
0370: /**
0371: * Delete the specified key exists in the registry.
0372: *
0373: * @param section The section of the registry
0374: * @param parentKey The specified parent key
0375: * @param childKey The specified child key
0376: * @return <i>true</i> if the specified key was deleted, <i>false</i> otherwise
0377: */
0378: public void deleteKey(int section, String parent, String child)
0379: throws NativeException {
0380: //validateSection(section);
0381: //validateKey(parent);
0382: //validateKeyName(child);
0383: //validateParenthood(parent, child);
0384:
0385: if (keyExists(section, parent, child)) {
0386: try {
0387: deleteKey0(section, parent, child);
0388: } catch (UnsatisfiedLinkError e) {
0389: throw new NativeException(
0390: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0391: }
0392: }
0393: }
0394:
0395: // value operations /////////////////////////////////////////////////////////////
0396: /**
0397: * Delete the specified value exists in the registry.
0398: *
0399: * @param section The section of the registry
0400: * @param key The specified key
0401: * @param value The specified value
0402: * @return <i>true</i> if the specified value was deleted, <i>false</i> otherwise
0403: */
0404: public void deleteValue(int section, String key, String name)
0405: throws NativeException {
0406: //validateSection(section);
0407: //validateKey(key);
0408: //validateValueName(name);
0409:
0410: if (keyExists(section, key)) {
0411: if (valueExists(section, key, name)) {
0412: try {
0413: deleteValue0(section, key, name);
0414: } catch (UnsatisfiedLinkError e) {
0415: throw new NativeException(
0416: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0417: }
0418: }
0419: } else {
0420: throw new NativeException(
0421: "Cannot delete value -- key does not exist");
0422: }
0423: }
0424:
0425: /**
0426: *
0427: * @param section
0428: * @param key
0429: * @param name
0430: * @return
0431: */
0432: public String getStringValue(int section, String key, String name)
0433: throws NativeException {
0434: return getStringValue(section, key, name, false);
0435: }
0436:
0437: /** Get string value.
0438: * @param section The section of the registry
0439: * @param key The specified key
0440: * @param name The specified value
0441: * @param expandable
0442: * If <code>expandable</code> is <i>true</i> and
0443: * the type of the value is REG_EXPAND_SZ the value would be expanded
0444: * @return The value of the name, <i>null</i> in case of any error
0445: */
0446: public String getStringValue(int section, String key, String name,
0447: boolean expand) throws NativeException {
0448: //validateSection(section);
0449: //validateKey(key);
0450: //validateValueName(name);
0451:
0452: if (keyExists(section, key)) {
0453: if (valueExists(section, key, name)) {
0454: try {
0455: return getStringValue0(section, key, name, expand);
0456: } catch (UnsatisfiedLinkError e) {
0457: throw new NativeException(
0458: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0459: }
0460: } else {
0461: throw new NativeException(
0462: "Cannot get string value -- value does not exist");
0463: }
0464: } else {
0465: throw new NativeException(
0466: "Cannot get string value -- key does not exist");
0467: }
0468: }
0469:
0470: public void setStringValue(int section, String key, String name,
0471: Object value) throws NativeException {
0472: setStringValue(section, key, name, value.toString());
0473: }
0474:
0475: /**
0476: *
0477: * @param section
0478: * @param key
0479: * @param name
0480: * @param value
0481: */
0482: public void setStringValue(int section, String key, String name,
0483: String value) throws NativeException {
0484: setStringValue(section, key, name, value, false);
0485: }
0486:
0487: /** Set string value.
0488: * @param section The section of the registry
0489: * @param key The specified key
0490: * @param name The specified value
0491: * @param value The specified value of the <code>name</code>
0492: * @param expandable
0493: * If <code>expandable</code> is <i>true</i> then the type would be
0494: * <code>REG_EXPAND_SZ</code> or <code>REG_SZ</code> otherwise
0495: * @return <i>true</i> if the value was successfully set
0496: * <br> <i>false</i> otherwise
0497: */
0498: public void setStringValue(int section, String key, String name,
0499: String value, boolean expandable) throws NativeException {
0500: //validateSection(section);
0501: //validateKey(key);
0502: validateValueName(name);
0503: validateStringValue(value);
0504:
0505: if (keyExists(section, key)) {
0506: try {
0507: setStringValue0(section, key, name, value, expandable);
0508: } catch (UnsatisfiedLinkError e) {
0509: throw new NativeException(
0510: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0511: }
0512: } else {
0513: throw new NativeException(
0514: "Cannot set string value -- key does not exist");
0515: }
0516: }
0517:
0518: /** Get integer value.
0519: * @param section The section of the registry
0520: * @param key The specified key
0521: * @param name The specified value
0522: * @return The value of the name, <i>-1</i> in case of any error
0523: */
0524: public int get32BitValue(int section, String key, String name)
0525: throws NativeException {
0526: //validateSection(section);
0527: //validateKey(key);
0528: //validateValueName(name);
0529:
0530: if (keyExists(section, key)) {
0531: if (valueExists(section, key, name)) {
0532: try {
0533: return get32BitValue0(section, key, name);
0534: } catch (UnsatisfiedLinkError e) {
0535: throw new NativeException(
0536: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0537: }
0538: } else {
0539: throw new NativeException(
0540: "Cannot get 32-bit value -- value does not exist");
0541: }
0542: } else {
0543: throw new NativeException(
0544: "Cannot get 32-bit value -- key does not exist");
0545: }
0546: }
0547:
0548: /** Set REG_DWORD value.
0549: * @param section The section of the registry
0550: * @param key The specified key
0551: * @param name The specified value
0552: * @param value The specified value of the <code>name</code>
0553: * @return <i>true</i> if the value was successfully set
0554: * <br> <i>false</i> otherwise
0555: */
0556: public void set32BitValue(int section, String key, String name,
0557: int value) throws NativeException {
0558: //validateSection(section);
0559: //validateKey(key);
0560: validateValueName(name);
0561: validate32BitValue(value);
0562:
0563: if (keyExists(section, key)) {
0564: try {
0565: set32BitValue0(section, key, name, value);
0566: } catch (UnsatisfiedLinkError e) {
0567: throw new NativeException(
0568: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0569: }
0570: } else {
0571: throw new NativeException(
0572: "Cannot set 32-bit value -- key does not exist");
0573: }
0574: }
0575:
0576: /** Get the array of strings of the specified value
0577: * @param section The section of the registry
0578: * @param key The specified key
0579: * @param name The specified value
0580: * @return The multri-string value of the name, <i>null</i> in case of any error
0581: */
0582: public String[] getMultiStringValue(int section, String key,
0583: String name) throws NativeException {
0584: //validateSection(section);
0585: //validateKey(key);
0586: //validateValueName(name);
0587:
0588: if (keyExists(section, key)) {
0589: if (valueExists(section, key, name)) {
0590: try {
0591: return getMultiStringValue0(section, key, name);
0592: } catch (UnsatisfiedLinkError e) {
0593: throw new NativeException(
0594: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0595: }
0596: } else {
0597: throw new NativeException(
0598: "Cannot get multistring value -- value does not exist");
0599: }
0600: } else {
0601: throw new NativeException(
0602: "Cannot get multistring value -- key does not exist");
0603: }
0604: }
0605:
0606: /** Set REG_MULTI_SZ value.
0607: * @param section The section of the registry
0608: * @param key The specified key
0609: * @param name The specified value
0610: * @param value The specified value of the <code>name</code>
0611: * @return <i>true</i> if the value was successfully set
0612: * <br> <i>false</i> otherwise
0613: */
0614: public void setMultiStringValue(int section, String key,
0615: String name, String[] value) throws NativeException {
0616: //validateSection(section);
0617: //validateKey(key);
0618: validateValueName(name);
0619: validateMultiStringValue(value);
0620:
0621: if (keyExists(section, key)) {
0622: try {
0623: setMultiStringValue0(section, key, name, value);
0624: } catch (UnsatisfiedLinkError e) {
0625: throw new NativeException(
0626: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0627: }
0628: } else {
0629: throw new NativeException(
0630: "Cannot set multistring value -- key does not exist");
0631: }
0632: }
0633:
0634: /**
0635: * Get binary value.
0636: *
0637: * @param section The section of the registry
0638: * @param key The specified key
0639: * @param name The specified value
0640: * @return The binary value of the name, <i>null</i> in case of any error
0641: */
0642: public byte[] getBinaryValue(int section, String key, String name)
0643: throws NativeException {
0644: //validateSection(section);
0645: //validateKey(key);
0646: //validateValueName(name);
0647:
0648: if (keyExists(section, key)) {
0649: if (valueExists(section, key, name)) {
0650: try {
0651: return getBinaryValue0(section, key, name);
0652: } catch (UnsatisfiedLinkError e) {
0653: throw new NativeException(
0654: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0655: }
0656: } else {
0657: throw new NativeException(
0658: "Cannot get binary value -- value does not exist");
0659: }
0660: } else {
0661: throw new NativeException(
0662: "Cannot get binary value -- key does not exist");
0663: }
0664: }
0665:
0666: /** Set binary (REG_BINARY) value.
0667: * @param section The section of the registry
0668: * @param key The specified key
0669: * @param name The specified value
0670: * @param value The specified value of the <code>name</code>
0671: * @return <i>true</i> if the value was successfully set
0672: * <br> <i>false</i> otherwise
0673: */
0674: public void setBinaryValue(int section, String key, String name,
0675: byte[] value) throws NativeException {
0676: //validateSection(section);
0677: //validateKey(key);
0678: validateValueName(name);
0679: validateBinaryValue(value);
0680:
0681: if (keyExists(section, key)) {
0682: try {
0683: setBinaryValue0(section, key, name, value);
0684: } catch (UnsatisfiedLinkError e) {
0685: throw new NativeException(
0686: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0687: }
0688: } else {
0689: throw new NativeException(
0690: "Cannot set binary value -- key does not exist");
0691: }
0692: }
0693:
0694: /**
0695: * Set new value of REG_NONE type
0696: *
0697: * @param section The section of the registry
0698: * @param key The specified key
0699: * @param value The specified value
0700: */
0701: public void setNoneValue(int section, String key, String name,
0702: byte... bytes) throws NativeException {
0703: //validateSection(section);
0704: //validateKey(key);
0705: validateValueName(name);
0706:
0707: if (keyExists(section, key)) {
0708: try {
0709: setNoneValue0(section, key, name, bytes);
0710: } catch (UnsatisfiedLinkError e) {
0711: throw new NativeException(
0712: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0713: }
0714: } else {
0715: throw new NativeException(
0716: "Cannot access value -- key does not exist");
0717: }
0718: }
0719:
0720: public void setAdditionalValues(int section, String key,
0721: Map<String, Object> values) throws NativeException {
0722: LogManager.log("setting " + values.size() + " values");
0723:
0724: for (String name : values.keySet()) {
0725: final Object value = values.get(name);
0726:
0727: LogManager.log(name + " = " + value.toString());
0728:
0729: if (value instanceof Short) {
0730: LogManager.log("Type is short. Set REG_DWORD value");
0731:
0732: set32BitValue(section, key, name, ((Short) value)
0733: .intValue());
0734: } else if (value instanceof Integer) {
0735: LogManager.log("Type is integer. Set REG_DWORD value");
0736:
0737: set32BitValue(section, key, name, ((Integer) value)
0738: .intValue());
0739: } else if (value instanceof Long) {
0740: LogManager.log("Type is long. Set REG_DWORD value");
0741:
0742: set32BitValue(section, key, name, ((Long) value)
0743: .intValue());
0744: } else if (value instanceof byte[]) {
0745: LogManager.log("Type is byte[]. Set REG_BINARY value");
0746:
0747: setBinaryValue(section, key, name, (byte[]) value);
0748: } else if (value instanceof String[]) {
0749: LogManager
0750: .log("Type is String[]. Set REG_MULTI_SZ value");
0751:
0752: setMultiStringValue(section, key, name,
0753: (String[]) value);
0754: } else if (value instanceof String) {
0755: LogManager.log("Type is String. Set REG_SZ value");
0756:
0757: setStringValue(section, key, name, (String) value,
0758: false);
0759: } else {
0760: LogManager
0761: .log("Type can't be determined. Set REG_SZ value");
0762:
0763: setStringValue(section, key, name, value.toString(),
0764: false);
0765: }
0766: }
0767: }
0768:
0769: /**
0770: * Checks whether the specified key exists and can be modified in the registry.
0771: *
0772: * @param section The section of the registry
0773: * @param key The specified key
0774: * @return <i>true</i> if the specified key can exists and can be modified, <i>false</i> otherwise
0775: */
0776: public boolean canModifyKey(int section, String key)
0777: throws NativeException {
0778: //validateSection(section);
0779: //validateKey(key);
0780: try {
0781: if (keyExists(section, key)) {
0782: boolean check = checkKeyAccess0(section, key,
0783: KEY_MODIFY_LEVEL);
0784:
0785: if (check) {
0786: // try to create/delete new sub key to be sure that we can modify the parent
0787: // this will require in most cases of vista with UAC enabled
0788: String randomKey = "rndkey"
0789: + new Random().nextLong();
0790: try {
0791: createKey0(section, key, randomKey);
0792: deleteKey0(section, key, randomKey);
0793: } catch (NativeException ex) {
0794: check = false;
0795: }
0796: }
0797:
0798: return check;
0799: } else {
0800: return canModifyKey(section, getKeyParent(key));
0801: }
0802: } catch (UnsatisfiedLinkError e) {
0803: throw new NativeException(
0804: ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING, e);
0805: }
0806: }
0807:
0808: // miscellanea //////////////////////////////////////////////////////////////////
0809: public String constructKey(String parent, String child) {
0810: return parent + SEPARATOR + child;
0811: }
0812:
0813: /**
0814: *
0815: * @param key
0816: * @return
0817: */
0818: public String getKeyParent(String key) {
0819: String temp = key;
0820:
0821: // strip the trailing separators
0822: while (temp.endsWith(SEPARATOR)) {
0823: temp = temp.substring(0, temp.length() - 1);
0824: }
0825:
0826: int index = temp.lastIndexOf(SEPARATOR);
0827: if (index != -1) {
0828: return temp.substring(0, index);
0829: } else {
0830: return StringUtils.EMPTY_STRING;
0831: }
0832: }
0833:
0834: /**
0835: *
0836: * @param key
0837: * @return
0838: */
0839: public String getKeyName(String key) {
0840: String temp = key;
0841:
0842: // strip the trailing separators
0843: while (temp.endsWith(SEPARATOR)) {
0844: temp = temp.substring(0, temp.length() - 1);
0845: }
0846:
0847: int index = temp.lastIndexOf(SEPARATOR);
0848: if (index != -1) {
0849: return temp.substring(index + 1);
0850: } else {
0851: return temp;
0852: }
0853: }
0854:
0855: // private //////////////////////////////////////////////////////////////////////
0856: private void validateSection(int section) throws NativeException {
0857: if ((section < HKEY_CLASSES_ROOT)
0858: || (section > HKEY_PERFORMANCE_TEXT)) {
0859: throw new NativeException("Section \"" + section + "\" is "
0860: + "invalid, should be between " + HKEY_CLASSES_ROOT
0861: + " " + "and " + HKEY_PERFORMANCE_TEXT);
0862: }
0863: }
0864:
0865: private void validateKey(String key) throws NativeException {
0866: if (key == null) {
0867: throw new NativeException("Key cannot be null");
0868: }
0869: }
0870:
0871: private void validateKeyName(String name) throws NativeException {
0872: if (name == null) {
0873: throw new NativeException("Key name cannot be null");
0874: }
0875: }
0876:
0877: private void validateParenthood(String parent, String child)
0878: throws NativeException {
0879: if (parent.equals(child)) {
0880: throw new NativeException("Parent cannot be equal to child");
0881: }
0882: }
0883:
0884: private void validateValueName(String name) throws NativeException {
0885: if (name == null) {
0886: throw new NativeException("Value name cannot be null");
0887: }
0888: }
0889:
0890: private void validateStringValue(String value)
0891: throws NativeException {
0892: if (value == null) {
0893: throw new NativeException("String value cannot be null");
0894: }
0895: }
0896:
0897: private void validate32BitValue(int value) throws NativeException {
0898: // it cannot be wrong, but just in case
0899: }
0900:
0901: private void validateMultiStringValue(String[] value)
0902: throws NativeException {
0903: if (value == null) {
0904: throw new NativeException(
0905: "Multistring value cannot be null");
0906: }
0907: }
0908:
0909: private void validateBinaryValue(byte[] value)
0910: throws NativeException {
0911: if (value == null) {
0912: throw new NativeException("Binary value cannot be null");
0913: }
0914: }
0915:
0916: // native declarations //////////////////////////////////////////////////////
0917: private native boolean keyExists0(int section, String key)
0918: throws NativeException;
0919:
0920: private native boolean valueExists0(int section, String key,
0921: String value) throws NativeException;
0922:
0923: private native boolean keyEmpty0(int section, String key)
0924: throws NativeException;
0925:
0926: private native int countSubKeys0(int section, String key)
0927: throws NativeException;
0928:
0929: private native int countValues0(int section, String key)
0930: throws NativeException;
0931:
0932: private native String[] getSubkeyNames0(int section, String key)
0933: throws NativeException;
0934:
0935: private native String[] getValueNames0(int section, String key)
0936: throws NativeException;
0937:
0938: private native int getValueType0(int section, String key,
0939: String value) throws NativeException;
0940:
0941: private native void createKey0(int section, String parent,
0942: String child) throws NativeException;
0943:
0944: private native void deleteKey0(int section, String parent,
0945: String child) throws NativeException;
0946:
0947: private native void deleteValue0(int section, String key,
0948: String value) throws NativeException;
0949:
0950: private native String getStringValue0(int section, String key,
0951: String name, boolean expand) throws NativeException;
0952:
0953: private native void setStringValue0(int section, String key,
0954: String name, String value, boolean expandable);
0955:
0956: private native int get32BitValue0(int section, String key,
0957: String name) throws NativeException;
0958:
0959: private native void set32BitValue0(int section, String key,
0960: String name, int value) throws NativeException;
0961:
0962: private native String[] getMultiStringValue0(int section,
0963: String key, String name) throws NativeException;
0964:
0965: private native void setMultiStringValue0(int section, String key,
0966: String name, String[] value) throws NativeException;
0967:
0968: private native byte[] getBinaryValue0(int section, String key,
0969: String name) throws NativeException;
0970:
0971: private native void setBinaryValue0(int section, String key,
0972: String name, byte[] value) throws NativeException;
0973:
0974: private native void setNoneValue0(int section, String key,
0975: String name, Object value) throws NativeException;
0976:
0977: private native boolean checkKeyAccess0(int section, String key,
0978: int level) throws NativeException;
0979:
0980: /////////////////////////////////////////////////////////////////////////////////
0981: // Constants
0982: public static final int HKEY_CLASSES_ROOT = 0;
0983: public static final int HKEY_CURRENT_USER = 1;
0984: public static final int HKEY_LOCAL_MACHINE = 2;
0985: public static final int HKEY_USERS = 3;
0986: public static final int HKEY_CURRENT_CONFIG = 4;
0987:
0988: public static final int HKEY_DYN_DATA = 5;
0989: public static final int HKEY_PERFORMANCE_DATA = 6;
0990: public static final int HKEY_PERFORMANCE_NLSTEXT = 7;
0991: public static final int HKEY_PERFORMANCE_TEXT = 8;
0992:
0993: public static final int HKCR = HKEY_CLASSES_ROOT;
0994: public static final int HKCU = HKEY_CURRENT_USER;
0995: public static final int HKLM = HKEY_LOCAL_MACHINE;
0996:
0997: public static final int REG_NONE = 0;
0998: public static final int REG_SZ = 1;
0999: public static final int REG_EXPAND_SZ = 2;
1000: public static final int REG_BINARY = 3;
1001: public static final int REG_DWORD_LITTLE_ENDIAN = 4;
1002: public static final int REG_DWORD = 4;
1003: public static final int REG_DWORD_BIG_ENDIAN = 5;
1004: public static final int REG_LINK = 6;
1005: public static final int REG_MULTI_SZ = 7;
1006: public static final int REG_RESOURCE_LIST = 8;
1007: public static final int REG_FULL_RESOURCE_DESCRIPTOR = 9;
1008: public static final int REG_RESOURCE_REQUIREMENTS_LIST = 10;
1009: public static final int REG_QWORD_LITTLE_ENDIAN = 11;
1010: public static final int REG_QWORD = 11;
1011:
1012: public static final String SEPARATOR = "\\";
1013:
1014: private static int KEY_READ_LEVEL = 0;
1015: private static int KEY_MODIFY_LEVEL = 1;
1016:
1017: private static final String ERROR_CANNOT_ACCESS_NATIVE_METHOD_STRING = ResourceUtils
1018: .getString(WindowsRegistry.class,
1019: "WR.error.cannot.access.native");//NOI18N
1020: }
|