litesoft
@ 474
litesoft / trunk / Java / core / Server / src / org / litesoft / configuration / ServerConfigDataAccessorFactoryProxy.java
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.configuration; import java.util.*; import org.litesoft.util.*; public class ServerConfigDataAccessorFactoryProxy implements ConfigDataAccessorFactory { private ConfigDataAccessorFactory mProxyFor; public ServerConfigDataAccessorFactoryProxy( ConfigDataAccessorFactory pProxyFor ) { mProxyFor = Utils.assertNotNull( "ProxyFor", pProxyFor ); } @Override public ConfigDataAccessor createConfigDataAccessor() { return new ConfigDataAccessorProxy( mProxyFor.createConfigDataAccessor() ); } private static class ConfigDataAccessorProxy implements ConfigDataAccessor { private final ConfigDataAccessor mProxyFor; private Map<String, String> mSystemKeyValues = new HashMap<String, String>(); private ConfigDataAccessorProxy( ConfigDataAccessor pProxyFor ) { mProxyFor = pProxyFor; try { mSystemKeyValues.putAll( System.getenv() ); } catch ( SecurityException e ) { System.err.println( "Unable to access Environment Variables" ); } try { Properties properties = System.getProperties(); for ( Map.Entry<Object, Object> zEntry : properties.entrySet() ) { String zKey = Utils.noEmpty( Utils.toString( zEntry.getKey() ) ); String zValue = Utils.noEmpty( Utils.toString( zEntry.getValue() ) ); if ( (zKey != null) && (zValue != null) ) { mSystemKeyValues.put( zKey, zValue ); } } } catch ( SecurityException e ) { System.err.println( "Unable to access System Properties" ); } } @Override public ConfigDataAccessor createConfigDataAccessor() { return this; } @Override public String[] getAllKeys() { Set<String> keys = new HashSet<String>( mSystemKeyValues.keySet() ); // Copy it for Mutation keys.addAll( Arrays.asList( mProxyFor.getAllKeys() ) ); return keys.toArray( new String[keys.size()] ); } @Override public String loadedFrom() { return mProxyFor.loadedFrom(); } @Override public String loadedFromDirectory() { return mProxyFor.loadedFromDirectory(); } @Override public Level[] getSetableSupportedLevels() { return mProxyFor.getSetableSupportedLevels(); } @Override public void setKeyValue( Level pLevel, String pKey, String pValue ) throws UnsupportedOperationException { synchronized ( mProxyFor ) { mProxyFor.setKeyValue( pLevel, pKey, pValue ); // Will validate pKey mSkipKeysEqual.add( pKey ); } } @Override public void overrideAllStartingWith( String pKeyPrefix, String pValue ) { synchronized ( mProxyFor ) { mProxyFor.overrideAllStartingWith( pKeyPrefix, pValue ); // Will validate pKeyPrefix mSkipKeysStartsWith.add( pKeyPrefix ); } } @Override public String getString( String pKey ) { synchronized ( mProxyFor ) { String zValue = mProxyFor.getString( pKey ); // Will validate pKey return skip( pKey ) ? zValue : Utils.deNull( getSystemString( pKey ), zValue ); } } private Set<String> mSkipKeysEqual = new HashSet<String>(); private Set<String> mSkipKeysStartsWith = new HashSet<String>(); private boolean skip( String pKey ) { if ( mSkipKeysEqual.contains( pKey ) ) { return true; } for ( String zKeyPrefix : mSkipKeysStartsWith ) { if ( pKey.startsWith( zKeyPrefix ) ) { return true; } } return false; } private String getSystemString( String pKey ) { return mSystemKeyValues.get( pKey ); } } } |