src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java

Print this page
rev 11676 : 8075156: (prefs) remove() should disallow the use of the null control character '\u0000' as key
Summary: Extend disallowing null control character key to remove()
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.prefs;
  27 
  28 import java.util.Map;
  29 import java.util.TreeMap;
  30 import java.util.StringTokenizer;
  31 import java.io.ByteArrayOutputStream;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 
  35 import sun.util.logging.PlatformLogger;
  36 
  37 /**
  38  * Windows registry based implementation of  <tt>Preferences</tt>.
  39  * <tt>Preferences</tt>' <tt>systemRoot</tt> and <tt>userRoot</tt> are stored in
  40  * <tt>HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs</tt> and
  41  * <tt>HKEY_CURRENT_USER\Software\JavaSoft\Prefs</tt> correspondingly.
  42  *
  43  * @author  Konstantin Kladko
  44  * @see Preferences
  45  * @see PreferencesFactory
  46  * @since 1.4
  47  */
  48 
  49 class WindowsPreferences extends AbstractPreferences{
  50 
  51     static {
  52         PrivilegedAction<Void> load = () -> {
  53             System.loadLibrary("prefs");
  54             return null;
  55         };
  56         AccessController.doPrivileged(load);
  57     }
  58 
  59     /**







  60      * Logger for error messages
  61      */
  62     private static PlatformLogger logger;
  63 
  64     /**
  65      * Windows registry path to <tt>Preferences</tt>'s root nodes.
  66      */
  67     private static final byte[] WINDOWS_ROOT_PATH
  68                                = stringToByteArray("Software\\JavaSoft\\Prefs");
  69 
  70     /**
  71      * Windows handles to <tt>HKEY_CURRENT_USER</tt> and
  72      * <tt>HKEY_LOCAL_MACHINE</tt> hives.
  73      */
  74     private static final int HKEY_CURRENT_USER = 0x80000001;
  75     private static final int HKEY_LOCAL_MACHINE = 0x80000002;
  76 
  77     /**
  78      * Mount point for <tt>Preferences</tt>'  user root.
  79      */


 603      * @see #openKey(byte[],int)
 604      * @see #openKey(int, byte[],int)
 605     */
 606     private void closeKey(int nativeHandle) {
 607         int result = WindowsRegCloseKey(nativeHandle);
 608         if (result != ERROR_SUCCESS) {
 609             logger().warning("Could not close windows "
 610             + "registry node " + byteArrayToString(windowsAbsolutePath()) +
 611             " at root 0x" + Integer.toHexString(rootNativeHandle()) +
 612             ". Windows RegCloseKey(...) returned error code " + result + ".");
 613         }
 614     }
 615 
 616      /**
 617      * Implements <tt>AbstractPreferences</tt> <tt>putSpi()</tt> method.
 618      * Puts name-value pair into the underlying Windows registry node.
 619      * Logs a warning, if Windows registry is unavailable.
 620      * @see #getSpi(String)
 621      */
 622     protected void putSpi(String javaName, String value) {





 623     int nativeHandle = openKey(KEY_SET_VALUE);
 624     if (nativeHandle == NULL_NATIVE_HANDLE) {
 625         isBackingStoreAvailable = false;
 626         return;
 627     }
 628     int result =  WindowsRegSetValueEx1(nativeHandle,
 629                           toWindowsName(javaName), toWindowsValueString(value));
 630     if (result != ERROR_SUCCESS) {
 631         logger().warning("Could not assign value to key " +
 632         byteArrayToString(toWindowsName(javaName))+ " at Windows registry node "
 633        + byteArrayToString(windowsAbsolutePath()) + " at root 0x"
 634        + Integer.toHexString(rootNativeHandle()) +
 635        ". Windows RegSetValueEx(...) returned error code " + result + ".");
 636         isBackingStoreAvailable = false;
 637         }
 638     closeKey(nativeHandle);
 639     }
 640 
 641     /**
 642      * Implements <tt>AbstractPreferences</tt> <tt>getSpi()</tt> method.


 650         return null;
 651     }
 652     Object resultObject =  WindowsRegQueryValueEx(nativeHandle,
 653                                                   toWindowsName(javaName));
 654     if (resultObject == null) {
 655         closeKey(nativeHandle);
 656         return null;
 657     }
 658     closeKey(nativeHandle);
 659     return toJavaValueString((byte[]) resultObject);
 660     }
 661 
 662     /**
 663      * Implements <tt>AbstractPreferences</tt> <tt>removeSpi()</tt> method.
 664      * Deletes a string name-value pair from the underlying Windows registry
 665      * node, if this value still exists.
 666      * Logs a warning, if Windows registry is unavailable or key has already
 667      * been deleted.
 668      */
 669     protected void removeSpi(String key) {



 670         int nativeHandle = openKey(KEY_SET_VALUE);
 671         if (nativeHandle == NULL_NATIVE_HANDLE) {
 672         return;
 673         }
 674         int result =
 675             WindowsRegDeleteValue(nativeHandle, toWindowsName(key));
 676         if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
 677             logger().warning("Could not delete windows registry "
 678             + "value " + byteArrayToString(windowsAbsolutePath())+ "\\" +
 679             toWindowsName(key) + " at root 0x" +
 680             Integer.toHexString(rootNativeHandle()) +
 681             ". Windows RegDeleteValue(...) returned error code " +
 682             result + ".");
 683             isBackingStoreAvailable = false;
 684         }
 685         closeKey(nativeHandle);
 686     }
 687 
 688     /**
 689      * Implements <tt>AbstractPreferences</tt> <tt>keysSpi()</tt> method.


   1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.prefs;
  27 


  28 import java.util.StringTokenizer;
  29 import java.io.ByteArrayOutputStream;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 
  33 import sun.util.logging.PlatformLogger;
  34 
  35 /**
  36  * Windows registry based implementation of  <tt>Preferences</tt>.
  37  * <tt>Preferences</tt>' <tt>systemRoot</tt> and <tt>userRoot</tt> are stored in
  38  * <tt>HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs</tt> and
  39  * <tt>HKEY_CURRENT_USER\Software\JavaSoft\Prefs</tt> correspondingly.
  40  *
  41  * @author  Konstantin Kladko
  42  * @see Preferences
  43  * @see PreferencesFactory
  44  * @since 1.4
  45  */
  46 
  47 class WindowsPreferences extends AbstractPreferences {
  48 
  49     static {
  50         PrivilegedAction<Void> load = () -> {
  51             System.loadLibrary("prefs");
  52             return null;
  53         };
  54         AccessController.doPrivileged(load);
  55     }
  56 
  57     /**
  58      * The code point U+0000, assigned to the null control character, is the
  59      * only character encoded in Unicode and ISO/IEC 10646 that is always
  60      * invalid in any XML 1.0 and 1.1 document.
  61      */
  62     private static final String CODE_POINT_U0000 = String.valueOf('\u0000');
  63 
  64     /**
  65      * Logger for error messages
  66      */
  67     private static PlatformLogger logger;
  68 
  69     /**
  70      * Windows registry path to <tt>Preferences</tt>'s root nodes.
  71      */
  72     private static final byte[] WINDOWS_ROOT_PATH
  73                                = stringToByteArray("Software\\JavaSoft\\Prefs");
  74 
  75     /**
  76      * Windows handles to <tt>HKEY_CURRENT_USER</tt> and
  77      * <tt>HKEY_LOCAL_MACHINE</tt> hives.
  78      */
  79     private static final int HKEY_CURRENT_USER = 0x80000001;
  80     private static final int HKEY_LOCAL_MACHINE = 0x80000002;
  81 
  82     /**
  83      * Mount point for <tt>Preferences</tt>'  user root.
  84      */


 608      * @see #openKey(byte[],int)
 609      * @see #openKey(int, byte[],int)
 610     */
 611     private void closeKey(int nativeHandle) {
 612         int result = WindowsRegCloseKey(nativeHandle);
 613         if (result != ERROR_SUCCESS) {
 614             logger().warning("Could not close windows "
 615             + "registry node " + byteArrayToString(windowsAbsolutePath()) +
 616             " at root 0x" + Integer.toHexString(rootNativeHandle()) +
 617             ". Windows RegCloseKey(...) returned error code " + result + ".");
 618         }
 619     }
 620 
 621      /**
 622      * Implements <tt>AbstractPreferences</tt> <tt>putSpi()</tt> method.
 623      * Puts name-value pair into the underlying Windows registry node.
 624      * Logs a warning, if Windows registry is unavailable.
 625      * @see #getSpi(String)
 626      */
 627     protected void putSpi(String javaName, String value) {
 628         if (javaName.indexOf(CODE_POINT_U0000) != -1) {
 629             throw new IllegalArgumentException("Key contains code point U+0000");
 630         } else if (value.indexOf(CODE_POINT_U0000) != -1) {
 631             throw new IllegalArgumentException("Value contains code point U+0000");
 632         }
 633         int nativeHandle = openKey(KEY_SET_VALUE);
 634         if (nativeHandle == NULL_NATIVE_HANDLE) {
 635             isBackingStoreAvailable = false;
 636             return;
 637         }
 638         int result = WindowsRegSetValueEx1(nativeHandle,
 639                 toWindowsName(javaName), toWindowsValueString(value));
 640         if (result != ERROR_SUCCESS) {
 641             logger().warning("Could not assign value to key " +
 642             byteArrayToString(toWindowsName(javaName))+ " at Windows registry node "
 643            + byteArrayToString(windowsAbsolutePath()) + " at root 0x"
 644            + Integer.toHexString(rootNativeHandle()) +
 645            ". Windows RegSetValueEx(...) returned error code " + result + ".");
 646             isBackingStoreAvailable = false;
 647         }
 648         closeKey(nativeHandle);
 649     }
 650 
 651     /**
 652      * Implements <tt>AbstractPreferences</tt> <tt>getSpi()</tt> method.


 660             return null;
 661         }
 662         Object resultObject = WindowsRegQueryValueEx(nativeHandle,
 663                 toWindowsName(javaName));
 664         if (resultObject == null) {
 665             closeKey(nativeHandle);
 666             return null;
 667         }
 668         closeKey(nativeHandle);
 669         return toJavaValueString((byte[]) resultObject);
 670     }
 671 
 672     /**
 673      * Implements <tt>AbstractPreferences</tt> <tt>removeSpi()</tt> method.
 674      * Deletes a string name-value pair from the underlying Windows registry
 675      * node, if this value still exists.
 676      * Logs a warning, if Windows registry is unavailable or key has already
 677      * been deleted.
 678      */
 679     protected void removeSpi(String key) {
 680         if (key.indexOf(CODE_POINT_U0000) != -1) {
 681             throw new IllegalArgumentException("Key contains code point U+0000");
 682         }
 683         int nativeHandle = openKey(KEY_SET_VALUE);
 684         if (nativeHandle == NULL_NATIVE_HANDLE) {
 685         return;
 686         }
 687         int result =
 688             WindowsRegDeleteValue(nativeHandle, toWindowsName(key));
 689         if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
 690             logger().warning("Could not delete windows registry "
 691             + "value " + byteArrayToString(windowsAbsolutePath())+ "\\" +
 692             toWindowsName(key) + " at root 0x" +
 693             Integer.toHexString(rootNativeHandle()) +
 694             ". Windows RegDeleteValue(...) returned error code " +
 695             result + ".");
 696             isBackingStoreAvailable = false;
 697         }
 698         closeKey(nativeHandle);
 699     }
 700 
 701     /**
 702      * Implements <tt>AbstractPreferences</tt> <tt>keysSpi()</tt> method.