KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > rss > Feed


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.rss;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.sslexplorer.boot.Util;
31 import com.sun.syndication.feed.synd.SyndFeed;
32 import com.sun.syndication.io.FeedException;
33 import com.sun.syndication.io.SyndFeedInput;
34 import com.sun.syndication.io.XmlReader;
35
36 /**
37  * Thread that wraps a {@link com.sun.syndication.io.SyndFeedInput} as is
38  * responsible for downloading and maintaining the current status of the feed.
39  * <p>
40  * The feed may be in one of 4 states :-
41  * <ul>
42  * <li>Loading. The feed is currently being downloaded from the source site.</li>
43  * <li>Loaded. The feed has successfuly been downloaded from the source site.</li>
44  * <li>Empty. The feed has successfuly been downloaded but was empty.</li>
45  * <li>Failed To Load. The feed failed to load.</li>
46  * </ul>
47  *
48  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
49  */

50 public class Feed {
51
52     final static Log log = LogFactory.getLog(Feed.class);
53     
54     /**
55      * Feed connect timeout
56      */

57     public static final int CONNECT_TIMEOUT = 5000;
58     
59     /**
60      * Feed read timeout
61      */

62     public static final int READ_TIMEOUT = 5000;
63
64     /**
65      * Loading. The feed is currently being downloaded from the source site.
66      */

67     public final static int STATUS_LOADING = 0;
68
69     /**
70      * Loaded. The feed has successfuly been downloaded from the source site.
71      */

72     public final static int STATUS_LOADED = 1;
73
74     /**
75      * Empty. The feed has successfuly been downloaded but was empty.
76      */

77     public final static int STATUS_EMPTY = 2;
78
79     /**
80      * Failed To Load. The feed failed to load.
81      */

82     public final static int STATUS_FAILED_TO_LOAD = 3;
83
84     // Private instance variables
85

86     private String JavaDoc feedName;
87     private SyndFeed feed;
88     private int status;
89     private SyndFeedInput input;
90     private URL JavaDoc url;
91
92     /**
93      * Constructor.
94      *
95      * @param feedName feed name
96      * @param input feed input
97      * @param url location
98      * @throws IOException on error loading feed
99      * @throws FeedException
100      */

101     public Feed(String JavaDoc feedName, SyndFeedInput input, URL JavaDoc url) throws IOException JavaDoc, FeedException {
102         super();
103         this.url = url;
104         this.feedName = feedName;
105         this.input = input;
106         this.status = STATUS_LOADING;
107     }
108
109     /**
110      * Get the feed name.
111      *
112      * @return feed
113      */

114     public String JavaDoc getFeedName() {
115         return feedName;
116     }
117
118     /**
119      * Get the feed object. This will only be available once the feed has
120      * successfuly been downloaded otherwise <code>null</code> will be returned.
121      *
122      * @return feed
123      */

124     public SyndFeed getFeed() {
125         return feed;
126     }
127
128     /**
129      * Get the feed status. See class documentation for details.
130      *
131      * @return status
132      */

133     public int getStatus() {
134         return status;
135     }
136
137     void load() throws IOException JavaDoc, FeedException {
138
139         if (log.isInfoEnabled()) {
140             log.info("Retrieving RSS feeds from " + url);
141         }
142         
143         URLConnection JavaDoc conx = url.openConnection();
144         conx.setConnectTimeout(Feed.CONNECT_TIMEOUT);
145         conx.setReadTimeout(Feed.READ_TIMEOUT);
146         InputStream JavaDoc inputStream = null;
147         try {
148             inputStream = conx.getInputStream();
149             status = STATUS_LOADING;
150             feed = input.build(new XmlReader(inputStream));
151             if (log.isInfoEnabled())
152                 log.info("Retrieved feed " + url);
153             status = STATUS_LOADED;
154             
155         } catch (IOException JavaDoc e) {
156             status = STATUS_FAILED_TO_LOAD;
157             throw e;
158         } catch (FeedException e) {
159             status = STATUS_FAILED_TO_LOAD;
160             throw e;
161         } finally {
162             Util.closeStream(inputStream);
163         }
164     }
165 }
166
Popular Tags