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.acting;
018:
019: import org.apache.bsf.BSFManager;
020: import org.apache.bsf.util.IOUtils;
021:
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.avalon.framework.thread.ThreadSafe;
024: import org.apache.cocoon.ProcessingException;
025: import org.apache.cocoon.environment.ObjectModelHelper;
026: import org.apache.cocoon.environment.Redirector;
027: import org.apache.cocoon.environment.SourceResolver;
028: import org.apache.excalibur.source.Source;
029:
030: import java.io.InputStreamReader;
031: import java.io.Reader;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * A simple action that executes any script that can be run by the BSF
038: *
039: * @author <a href="mailto:jafoster@uwaterloo.ca">Jason Foster</a>
040: * @version CVS $Id: ScriptAction.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042:
043: public class ScriptAction extends ServiceableAction implements
044: ThreadSafe {
045:
046: public Map act(Redirector redirector, SourceResolver resolver,
047: Map objectModel, String source, Parameters par)
048: throws Exception {
049: Source src = null;
050: try {
051: // Figure out what script to open. A missing script name is caught
052: // by the resolver/SystemId grouping later on and causes an exception
053: String scriptName = source;
054:
055: // Locate the appropriate file on the filesytem
056: src = resolver.resolveURI(scriptName);
057: String systemID = src.getURI();
058:
059: if (this .getLogger().isDebugEnabled()) {
060: getLogger().debug("script source [" + scriptName + "]");
061: getLogger().debug(
062: "script resolved to [" + systemID + "]");
063: }
064:
065: Reader in = new InputStreamReader(src.getInputStream());
066:
067: // Set up the BSF manager and register relevant helper "beans"
068:
069: BSFManager mgr = new BSFManager();
070: HashMap actionMap = new HashMap();
071:
072: // parameters to act(...)
073: mgr.registerBean("resolver", resolver);
074: mgr.registerBean("objectModel", objectModel);
075: mgr.registerBean("parameters", par);
076:
077: // ScriptAction housekeeping
078: mgr.registerBean("actionMap", actionMap);
079:
080: // helpers
081:
082: mgr.registerBean("logger", getLogger());
083: mgr.registerBean("request", (ObjectModelHelper
084: .getRequest(objectModel)));
085: mgr.registerBean("scriptaction", this );
086: mgr.registerBean("manager", this .manager);
087:
088: if (this .getLogger().isDebugEnabled()) {
089: getLogger().debug("BSFManager execution begining");
090: }
091:
092: // Execute the script
093:
094: mgr.exec(BSFManager.getLangFromFilename(systemID),
095: systemID, 0, 0, IOUtils.getStringFromReader(in));
096:
097: if (this .getLogger().isDebugEnabled()) {
098: getLogger().debug("BSFManager execution complete");
099: }
100:
101: // Figure out what to return
102: // TODO: decide on a more robust communication method
103:
104: if (actionMap.containsKey("scriptaction-continue")) {
105: return (Collections.unmodifiableMap(actionMap));
106: } else {
107: return (null);
108: }
109: } catch (Exception e) {
110: throw new ProcessingException(
111: "Exception in ScriptAction.act()", e);
112: } finally {
113: resolver.release(src);
114: } // try/catch
115: } // public Map act(...)
116: } // public class ScriptAction
|