KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > UpdateChannelTask


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26 // $Id: UpdateChannelTask.java,v 1.33 2004/05/13 22:55:17 niko_schmuck Exp $
27

28 package de.nava.informa.utils;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.TimerTask JavaDoc;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import de.nava.informa.core.*;
39 import de.nava.informa.core.ChannelBuilderIF;
40 import de.nava.informa.impl.basic.ChannelBuilder;
41 import de.nava.informa.core.ChannelFormat;
42 import de.nava.informa.core.ChannelIF;
43 import de.nava.informa.core.ItemIF;
44 import de.nava.informa.core.ParseException;
45 import de.nava.informa.core.UnsupportedFormatException;
46 import de.nava.informa.parsers.FeedParser;
47
48 /**
49  * Class for performing a channel retrieval in periodic intervals as a
50  * TimerTask. The existing items of a channel are compared to the
51  * items contained in the newly gotten channel, and if there are any
52  * new items they are appended.
53  *
54  * @author Niko Schmuck (niko@nava.de)
55  */

56 public class UpdateChannelTask extends TimerTask JavaDoc {
57
58   private static Log logger = LogFactory.getLog(UpdateChannelTask.class);
59
60   private ChannelRegistry registry;
61   private ChannelIF channel;
62   private ChannelBuilderIF builder;
63   private UpdateChannelInfo info;
64   private ChannelBuilderIF tempBuilder;
65
66   public UpdateChannelTask(
67     ChannelRegistry registry,
68     ChannelBuilderIF builder,
69     ChannelIF channel,
70     UpdateChannelInfo info) {
71     this.registry = registry;
72     this.channel = channel;
73     this.builder = builder;
74     this.info = info;
75
76     // builder that is passed in on construction *may* be for persistent Channels.
77
// tempBuilder is forced to be memory only so it will not be persisted.
78
tempBuilder = new ChannelBuilder();
79   }
80
81   public void run() {
82     logger.info("Task Run()");
83     Thread.currentThread().setName("Informa Update Channel Task");
84     /**
85      * ChannelBuilder is not re-entrant and it is shared by all the
86      * UpdateChannelTasks which are created by single ChannelRegistry.
87      * Note that all the beginTransaction() must have a corresponding endTransaction()
88      */

89     synchronized (builder) {
90       if (!info.getFormatDetected())
91         /**
92          * If this is the first time we see this Channel, then we will now attempt
93          * to parse it and if this works we remember the format and proceed.
94          * Otherwise we trigger error case handling and eventually deactivate it.
95          */

96         {
97         try {
98           builder.beginTransaction();
99           ChannelFormat format =
100             FormatDetector.getFormat(channel.getLocation());
101           channel.setFormat(format);
102           info.setFormatDetected(true);
103           channel.setLastUpdated(new Date JavaDoc());
104           builder.endTransaction();
105         } catch (UnsupportedFormatException ex) {
106           logger.info("Unsupported format for Channel");
107           incrementProblems(ex);
108           return;
109         } catch (IOException JavaDoc ioe) {
110           logger.info("Cannot retrieve Channel");
111           incrementProblems(ioe);
112           return;
113         } catch (ChannelBuilderException e) {
114           e.printStackTrace();
115         }
116       }
117       try {
118         synchronized (channel) {
119           builder.beginTransaction();
120           ChannelIF tempChannel =
121             FeedParser.parse(tempBuilder, channel.getLocation());
122           logger.info(
123             "Updating channel from "
124               + channel.getLocation()
125               + ": "
126               + tempChannel
127               + "(new) "
128               + channel
129               + "(old)");
130           InformaUtils.copyChannelProperties(tempChannel, channel);
131           builder.update(channel);
132           channel.setLastUpdated(new Date JavaDoc());
133           // compare with existing items, only add new ones
134
if (tempChannel.getItems().isEmpty()) {
135             logger.warn("No items found in channel " + channel);
136           } else {
137             Iterator JavaDoc it = tempChannel.getItems().iterator();
138             while (it.hasNext()) {
139               ItemIF item = (ItemIF) it.next();
140               if (!channel.getItems().contains(item)) {
141                 logger.debug("Found new item: " + item);
142                 channel.addItem(builder.createItem(null, item));
143                 // }
144
}
145             } // while more items
146
}
147           builder.endTransaction();
148         }
149       } catch (ParseException pe) {
150         incrementProblems(pe);
151       } catch (IOException JavaDoc ioe) {
152         incrementProblems(ioe);
153       } catch (ChannelBuilderException e) {
154         e.printStackTrace();
155       }
156     }
157   }
158
159    /**
160    * Increases the count of problems occurred so far and put out an error
161    * message based on the given exception.
162    */

163   private void incrementProblems(Exception JavaDoc e) {
164     info.increaseProblemsOccurred(e);
165     if (info.shouldDeactivate()) {
166       logger.warn(
167         "Deactivating channel after "
168           + info.getNrProblemsOccurred()
169           + " problems occurred.");
170       registry.deactivateChannel(channel);
171     }
172     logger.warn(e);
173   }
174
175 }
176
Popular Tags