KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > process > ResultConfiguration


1 package org.apache.slide.projector.processor.process;
2
3 import java.io.IOException JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import org.apache.slide.projector.ContentType;
10 import org.apache.slide.projector.Context;
11 import org.apache.slide.projector.HttpContext;
12 import org.apache.slide.projector.ProcessException;
13 import org.apache.slide.projector.Result;
14 import org.apache.slide.projector.Store;
15 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
16 import org.apache.slide.projector.i18n.ErrorMessage;
17 import org.apache.slide.projector.util.StoreHelper;
18 import org.apache.slide.projector.value.MapValue;
19 import org.apache.slide.projector.value.StreamableValue;
20 import org.apache.slide.projector.value.Value;
21
22 class ResultConfiguration {
23     private static Logger JavaDoc logger = Logger.getLogger(ResultConfiguration.class.getName());
24
25     private int store = Store.NONE;
26     private String JavaDoc name, key, domain;
27     private boolean presentable;
28     private long timeout = -1;
29
30     public ResultConfiguration(String JavaDoc name, String JavaDoc store, String JavaDoc domain, String JavaDoc key, boolean presentable, long timeout) {
31         this(name, StoreHelper.getStoreByName(store), domain, key, presentable, timeout);
32     }
33
34     public ResultConfiguration(String JavaDoc name, int store, String JavaDoc domain, String JavaDoc key, boolean presentable, long timeout) {
35         this.name = name;
36         this.store = store;
37         this.domain = domain;
38         this.key = key;
39         this.presentable = presentable;
40         this.timeout = timeout;
41     }
42
43     public int getStore() {
44         return store;
45     }
46
47     public String JavaDoc getDomain() {
48         return domain;
49     }
50     
51     public String JavaDoc getKey() {
52         return key;
53     }
54
55     public boolean isPresentable() {
56         return presentable;
57     }
58
59     public String JavaDoc getName() {
60         return name;
61     }
62
63     protected void storeValue(Value value, Store stepStore, Result result, ResultEntryDescriptor[] resultEntryDescriptors, Context context) throws ProcessException {
64         if ( value == null ) {
65             throw new ProcessException(new ErrorMessage("valueToStoreNotFound"));
66         }
67         if (presentable ) {
68             if ( value instanceof StreamableValue ) {
69                 if ( context instanceof HttpContext ) {
70                     ((HttpContext)context).setPresentableResource((StreamableValue)value);
71                 } else {
72                     logger.log(Level.FINE, "Result can only be shown in http-context!");
73                 }
74             } else {
75                 throw new ProcessException(new ErrorMessage("unpresentableValue"));
76             }
77         }
78         Store resultStore = stepStore;
79         if ( store == Store.OUTPUT ) {
80             // check if result is defined
81
boolean resultDefined = false;
82             for ( int i = 0; i < resultEntryDescriptors.length; i++ ) {
83                 if ( resultEntryDescriptors[i].getName().equals(key) ) {
84                     // check if result matches description
85
if ( ContentType.matches(resultEntryDescriptors[i].getContentType(), value.getContentType()) ) {
86                         result.addResultEntry(key, value);
87                         resultDefined = true;
88                         break;
89                     } else {
90                         throw new ProcessException(new ErrorMessage("contentTypeMismatch", new String JavaDoc[] { key, value.getContentType(), resultEntryDescriptors[i].getContentType()}));
91                     }
92                 }
93             }
94             if ( !resultDefined ) {
95                 throw new ProcessException(new ErrorMessage("undefinedResultKey", new String JavaDoc[] { key }));
96             }
97         } else {
98             if ( store != Store.NONE ) {
99                 resultStore = context.getStore(store);
100             }
101             if ( resultStore == null ) {
102                 throw new ProcessException(new ErrorMessage("storeNotAvailable", new String JavaDoc[] { key, Store.stores[store] }));
103             } else {
104                 try {
105                     String JavaDoc evaluatedKey = Process.evaluateKey(key, context);
106                     if ( evaluatedKey != null ) {
107                         if ( domain != null ) {
108                             Map JavaDoc map;
109                             MapValue mapResource = (MapValue)resultStore.get(domain);
110                             if ( mapResource == null ) {
111                                 map = new HashMap JavaDoc();
112                                 mapResource = new MapValue(map);
113                             } else {
114                                 map = mapResource.getMap();
115                             }
116                             map.put(evaluatedKey, value);
117                             evaluatedKey = domain;
118                             value = mapResource;
119                         }
120                         if ( timeout != -1 ) {
121                             resultStore.put(evaluatedKey, value, timeout);
122                         } else {
123                             resultStore.put(evaluatedKey, value);
124                         }
125                     }
126                 } catch ( IOException JavaDoc e ) {
127                     throw new ProcessException(new ErrorMessage("storingResultFailed"), e);
128                 }
129             }
130         }
131     }
132 }
Popular Tags