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