1 16 package org.apache.cocoon.components.xscript; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.component.Component; 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.parameters.ParameterException; 22 import org.apache.avalon.framework.parameters.Parameterizable; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.thread.ThreadSafe; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.context.ContextException; 30 31 import org.apache.cocoon.environment.Request; 32 import org.apache.cocoon.environment.ObjectModelHelper; 33 import org.apache.cocoon.environment.Context; 34 import org.apache.cocoon.environment.Session; 35 import org.apache.cocoon.Constants; 36 37 import java.util.Map ; 38 39 46 public class XScriptManagerImpl 47 extends AbstractLogEnabled 48 implements XScriptManager, Serviceable, Component, Parameterizable, Contextualizable, ThreadSafe, Disposable 49 { 50 public static final String CONTEXT = "org.apache.cocoon.components.xscript.scope"; 51 52 55 protected ServiceManager manager; 56 57 60 protected Context context; 61 62 63 public void contextualize(org.apache.avalon.framework.context.Context context) 64 throws ContextException 65 { 66 this.context = (Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 67 } 68 69 public void service(ServiceManager manager) throws ServiceException 70 { 71 this.manager = manager; 72 } 73 74 public void register(XScriptObject object) { 75 try { 76 object.service(manager); 77 } catch (ServiceException ignored) { } 78 } 79 80 public void parameterize(Parameters params) 81 throws ParameterException 82 { 83 String [] names = params.getNames(); 84 85 XScriptVariableScope s = new XScriptVariableScope(); 86 context.setAttribute(CONTEXT, s); 87 88 for (int i = 0; i < names.length; i++) { 89 String resourceString = params.getParameter(names[i]); 90 XScriptObject resource = new XScriptObjectFromURL(this, resourceString); 91 s.put(names[i], resource); 92 } 93 } 94 95 public void dispose() { 96 if (context != null) { 97 context.removeAttribute(CONTEXT); 98 } 99 } 100 101 private IllegalArgumentException 102 createAccessException(String msg, String name, int scope) 103 { 104 StringBuffer message = new StringBuffer ("Cannot ").append(msg) 105 .append(" variable named '").append(name) 106 .append("' in "); 107 108 if (scope == XScriptManager.GLOBAL_SCOPE) 109 message.append("global scope"); 110 else if (scope == XScriptManager.SESSION_SCOPE) 111 message.append("session scope"); 112 else if (scope == XScriptManager.PAGE_SCOPE) 113 message.append("page scope"); 114 else if (scope == XScriptManager.REQUEST_SCOPE) 115 message.append("request scope"); 116 else if (scope == XScriptManager.ALL_SCOPES) 117 message.append("any scope"); 118 else 119 message.append("unknown scope (").append(scope).append(")"); 120 121 return new IllegalArgumentException (message.toString()); 122 } 123 124 public XScriptObject get(XScriptVariableScope pageScope, 125 Map objectModel, 126 String name, 127 int scope) 128 throws IllegalArgumentException 129 { 130 XScriptObject o; 131 XScriptVariableScope s = null; 132 133 if (scope == XScriptManager.GLOBAL_SCOPE) { 134 s = (XScriptVariableScope) ObjectModelHelper.getContext(objectModel).getAttribute(CONTEXT); 135 } else if (scope == XScriptManager.SESSION_SCOPE) { 136 Request request = ObjectModelHelper.getRequest(objectModel); 137 s = (XScriptVariableScope) request.getSession().getAttribute(CONTEXT); 138 } else if (scope == XScriptManager.REQUEST_SCOPE) { 139 Request request = ObjectModelHelper.getRequest(objectModel); 140 s = (XScriptVariableScope) request.getAttribute(CONTEXT); 141 } else if (scope == XScriptManager.PAGE_SCOPE) { 142 s = pageScope; 143 } else if (scope == XScriptManager.ALL_SCOPES) { 144 Request request = ObjectModelHelper.getRequest(objectModel); 145 146 s = (XScriptVariableScope) request.getAttribute(CONTEXT); 148 if (s != null) { 149 o = s.get(name); 150 if (o != null) { 151 return o; 152 } 153 } 154 s = (XScriptVariableScope) request.getSession().getAttribute(CONTEXT); 156 if (s != null) { 157 o = s.get(name); 158 if (o != null) { 159 return o; 160 } 161 } 162 o = pageScope.get(name); 164 if (o != null) { 165 return o; 166 } 167 s = (XScriptVariableScope) ObjectModelHelper.getContext(objectModel).getAttribute(CONTEXT); 169 if (s != null) { 170 o = s.get(name); 171 if (o != null) { 172 return o; 173 } 174 } 175 s = null; 177 } 178 179 if (s != null) { 180 o = s.get(name); 181 if (o != null) { 182 return o; 183 } 184 } 185 186 throw createAccessException("find", name, scope); 187 } 188 189 public XScriptObject getFirst(XScriptVariableScope pageScope, 190 Map objectModel, 191 String name) 192 throws IllegalArgumentException 193 { 194 return get(pageScope, objectModel, name, ALL_SCOPES); 195 } 196 197 public void put(XScriptVariableScope pageScope, 198 Map objectModel, 199 String name, 200 XScriptObject value, 201 int scope) 202 { 203 XScriptVariableScope s; 204 205 if (scope == XScriptManager.GLOBAL_SCOPE) { 206 Context context = ObjectModelHelper.getContext(objectModel); 207 synchronized (context) { 208 s = (XScriptVariableScope) context.getAttribute(CONTEXT); 209 if (s == null) { 210 context.setAttribute(CONTEXT, s = new XScriptVariableScope()); 211 } 212 } 213 } else if (scope == XScriptManager.SESSION_SCOPE) { 214 Session session = ObjectModelHelper.getRequest(objectModel).getSession(); 215 synchronized (session) { 216 s = (XScriptVariableScope) session.getAttribute(CONTEXT); 217 if (s == null) { 218 session.setAttribute(CONTEXT, s = new XScriptVariableScope()); 219 } 220 } 221 } else if (scope == XScriptManager.REQUEST_SCOPE) { 222 Request request = ObjectModelHelper.getRequest(objectModel); 223 synchronized (request) { 224 s = (XScriptVariableScope) request.getAttribute(CONTEXT); 225 if (s == null) { 226 request.setAttribute(CONTEXT, s = new XScriptVariableScope()); 227 } 228 } 229 } else if (scope == XScriptManager.PAGE_SCOPE) { 230 s = pageScope; 231 } else { 232 throw createAccessException("create", name, scope); 233 } 234 235 s.put(name, value); 236 } 237 238 public XScriptObject remove(XScriptVariableScope pageScope, 239 Map objectModel, 240 String name, 241 int scope) 242 throws IllegalArgumentException 243 { 244 XScriptObject o; 245 XScriptVariableScope s = null; 246 247 if (scope == XScriptManager.GLOBAL_SCOPE) { 248 s = (XScriptVariableScope) ObjectModelHelper.getContext(objectModel).getAttribute(CONTEXT); 249 } else if (scope == XScriptManager.SESSION_SCOPE) { 250 Request request = ObjectModelHelper.getRequest(objectModel); 251 s = (XScriptVariableScope) request.getSession().getAttribute(CONTEXT); 252 } else if (scope == XScriptManager.REQUEST_SCOPE) { 253 Request request = ObjectModelHelper.getRequest(objectModel); 254 s = (XScriptVariableScope) request.getAttribute(CONTEXT); 255 } else if (scope == XScriptManager.PAGE_SCOPE) { 256 s = pageScope; 257 } else if (scope == XScriptManager.ALL_SCOPES) { 258 Request request = ObjectModelHelper.getRequest(objectModel); 259 260 s = (XScriptVariableScope) request.getAttribute(CONTEXT); 262 if (s != null) { 263 o = s.remove(name); 264 if (o != null) { 265 return o; 266 } 267 } 268 s = (XScriptVariableScope) request.getSession().getAttribute(CONTEXT); 270 if (s != null) { 271 o = s.remove(name); 272 if (o != null) { 273 return o; 274 } 275 } 276 o = pageScope.remove(name); 278 if (o != null) { 279 return o; 280 } 281 s = (XScriptVariableScope) ObjectModelHelper.getContext(objectModel).getAttribute(CONTEXT); 283 if (s != null) { 284 o = s.remove(name); 285 if (o != null) { 286 return o; 287 } 288 } 289 s = null; 291 } 292 293 if (s != null) { 294 o = s.remove(name); 295 if (o != null) { 296 return o; 297 } 298 } 299 300 throw createAccessException("remove", name, scope); 301 } 302 303 public XScriptObject removeFirst(XScriptVariableScope pageScope, Map objectModel, 304 String name) 305 throws IllegalArgumentException 306 { 307 return remove(pageScope, objectModel, name, ALL_SCOPES); 308 } 309 } 310 | Popular Tags |