KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > ScriptAction


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.acting;
17
18 import org.apache.bsf.BSFManager;
19 import org.apache.bsf.util.IOUtils;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.Redirector;
26 import org.apache.cocoon.environment.SourceResolver;
27 import org.apache.excalibur.source.Source;
28
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * A simple action that executes any script that can be run by the BSF
37  *
38  * @author <a HREF="mailto:jafoster@uwaterloo.ca">Jason Foster</a>
39  * @version CVS $Id: ScriptAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

41
42 public class ScriptAction
43 extends ServiceableAction
44 implements ThreadSafe {
45
46
47     public Map JavaDoc act( Redirector redirector,
48                     SourceResolver resolver,
49                     Map JavaDoc objectModel,
50                     String JavaDoc source,
51                     Parameters par )
52     throws Exception JavaDoc {
53         Source src = null;
54         try {
55             // Figure out what script to open. A missing script name is caught
56
// by the resolver/SystemId grouping later on and causes an exception
57
String JavaDoc scriptName = source;
58
59             // Locate the appropriate file on the filesytem
60
src = resolver.resolveURI(scriptName);
61             String JavaDoc systemID = src.getURI();
62
63             if (this.getLogger().isDebugEnabled()) {
64                 getLogger().debug("script source [" + scriptName + "]");
65                 getLogger().debug("script resolved to [" + systemID + "]");
66             }
67
68             Reader JavaDoc in = new InputStreamReader JavaDoc(src.getInputStream());
69
70             // Set up the BSF manager and register relevant helper "beans"
71

72             BSFManager mgr = new BSFManager();
73             HashMap JavaDoc actionMap = new HashMap JavaDoc();
74
75             // parameters to act(...)
76
mgr.registerBean("resolver", resolver);
77             mgr.registerBean("objectModel", objectModel);
78             mgr.registerBean("parameters", par);
79
80             // ScriptAction housekeeping
81
mgr.registerBean("actionMap", actionMap);
82
83             // helpers
84

85             mgr.registerBean("logger", getLogger());
86             mgr.registerBean("request", ( ObjectModelHelper.getRequest(objectModel) ) );
87             mgr.registerBean("scriptaction", this );
88             mgr.registerBean("manager", this.manager );
89
90             if (this.getLogger().isDebugEnabled()) {
91                 getLogger().debug("BSFManager execution begining");
92             }
93
94             // Execute the script
95

96             mgr.exec(BSFManager.getLangFromFilename(systemID), systemID, 0, 0,
97                     IOUtils.getStringFromReader(in));
98
99             if (this.getLogger().isDebugEnabled()) {
100                 getLogger().debug("BSFManager execution complete");
101             }
102
103             // Figure out what to return
104
// TODO: decide on a more robust communication method
105

106             if ( actionMap.containsKey( "scriptaction-continue" )) {
107                 return ( Collections.unmodifiableMap(actionMap) );
108             } else {
109                 return (null);
110             }
111         } catch (Exception JavaDoc e) {
112             throw new ProcessingException(
113                     "Exception in ScriptAction.act()", e);
114         } finally {
115             resolver.release( src );
116         } // try/catch
117
} // public Map act(...)
118
} // public class ScriptAction
119
Popular Tags