| 1 | package org.apache.velocity.context; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2000,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 java.util.HashMap; |
| 20 | |
| 21 | import org.apache.velocity.runtime.RuntimeServices; |
| 22 | import org.apache.velocity.runtime.RuntimeConstants; |
| 23 | import org.apache.velocity.runtime.directive.VMProxyArg; |
| 24 | import org.apache.velocity.util.introspection.IntrospectionCacheData; |
| 25 | import org.apache.velocity.runtime.resource.Resource; |
| 26 | import org.apache.velocity.app.event.EventCartridge; |
| 27 | |
| 28 | /** |
| 29 | * This is a special, internal-use-only context implementation to be |
| 30 | * used for the new Velocimacro implementation. |
| 31 | * |
| 32 | * The main distinguishing feature is the management of the VMProxyArg objects |
| 33 | * in the put() and get() methods. |
| 34 | * |
| 35 | * Further, this context also supports the 'VM local context' mode, where |
| 36 | * any get() or put() of references that aren't args to the VM are considered |
| 37 | * local to the vm, protecting the global context. |
| 38 | * |
| 39 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 40 | * @version $Id: VMContext.java,v 1.9.10.1 2004/03/03 23:22:54 geirm Exp $ |
| 41 | */ |
| 42 | public class VMContext implements InternalContextAdapter |
| 43 | { |
| 44 | /** container for our VMProxy Objects */ |
| 45 | HashMap vmproxyhash = new HashMap(); |
| 46 | |
| 47 | /** container for any local or constant VMProxy items */ |
| 48 | HashMap localcontext = new HashMap(); |
| 49 | |
| 50 | /** the base context store. This is the 'global' context */ |
| 51 | InternalContextAdapter innerContext = null; |
| 52 | |
| 53 | /** context that we are wrapping */ |
| 54 | InternalContextAdapter wrappedContext = null; |
| 55 | |
| 56 | /** support for local context scope feature, where all references are local */ |
| 57 | private boolean localcontextscope = false; |
| 58 | |
| 59 | /** |
| 60 | * CTOR, wraps an ICA |
| 61 | */ |
| 62 | public VMContext( InternalContextAdapter inner, RuntimeServices rsvc ) |
| 63 | { |
| 64 | localcontextscope = rsvc.getBoolean( RuntimeConstants.VM_CONTEXT_LOCALSCOPE, false ); |
| 65 | |
| 66 | wrappedContext = inner; |
| 67 | innerContext = inner.getBaseContext(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * return the inner / user context |
| 72 | */ |
| 73 | public Context getInternalUserContext() |
| 74 | { |
| 75 | return innerContext.getInternalUserContext(); |
| 76 | } |
| 77 | |
| 78 | public InternalContextAdapter getBaseContext() |
| 79 | { |
| 80 | return innerContext.getBaseContext(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Used to put VMProxyArgs into this context. It separates |
| 85 | * the VMProxyArgs into constant and non-constant types |
| 86 | * pulling out the value of the constant types so they can |
| 87 | * be modified w/o damaging the VMProxyArg, and leaving the |
| 88 | * dynamic ones, as they modify context rather than their own |
| 89 | * state |
| 90 | * @param vmpa VMProxyArg to add |
| 91 | */ |
| 92 | public void addVMProxyArg( VMProxyArg vmpa ) |
| 93 | { |
| 94 | /* |
| 95 | * ask if it's a constant : if so, get the value and put into the |
| 96 | * local context, otherwise, put the vmpa in our vmproxyhash |
| 97 | */ |
| 98 | |
| 99 | String key = vmpa.getContextReference(); |
| 100 | |
| 101 | if ( vmpa.isConstant() ) |
| 102 | { |
| 103 | localcontext.put( key, vmpa.getObject( wrappedContext ) ); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | vmproxyhash.put( key, vmpa ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Impl of the Context.put() method. |
| 113 | * |
| 114 | * @param key name of item to set |
| 115 | * @param value object to set to key |
| 116 | * @return old stored object |
| 117 | */ |
| 118 | public Object put(String key, Object value) |
| 119 | { |
| 120 | /* |
| 121 | * first see if this is a vmpa |
| 122 | */ |
| 123 | |
| 124 | VMProxyArg vmpa = (VMProxyArg) vmproxyhash.get( key ); |
| 125 | |
| 126 | if( vmpa != null) |
| 127 | { |
| 128 | return vmpa.setObject( wrappedContext, value ); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | if(localcontextscope) |
| 133 | { |
| 134 | /* |
| 135 | * if we have localcontextscope mode, then just |
| 136 | * put in the local context |
| 137 | */ |
| 138 | |
| 139 | return localcontext.put( key, value ); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | /* |
| 144 | * ok, how about the local context? |
| 145 | */ |
| 146 | |
| 147 | if (localcontext.containsKey( key )) |
| 148 | { |
| 149 | return localcontext.put( key, value); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | /* |
| 154 | * otherwise, let them push it into the 'global' context |
| 155 | */ |
| 156 | |
| 157 | return innerContext.put( key, value ); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Impl of the Context.gut() method. |
| 165 | * |
| 166 | * @param key name of item to get |
| 167 | * @return stored object or null |
| 168 | */ |
| 169 | public Object get( String key ) |
| 170 | { |
| 171 | /* |
| 172 | * first, see if it's a VMPA |
| 173 | */ |
| 174 | |
| 175 | Object o = null; |
| 176 | |
| 177 | VMProxyArg vmpa = (VMProxyArg) vmproxyhash.get( key ); |
| 178 | |
| 179 | if( vmpa != null ) |
| 180 | { |
| 181 | o = vmpa.getObject( wrappedContext ); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | if(localcontextscope) |
| 186 | { |
| 187 | /* |
| 188 | * if we have localcontextscope mode, then just |
| 189 | * put in the local context |
| 190 | */ |
| 191 | |
| 192 | o = localcontext.get( key ); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | /* |
| 197 | * try the local context |
| 198 | */ |
| 199 | |
| 200 | o = localcontext.get( key ); |
| 201 | |
| 202 | if ( o == null) |
| 203 | { |
| 204 | /* |
| 205 | * last chance |
| 206 | */ |
| 207 | |
| 208 | o = innerContext.get( key ); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return o; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * not yet impl |
| 218 | */ |
| 219 | public boolean containsKey(Object key) |
| 220 | { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * impl badly |
| 226 | */ |
| 227 | public Object[] getKeys() |
| 228 | { |
| 229 | return vmproxyhash.keySet().toArray(); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * impl badly |
| 234 | */ |
| 235 | public Object remove(Object key) |
| 236 | { |
| 237 | return vmproxyhash.remove( key ); |
| 238 | } |
| 239 | |
| 240 | public void pushCurrentTemplateName( String s ) |
| 241 | { |
| 242 | innerContext.pushCurrentTemplateName( s ); |
| 243 | } |
| 244 | |
| 245 | public void popCurrentTemplateName() |
| 246 | { |
| 247 | innerContext.popCurrentTemplateName(); |
| 248 | } |
| 249 | |
| 250 | public String getCurrentTemplateName() |
| 251 | { |
| 252 | return innerContext.getCurrentTemplateName(); |
| 253 | } |
| 254 | |
| 255 | public Object[] getTemplateNameStack() |
| 256 | { |
| 257 | return innerContext.getTemplateNameStack(); |
| 258 | } |
| 259 | |
| 260 | public IntrospectionCacheData icacheGet( Object key ) |
| 261 | { |
| 262 | return innerContext.icacheGet( key ); |
| 263 | } |
| 264 | |
| 265 | public void icachePut( Object key, IntrospectionCacheData o ) |
| 266 | { |
| 267 | innerContext.icachePut( key, o ); |
| 268 | } |
| 269 | |
| 270 | public EventCartridge attachEventCartridge( EventCartridge ec ) |
| 271 | { |
| 272 | return innerContext.attachEventCartridge( ec ); |
| 273 | } |
| 274 | |
| 275 | public EventCartridge getEventCartridge() |
| 276 | { |
| 277 | return innerContext.getEventCartridge(); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | public void setCurrentResource( Resource r ) |
| 282 | { |
| 283 | innerContext.setCurrentResource( r ); |
| 284 | } |
| 285 | |
| 286 | public Resource getCurrentResource() |
| 287 | { |
| 288 | return innerContext.getCurrentResource(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |