View Javadoc

1   package org.apache.velocity.context;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.app.event.EventCartridge;
23  import org.apache.velocity.runtime.resource.Resource;
24  import org.apache.velocity.util.introspection.IntrospectionCacheData;
25  
26  /**
27   *  This adapter class is the container for all context types for internal
28   *  use.  The AST now uses this class rather than the app-level Context
29   *  interface to allow flexibility in the future.
30   *
31   *  Currently, we have two context interfaces which must be supported :
32   *  <ul>
33   *  <li> Context : used for application/template data access
34   *  <li> InternalHousekeepingContext : used for internal housekeeping and caching
35   *  <li> InternalWrapperContext : used for getting root cache context and other
36   *       such.
37   *  <li> InternalEventContext : for event handling.
38   *  </ul>
39   *
40   *  This class implements the two interfaces to ensure that all methods are
41   *  supported.  When adding to the interfaces, or adding more context
42   *  functionality, the interface is the primary definition, so alter that first
43   *  and then all classes as necessary.  As of this writing, this would be
44   *  the only class affected by changes to InternalContext
45   *
46   *  This class ensures that an InternalContextBase is available for internal
47   *  use.  If an application constructs their own Context-implementing
48   *  object w/o subclassing AbstractContext, it may be that support for
49   *  InternalContext is not available.  Therefore, InternalContextAdapter will
50   *  create an InternalContextBase if necessary for this support.  Note that
51   *  if this is necessary, internal information such as node-cache data will be
52   *  lost from use to use of the context.  This may or may not be important,
53   *  depending upon application.
54   *
55   *
56   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
57   * @version $Id: InternalContextAdapterImpl.java 471908 2006-11-06 22:39:28Z henning $
58   */
59  public final class InternalContextAdapterImpl implements InternalContextAdapter
60  {
61      /**
62       *  the user data Context that we are wrapping
63       */
64      Context context = null;
65  
66      /**
67       *  the ICB we are wrapping.  We may need to make one
68       *  if the user data context implementation doesn't
69       *  support one.  The default AbstractContext-derived
70       *  VelocityContext does, and it's recommended that
71       *  people derive new contexts from AbstractContext
72       *  rather than piecing things together
73       */
74      InternalHousekeepingContext icb = null;
75  
76      /**
77       *  The InternalEventContext that we are wrapping.  If
78       *  the context passed to us doesn't support it, no
79       *  biggie.  We don't make it for them - since its a
80       *  user context thing, nothing gained by making one
81       *  for them now
82       */
83      InternalEventContext iec = null;
84  
85      /**
86       *  CTOR takes a Context and wraps it, delegating all 'data' calls
87       *  to it.
88       *
89       *  For support of internal contexts, it will create an InternalContextBase
90       *  if need be.
91       * @param c
92       */
93      public InternalContextAdapterImpl( Context c )
94      {
95          context = c;
96  
97          if ( !( c instanceof InternalHousekeepingContext ))
98          {
99              icb = new InternalContextBase();
100         }
101         else
102         {
103             icb = (InternalHousekeepingContext) context;
104         }
105 
106         if ( c instanceof InternalEventContext)
107         {
108             iec = ( InternalEventContext) context;
109         }
110     }
111 
112     /* --- InternalHousekeepingContext interface methods --- */
113 
114     /**
115      * @see org.apache.velocity.context.InternalHousekeepingContext#pushCurrentTemplateName(java.lang.String)
116      */
117     public void pushCurrentTemplateName( String s )
118     {
119         icb.pushCurrentTemplateName( s );
120     }
121 
122     /**
123      * @see org.apache.velocity.context.InternalHousekeepingContext#popCurrentTemplateName()
124      */
125     public void popCurrentTemplateName()
126     {
127         icb.popCurrentTemplateName();
128     }
129 
130     /**
131      * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentTemplateName()
132      */
133     public String getCurrentTemplateName()
134     {
135         return icb.getCurrentTemplateName();
136     }
137 
138     /**
139      * @see org.apache.velocity.context.InternalHousekeepingContext#getTemplateNameStack()
140      */
141     public Object[] getTemplateNameStack()
142     {
143         return icb.getTemplateNameStack();
144     }
145 
146     /**
147      * @see org.apache.velocity.context.InternalHousekeepingContext#icacheGet(java.lang.Object)
148      */
149     public IntrospectionCacheData icacheGet( Object key )
150     {
151         return icb.icacheGet( key );
152     }
153 
154     /**
155      * @see org.apache.velocity.context.InternalHousekeepingContext#icachePut(java.lang.Object, org.apache.velocity.util.introspection.IntrospectionCacheData)
156      */
157     public void icachePut( Object key, IntrospectionCacheData o )
158     {
159         icb.icachePut( key, o );
160     }
161 
162    /**
163     * @see org.apache.velocity.context.InternalHousekeepingContext#setCurrentResource(org.apache.velocity.runtime.resource.Resource)
164     */
165     public void setCurrentResource( Resource r )
166     {
167         icb.setCurrentResource(r);
168     }
169 
170     /**
171      * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentResource()
172      */
173     public Resource getCurrentResource()
174     {
175         return icb.getCurrentResource();
176     }
177 
178 
179     /**
180      * @see org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
181      */
182     public boolean getAllowRendering()
183     {
184        return icb.getAllowRendering();
185     }
186 
187     /**
188      * @see org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
189      */
190     public void setAllowRendering(boolean v)
191     {
192         icb.setAllowRendering(v);
193     }
194 
195 
196     /* ---  Context interface methods --- */
197 
198     /**
199      * @see org.apache.velocity.context.Context#put(java.lang.String, java.lang.Object)
200      */
201     public Object put(String key, Object value)
202     {
203         return context.put( key , value );
204     }
205     
206     /**
207      * @see InternalWrapperContext#localPut(String, Object)
208      */
209     public Object localPut(final String key, final Object value)
210     {
211 	return put(key, value);
212     }
213 
214     /**
215      * @see org.apache.velocity.context.Context#get(java.lang.String)
216      */
217     public Object get(String key)
218     {
219         return context.get( key );
220     }
221 
222     /**
223      * @see org.apache.velocity.context.Context#containsKey(java.lang.Object)
224      */
225     public boolean containsKey(Object key)
226     {
227         return context.containsKey( key );
228     }
229 
230     /**
231      * @see org.apache.velocity.context.Context#getKeys()
232      */
233     public Object[] getKeys()
234     {
235         return context.getKeys();
236     }
237 
238     /**
239      * @see org.apache.velocity.context.Context#remove(java.lang.Object)
240      */
241     public Object remove(Object key)
242     {
243         return context.remove( key );
244     }
245 
246 
247     /* ---- InternalWrapperContext --- */
248 
249     /**
250      *  returns the user data context that
251      *  we are wrapping
252      * @return The internal user data context.
253      */
254     public Context getInternalUserContext()
255     {
256         return context;
257     }
258 
259     /**
260      *  Returns the base context that we are
261      *  wrapping. Here, its this, but for other thing
262      *  like VM related context contortions, it can
263      *  be something else
264      * @return The base context.
265      */
266     public InternalContextAdapter getBaseContext()
267     {
268         return this;
269     }
270 
271     /* -----  InternalEventContext ---- */
272 
273     /**
274      * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
275      */
276     public EventCartridge attachEventCartridge( EventCartridge ec )
277     {
278         if (iec != null)
279         {
280             return iec.attachEventCartridge( ec );
281         }
282 
283         return null;
284     }
285 
286     /**
287      * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
288      */
289     public EventCartridge getEventCartridge()
290     {
291         if ( iec != null)
292         {
293             return iec.getEventCartridge( );
294         }
295 
296         return null;
297     }
298 }
299 
300