1 55 package org.apache.avalon.framework.context; 56 57 import java.io.Serializable ; 58 import java.util.Hashtable ; 59 import java.util.Map ; 60 61 68 public class DefaultContext 69 implements Context 70 { 71 private static final class Hidden implements Serializable 72 { 73 } 74 75 private static final Hidden HIDDEN_MAKER = new Hidden(); 76 77 private final Map m_contextData; 78 private final Context m_parent; 79 private boolean m_readOnly; 80 81 87 public DefaultContext( final Map contextData, final Context parent ) 88 { 89 m_parent = parent; 90 m_contextData = contextData; 91 } 92 93 98 public DefaultContext( final Map contextData ) 99 { 100 this( contextData, null ); 101 } 102 103 108 public DefaultContext( final Context parent ) 109 { 110 this( new Hashtable (), parent ); 111 } 112 113 117 public DefaultContext() 118 { 119 this( (Context)null ); 120 } 121 122 129 public Object get( final Object key ) 130 throws ContextException 131 { 132 final Object data = m_contextData.get( key ); 133 134 if( null != data ) 135 { 136 if( data instanceof Hidden ) 137 { 138 final String message = "Unable to locate " + key; 140 throw new ContextException( message ); 141 } 142 143 if( data instanceof Resolvable ) 144 { 145 return ( (Resolvable)data ).resolve( this ); 146 } 147 148 return data; 149 } 150 151 if( null == m_parent ) 153 { 154 final String message = 156 "Unable to resolve context key: " + key; 157 throw new ContextException( message ); 158 } 159 160 return m_parent.get( key ); 161 } 162 163 170 public void put( final Object key, final Object value ) 171 throws IllegalStateException 172 { 173 checkWriteable(); 174 if( null == value ) 175 { 176 m_contextData.remove( key ); 177 } 178 else 179 { 180 m_contextData.put( key, value ); 181 } 182 } 183 184 193 public void hide( final Object key ) 194 throws IllegalStateException 195 { 196 checkWriteable(); 197 m_contextData.put( key, HIDDEN_MAKER ); 198 } 199 200 205 protected final Map getContextData() 206 { 207 return m_contextData; 208 } 209 210 215 protected final Context getParent() 216 { 217 return m_parent; 218 } 219 220 225 public void makeReadOnly() 226 { 227 m_readOnly = true; 228 } 229 230 235 protected final void checkWriteable() 236 throws IllegalStateException 237 { 238 if( m_readOnly ) 239 { 240 final String message = 241 "Context is read only and can not be modified"; 242 throw new IllegalStateException ( message ); 243 } 244 } 245 } 246 | Popular Tags |