KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > repository > ContentInstance


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

44 package org.jpublish.repository;
45
46 import java.io.IOException JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.InputStreamReader JavaDoc;
49 import java.util.*;
50
51 import com.anthonyeden.lib.config.Configuration;
52 import com.anthonyeden.lib.config.ConfigurationException;
53 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory;
54 import com.anthonyeden.lib.util.IOUtilities;
55 import com.anthonyeden.lib.util.MessageUtilities;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.apache.commons.vfs.FileContent;
59 import org.apache.commons.vfs.FileName;
60 import org.apache.commons.vfs.FileObject;
61 import org.apache.commons.vfs.FileSystemException;
62 import org.jpublish.JPublishEngine;
63 import org.jpublish.RequestContext;
64 import org.jpublish.SiteContext;
65 import org.jpublish.action.Action;
66 import org.jpublish.action.ActionWrapper;
67 import org.jpublish.util.Property;
68
69 /**
70  * A representation of a single content item.
71  *
72  * <p>Actions attached to a content item will be triggered each time the content is requested. Actions will be triggered
73  * in the order that they are listed within the content's configuration.</p>
74  *
75  * <p>There should only be a single ContentInstance in memory for each path. Each ContentInstance is actually wrapped in
76  * a Content class which provides request-specific features.</p>
77  *
78  * @author Anthony Eden
79  */

80
81 public class ContentInstance {
82
83     private Log log = LogFactory.getLog(ContentInstance.class);
84
85     private SiteContext siteContext = null;
86     private List contentActions = new ArrayList();
87     private FileObject file = null;
88     private FileName fileName = null;
89     private FileContent fileContent = null;
90     private String JavaDoc path = null;
91     private String JavaDoc viewRendererName = null;
92     private Map properties = new HashMap();
93     private Repository repository = null;
94
95     private long timeLastLoaded = -1;
96     private String JavaDoc contentString = null;
97
98     /**
99      * Construct a new Content instance for the given path.
100      *
101      * @param siteContext The SiteContext
102      * @param file The FileObject
103      * @param path The path
104      * @param repository The Repository
105      * @throws FileSystemException Description of the Exception
106      */

107
108     public ContentInstance(SiteContext siteContext, FileObject file,
109             String JavaDoc path, Repository repository) throws FileSystemException {
110         this.siteContext = siteContext;
111         this.file = file;
112         this.fileName = file.getName();
113         this.fileContent = file.getContent();
114         this.path = path;
115         this.repository = repository;
116     }
117
118     /**
119      * Get the SiteContext.
120      *
121      * @return The SiteContext
122      */

123
124     public SiteContext getSiteContext() {
125         return siteContext;
126     }
127
128     /**
129      * Get the Repository which this ContentInstance came from.
130      *
131      * @return The Repository
132      */

133
134     public Repository getRepository() {
135         return repository;
136     }
137
138     /**
139      * Get the FileObject which this content is connected to.
140      *
141      * @return The FileObject
142      */

143
144     public FileObject getFile() {
145         return file;
146     }
147
148     /**
149      * Get the content path.
150      *
151      * @return The content path
152      */

153
154     public String JavaDoc getPath() {
155         return path;
156     }
157
158     /**
159      * Get the view renderer name. This method will return null if the page should use the default view renderer.
160      *
161      * @return The view renderer name or null
162      */

163
164     public String JavaDoc getViewRendererName() {
165         return viewRendererName;
166     }
167
168     /**
169      * Set the view renderer name. Set this value to null to specify that the default view renderer should be used.
170      *
171      * @param viewRendererName The view renderer name or null to reset
172      */

173
174     public void setViewRendererName(String JavaDoc viewRendererName) {
175         this.viewRendererName = viewRendererName;
176     }
177
178     /**
179      * Get a List of content actions. To add an action to the content just add the action to this List. Content actions
180      * are triggered each time the content is requested.
181      *
182      * @return A List of content actions
183      */

184
185     public List getContentActions() {
186         return contentActions;
187     }
188
189     /**
190      * Get the named property using the default Locale. If the property is not found then return null.
191      *
192      * @param name The property name
193      * @return The value or null
194      */

195
196     public String JavaDoc getProperty(String JavaDoc name) {
197         return getProperty(name, Locale.getDefault());
198     }
199
200     /**
201      * Get the Locale-specific value for the given named property. If the property is not found then return null. This
202      * method will try to find the most suitable locale by searching the property values in the following manner: <p>
203      *
204      * language + "_" + country + "_" + variant<br> language + "_" + country<br> langauge<br> "" </p>
205      *
206      * @param name The property name
207      * @param locale The locale
208      * @return The value
209      */

210
211     public String JavaDoc getProperty(String JavaDoc name, Locale locale) {
212         if (log.isDebugEnabled()) {
213             log.debug("Get property [name=" + name + ",locale=" + locale + "]");
214         }
215         Property property = (Property) properties.get(name);
216         if (property != null) {
217             return property.getValue(locale);
218         } else {
219             return null;
220         }
221     }
222
223     /**
224      * Get the named property. This method is equivilent to the <code>getProperty(name)</code> method. This method is
225      * provided as a convenience to view code.
226      *
227      * @param name The property name
228      * @return The value
229      */

230
231     public String JavaDoc get(String JavaDoc name) {
232         if (log.isDebugEnabled()) {
233             log.debug("get(" + name + ") called to retrieve property");
234         }
235         return getProperty(name);
236     }
237
238     /**
239      * Execute the content actions.
240      */

241
242     public void executeActions() {
243         RequestContext context = RequestContext.getRequestContext();
244         Iterator contentActions = getContentActions().iterator();
245         while (contentActions.hasNext()) {
246             ((ActionWrapper) contentActions.next()).execute(context);
247         }
248     }
249
250     /**
251      * Return a String representation of the content.
252      *
253      * @return A String representation of content
254      * @throws IOException
255      */

256
257     public synchronized String JavaDoc getContentString() throws IOException JavaDoc {
258         log.debug("getContentString()");
259         // check if reload is necessary
260
boolean reloadContent = false;
261         long contentLastModified = fileContent.getLastModifiedTime();
262         if (contentLastModified > timeLastLoaded) {
263             if (log.isDebugEnabled()) {
264                 log.debug("Loading updated or new content: " + fileName);
265             }
266             reloadContent = true;
267         }
268
269         // load or reload the content if necessary
270
if (reloadContent || contentString == null) {
271             timeLastLoaded = System.currentTimeMillis();
272             contentString = IOUtilities.getStringFromReader(new InputStreamReader JavaDoc(fileContent.getInputStream()));
273         }
274
275         return contentString;
276     }
277
278     /**
279      * Get the content InputStream.
280      *
281      * @return An InputStream
282      * @throws IOException
283      */

284
285     public InputStream JavaDoc getContentInputStream() throws IOException JavaDoc {
286         log.debug("getContentInputStream()");
287         return fileContent.getInputStream();
288     }
289
290     /**
291      * Load the content configuration from the XML stream.
292      *
293      * @param in The InputStream
294      * @throws ConfigurationException
295      */

296
297     public synchronized void loadConfiguration(InputStream JavaDoc in) throws ConfigurationException {
298         log.debug("Loading content configuration.");
299         Configuration configuration =
300                 SAXConfigurationFactory.getInstance().getConfiguration(fileName.getPath(), in);
301         loadConfiguration(configuration);
302     }
303
304     /**
305      * Load the content configuration from the given Configuration object.
306      *
307      * @param configuration The Configuration object
308      * @throws ConfigurationException
309      */

310
311     public synchronized void loadConfiguration(Configuration configuration)
312             throws ConfigurationException {
313         contentActions.clear();
314         setViewRendererName(configuration.getChildValue("view-renderer"));
315
316         // load content actions
317
log.debug("Looping through content-action elements.");
318         Iterator contentActionElements =
319                 configuration.getChildren("content-action").iterator();
320         while (contentActionElements.hasNext()) {
321             Configuration contentActionElement =
322                     (Configuration) contentActionElements.next();
323             String JavaDoc name = contentActionElement.getAttribute("name");
324
325             if (name == null) {
326                 Object JavaDoc[] args = {};
327                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
328                         JPublishEngine.MESSAGE_PACKAGE, "actionNameRequired",
329                         args);
330                 throw new ConfigurationException(msg, contentActionElement);
331             }
332
333             Action action = siteContext.getActionManager().findAction(name);
334
335             contentActions.add(new ActionWrapper(action, contentActionElement));
336         }
337
338         // load content properties
339
log.debug("Loading content properties");
340         Iterator propertyElements =
341                 configuration.getChildren("property").iterator();
342         while (propertyElements.hasNext()) {
343             Configuration propertyElement =
344                     (Configuration) propertyElements.next();
345             setProperty(propertyElement.getAttribute("name"),
346                     propertyElement.getValue(),
347                     propertyElement.getAttribute("locale"));
348         }
349     }
350
351     /**
352      * Set the property value.
353      *
354      * @param name The property name
355      * @param value The value
356      * @param locale The locale String or null
357      */

358
359     private void setProperty(String JavaDoc name, String JavaDoc value, String JavaDoc locale) {
360         if (log.isDebugEnabled()) {
361             log.debug("setProperty() [name=" + name + ",value=" + value +
362                     ",locale=" + locale);
363         }
364         Property property = (Property) properties.get(name);
365         if (property == null) {
366             // named property not in property map
367
property = new Property(name);
368             properties.put(name, property);
369         }
370         property.setValue(value, locale);
371     }
372
373 }
374
375
Popular Tags