001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.xscript;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.component.Component;
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.parameters.ParameterException;
023: import org.apache.avalon.framework.parameters.Parameterizable;
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.Serviceable;
028: import org.apache.avalon.framework.thread.ThreadSafe;
029: import org.apache.avalon.framework.context.Contextualizable;
030: import org.apache.avalon.framework.context.ContextException;
031:
032: import org.apache.cocoon.environment.Request;
033: import org.apache.cocoon.environment.ObjectModelHelper;
034: import org.apache.cocoon.environment.Context;
035: import org.apache.cocoon.environment.Session;
036: import org.apache.cocoon.Constants;
037:
038: import java.util.Map;
039:
040: /**
041: * The actual implementation of the <code>XScriptManager</code> interface.
042: *
043: * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
044: * @version CVS $Id: XScriptManagerImpl.java 433543 2006-08-22 06:22:54Z crossley $
045: * @since August 4, 2001
046: */
047: public class XScriptManagerImpl extends AbstractLogEnabled implements
048: XScriptManager, Serviceable, Component, Parameterizable,
049: Contextualizable, ThreadSafe, Disposable {
050: public static final String CONTEXT = "org.apache.cocoon.components.xscript.scope";
051:
052: /**
053: * The <code>ServiceManager</code> instance.
054: */
055: protected ServiceManager manager;
056:
057: /**
058: * The <code>Context</code> instance.
059: */
060: protected Context context;
061:
062: public void contextualize(
063: org.apache.avalon.framework.context.Context context)
064: throws ContextException {
065: this .context = (Context) context
066: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
067: }
068:
069: public void service(ServiceManager manager) throws ServiceException {
070: this .manager = manager;
071: }
072:
073: public void register(XScriptObject object) {
074: try {
075: object.service(manager);
076: } catch (ServiceException ignored) {
077: }
078: }
079:
080: public void parameterize(Parameters params)
081: throws ParameterException {
082: String[] names = params.getNames();
083:
084: XScriptVariableScope s = new XScriptVariableScope();
085: context.setAttribute(CONTEXT, s);
086:
087: for (int i = 0; i < names.length; i++) {
088: String resourceString = params.getParameter(names[i]);
089: XScriptObject resource = new XScriptObjectFromURL(this ,
090: resourceString);
091: s.put(names[i], resource);
092: }
093: }
094:
095: public void dispose() {
096: if (context != null) {
097: context.removeAttribute(CONTEXT);
098: }
099: }
100:
101: private IllegalArgumentException createAccessException(String msg,
102: String name, int scope) {
103: StringBuffer message = new StringBuffer("Cannot ").append(msg)
104: .append(" variable named '").append(name).append(
105: "' in ");
106:
107: if (scope == XScriptManager.GLOBAL_SCOPE)
108: message.append("global scope");
109: else if (scope == XScriptManager.SESSION_SCOPE)
110: message.append("session scope");
111: else if (scope == XScriptManager.PAGE_SCOPE)
112: message.append("page scope");
113: else if (scope == XScriptManager.REQUEST_SCOPE)
114: message.append("request scope");
115: else if (scope == XScriptManager.ALL_SCOPES)
116: message.append("any scope");
117: else
118: message.append("unknown scope (").append(scope).append(")");
119:
120: return new IllegalArgumentException(message.toString());
121: }
122:
123: public XScriptObject get(XScriptVariableScope pageScope,
124: Map objectModel, String name, int scope)
125: throws IllegalArgumentException {
126: XScriptObject o;
127: XScriptVariableScope s = null;
128:
129: if (scope == XScriptManager.GLOBAL_SCOPE) {
130: s = (XScriptVariableScope) ObjectModelHelper.getContext(
131: objectModel).getAttribute(CONTEXT);
132: } else if (scope == XScriptManager.SESSION_SCOPE) {
133: Request request = ObjectModelHelper.getRequest(objectModel);
134: s = (XScriptVariableScope) request.getSession()
135: .getAttribute(CONTEXT);
136: } else if (scope == XScriptManager.REQUEST_SCOPE) {
137: Request request = ObjectModelHelper.getRequest(objectModel);
138: s = (XScriptVariableScope) request.getAttribute(CONTEXT);
139: } else if (scope == XScriptManager.PAGE_SCOPE) {
140: s = pageScope;
141: } else if (scope == XScriptManager.ALL_SCOPES) {
142: Request request = ObjectModelHelper.getRequest(objectModel);
143:
144: // Lookup in the request scope first.
145: s = (XScriptVariableScope) request.getAttribute(CONTEXT);
146: if (s != null) {
147: o = s.get(name);
148: if (o != null) {
149: return o;
150: }
151: }
152: // No luck finding `name' in request scope, try in session scope.
153: s = (XScriptVariableScope) request.getSession()
154: .getAttribute(CONTEXT);
155: if (s != null) {
156: o = s.get(name);
157: if (o != null) {
158: return o;
159: }
160: }
161: // No luck finding `name' in session scope, try in page scope.
162: o = pageScope.get(name);
163: if (o != null) {
164: return o;
165: }
166: // No luck finding `name' in the page scope, try the global scope.
167: s = (XScriptVariableScope) ObjectModelHelper.getContext(
168: objectModel).getAttribute(CONTEXT);
169: if (s != null) {
170: o = s.get(name);
171: if (o != null) {
172: return o;
173: }
174: }
175: // Not found, throw exception
176: 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, String name)
191: throws IllegalArgumentException {
192: return get(pageScope, objectModel, name, ALL_SCOPES);
193: }
194:
195: public void put(XScriptVariableScope pageScope, Map objectModel,
196: String name, XScriptObject value, int scope) {
197: XScriptVariableScope s;
198:
199: if (scope == XScriptManager.GLOBAL_SCOPE) {
200: Context context = ObjectModelHelper.getContext(objectModel);
201: synchronized (context) {
202: s = (XScriptVariableScope) context
203: .getAttribute(CONTEXT);
204: if (s == null) {
205: context.setAttribute(CONTEXT,
206: s = new XScriptVariableScope());
207: }
208: }
209: } else if (scope == XScriptManager.SESSION_SCOPE) {
210: Session session = ObjectModelHelper.getRequest(objectModel)
211: .getSession();
212: synchronized (session) {
213: s = (XScriptVariableScope) session
214: .getAttribute(CONTEXT);
215: if (s == null) {
216: session.setAttribute(CONTEXT,
217: s = new XScriptVariableScope());
218: }
219: }
220: } else if (scope == XScriptManager.REQUEST_SCOPE) {
221: Request request = ObjectModelHelper.getRequest(objectModel);
222: synchronized (request) {
223: s = (XScriptVariableScope) request
224: .getAttribute(CONTEXT);
225: if (s == null) {
226: request.setAttribute(CONTEXT,
227: s = new XScriptVariableScope());
228: }
229: }
230: } else if (scope == XScriptManager.PAGE_SCOPE) {
231: s = pageScope;
232: } else {
233: throw createAccessException("create", name, scope);
234: }
235:
236: s.put(name, value);
237: }
238:
239: public XScriptObject remove(XScriptVariableScope pageScope,
240: Map objectModel, String name, int scope)
241: throws IllegalArgumentException {
242: XScriptObject o;
243: XScriptVariableScope s = null;
244:
245: if (scope == XScriptManager.GLOBAL_SCOPE) {
246: s = (XScriptVariableScope) ObjectModelHelper.getContext(
247: objectModel).getAttribute(CONTEXT);
248: } else if (scope == XScriptManager.SESSION_SCOPE) {
249: Request request = ObjectModelHelper.getRequest(objectModel);
250: s = (XScriptVariableScope) request.getSession()
251: .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: // Lookup in the request scope first.
261: s = (XScriptVariableScope) request.getAttribute(CONTEXT);
262: if (s != null) {
263: o = s.remove(name);
264: if (o != null) {
265: return o;
266: }
267: }
268: // No luck finding `name' in request scope, try in session scope.
269: s = (XScriptVariableScope) request.getSession()
270: .getAttribute(CONTEXT);
271: if (s != null) {
272: o = s.remove(name);
273: if (o != null) {
274: return o;
275: }
276: }
277: // No luck finding `name' in session scope, try in page scope.
278: o = pageScope.remove(name);
279: if (o != null) {
280: return o;
281: }
282: // No luck finding `name' in the page scope, try the global scope.
283: s = (XScriptVariableScope) ObjectModelHelper.getContext(
284: objectModel).getAttribute(CONTEXT);
285: if (s != null) {
286: o = s.remove(name);
287: if (o != null) {
288: return o;
289: }
290: }
291: // Not found, throw exception
292: s = null;
293: }
294:
295: if (s != null) {
296: o = s.remove(name);
297: if (o != null) {
298: return o;
299: }
300: }
301:
302: throw createAccessException("remove", name, scope);
303: }
304:
305: public XScriptObject removeFirst(XScriptVariableScope pageScope,
306: Map objectModel, String name)
307: throws IllegalArgumentException {
308: return remove(pageScope, objectModel, name, ALL_SCOPES);
309: }
310: }
|