KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > context > DefaultContext


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software
24  * itself, if and wherever such third-party acknowledgments
25  * normally appear.
26  *
27  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
28  * must not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.apache.avalon.framework.context;
56
57 import java.io.Serializable JavaDoc;
58 import java.util.Hashtable JavaDoc;
59 import java.util.Map JavaDoc;
60
61 /**
62  * Default implementation of Context.
63  * This implementation is a static hierarchial store.
64  *
65  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
66  * @version CVS $Revision: 1.25 $ $Date: 2003/02/11 15:58:40 $
67  */

68 public class DefaultContext
69     implements Context
70 {
71     private static final class Hidden implements Serializable JavaDoc
72     {
73     }
74
75     private static final Hidden HIDDEN_MAKER = new Hidden();
76
77     private final Map JavaDoc m_contextData;
78     private final Context m_parent;
79     private boolean m_readOnly;
80
81     /**
82      * Create a Context with specified data and parent.
83      *
84      * @param contextData the context data
85      * @param parent the parent Context (may be null)
86      */

87     public DefaultContext( final Map JavaDoc contextData, final Context parent )
88     {
89         m_parent = parent;
90         m_contextData = contextData;
91     }
92
93     /**
94      * Create a Context with specified data.
95      *
96      * @param contextData the context data
97      */

98     public DefaultContext( final Map JavaDoc contextData )
99     {
100         this( contextData, null );
101     }
102
103     /**
104      * Create a Context with specified parent.
105      *
106      * @param parent the parent Context (may be null)
107      */

108     public DefaultContext( final Context parent )
109     {
110         this( new Hashtable JavaDoc(), parent );
111     }
112
113     /**
114      * Create a Context with no parent.
115      *
116      */

117     public DefaultContext()
118     {
119         this( (Context)null );
120     }
121
122     /**
123      * Retrieve an item from the Context.
124      *
125      * @param key the key of item
126      * @return the item stored in context
127      * @throws ContextException if item not present
128      */

129     public Object JavaDoc get( final Object JavaDoc key )
130         throws ContextException
131     {
132         final Object JavaDoc data = m_contextData.get( key );
133
134         if( null != data )
135         {
136             if( data instanceof Hidden )
137             {
138                 // Always fail.
139
final String JavaDoc 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 data was null, check the parent
152
if( null == m_parent )
153         {
154             // There was no parent, and no data
155
final String JavaDoc message =
156                 "Unable to resolve context key: " + key;
157             throw new ContextException( message );
158         }
159
160         return m_parent.get( key );
161     }
162
163     /**
164      * Helper method fo adding items to Context.
165      *
166      * @param key the items key
167      * @param value the item
168      * @throws IllegalStateException if context is read only
169      */

170     public void put( final Object JavaDoc key, final Object JavaDoc value )
171         throws IllegalStateException JavaDoc
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     /**
185      * Hides the item in the context.
186      * After remove(key) has been called, a get(key)
187      * will always fail, even if the parent context
188      * has such a mapping.
189      *
190      * @param key the items key
191      * @throws IllegalStateException if context is read only
192      */

193     public void hide( final Object JavaDoc key )
194         throws IllegalStateException JavaDoc
195     {
196         checkWriteable();
197         m_contextData.put( key, HIDDEN_MAKER );
198     }
199
200     /**
201      * Utility method to retrieve context data.
202      *
203      * @return the context data
204      */

205     protected final Map JavaDoc getContextData()
206     {
207         return m_contextData;
208     }
209
210     /**
211      * Get parent context if any.
212      *
213      * @return the parent Context (may be null)
214      */

215     protected final Context getParent()
216     {
217         return m_parent;
218     }
219
220     /**
221      * Make the context read-only.
222      * Any attempt to write to the context via put()
223      * will result in an IllegalStateException.
224      */

225     public void makeReadOnly()
226     {
227         m_readOnly = true;
228     }
229
230     /**
231      * Utility method to check if context is writeable and if not throw exception.
232      *
233      * @throws IllegalStateException if context is read only
234      */

235     protected final void checkWriteable()
236         throws IllegalStateException JavaDoc
237     {
238         if( m_readOnly )
239         {
240             final String JavaDoc message =
241                 "Context is read only and can not be modified";
242             throw new IllegalStateException JavaDoc( message );
243         }
244     }
245 }
246
Popular Tags