KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > component > AbstractComponent


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.component;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.riotfamily.common.markup.TagWriter;
42 import org.riotfamily.components.Component;
43 import org.riotfamily.components.ComponentVersion;
44 import org.riotfamily.components.PropertyProcessor;
45 import org.springframework.util.Assert;
46
47 /**
48  * Abstract base class for component implementations.
49  */

50 public abstract class AbstractComponent implements Component {
51
52     public static final String JavaDoc CONTAINER = AbstractComponent.class.getName()
53             + ".container";
54
55     public static final String JavaDoc COMPONENT_ID = "componentId";
56
57     public static final String JavaDoc THIS = "this";
58
59     public static final String JavaDoc PARENT_ID = "parentId";
60
61     public static final String JavaDoc POSITION_CLASS = "positionClass";
62
63     protected Log log = LogFactory.getLog(AbstractComponent.class);
64
65     private List JavaDoc propertyProcessors = new ArrayList JavaDoc();
66
67     public void setPropertyProcessors(List JavaDoc propertyProcessors) {
68         Assert.notNull(propertyProcessors);
69         this.propertyProcessors = propertyProcessors;
70     }
71
72     public void addPropertyProcessor(PropertyProcessor propertyProcessor) {
73         propertyProcessors.add(propertyProcessor);
74     }
75
76     public List JavaDoc getPropertyProcessors() {
77         return propertyProcessors;
78     }
79
80     public final void render(ComponentVersion componentVersion,
81             String JavaDoc positionClassName, HttpServletRequest JavaDoc request,
82             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
83
84         try {
85             request.setAttribute(CONTAINER, componentVersion.getContainer());
86             renderInternal(componentVersion, positionClassName, request, response);
87         }
88         catch (Exception JavaDoc e) {
89             log.error("Error rendering component", e);
90
91             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
92             StringWriter JavaDoc strace = new StringWriter JavaDoc();
93             e.printStackTrace(new PrintWriter JavaDoc(strace));
94
95             TagWriter pre = new TagWriter(response.getWriter());
96             pre.start("pre")
97                     .attribute("class", "riot-stacktrace")
98                     .body(strace.toString());
99
100             pre.end();
101         }
102     }
103
104     public boolean isDynamic() {
105         return false;
106     }
107
108     public Map JavaDoc buildModel(ComponentVersion componentVersion) {
109         Map JavaDoc model = new HashMap JavaDoc();
110         model.putAll(componentVersion.getProperties());
111         Iterator JavaDoc it = propertyProcessors.iterator();
112         while (it.hasNext()) {
113             PropertyProcessor pp = (PropertyProcessor) it.next();
114             pp.resolveStrings(model);
115         }
116         return model;
117     }
118
119     public void updateProperties(ComponentVersion version, Map JavaDoc model) {
120         Iterator JavaDoc it = propertyProcessors.iterator();
121         while (it.hasNext()) {
122             PropertyProcessor pp = (PropertyProcessor) it.next();
123             pp.convertToStrings(model);
124         }
125
126         Map JavaDoc props = version.getProperties();
127
128         it = model.entrySet().iterator();
129         while (it.hasNext()) {
130             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
131             Assert.isInstanceOf(String JavaDoc.class, entry.getKey(),
132                     "Map must only contain String keys.");
133
134             if (entry.getValue() == null) {
135                 props.remove(entry.getKey());
136             }
137             else {
138                 Assert.isInstanceOf(String JavaDoc.class, entry.getValue(),
139                         "Map must only contain String values.");
140
141                 props.put(entry.getKey(), entry.getValue());
142             }
143         }
144         version.setDirty(true);
145     }
146
147     protected abstract void renderInternal(ComponentVersion componentVersion,
148             String JavaDoc positionClassName, HttpServletRequest JavaDoc request,
149             HttpServletResponse JavaDoc response) throws Exception JavaDoc;
150
151     public Collection JavaDoc getCacheTags(ComponentVersion version) {
152         ArrayList JavaDoc result = new ArrayList JavaDoc();
153         Iterator JavaDoc it = propertyProcessors.iterator();
154         while (it.hasNext()) {
155             PropertyProcessor pp = (PropertyProcessor) it.next();
156             String JavaDoc[] tags = pp.getCacheTags(version.getProperties());
157             for (int i = 0; tags != null && i < tags.length; i++) {
158                 result.add(tags[i]);
159             }
160         }
161         return result;
162     }
163
164 }
165
Popular Tags