You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yacy_search_server/source/de/anomic/xml/RSSFeed.java

163 lines
5.0 KiB

// RSSFeed.java
// (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 24.04.2008 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.xml;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class RSSFeed implements Iterable<RSSMessage> {
// static channel names of feeds
public static final String PEERNEWS = "PEERNEWS";
public static final String REMOTESEARCH = "REMOTESEARCH";
public static final String LOCALSEARCH = "LOCALSEARCH";
public static final String REMOTEINDEXING = "REMOTEINDEXING";
public static final String LOCALINDEXING = "LOCALINDEXING";
/**
* the following private channels are declared to prevent that an access to the feed servlet
* gets results from news channels that are not for the public
*/
public static HashSet<String> privateChannels = new HashSet<String>();
static {
privateChannels.add(LOCALSEARCH);
privateChannels.add(LOCALINDEXING);
}
// class variables
private RSSMessage channel;
private String imageURL;
ConcurrentLinkedQueue<String> messageQueue; // a list of GUIDs, so the items can be retrieved by a specific order
ConcurrentHashMap<String, RSSMessage> messages; // a guid:Item map
private int maxsize;
public RSSFeed() {
messageQueue = new ConcurrentLinkedQueue<String>();
messages = new ConcurrentHashMap<String, RSSMessage>();
channel = null;
maxsize = Integer.MAX_VALUE;
}
public RSSFeed(int maxsize) {
this();
this.maxsize = maxsize;
}
public void setMaxsize(int maxsize) {
this.maxsize = maxsize;
while (messageQueue.size() > this.maxsize) pollMessage();
}
public void setChannel(RSSMessage channelItem) {
this.channel = channelItem;
}
public RSSMessage getChannel() {
return channel;
}
public void setImage(String imageURL) {
this.imageURL = imageURL;
}
public String getImage() {
return this.imageURL;
}
public void addMessage(RSSMessage item) {
String guid = item.getGuid();
messageQueue.add(guid);
messages.put(guid, item);
while (messageQueue.size() > this.maxsize) pollMessage();
}
public RSSMessage getMessage(String guid) {
// retrieve item by guid
return messages.get(guid);
}
public int size() {
return messages.size();
}
public Iterator<RSSMessage> iterator() {
return new messageIterator();
}
public RSSMessage pollMessage() {
// retrieve and delete item
if (messageQueue.size() == 0) return null;
String nextGUID = messageQueue.poll();
if (nextGUID == null) return null;
return messages.remove(nextGUID);
}
public class messageIterator implements Iterator<RSSMessage>{
Iterator<String> GUIDiterator;
String lastGUID;
public messageIterator() {
GUIDiterator = messageQueue.iterator();
lastGUID = null;
}
public boolean hasNext() {
return GUIDiterator.hasNext();
}
public RSSMessage next() {
lastGUID = GUIDiterator.next();
if (lastGUID == null) return null;
return messages.get(lastGUID);
}
public void remove() {
if (lastGUID == null) return;
GUIDiterator.remove();
messages.remove(lastGUID);
}
}
/**
* the following static channels object is used to organize a storage array for RSS feeds
*/
private static final ConcurrentHashMap<String, RSSFeed> channels = new ConcurrentHashMap<String, RSSFeed>();
public static RSSFeed channels(String channelName) {
RSSFeed feed = channels.get(channelName);
if (feed != null) return feed;
feed = new RSSFeed();
feed.setChannel(new RSSMessage(channelName, "", ""));
channels.put(channelName, feed);
return feed;
}
}