KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > component > AbstractComponent


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.component;
50
51 import java.io.IOException JavaDoc;
52 import java.io.StringWriter JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import com.anthonyeden.lib.config.Configuration;
57 import com.anthonyeden.lib.config.ConfigurationException;
58 import com.anthonyeden.lib.util.ClassUtilities;
59 import com.anthonyeden.lib.util.MessageUtilities;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
62 import org.jpublish.JPublishEngine;
63 import org.jpublish.RequestContext;
64 import org.jpublish.SiteContext;
65 import org.jpublish.repository.Repository;
66 import org.jpublish.view.ViewRenderException;
67 import org.jpublish.view.ViewRenderer;
68
69 /**
70  * Abstract base implementation of the JPublishComponent interface. Component implementations can extend this base class
71  * to reduce the amount of work necessary to develop components.
72  *
73  * @author Anthony Eden
74  */

75
76 public abstract class AbstractComponent implements Component {
77
78     private Log log = LogFactory.getLog(AbstractComponent.class);
79
80     protected String JavaDoc name = null;
81     protected String JavaDoc description = null;
82     protected Map JavaDoc properties = new HashMap JavaDoc();
83
84     protected Repository viewRepository = null;
85     protected ViewRenderer viewRenderer = null;
86
87     protected SiteContext siteContext = null;
88
89     /**
90      * Construct a new AbstractComponent.
91      */

92
93     protected AbstractComponent() {
94
95     }
96
97     /**
98      * Get the component's name.
99      *
100      * @return The name
101      */

102
103     public String JavaDoc getName() {
104         return name;
105     }
106
107     /**
108      * Get a description of the component.
109      *
110      * @return The component description
111      */

112
113     public String JavaDoc getDescription() {
114         return description;
115     }
116
117     /**
118      * Get a Map of component properties.
119      *
120      * @return A Map of properties
121      */

122
123     public Map JavaDoc getProperties() {
124         return properties;
125     }
126
127     /**
128      * Get the SiteContext.
129      *
130      * @return The SiteContext
131      */

132
133     public SiteContext getSiteContext() {
134         return siteContext;
135     }
136
137     /**
138      * Set the SiteContext.
139      *
140      * @param siteContext The SiteContext
141      */

142
143     public void setSiteContext(SiteContext siteContext) {
144         this.siteContext = siteContext;
145     }
146
147     /**
148      * Get the component's view repository.
149      *
150      * @return The view repository
151      */

152
153     public Repository getViewRepository() {
154         return viewRepository;
155     }
156
157     /**
158      * Set the component's view repository.
159      *
160      * @param viewRepository The new view repository
161      */

162
163     public void setViewRepository(Repository viewRepository) {
164         this.viewRepository = viewRepository;
165     }
166
167     /**
168      * Get the component's view renderer. This method may return null which indicates that the component should use the
169      * default view renderer specified in the SiteContext.
170      *
171      * @return The view renderer
172      */

173
174     public ViewRenderer getViewRenderer() {
175         return viewRenderer;
176     }
177
178     /**
179      * Set the component's view renderer. Set the value to null to indicate that the component should use the default
180      * view renderer specified in the SiteContext.
181      *
182      * @param viewRenderer The new view renderer
183      */

184
185     public void setViewRenderer(ViewRenderer viewRenderer) {
186         this.viewRenderer = viewRenderer;
187     }
188
189     /**
190      * Load the component's configuration data. Implementations should override this method if they require
191      * configuration. If an implementation does override this method the implementation should call
192      * <code>super.loadConfiguration(configuration)</code> first.
193      *
194      * @param configuration The configuration data
195      * @throws ConfigurationException
196      */

197
198     public void loadConfiguration(Configuration configuration)
199             throws ConfigurationException {
200         // Load the ViewRenderer for this component
201
Configuration viewRendererConfiguration =
202                 configuration.getChild("view-renderer");
203         if (viewRendererConfiguration != null) {
204             String JavaDoc viewRendererClass =
205                     viewRendererConfiguration.getAttribute("classname");
206
207             try {
208                 viewRenderer = (ViewRenderer) ClassUtilities.loadClass(viewRendererClass).newInstance();
209                 viewRenderer.setSiteContext(siteContext);
210             } catch (ClassNotFoundException JavaDoc e) {
211                 Object JavaDoc[] args = {viewRendererClass};
212                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
213                         JPublishEngine.MESSAGE_PACKAGE,
214                         "viewRendererClassNotFoundError", args);
215                 throw new ConfigurationException(msg,
216                         viewRendererConfiguration);
217             } catch (InstantiationException JavaDoc e) {
218                 Object JavaDoc[] args = {viewRendererClass};
219                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
220                         JPublishEngine.MESSAGE_PACKAGE,
221                         "viewRendererInstantiationError", args);
222                 throw new ConfigurationException(msg,
223                         viewRendererConfiguration);
224             } catch (IllegalAccessException JavaDoc e) {
225                 Object JavaDoc[] args = {viewRendererClass};
226                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
227                         JPublishEngine.MESSAGE_PACKAGE,
228                         "viewRendererIllegalAccessError", args);
229                 throw new ConfigurationException(msg,
230                         viewRendererConfiguration);
231             }
232             //} catch (Exception e) {
233
// throw new ConfigurationException(
234
// "Error loading view renderer: " + e.getMessage(), e);
235
//}
236
viewRenderer.loadConfiguration(viewRendererConfiguration);
237             try {
238                 viewRenderer.init();
239             } catch (Exception JavaDoc e) {
240                 Object JavaDoc[] args = {
241                     viewRenderer.getClass().getName(),
242                     e.getMessage()
243                 };
244                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
245                         JPublishEngine.MESSAGE_PACKAGE, "viewRendererInitError",
246                         args);
247                 throw new ConfigurationException(msg, e, configuration);
248             }
249         }
250
251         if (viewRenderer == null) {
252             viewRenderer = siteContext.getViewRenderer();
253         }
254         
255         // Select the repository to use
256
Configuration repositoryConfiguration =
257                 configuration.getChild("repository");
258         if (repositoryConfiguration != null) {
259             String JavaDoc name = repositoryConfiguration.getAttribute("name");
260             if (name == null) {
261                 Object JavaDoc[] args = {};
262                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
263                         JPublishEngine.MESSAGE_PACKAGE, "repositoryNameRequired",
264                         args);
265                 throw new ConfigurationException(msg, repositoryConfiguration);
266             } else {
267                 if (log.isDebugEnabled()) {
268                     log.debug("Using repository name: " + name);
269                 }
270                 viewRepository = siteContext.getRepository(name);
271             }
272         } else {
273             log.error("Repository configuration element not found");
274         }
275
276         if (viewRepository == null) {
277             Object JavaDoc[] args = {getName()};
278             String JavaDoc msg = MessageUtilities.getMessage(getClass(),
279                     JPublishEngine.MESSAGE_PACKAGE, "repositoryRequired", args);
280             throw new ConfigurationException(msg, configuration);
281         }
282     }
283
284     /**
285      * Internal method which can be used by subclasses to pass the view through the component's view renderer.
286      *
287      * @param context The context
288      * @return The merged view
289      * @throws ViewRenderException
290      * @throws IOException
291      */

292
293     protected String JavaDoc renderView(String JavaDoc path, RequestContext context)
294             throws ViewRenderException, IOException JavaDoc {
295         ViewRenderer viewRenderer = getViewRenderer();
296         StringWriter JavaDoc out = new StringWriter JavaDoc();
297         viewRenderer.render(context, path, out);
298         return out.toString();
299     }
300
301 }
302
Popular Tags