KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.jbi.JBIException;
25 import javax.jbi.messaging.InOnly;
26 import javax.jbi.messaging.NormalizedMessage;
27 import javax.xml.transform.Source JavaDoc;
28 import javax.xml.transform.dom.DOMSource JavaDoc;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.components.util.PollingComponentSupport;
32
33 import com.sun.syndication.feed.synd.SyndEntry;
34 import com.sun.syndication.feed.synd.SyndFeed;
35 import com.sun.syndication.feed.synd.SyndFeedImpl;
36 import com.sun.syndication.io.SyndFeedInput;
37 import com.sun.syndication.io.SyndFeedOutput;
38 import com.sun.syndication.io.XmlReader;
39
40 /**
41  * The RssPollingComponent polls for updates to RSS feeds
42  *
43  * @version $Revision: 426415 $
44  */

45 public class RssPollingComponent extends PollingComponentSupport {
46     private static final Log log = LogFactory.getLog(RssPollingComponent.class);
47     private List JavaDoc urlStrings = new ArrayList JavaDoc();
48     private List JavaDoc urls = new ArrayList JavaDoc();
49     private Date JavaDoc lastPolledDate = new Date JavaDoc();
50     private String JavaDoc outputType = "rss_2.0";
51     
52
53     
54     /**
55      * @return Returns the urlStrings.
56      */

57     public List JavaDoc getUrlStrings() {
58         return urlStrings;
59     }
60
61     /**
62      * @param urlStrings The urlStrings to set.
63      */

64     public void setUrlStrings(List JavaDoc urlStrings) {
65         this.urlStrings = urlStrings;
66     }
67
68     /**
69      * @return Returns the outputType.
70      */

71     public String JavaDoc getOutputType() {
72         return outputType;
73     }
74
75     /**
76      * @param outputType The outputType to set.
77      */

78     public void setOutputType(String JavaDoc outputType) {
79         this.outputType = outputType;
80     }
81
82     /**
83      * @return Returns the lastPolledDate.
84      */

85     public Date JavaDoc getLastPolledDate() {
86         return lastPolledDate;
87     }
88
89     /**
90      * @param lastPolledDate The lastPolledDate to set.
91      */

92     public void setLastPolledDate(Date JavaDoc lastPolledDate) {
93         this.lastPolledDate = lastPolledDate;
94     }
95
96    
97     
98     protected void init() throws JBIException {
99         urls.clear();
100         if (urlStrings != null) {
101             for (int i = 0;i < urlStrings.size();i++) {
102                 try {
103                     urls.add(new URL JavaDoc(urlStrings.get(i).toString()));
104                 }
105                 catch (MalformedURLException JavaDoc e) {
106                     log.warn("URL: " + urlStrings.get(i) + " is badly formed", e);
107                 }
108             }
109         }
110         super.init();
111        
112     }
113
114     /**
115      * Poll for updates
116      */

117     public void poll() {
118         List JavaDoc list = getLastesEntries();
119         if (list != null && !list.isEmpty()) {
120             SyndFeed feed = new SyndFeedImpl();
121             feed.setFeedType(outputType);
122             feed.setTitle("Aggregated Feed");
123             feed.setDescription("Anonymous Aggregated Feed");
124             feed.setAuthor("servicemix");
125             feed.setLink("http://www.servicemix.org");
126             feed.setEntries(list);
127             // send on to the nmr ...
128
SyndFeedOutput output = new SyndFeedOutput();
129             try {
130                 Source JavaDoc source = new DOMSource JavaDoc(output.outputW3CDom(feed));
131                 InOnly exchange = getExchangeFactory().createInOnlyExchange();
132                 NormalizedMessage message = exchange.createMessage();
133                 message.setContent(source);
134                 exchange.setInMessage(message);
135                 send(exchange);
136             }
137             catch (Exception JavaDoc e) {
138                 log.error("Failed to send RSS message to the NMR");
139             }
140             finally {
141                 lastPolledDate = new Date JavaDoc();
142             }
143         }
144     }
145
146     protected List JavaDoc getLastesEntries() {
147         List JavaDoc result = new ArrayList JavaDoc();
148         SyndFeedInput input = new SyndFeedInput();
149         for (int i = 0;i < urls.size();i++) {
150             URL JavaDoc inputUrl = (URL JavaDoc) urls.get(i);
151             SyndFeed inFeed;
152             try {
153                 inFeed = input.build(new XmlReader(inputUrl));
154                 List JavaDoc entries = inFeed.getEntries();
155                 for (int k = 0;k < entries.size();k++) {
156                     SyndEntry entry = (SyndEntry) entries.get(k);
157                     if (entry.getPublishedDate().after(getLastPolledDate())) {
158                         result.add(entry);
159                     }
160                 }
161             }
162             catch (Exception JavaDoc e) {
163                 log.warn("Failed to process feed from: " + inputUrl, e);
164             }
165         }
166         return result;
167     }
168 }
169
Popular Tags