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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.configuration;

import java.util.*;

import org.litesoft.core.typeutils.*;
import org.litesoft.core.typeutils.Objects;

public abstract class Configuration
{
    private static Configuration sConfiguration = null;
    private static Set<ConfigLoadedListener> sConfigLoadedListeners = new HashSet<ConfigLoadedListener>();

    public synchronized static boolean isInstantiated()
    {
        return (sConfiguration != null);
    }

    public synchronized static void chkInstantiated()
    {
        if ( sConfiguration == null )
        {
            throw new IllegalStateException( "Configuration never Initialized" );
        }
    }

    public synchronized static void clear()
    {
        sConfiguration = null;
    }

    private synchronized static void setInstantiated( Configuration pConfiguration )
    {
        if ( sConfiguration != null )
        {
            throw new IllegalStateException( "Attempt to multi-define the Configuration" );
        }
        sConfiguration = pConfiguration;
    }

    private static synchronized Configuration getInstance()
    {
        chkInstantiated();
        return sConfiguration;
    }

    protected static synchronized ConfigDataAccessor getConfigDataAccessor()
    {
        return getInstance().insureConfigDataAccessorInstantiated();
    }

    /**
     * only called from synchronized static block!
     */
    private ConfigDataAccessor insureConfigDataAccessorInstantiated()
    {
        if ( mAccessor == null )
        {
            mAccessor = mFactory.createConfigDataAccessor();
            mFactory = null; // Release it
            notifyJustLoadedListeners();
        }
        return mAccessor;
    }

    /**
     * only called from synchronized static block!
     */
    protected boolean isLoaded()
    {
        return (mAccessor != null);
    }

    /**
     * only called from synchronized static block!
     */
    private static boolean isInstantiatedAndLoaded()
    {
        return isInstantiated() && getInstance().isLoaded();
    }

    public synchronized static void add( ConfigLoadedListener pConfigLoadedListener )
    {
        if ( isInstantiatedAndLoaded() )
        {
            throw new IllegalStateException( "May not add a ConfigLoadedListener AFTER the Configuration is already loaded" );
        }
        if ( pConfigLoadedListener != null )
        {
            sConfigLoadedListeners.add( pConfigLoadedListener );
        }
    }

    /**
     * only called from synchronized static block!
     */
    private static void notifyJustLoadedListeners()
    {
        for ( ConfigLoadedListener listener : sConfigLoadedListeners )
        {
            listener.configurationJustLoaded();
        }
        sConfigLoadedListeners.clear();
    }

    protected ConfigDataAccessor mAccessor = null;
    private ConfigDataAccessorFactory mFactory;

    protected Configuration( ConfigDataAccessorFactory pFactory )
    {
        Objects.assertNotNull( "ConfigDataAccessorFactory", mFactory = pFactory );
        setInstantiated( this );
    }

    public static String[] getAllKeys()
    {
        return getConfigDataAccessor().getAllKeys();
    }

    public static Map<String, String> getCompleteConfigMap()
    {
        Map<String, String> config = new HashMap<String, String>();
        String[] keys = getConfigDataAccessor().getAllKeys();
        for ( String zKey : keys )
        {
            config.put( zKey, getString( zKey ) );
        }
        return config;
    }

    /**
     * @return !null - source of Config data.
     */
    public static String loadedFrom()
    {
        return getConfigDataAccessor().loadedFrom();
    }

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

    /**
     * All implementations MUST support RUN level, but do not have to 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
     */
    public static void setKeyValue( ConfigDataAccessor.Level pLevel, String pKey, String pValue )
            throws UnsupportedOperationException
    {
        getConfigDataAccessor().setKeyValue( pLevel, pKey, pValue );
    }

    /**
     * @param pKey not null or empty
     *
     * @return null if not found or value associated with key
     */
    public static String getString( String pKey )
    {
        return getConfigDataAccessor().getString( pKey );
    }

    /**
     * @param pKey not null or empty
     *
     * @return "" if not found or value associated with key, trimmed
     */
    public static String getStringTrimmed( String pKey )
    {
        return Strings.deNull( getString( pKey ) ).trim();
    }

    /**
     * @param pKey not null or empty
     *
     * @return !null / !"" value associated with key, trimmed
     */
    public static String getStringRequired( String pKey )
    {
        String value = getStringTrimmed( pKey );
        if ( value.length() != 0 )
        {
            return value;
        }
        throw new IllegalStateException( loadedFrom() + " should have a entry that defines: " + pKey );
    }

    /**
     * @param pKey not null or empty
     *
     * @return pDefault if not found (or empty) or value associated with key
     */
    public static String getString( String pKey, String pDefault )
    {
        String value = getStringTrimmed( pKey );
        return (value.length() != 0) ? value : pDefault;
    }

    /**
     * @param pKey not null or empty
     *
     * @return true or false as the trimmed value associated with key indicates, or pDefault if it is not parsable
     */
    public static boolean getBoolean( String pKey, boolean pDefault )
    {
        String value = getStringTrimmed( pKey );
        if ( value.length() != 0 )
        {
            return Boolean.valueOf( value );
        }
        return pDefault;
    }

    /**
     * @param pKey not null or empty
     *
     * @return parseInt of the trimmed value associated with key, or pDefault if it is not parsable
     */
    public static int getInteger( String pKey, int pDefault )
    {
        String value = getStringTrimmed( pKey );
        if ( value.length() != 0 )
        {
            try
            {
                return Integer.parseInt( value );
            }
            catch ( NumberFormatException e )
            {
                // Whatever...
            }
        }
        return pDefault;
    }

    public static class CachedConfigInt
    {
        private String mKey;
        private int mDefault;
        private Integer mValue = null;

        public CachedConfigInt( String pKey, int pDefault )
        {
            mKey = pKey;
            mDefault = pDefault;
        }

        public synchronized int getInteger()
        {
            if ( mValue == null )
            {
                mValue = Configuration.getInteger( mKey, mDefault );
            }
            return mValue;
        }
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/configuration/Configuration.java

Diff revisions: vs.
Revision Author Commited Message
917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
474 Diff Diff GeorgeS picture GeorgeS Fri 02 Sep, 2011 14:29:50 +0000

Switch to Properties and eliminate some of the Per App shit

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

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000