KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > ProcessorModule


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module;
11
12 import java.util.*;
13 import javax.servlet.http.*;
14 import org.mmbase.module.core.*;
15 import org.mmbase.bridge.Cloud;
16 import org.mmbase.util.*;
17 import org.mmbase.util.functions.*;
18 import org.mmbase.util.logging.*;
19
20
21 /**
22  * The Processor Module extends the basic module to the Processor
23  * interface so it can perform for servscan (pagelets).
24  *
25  * @author Daniel Ockeloen
26  * @todo Should be abstract, deprecated?
27  */

28 public class ProcessorModule extends Module implements ProcessorInterface {
29     private static final Logger log = Logging.getLoggerInstance(ProcessorModule.class);
30     /**
31      * {@inheritDoc}
32      **/

33     public MMObjectBuilder getListBuilder(String JavaDoc command, Map params) {
34         return new VirtualBuilder(null);
35     }
36
37     protected static final Parameter[] PARAMS_PAGEINFO = new Parameter[] {Parameter.REQUEST, Parameter.RESPONSE, Parameter.CLOUD};
38     protected static final Parameter.Wrapper PARAM_PAGEINFO = new Parameter.Wrapper(PARAMS_PAGEINFO);
39
40
41     /**
42      * Used by function wrappers.
43      * @since MMBase-1.8
44      */

45     private static PageInfo getPageInfo(Parameters arguments) {
46         PageInfo pageInfo = null;
47         if (arguments.indexOfParameter(Parameter.REQUEST)> -1) {
48             HttpServletRequest req = (HttpServletRequest) arguments.get(Parameter.REQUEST);
49             HttpServletResponse res = (HttpServletResponse) arguments.get(Parameter.RESPONSE);
50             Cloud cloud = (Cloud) arguments.get(Parameter.CLOUD);
51             pageInfo = new PageInfo(req, res, cloud);
52         }
53         return pageInfo;
54     }
55     /**
56      * Used by function wrappers.
57      * @since MMBase-1.8
58      */

59     private static String JavaDoc getCommand(String JavaDoc functionName, Parameters arguments) {
60         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(functionName);
61         Iterator i = arguments.iterator();
62         while (i.hasNext()) {
63             Object JavaDoc argument = i.next();
64             if (argument instanceof String JavaDoc && ! "".equals(argument)) {
65                 buf.append('-').append(argument);
66             }
67         }
68         return buf.toString();
69     }
70     /**
71      * Function implementation around {@link #getNodeList(Object, String, Map)}. See in MMAdmin for an example on how to use.
72      * @since MMBase-1.8
73      */

74     protected class GetNodeListFunction extends AbstractFunction {
75         public GetNodeListFunction(String JavaDoc name, Parameter[] params) {
76             super(name, params, ReturnType.NODELIST);
77         }
78         public Object JavaDoc getFunctionValue(Parameters arguments) {
79             return getNodeList(getPageInfo(arguments), getCommand(getName(), arguments), arguments.toMap());
80         }
81     }
82     /**
83      * Function implementation around {@link #replace(PageInfo, String)}. See in MMAdmin for an example on how to use.
84      * @since MMBase-1.8
85      */

86     protected class ReplaceFunction extends AbstractFunction {
87         public ReplaceFunction(String JavaDoc name, Parameter[] params) {
88             super(name, params, ReturnType.STRING);
89         }
90         public Object JavaDoc getFunctionValue(Parameters arguments) {
91             return replace(getPageInfo(arguments), getCommand(getName(), arguments));
92         }
93     }
94     /**
95      * Function implementation around {@link #process(PageInfo, Hashtable, Hashtable)}. See in
96      * MMAdmin for an example on how to use. It does not support multipible commands, so the first
97      * Hashtable always contains precisely one entry. The value of the entry is the value of the
98      * first string parameter or the empty string. All parameters are added to the second Hashtable
99      * parameter ('vars'), and this is also returned (because sometimes also results are put in it).
100      * @since MMBase-1.8
101      */

102     protected class ProcessFunction extends AbstractFunction {
103         public ProcessFunction(String JavaDoc name, Parameter[] params) {
104             super(name, params, ReturnType.MAP);
105         }
106
107         public Object JavaDoc getFunctionValue(Parameters arguments) {
108             Hashtable cmds = new Hashtable();
109             Hashtable vars = new Hashtable();
110             Parameter[] def = arguments.getDefinition();
111             for (int i = 0; i < def.length; i++) {
112                 Parameter param = def[i];
113                 Object JavaDoc value = arguments.get(param);
114                 if (String JavaDoc.class.isAssignableFrom(param.getTypeAsClass()) && cmds.size() == 0) {
115                     cmds.put(getName(), value);
116                 }
117                 vars.put(param.getName(), value);
118             }
119             if (cmds.size() == 0) cmds.put(getName(), "");
120             boolean ok = process(getPageInfo(arguments), cmds, vars);
121             return vars;
122         }
123     }
124
125     /**
126      * This method is a wrapper around {@link #getList(PageInfo, StringTagger, String)}
127      * @param context The PageInfo object. It beats me why it is Object and not PageInfo. I think it's silly.
128      * @param command The command to execute
129      * @param params Parameters, they will be added to the StringTagger.
130      **/

131     public Vector getNodeList(Object JavaDoc context, String JavaDoc command, Map params) {
132         StringTagger tagger=null;
133         if (params instanceof StringTagger) {
134             tagger = (StringTagger)params;
135         } else {
136             tagger = new StringTagger("");
137             if (params != null) {
138                 for (Iterator entries = params.entrySet().iterator(); entries.hasNext(); ) {
139                     Map.Entry entry = (Map.Entry) entries.next();
140                     String JavaDoc key=(String JavaDoc) entry.getKey();
141                     Object JavaDoc o = entry.getValue();
142                     if (o instanceof Vector) {
143                         tagger.setValues(key, (Vector)o);
144                     } else {
145                         tagger.setValue(key, "" + o);
146                     }
147                 }
148             }
149         }
150         PageInfo sp = null;
151         if (context instanceof PageInfo) {
152             sp = (PageInfo)context;
153         }
154         Vector v = getList(sp, tagger, command);
155         int items = 1;
156         try { items = Integer.parseInt(tagger.Value("ITEMS")); } catch (NumberFormatException JavaDoc e) {}
157         Vector fieldlist = tagger.Values("FIELDS");
158         Vector res = new Vector(v.size() / items);
159         MMObjectBuilder bul = getListBuilder(command, params);
160         for(int i= 0; i < v.size(); i+=items) {
161             VirtualNode node = new VirtualNode(bul);
162             for(int j= 0; (j<items) && (j<v.size()); j++) {
163                 if ((fieldlist!=null) && (j<fieldlist.size())) {
164                     node.setValue((String JavaDoc)fieldlist.get(j),v.get(i+j));
165                 } else {
166                     node.setValue("item"+(j+1),v.get(i+j));
167                 }
168             }
169             res.add(node);
170         }
171         return res;
172     }
173
174     /**
175      * @javadoc
176      **/

177     public Vector getList(PageInfo sp,StringTagger params, String JavaDoc command) {
178         throw new UnsupportedOperationException JavaDoc("Module " + this.getClass().getName() + " does not implement LIST");
179     }
180
181     /**
182      * {@inheritDoc}
183      */

184     public boolean process(PageInfo sp, Hashtable cmds, Hashtable vars) {
185         return false;
186     }
187
188     /**
189      * {@inheritDoc}
190      **/

191     public String JavaDoc replace (PageInfo sp, String JavaDoc command) {
192         return "This module doesn't implement this processor call";
193     }
194
195     /**
196      * {@inheritDoc}
197      * who the hell uses this (daniel)
198      **/

199     public String JavaDoc replace (PageInfo sp, StringTagger command) {
200         return "This module doesn't implement this processor call";
201     }
202
203     /**
204      * {@inheritDoc}
205      */

206     public boolean cacheCheck(PageInfo sp, String JavaDoc cmd) {
207         return false;
208     }
209
210
211     /**
212      * What should this do, when is this called?
213      * @deprecated called by nothing
214      * @javadoc
215      */

216
217      public void reload() {
218      }
219
220
221     /**
222      * What should this do, when is this called?
223      * @deprecated called by nothing
224      * @javadoc
225      */

226     public void unload() {
227     }
228
229     /**
230      * {@inheritDoc}
231      * @scope abstract
232      */

233     public void init() {
234     }
235
236     /**
237      * {@inheritDoc}
238      * @scope abstract
239      */

240     public void onload() {
241     }
242
243
244 }
245
Popular Tags