Subversion Repository Public Repository

litesoft

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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.configuration;

import java.util.*;
import java.util.prefs.*;

import org.litesoft.core.util.*;
import org.litesoft.util.*;

public class ConfigAccessorFactoryPreferences implements ConfigDataAccessorFactory
{
    private ConfigDataAccessor mAccessor;

    public ConfigAccessorFactoryPreferences( String pLoadedFrom, ConfigDataAccessor.Level pLevel, Preferences pPrefs )
    {
        Utils.assertNotNull( "Preferences", pPrefs );
        Utils.assertNotNull( "Level", pLevel );
        mAccessor = new ConfigDataAccessorImpl( pLoadedFrom, pLevel, pPrefs );
    }

    public ConfigAccessorFactoryPreferences( String pLoadedFrom, String pPreferenceNodeKey_ApplicationName )
    {
        this( pLoadedFrom, ConfigDataAccessor.Level.MACHINE, Preferences.systemRoot().node( pPreferenceNodeKey_ApplicationName ) );
        // this( pLoadedFrom, ConfigDataAccessor.Level.USER, Preferences.userRoot().node( pPreferenceNodeKey_ApplicationName ) );
    }

    @Override
    public ConfigDataAccessor createConfigDataAccessor()
    {
        return mAccessor;
    }

    private static class ConfigDataAccessorImpl implements ConfigDataAccessor
    {
        private final String mLoadedFrom;
        private final ConfigDataAccessor.Level mLevel;
        private final Preferences mPrefs;
        private final Map<String, String> mRunLevelKeyValues = new HashMap<String, String>();

        public ConfigDataAccessorImpl( String pLoadedFrom, ConfigDataAccessor.Level pLevel, Preferences pPrefs )
        {
            mLoadedFrom = pLoadedFrom;
            mLevel = pLevel;
            mPrefs = pPrefs;
        }

        @Override
        public String loadedFrom()
        {
            return mLoadedFrom;
        }

        @Override
        public String loadedFromDirectory()
        {
            return null;
        }

        /**
         * All implementations MUST support RUN level, but may not support other levels
         *
         * @return an array with at least 1 (RUN) entry
         */
        @Override
        public Level[] getSetableSupportedLevels()
        {
            return new Level[]{Level.RUN, mLevel};
        }

        /**
         * All implementations MUST support RUN level, but may not support other levels
         *
         * @param pLevel null interpreted as RUN
         * @param pKey   not null or empty
         * @param pValue null means remove
         *
         * @throws UnsupportedOperationException if requested level not supported
         */
        @Override
        public synchronized void setKeyValue( Level pLevel, String pKey, String pValue )
                throws UnsupportedOperationException
        {
            pKey = Utils.assertNotNullNotEmpty( "Key", pKey );
            Level zLevel = Utils.deNull( pLevel, Level.RUN );
            if ( Level.RUN.equals( zLevel ) )
            {
                if ( pValue == null )
                {
                    mRunLevelKeyValues.remove( pKey );
                }
                else
                {
                    mRunLevelKeyValues.put( pKey, pValue );
                }
                return;
            }
            if ( !zLevel.equals( mLevel ) )
            {
                throw new UnsupportedOperationException( "setKeyValue for Level '" + pLevel + "' not supported" );
            }
            if ( pValue == null )
            {
                try
                {
                    mPrefs.remove( pKey );
                }
                catch ( IllegalStateException e )
                {
                    // No Node so technically already Removed!
                }
            }
            else
            {
                try
                {
                    mPrefs.put( pKey, pValue );
                }
                catch ( IllegalStateException e )
                {
                    throw new PersistenceException( "Unable to setKeyValue( " + mLevel + ", \"" + pKey + "\", " + pValue + " ) keys for Preference Node: " + mPrefs, e );
                }
            }
            try
            {
                mPrefs.flush();
            }
            catch ( BackingStoreException e )
            {
                if ( pValue != null )
                {
                    pValue = "\"" + pValue + "\"";
                }
                throw new PersistenceException( "Unable to setKeyValue( " + mLevel + ", \"" + pKey + "\", " + pValue + " ) keys for Preference Node: " + mPrefs, e );
            }
        }

        @Override
        public synchronized String[] getAllKeys()
        {
            Set<String> zKeys = new HashSet<String>();
            try
            {
                String[] zPrefKeys = mPrefs.keys();
                zKeys.addAll( Arrays.asList( zPrefKeys ) );
            }
            catch ( BackingStoreException e )
            {
                throw new PersistenceException( "Unable to get keys for Preference Node: " + mPrefs, e );
            }
            catch ( IllegalStateException e )
            {
                // No Node == No Keys!
            }

            zKeys.addAll( mRunLevelKeyValues.keySet() );

            return zKeys.toArray( new String[zKeys.size()] );
        }

        /**
         * @param pKey not null or empty
         *
         * @return null if not found or value associated with key
         */
        @Override
        public synchronized String getString( String pKey )
        {
            pKey = Utils.assertNotNullNotEmpty( "Key", pKey );
            String rv = mRunLevelKeyValues.get( pKey );
            if ( rv == null )
            {
                try
                {
                    rv = mPrefs.get( pKey, null );
                }
                catch ( IllegalStateException e )
                {
                    // No Node == No Value -> null!
                }
            }
            return rv;
        }
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/configuration/ConfigAccessorFactoryPreferences.java

Diff revisions: vs.
Revision Author Commited Message
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

48 Diff Diff GeorgeS picture GeorgeS Sun 11 Apr, 2010 16:15:16 +0000
24 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 01:51:38 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000