/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** All rights reserved **
** **
** This program and the accompanying materials are made available under **
** the terms of the Eclipse Public License v1.0 which accompanies this **
** distribution, and is available at: **
** http://www.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** Christophe Bouhier - podcast plugin **
** **
** ********************************************************************** */
package org.rssowl.contrib.podcast.core.process;
import java.net.URI;
import java.util.Collection;
import org.rssowl.contrib.i18n.Messages;
import org.rssowl.contrib.podcast.core.codelets.InspectSequence;
import org.rssowl.contrib.podcast.core.download.DownloadUtil;
import org.rssowl.contrib.podcast.model.IPersonalBookMark;
import org.rssowl.contrib.podcast.model.Model;
import org.rssowl.contrib.podcast.model.PersonalCache;
import org.rssowl.contrib.podcast.util.Logger;
import org.rssowl.core.persist.IBookMark;
import org.rssowl.core.persist.IFeed;
import org.rssowl.core.persist.service.PersistenceException;
import org.rssowl.core.util.ITask;
import org.rssowl.core.util.JobQueue;
/**
* Implements several functions related to collecting, inspecting and
* downloading podcasts.
* <ul>
* <li>allInstruction: runs through the complete sequence of instructions</li>
* <li>previewInstruction: </li>
* </ul>
*
* @author christophe.bouhier
*
*/
public class InstructionService {
private Logger mLog = new Logger(getClass().getName());
/* Max. number of concurrent running reload Jobs */
private static final int MAX_CONCURRENT_DOWNLOAD_JOBS = 5;
private static InstructionService sSelf;
/* Queue for collecting bookmarks */
private JobQueue mExecQueue;
private InstructionNotifier mNotifier;
public static InstructionService getInstance() {
if (sSelf == null) {
sSelf = new InstructionService();
}
return sSelf;
}
public InstructionService() {
init();
}
private void init() {
mExecQueue = new JobQueue("Collect Bookmarks",
MAX_CONCURRENT_DOWNLOAD_JOBS, 5, true, 0);
mNotifier = new InstructionNotifier();
}
public InstructionNotifier getNotifier() {
return mNotifier;
}
/**
* Preview a feed. Initiates an instruction containing Collection &
* Inspection of a feed.
*
* @param src
* @param feed
*/
public void inspectInstruction(Object pSrc, IPersonalBookMark pPBookMark) {
if (pPBookMark != null) {
InspectSequence lSequence = new InspectSequence();
lSequence.invoke(pPBookMark);
}
}
/**
* @category UNDER CONSTRUCTION
* @param pSrc
* @param download
* @return
*/
public int allInstructions(Object pSrc, boolean download) {
// Scanning is prohibited, while the podcast folder is not set.
// The user is asked to set it when it doesn't exist. (See FileHandler)
while (DownloadUtil.getPodcastFolder() == null) {
try {
synchronized (this) {
this.wait(1000);
}
} catch (InterruptedException e) {
mLog.info("interrupted" + e.getMessage());
}
}
int lScanned = 0; // Number of polled feeds.
Collection<IFeed> mFeeds = Model.getInstance().getFeeds();
for (IFeed lFeed : mFeeds) {
mLog.info(Messages.getString("sequenceControl.start"));
IBookMark lBookMark = Model.getInstance().getBookMark(lFeed);
IPersonalBookMark lPBookMark = PersonalCache.getInstance()
.getPersonalBookMark(lBookMark);
// Creating the personal bookmark here.
try {
URI lUri = lPBookMark.getFeedLinkReference().resolve()
.getLink();
if (lPBookMark.isSubscribed() && lUri != null) {
lScanned++;
mLog.info("Scanning the feeds"
+ lPBookMark.getFeedLinkReference().resolve()
.getLink().getHost());
// CB TODO migrate instruction handling
// lPBookMark.setInstruction(new Instruction(pSrc, true, true,
// true, true, download, true));
// CB TODO, create a sequence of codelets and execute them now.
// scheduleCollect(lPBookMark);
}
} catch (PersistenceException e) {
mLog.warn(e.getLocalizedMessage());
}
}
int lSize = mFeeds.size();
mLog.info("Number of bookmarks scanned:"
+ new Integer(lScanned).toString() + " of: "
+ new Integer(lSize).toString());
return lScanned;
}
/**
* Add a Bookmark to the collection queue.
*
* @param pPBookMark
*/
/**
* @param pTask
*/
public void schedule(ITask pTask) {
if (!mExecQueue.isQueued(pTask)) {
mExecQueue.schedule(pTask);
}
mLog.info("Add bookmark: " + pTask + " to the collection queue");
}
}