directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r209425 - in /directory/sandbox/trunk/osgi-spec/trunk/preferences/src: ./ main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/preferences/ main/java/org/apache/preferences/service/ main/java/org/apache/preferences/store/
Date Wed, 06 Jul 2005 07:58:27 GMT
Author: erodriguez
Date: Wed Jul  6 00:58:25 2005
New Revision: 209425

URL: http://svn.apache.org/viewcvs?rev=209425&view=rev
Log:
OSGi Preferences service implementation.

Added:
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/Activator.java
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/PrefsStorage.java
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/Base64.java
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesImpl.java
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceFactory.java
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceImpl.java
    directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/store/

Added: directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/Activator.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/Activator.java?rev=209425&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/Activator.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/Activator.java
Wed Jul  6 00:58:25 2005
@@ -0,0 +1,65 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.preferences;
+
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.prefs.PreferencesService;
+import org.apache.preferences.service.PreferencesServiceFactory;
+
+
+public class Activator implements BundleActivator
+{
+    BundleContext bc;
+    ServiceRegistration reg = null;
+
+    public void start( BundleContext bc )
+    {
+        this.bc = bc;
+        register();
+    }
+
+    public void stop( BundleContext bc )
+    {
+        // do nothing
+    }
+    
+    void register()
+    {
+        if ( reg == null )
+        {
+            Hashtable props = new Hashtable();
+            reg = bc.registerService( PreferencesService.class.getName(), new PreferencesServiceFactory(),
props );
+        }
+    }
+    
+    public static void error( String message )
+    {
+        System.out.println( message );
+    }
+    
+    public static void error( String message, Exception e )
+    {
+        System.out.println( message );
+        e.printStackTrace();
+    }
+}
+

Added: directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/PrefsStorage.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/PrefsStorage.java?rev=209425&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/PrefsStorage.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/PrefsStorage.java
Wed Jul  6 00:58:25 2005
@@ -0,0 +1,48 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.preferences;
+
+import org.osgi.service.prefs.Preferences;
+
+/**
+ * Abstraction interface for actual storage of prefs data.
+ * All paths are absolute.
+ */
+public interface PrefsStorage
+{
+    public String[] getChildrenNames( String path );
+
+    public void removeKey( String path, String key );
+
+    public void removeNode( String path );
+
+    public void put( String path, String key, String val );
+
+    public String get( String path, String key, String def );
+
+    public String[] getKeys( String path );
+
+    public Preferences getNode( String path, boolean bCreate );
+
+    public boolean nodeExists( String path );
+
+    public void removeAllKeys( String path );
+
+    public void flush( String path );
+}
+

Added: directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/Base64.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/Base64.java?rev=209425&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/Base64.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/Base64.java
Wed Jul  6 00:58:25 2005
@@ -0,0 +1,245 @@
+package org.apache.preferences.service;
+
+/*
+ * @(#)Base64.java	1.4 03/01/23
+ *
+ * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
+ * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ */
+
+/**
+ * Static methods for translating Base64 encoded strings to byte arrays
+ * and vice-versa.
+ *
+ * @author  Josh Bloch
+ * @version 1.4, 01/23/03
+ * @see     Preferences
+ * @since   1.4
+ */
+class Base64 {
+    /**
+     * Translates the specified byte array into a Base64 string as per
+     * Preferences.put(byte[]).
+     */
+    static String byteArrayToBase64(byte[] a) {
+        return byteArrayToBase64(a, false);
+    }
+
+    /**
+     * Translates the specified byte array into an "aternate representation"
+     * Base64 string.  This non-standard variant uses an alphabet that does
+     * not contain the uppercase alphabetic characters, which makes it
+     * suitable for use in situations where case-folding occurs.
+     */
+    static String byteArrayToAltBase64(byte[] a) {
+        return byteArrayToBase64(a, true);
+    }
+
+    private static String byteArrayToBase64(byte[] a, boolean alternate) {
+        int aLen = a.length;
+        int numFullGroups = aLen/3;
+        int numBytesInPartialGroup = aLen - 3*numFullGroups;
+        int resultLen = 4*((aLen + 2)/3);
+        StringBuffer result = new StringBuffer(resultLen);
+        char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
+
+        // Translate all full groups from byte array elements to Base64
+        int inCursor = 0;
+        for (int i=0; i<numFullGroups; i++) {
+            int byte0 = a[inCursor++] & 0xff;
+            int byte1 = a[inCursor++] & 0xff;
+            int byte2 = a[inCursor++] & 0xff;
+            result.append(intToAlpha[byte0 >> 2]);
+            result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
+            result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
+            result.append(intToAlpha[byte2 & 0x3f]);
+        }
+
+        // Translate partial group if present
+        if (numBytesInPartialGroup != 0) {
+            int byte0 = a[inCursor++] & 0xff;
+            result.append(intToAlpha[byte0 >> 2]);
+            if (numBytesInPartialGroup == 1) {
+                result.append(intToAlpha[(byte0 << 4) & 0x3f]);
+                result.append("==");
+            } else {
+                // assert numBytesInPartialGroup == 2;
+                int byte1 = a[inCursor++] & 0xff;
+                result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
+                result.append(intToAlpha[(byte1 << 2)&0x3f]);
+                result.append('=');
+            }
+        }
+        // assert inCursor == a.length;
+        // assert result.length() == resultLen;
+        return result.toString();
+    }
+
+    /**
+     * This array is a lookup table that translates 6-bit positive integer
+     * index values into their "Base64 Alphabet" equivalents as specified 
+     * in Table 1 of RFC 2045.
+     */
+    private static final char intToBase64[] = {
+        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
+    };
+
+    /**
+     * This array is a lookup table that translates 6-bit positive integer
+     * index values into their "Alternate Base64 Alphabet" equivalents.
+     * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
+     * This alternate alphabet does not use the capital letters.  It is
+     * designed for use in environments where "case folding" occurs.
+     */
+    private static final char intToAltBase64[] = {
+        '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
+        ';', '<', '>', '@', '[', ']', '^',  '`', '_', '{', '|', '}', '~',
+        'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm',
+        'n', 'o', 'p', 'q', 'r', 's', 't',  'u', 'v', 'w', 'x', 'y', 'z',
+        '0', '1', '2', '3', '4', '5', '6',  '7', '8', '9', '+', '?'
+    };
+
+    /**
+     * Translates the specified Base64 string (as per Preferences.get(byte[]))
+     * into a byte array.
+     * 
+     * @throw IllegalArgumentException if <tt>s</tt> is not a valid Base64
+     *        string.
+     */
+    static byte[] base64ToByteArray(String s) {
+        return base64ToByteArray(s, false);
+    }
+
+    /**
+     * Translates the specified "aternate representation" Base64 string
+     * into a byte array.
+     * 
+     * @throw IllegalArgumentException or ArrayOutOfBoundsException
+     *        if <tt>s</tt> is not a valid alternate representation
+     *        Base64 string.
+     */
+    static byte[] altBase64ToByteArray(String s) {
+        return base64ToByteArray(s, true);
+    }
+
+    private static byte[] base64ToByteArray(String s, boolean alternate) {
+        byte[] alphaToInt = (alternate ?  altBase64ToInt : base64ToInt);
+        int sLen = s.length();
+        int numGroups = sLen/4;
+        if (4*numGroups != sLen)
+            throw new IllegalArgumentException(
+                "String length must be a multiple of four.");
+        int missingBytesInLastGroup = 0;
+        int numFullGroups = numGroups;
+        if (sLen != 0) {
+            if (s.charAt(sLen-1) == '=') {
+                missingBytesInLastGroup++;
+                numFullGroups--;
+            }
+            if (s.charAt(sLen-2) == '=')
+                missingBytesInLastGroup++;
+        }
+        byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
+
+        // Translate all full groups from base64 to byte array elements
+        int inCursor = 0, outCursor = 0;
+        for (int i=0; i<numFullGroups; i++) {
+            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
+            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
+            int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
+            int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
+            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
+            result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
+            result[outCursor++] = (byte) ((ch2 << 6) | ch3);
+        }
+
+        // Translate partial group, if present
+        if (missingBytesInLastGroup != 0) {
+            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
+            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
+            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
+
+            if (missingBytesInLastGroup == 1) {
+                int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
+                result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
+            }
+        }
+        // assert inCursor == s.length()-missingBytesInLastGroup;
+        // assert outCursor == result.length;
+        return result;
+    }
+
+    /**
+     * Translates the specified character, which is assumed to be in the
+     * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
+     *
+     * @throw IllegalArgumentException or ArrayOutOfBoundsException if
+     *        c is not in the Base64 Alphabet.
+     */
+    private static int base64toInt(char c, byte[] alphaToInt) {
+        int result = alphaToInt[c];
+        if (result < 0)
+            throw new IllegalArgumentException("Illegal character " + c);
+        return result;
+    }
+
+    /**
+     * This array is a lookup table that translates unicode characters
+     * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
+     * into their 6-bit positive integer equivalents.  Characters that
+     * are not in the Base64 alphabet but fall within the bounds of the
+     * array are translated to -1.
+     */
+    private static final byte base64ToInt[] = {
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
+        55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
+        5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
+        24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
+        35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
+    };
+
+    /**
+     * This array is the analogue of base64ToInt, but for the nonstandard
+     * variant that avoids the use of uppercase alphabetic characters.
+     */
+    private static final byte altBase64ToInt[] = {
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
+        2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1 , 52, 53, 54, 55, 56, 57,
+        58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33,
+        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
+        51, 22, 23, 24, 25
+    };
+    
+    public static void main(String args[]) {
+        int numRuns  = Integer.parseInt(args[0]);
+        int numBytes = Integer.parseInt(args[1]);
+        java.util.Random rnd = new java.util.Random();
+        for (int i=0; i<numRuns; i++) {
+            for (int j=0; j<numBytes; j++) {
+                byte[] arr = new byte[j];
+                for (int k=0; k<j; k++)
+                    arr[k] = (byte)rnd.nextInt();
+
+                String s = byteArrayToBase64(arr);
+                byte [] b = base64ToByteArray(s);
+                if (!java.util.Arrays.equals(arr, b))
+                    System.out.println("Dismal failure!");
+
+                s = byteArrayToAltBase64(arr);
+                b = altBase64ToByteArray(s);
+                if (!java.util.Arrays.equals(arr, b))
+                    System.out.println("Alternate dismal failure!");
+            }
+        }
+    }
+}
+

Added: directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesImpl.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesImpl.java?rev=209425&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesImpl.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesImpl.java
Wed Jul  6 00:58:25 2005
@@ -0,0 +1,372 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.preferences.service;
+
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
+import org.apache.preferences.Activator;
+import org.apache.preferences.PrefsStorage;
+
+
+public class PreferencesImpl implements Preferences
+{
+    private PrefsStorage storage;
+    private String path;
+    private String parentPath;
+    private String name;
+    private boolean bStale = false;
+
+    public PreferencesImpl( PrefsStorage storage, String path )
+    {
+        this.storage = storage;
+        this.path = path;
+
+        if ( "/".equals( path ) )
+        {
+            path = "";
+        }
+        
+        int ix = path.lastIndexOf( "/" );
+        
+        if ( ix != -1 )
+        {
+            name = path.substring( ix + 1 );
+            parentPath = path.substring( 0, ix );
+        }
+        else
+        {
+            name = "";
+            parentPath = null;
+        }
+    }
+
+    public String absolutePath()
+    {
+        return "".equals( path ) ? "/" : path;
+    }
+
+    public String[] childrenNames()
+    {
+        assertValid();
+        return storage.getChildrenNames( path );
+    }
+
+    public void clear()
+    {
+        assertValid();
+        storage.removeAllKeys( path );
+    }
+
+    public void flush()
+    {
+        assertValid();
+        storage.flush( path );
+    }
+
+    public String get( String key, String def )
+    {
+        if ( key == null )
+        {
+            throw new NullPointerException( "Null key" );
+        }
+        
+        assertValid();
+        return storage.get( path, key, def );
+    }
+
+    public boolean getBoolean( String key, boolean def )
+    {
+        return "true".equals( get( key, def ? "true" : "false" ) );
+    }
+
+    public byte[] getByteArray( String key, byte[] def )
+    {
+        String s = get( key, null );
+        
+        if ( s == null )
+        {
+            return def;
+        }
+        
+        return Base64.base64ToByteArray( s );
+    }
+
+    public double getDouble( String key, double def )
+    {
+        try
+        {
+            return Double.parseDouble( get(key, Double.toString( def ) ) );
+        }
+        catch (NumberFormatException e)
+        {
+            return def;
+        }
+    }
+
+    public float getFloat( String key, float def )
+    {
+        try
+        {
+            return Float.parseFloat( get( key, Float.toString( def ) ) );
+        }
+        catch (NumberFormatException e)
+        {
+            return def;
+        }
+    }
+
+    public int getInt( String key, int def )
+    {
+        try
+        {
+            return Integer.parseInt( get( key, Integer.toString( def ) ) );
+        }
+        catch (NumberFormatException e)
+        {
+            return def;
+        }
+    }
+
+    public long getLong( String key, long def )
+    {
+        try
+        {
+            return Long.parseLong( get( key, Long.toString( def ) ) );
+        }
+        catch (NumberFormatException e)
+        {
+            return def;
+        }
+    }
+
+    public String[] keys() throws BackingStoreException
+    {
+        assertValid();
+        
+        try
+        {
+            return storage.getKeys( path );
+        }
+        catch (Exception e)
+        {
+            Activator.error( "keys: Failed to get keys for " + path );
+            
+            throw new BackingStoreException( e.getMessage() );
+        }
+    }
+
+    public String name()
+    {
+        return name;
+    }
+    
+    public boolean isStale()
+    {
+        return bStale;
+    }
+    
+    public void setStale( boolean setting )
+    {
+        bStale = setting;
+    }
+
+    public Preferences node( String pathName )
+    {
+
+        assertPath( pathName );
+        assertValid();
+
+        return storage.getNode( absPath( pathName ), true );
+    }
+
+    public boolean nodeExists( String pathName )
+    {
+        assertPath( pathName );
+
+        if ( bStale )
+        {
+            if ( !"".equals( pathName ) )
+            {
+                throw new IllegalStateException( "node removed" );
+            }
+        }
+
+        return storage.nodeExists( absPath( pathName ) );
+    }
+
+    public Preferences parent()
+    {
+        if ( parentPath == null )
+        {
+            return null;
+        }
+
+        assertValid();
+
+        return storage.getNode( parentPath, false );
+    }
+
+    public void put( String key, String value )
+    {
+        if ( key == null )
+        {
+            throw new NullPointerException( "null key" );
+        }
+        
+        if ( value == null )
+        {
+            throw new NullPointerException( "null value" );
+        }
+        
+        assertValid();
+
+        try
+        {
+            storage.put( path, key, value );
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException( "Failed to store " + key + ", " + e );
+        }
+    }
+
+    public void putByteArray( String key, byte[] value )
+    {
+        if ( key == null )
+        {
+            throw new NullPointerException( "null key" );
+        }
+        
+        if ( value == null )
+        {
+            throw new NullPointerException( "null value" );
+        }
+        
+        assertValid();
+
+        put( key, Base64.byteArrayToBase64( value ) );
+    }
+
+    public void putBoolean( String key, boolean value )
+    {
+        put( key, value ? "true" : "false" );
+    }
+
+    public void putDouble( String key, double value )
+    {
+        put( key, Double.toString( value ) );
+    }
+
+    public void putFloat( String key, float value )
+    {
+        put( key, Float.toString( value ) );
+    }
+
+    public void putInt( String key, int value )
+    {
+        put( key, Integer.toString( value ) );
+    }
+
+    public void putLong( String key, long value )
+    {
+        put( key, Long.toString( value ) );
+    }
+
+    public void remove( String key )
+    {
+        assertValid();
+        storage.removeKey( path, key );
+    }
+
+    public void removeNode()
+    {
+        assertValid();
+        
+        if ( "".equals( path ) )
+        {
+            throw new RuntimeException( "Cannot remove root node" );
+        }
+        
+        storage.removeNode( path );
+        bStale = true;
+    }
+
+    public void sync()
+    {
+        assertValid();
+    }
+
+    protected String absPath( String pathName )
+    {
+        if ( pathName.startsWith( "/" ) )
+        {
+            return pathName;
+        }
+        
+        if ( "".equals( pathName ) )
+        {
+            return path;
+        }
+
+        return path + "/" + pathName;
+    }
+
+    protected void assertValid()
+    {
+        if ( bStale || !storage.nodeExists( path ) )
+        {
+            throw new IllegalStateException( "no node at '" + path + "'" );
+        }
+    }
+
+    protected static void assertPath( String pathName )
+    {
+        if ( -1 != pathName.indexOf( "//" ) )
+        {
+            throw new IllegalArgumentException( "Illegal // in path name '" + pathName +
"'" );
+        }
+        
+        if ( pathName.length() > 1 && pathName.endsWith( "/" ) )
+        {
+            throw new IllegalArgumentException( "Trailing / in path name '" + pathName +
"'" );
+        }
+    }
+
+    public int hashCode()
+    {
+        return path.hashCode();
+    }
+
+    public boolean equals( Object other )
+    {
+        if ( other == null || !( other instanceof PreferencesImpl ) )
+        {
+            return false;
+        }
+        
+        PreferencesImpl pi = (PreferencesImpl) other;
+        return path.equals( pi.path );
+    }
+    
+    public String toString()
+    {
+        return "Preferences[" + "path=" + path + ", name=" + name
+                + ", parentPath=" + parentPath + ", bStale=" + bStale + "]";
+    }
+}
+

Added: directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceFactory.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceFactory.java?rev=209425&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceFactory.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceFactory.java
Wed Jul  6 00:58:25 2005
@@ -0,0 +1,66 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.preferences.service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.apache.preferences.Activator;
+
+
+public class PreferencesServiceFactory implements ServiceFactory
+{
+    private Map prefsMap = new HashMap();
+
+    public Object getService( Bundle bundle, ServiceRegistration reg )
+    {
+        synchronized ( prefsMap )
+        {
+            PreferencesServiceImpl prefs = (PreferencesServiceImpl) prefsMap.get( bundle
);
+
+            if ( prefs == null )
+            {
+                prefs = new PreferencesServiceImpl( bundle );
+                prefsMap.put( bundle, prefs );
+            }
+            return prefs;
+        }
+    }
+
+    public void ungetService( Bundle bundle, ServiceRegistration registration, Object service
)
+    {
+        synchronized ( prefsMap )
+        {
+            PreferencesServiceImpl prefs = (PreferencesServiceImpl) prefsMap.get( bundle
);
+
+            if ( prefs != null )
+            {
+                prefs.flush();
+                prefsMap.remove( bundle );
+            }
+            else
+            {
+                Activator.error( "No prefs for unget from bundle #" + bundle.getBundleId()
);
+            }
+        }
+    }
+}
+

Added: directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceImpl.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceImpl.java?rev=209425&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceImpl.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/preferences/src/main/java/org/apache/preferences/service/PreferencesServiceImpl.java
Wed Jul  6 00:58:25 2005
@@ -0,0 +1,88 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.preferences.service;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.service.prefs.Preferences;
+import org.osgi.service.prefs.PreferencesService;
+import org.apache.preferences.store.PrefsStorageFile;
+
+
+public class PreferencesServiceImpl implements PreferencesService
+{
+    private static String USERS_BASE  = "users";
+    private static String SYSTEM_BASE = "system";
+    private String userBase;
+    private String systemBase;
+    private PrefsStorageFile systemStorage;
+    private Map userStorage = new HashMap();
+
+    PreferencesServiceImpl( Bundle bundle )
+    {
+        userBase      = USERS_BASE + "/" + bundle.getBundleId();
+        systemBase    = SYSTEM_BASE + "/" + bundle.getBundleId();
+        systemStorage = new PrefsStorageFile( systemBase );
+    }
+
+    public Preferences getSystemPreferences()
+    {
+        return systemStorage.getNode( "", true );
+    }
+
+    public Preferences getUserPreferences( String name )
+    {
+        synchronized ( userStorage )
+        {
+            PrefsStorageFile storage = (PrefsStorageFile) userStorage.get( name );
+            
+            if ( storage == null )
+            {
+                storage = new PrefsStorageFile( userBase + "/" + name );
+                userStorage.put( name, storage );
+            }
+            
+            return storage.getNode( "", true );
+        }
+    }
+
+    public String[] getUsers()
+    {
+        synchronized ( userStorage )
+        {
+            String[] names = new String[ userStorage.size() ];
+            int i = 0;
+            
+            for ( Iterator it = userStorage.keySet().iterator(); it.hasNext(); )
+            {
+                names[i++] = (String) it.next();
+            }
+            
+            return names;
+        }
+    }
+
+    void flush()
+    {
+        systemStorage.flush( null );
+    }
+}
+



Mime
View raw message