/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package getfacts;
import getfacts.plugins.HtmlReader;
import getfacts.plugins.feeds.RssReader;
import getfacts.plugins.XmlReader;
import getfacts.toolkit.ThreadToolkit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author jmc15
*/
public class Fetcher
{
private static Configuration configuration;
private static Thread fetcherThread;
private static HashMap<ConfigurationItems, FetcherInterface> plugins;
public static final Object fetcherLock = new ArrayList();
private static boolean runThread = false;
private static FetcherObserver fetcherObserver;
public static void stop()
{
try
{
runThread =false;
fetcherThread.interrupt();
fetcherThread.join(250);
}
catch (InterruptedException ex)
{
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void start(FetcherObserver o)
{
fetcherObserver = o;
plugins = new HashMap<ConfigurationItems, FetcherInterface>();
configuration = ConfigurationSafe.loadConfiguration();
fetcherThread = new Thread( new Runnable() {
@Override
public void run()
{
runThread = true;
fetcherThreadBody();
}
});
fetcherThread.setName("fetcherThread");
fetcherThread.setPriority( ThreadToolkit.getPriority(50f) );
fetcherThread.start();
}
private static void fetcherThreadBody()
{
while(runThread==true)
{
for(int index=0; index<configuration.getSize(); index++)
{
ConfigurationItems items = configuration.getConfigurationAt(index);
synchronized(fetcherLock)
{
//
// RSS READER ?
//
if( items.getPluginName().compareTo(RssReader.PLUGIN_NAME)== 0 )
{
FetcherInterface plugin = plugins.get(items);
if( plugin == null )
{
plugin = new RssReader();
plugins.put(items, plugin);
}
try
{
FrontPage fp = plugin.GetFacts(items, fetcherObserver);
NewsSafe.storeNews(fp);
fp.dispose();
}
catch (IOException ex)
{
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
//
// Html READER ?
//
else if( items.getPluginName().compareTo(HtmlReader.PLUGIN_NAME)== 0 )
{
FetcherInterface plugin = plugins.get(items);
if( plugin == null )
{
plugin = new HtmlReader();
plugins.put(items, plugin);
}
try
{
FrontPage fp = plugin.GetFacts(items, fetcherObserver);
NewsSafe.storeNews(fp);
fp.dispose();
}
catch (IOException ex)
{
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if( items.getPluginName().compareTo("getfacts.plugins.XmlReader")== 0 )
{
FetcherInterface plugin = plugins.get(items);
if( plugin == null )
{
plugin = new XmlReader();
plugins.put(items, plugin);
}
try
{
FrontPage fp = plugin.GetFacts(items, fetcherObserver);
NewsSafe.storeNews(fp);
fp.dispose();
}
catch (IOException ex)
{
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
/*
else if( items.getPluginName().compareTo("getfacts.plugins.websites.Leboncoin_fr")== 0 )
{
FetcherInterface plugin = plugins.get(items);
if( plugin == null )
{
plugin = new Leboncoin_fr();
plugins.put(items, plugin);
}
try
{
FrontPage fp = plugin.GetFacts(items);
NewsSafe.storeNews(fp);
fp.dispose();
}
catch (IOException ex)
{
Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
}
} */
}
}
try {
Thread.sleep(10*60*1000);
} catch (InterruptedException ex) {
//Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
continue;
}
}
System.out.println("quit fetcherThread");
}
}