KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > core > CachedContent


1 package org.apache.slide.projector.processor.core;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import org.apache.slide.projector.Context;
8 import org.apache.slide.projector.HttpContext;
9 import org.apache.slide.projector.Processor;
10 import org.apache.slide.projector.Projector;
11 import org.apache.slide.projector.Result;
12 import org.apache.slide.projector.Store;
13 import org.apache.slide.projector.URI;
14 import org.apache.slide.projector.descriptor.ParameterDescriptor;
15 import org.apache.slide.projector.descriptor.ResultDescriptor;
16 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
17 import org.apache.slide.projector.descriptor.StateDescriptor;
18 import org.apache.slide.projector.descriptor.URIValueDescriptor;
19 import org.apache.slide.projector.engine.Job;
20 import org.apache.slide.projector.engine.ProcessorManager;
21 import org.apache.slide.projector.engine.Scheduler;
22 import org.apache.slide.projector.expression.EventExpression;
23 import org.apache.slide.projector.i18n.DefaultMessage;
24 import org.apache.slide.projector.i18n.ParameterMessage;
25 import org.apache.slide.projector.processor.SimpleProcessor;
26 import org.apache.slide.projector.value.InputStreamValue;
27 import org.apache.slide.projector.value.MultipleStreamableValue;
28 import org.apache.slide.projector.value.StreamableValue;
29 import org.apache.slide.projector.value.StringValue;
30 import org.apache.slide.projector.value.URIValue;
31 import org.apache.slide.projector.value.Value;
32
33 /**
34  * @version $Revision: 1.4 $
35  */

36
37 public class CachedContent implements Processor {
38     private static Logger JavaDoc logger = Logger.getLogger(CachedContent.class.getName());
39
40     public final static String JavaDoc URL = "url";
41
42     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
43         URI uri = (URI)parameter.get(SimpleProcessor.INPUT);
44         String JavaDoc url = "";
45         if ( context instanceof HttpContext ) {
46             url = ((HttpContext)context).getContextPath()+"/"+ProcessorManager.getInstance().getURI(this)+"?input="+uri;
47         }
48         // check for cached result
49
Map JavaDoc resultEntries = new HashMap JavaDoc();
50         Value output = (Value)context.getStore(Store.CACHE).get(uri.toString());
51         if ( output == null ) {
52             output = Projector.getRepository().getResource(uri, context.getCredentials());
53             if ( output instanceof InputStreamValue ) {
54                 output = new MultipleStreamableValue((StreamableValue)output);
55             }
56             // cache result...
57
context.getStore(Store.CACHE).put(uri.toString(), output);
58             // ...and add dispose condition
59
Map JavaDoc jobParameter = new HashMap JavaDoc();
60             jobParameter.put(Dispose.STORE, new StringValue(Store.stores[Store.CACHE]));
61             jobParameter.put(Dispose.KEY, new StringValue(uri.toString()));
62             EventExpression expression = new EventExpression("Update");
63             expression.addProperty(EventExpression.DEPTH, "0");
64             expression.addProperty(EventExpression.URI, uri.toString());
65             Scheduler.getInstance().registerJob(new Job(context.getProcessId()+":"+context.getStep(), new URIValue("dispose"), expression, expression, jobParameter, false, false));
66             Scheduler.getInstance().saveJobs();
67         }
68         resultEntries.put(SimpleProcessor.OUTPUT, output);
69         // build url to activate this processor on top level
70
resultEntries.put(URL, new StringValue(url));
71         return new Result(StateDescriptor.OK, resultEntries);
72     }
73
74     public ParameterDescriptor[] getParameterDescriptors() {
75         return new ParameterDescriptor[]{ new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage("cachedContent/parameter/input"), new URIValueDescriptor()) }; }
76
77     public ResultDescriptor getResultDescriptor() {
78         return new ResultDescriptor(new StateDescriptor[]{ StateDescriptor.OK_DESCRIPTOR },
79                 new ResultEntryDescriptor[] {
80                     new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new DefaultMessage("cachedContent/result/output"), "*", true),
81                     new ResultEntryDescriptor(URL, new DefaultMessage("cachedContent/result/url"), "text/url", false),
82         }
83         );
84     }
85 }
86
Popular Tags