KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xscript > XScriptManagerImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
38
39 /**
40  * The actual implementation of the <code>XScriptManager</code> interface.
41  *
42  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
43  * @version CVS $Id: XScriptManagerImpl.java 43553 2004-09-09 01:07:35Z vgritsenko $
44  * @since August 4, 2001
45  */

46 public class XScriptManagerImpl
47         extends AbstractLogEnabled
48         implements XScriptManager, Serviceable, Component, Parameterizable, Contextualizable, ThreadSafe, Disposable
49 {
50     public static final String JavaDoc CONTEXT = "org.apache.cocoon.components.xscript.scope";
51
52     /**
53      * The <code>ServiceManager</code> instance.
54      */

55     protected ServiceManager manager;
56
57     /**
58      * The <code>Context</code> instance.
59      */

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 JavaDoc[] 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 JavaDoc 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 JavaDoc
102             createAccessException(String JavaDoc msg, String JavaDoc name, int scope)
103     {
104         StringBuffer JavaDoc message = new StringBuffer JavaDoc("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 JavaDoc(message.toString());
122     }
123
124     public XScriptObject get(XScriptVariableScope pageScope,
125                              Map JavaDoc objectModel,
126                              String JavaDoc name,
127                              int scope)
128             throws IllegalArgumentException JavaDoc
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             // Lookup in the request scope first.
147
s = (XScriptVariableScope) request.getAttribute(CONTEXT);
148             if (s != null) {
149                 o = s.get(name);
150                 if (o != null) {
151                     return o;
152                 }
153             }
154             // No luck finding `name' in request scope, try in session scope.
155
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             // No luck finding `name' in session scope, try in page scope.
163
o = pageScope.get(name);
164             if (o != null) {
165                 return o;
166             }
167             // No luck finding `name' in the page scope, try the global scope.
168
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             // 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 JavaDoc objectModel,
191                                   String JavaDoc name)
192             throws IllegalArgumentException JavaDoc
193     {
194         return get(pageScope, objectModel, name, ALL_SCOPES);
195     }
196
197     public void put(XScriptVariableScope pageScope,
198                     Map JavaDoc objectModel,
199                     String JavaDoc 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 JavaDoc objectModel,
240                                 String JavaDoc name,
241                                 int scope)
242             throws IllegalArgumentException JavaDoc
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             // 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().getAttribute(CONTEXT);
270             if (s != null) {
271                 o = s.remove(name);
272                 if (o != null) {
273                     return o;
274                 }
275             }
276             // No luck finding `name' in session scope, try in page scope.
277
o = pageScope.remove(name);
278             if (o != null) {
279                 return o;
280             }
281             // No luck finding `name' in the page scope, try the global scope.
282
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             // Not found, throw exception
290
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 JavaDoc objectModel,
304                                      String JavaDoc name)
305             throws IllegalArgumentException JavaDoc
306     {
307         return remove(pageScope, objectModel, name, ALL_SCOPES);
308     }
309 }
310
Popular Tags