KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > rss > FeedWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.rss;
18
19 import com.sun.syndication.feed.synd.SyndContent;
20 import com.sun.syndication.feed.synd.SyndContentImpl;
21 import com.sun.syndication.feed.synd.SyndEntry;
22 import com.sun.syndication.feed.synd.SyndEntryImpl;
23 import com.sun.syndication.feed.synd.SyndFeed;
24 import com.sun.syndication.feed.synd.SyndFeedImpl;
25 import com.sun.syndication.io.FeedException;
26 import com.sun.syndication.io.SyndFeedInput;
27 import com.sun.syndication.io.SyndFeedOutput;
28 import com.sun.syndication.io.XmlReader;
29
30 import org.apache.servicemix.components.util.OutBinding;
31 import org.apache.servicemix.expression.Expression;
32 import org.apache.servicemix.expression.ExpressionHelper;
33 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
34
35 import javax.jbi.JBIException;
36 import javax.jbi.messaging.MessageExchange;
37 import javax.jbi.messaging.MessagingException;
38 import javax.jbi.messaging.NormalizedMessage;
39 import javax.xml.transform.Source JavaDoc;
40 import javax.xml.transform.TransformerException JavaDoc;
41
42 import java.io.File JavaDoc;
43 import java.io.FileWriter JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.Writer JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Date JavaDoc;
48 import java.util.List JavaDoc;
49
50 /**
51  * This component generates an RSS/Atom Feed from the JBI messages it receives.
52  *
53  * @version $Revision: 426415 $
54  */

55 public class FeedWriter extends OutBinding {
56
57     private String JavaDoc feedType = "atom_0.3";
58     private SyndFeed cachedFeed;
59     private String JavaDoc title;
60     private String JavaDoc link = "http://servicemix.org/RSS";
61     private Expression entryTitle;
62     private Expression entryValue;
63     private String JavaDoc feedDescription = "This feed is auto-generated by ServiceMix";
64     private int maximumEntryCount = 200;
65     private File JavaDoc feedFile;
66     private String JavaDoc contentType = "text/plain";
67     private SourceTransformer sourceTransformer = new SourceTransformer();
68     private boolean loadOnStartup = true;
69
70     public SyndFeed getCachedFeed() throws IllegalArgumentException JavaDoc, FeedException, IOException JavaDoc {
71         if (cachedFeed == null) {
72             cachedFeed = loadOrCreateFeed();
73         }
74         return cachedFeed;
75     }
76
77     public void setCachedFeed(SyndFeed feed) {
78         this.cachedFeed = feed;
79     }
80
81     public String JavaDoc getFeedType() {
82         return feedType;
83     }
84
85     public void setFeedType(String JavaDoc feedType) {
86         this.feedType = feedType;
87     }
88
89     public String JavaDoc getFeedDescription() {
90         return feedDescription;
91     }
92
93     public void setFeedDescription(String JavaDoc feedDescription) {
94         this.feedDescription = feedDescription;
95     }
96
97     public String JavaDoc getTitle() {
98         if (title == null) {
99             title = "ServiceMix feed for service: " + getService();
100         }
101         return title;
102     }
103
104     public void setTitle(String JavaDoc title) {
105         this.title = title;
106     }
107
108     public String JavaDoc getLink() {
109         return link;
110     }
111
112     public void setLink(String JavaDoc link) {
113         this.link = link;
114     }
115
116     public Expression getEntryTitle() {
117         return entryTitle;
118     }
119
120     public void setEntryTitle(Expression title) {
121         this.entryTitle = title;
122     }
123
124     public Expression getEntryValue() {
125         return entryValue;
126     }
127
128     public void setEntryValue(Expression entryValue) {
129         this.entryValue = entryValue;
130     }
131
132     public int getMaximumEntryCount() {
133         return maximumEntryCount;
134     }
135
136     public void setMaximumEntryCount(int maximumEntryCount) {
137         this.maximumEntryCount = maximumEntryCount;
138     }
139
140     public File JavaDoc getFeedFile() {
141         if (feedFile == null) {
142             throw new IllegalArgumentException JavaDoc("You must set the 'feedFile' property");
143         }
144         return feedFile;
145     }
146
147     public void setFeedFile(File JavaDoc feedFile) {
148         this.feedFile = feedFile;
149     }
150
151     public String JavaDoc getContentType() {
152         return contentType;
153     }
154
155     public void setContentType(String JavaDoc contentType) {
156         this.contentType = contentType;
157     }
158
159     public SourceTransformer getSourceTransformer() {
160         return sourceTransformer;
161     }
162
163     public void setSourceTransformer(SourceTransformer sourceTransformer) {
164         this.sourceTransformer = sourceTransformer;
165     }
166
167     public boolean isLoadOnStartup() {
168         return loadOnStartup;
169     }
170
171     public void setLoadOnStartup(boolean loadOnStartup) {
172         this.loadOnStartup = loadOnStartup;
173     }
174
175     // Implementation methods
176
// -------------------------------------------------------------------------
177

178     protected void init() throws JBIException {
179         super.init();
180
181         // enforce validation
182
File JavaDoc file = getFeedFile();
183         if (file.exists() && file.isDirectory()) {
184             throw new IllegalArgumentException JavaDoc("Cannot generate the cachedFeed as the cachedFeed file is a directory: " + file);
185         }
186     }
187
188     protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception JavaDoc {
189         SyndFeed feed = getCachedFeed();
190         addMessageToFeed(feed, exchange, message);
191         removeExpiredEntries(feed);
192         writeFeed(feed, exchange, message);
193         done(exchange);
194     }
195
196     protected void addMessageToFeed(SyndFeed feed, MessageExchange exchange, NormalizedMessage message) throws TransformerException JavaDoc, MessagingException {
197         List JavaDoc entries = feed.getEntries();
198         SyndEntry entry = createEntry(exchange, message);
199         SyndContent description = createEntryContent(exchange, message);
200         entry.setDescription(description);
201         
202         // TODO this line really should work but for some reason it doesn't
203
// entries.add(0, entry);
204

205         List JavaDoc temp = new ArrayList JavaDoc();
206         temp.add(entry);
207         temp.addAll(entries);
208         feed.setEntries(temp);
209     }
210
211     protected SyndEntry createEntry(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
212         SyndEntry entry = new SyndEntryImpl();
213
214         entry.setTitle(ExpressionHelper.asString(getEntryTitle(), exchange, message, "ServiceMix Feed"));
215         entry.setLink(getLink());
216         entry.setPublishedDate(new Date JavaDoc());
217         return entry;
218     }
219
220     protected SyndContent createEntryContent(MessageExchange exchange, NormalizedMessage message) throws TransformerException JavaDoc, MessagingException {
221         SyndContent description = new SyndContentImpl();
222         description.setType(contentType);
223         Source JavaDoc content = message.getContent();
224
225         String JavaDoc value = ExpressionHelper.asString(getEntryValue(), exchange, message, null);
226         if (value == null && content != null) {
227             value = getSourceTransformer().toString(content);
228             value = encodeContent(value);
229         }
230         if (value != null) {
231             description.setValue(value);
232         }
233         return description;
234     }
235
236     /**
237      * Encodes the content so its valid XML when used inside an entry
238      */

239     protected String JavaDoc encodeContent(String JavaDoc value) {
240         return "<![CDATA[" + value + "]]>";
241     }
242
243     protected void writeFeed(SyndFeed feed, MessageExchange messageExchange, NormalizedMessage message) throws IOException JavaDoc, FeedException {
244         Writer JavaDoc writer = new FileWriter JavaDoc(feedFile);
245         SyndFeedOutput output = new SyndFeedOutput();
246         output.output(feed, writer);
247         writer.close();
248     }
249
250     /**
251      * Removes any old entires no longer required in the cachedFeed
252      *
253      * @param feed
254      */

255     protected void removeExpiredEntries(SyndFeed feed) {
256         // lets limit the count
257
if (maximumEntryCount > 0) {
258             List JavaDoc entries = feed.getEntries();
259             int size = entries.size();
260             if (size > maximumEntryCount) {
261                 entries.subList(maximumEntryCount, size).clear();
262             }
263         }
264     }
265
266     protected SyndFeed loadOrCreateFeed() throws IllegalArgumentException JavaDoc, FeedException, IOException JavaDoc {
267         if (isLoadOnStartup()) {
268             File JavaDoc file = getFeedFile();
269             if (file.exists() && file.isFile()) {
270                 SyndFeedInput input = new SyndFeedInput();
271                 XmlReader xmlReader = new XmlReader(file);
272                 return input.build(xmlReader);
273             }
274         }
275         return createFeed();
276     }
277
278     protected SyndFeed createFeed() {
279         SyndFeed feed = new SyndFeedImpl();
280         feed.setFeedType(feedType);
281
282         feed.setTitle(getTitle());
283         feed.setLink(getLink());
284         feed.setDescription(getFeedDescription());
285         return feed;
286     }
287
288 }
289
Popular Tags