yacy_search_server/source/de/anomic/plasma/crawler/plasmaCrawlerFactory.java
theli 09b106eb04 *) next step of restructuring for new crawlers
- adding interface class (plasma/crawler/plasmaCrawlWorker.java) for protocol specific crawl-worker threads 
   - moving reusable code into abstract crawl-worker class AbstractCrawlWorker.java
   - the load method of the worker threads should not be called directly anymore (e.g. by the snippet fetcher)
     to crawl a page and wait for the result use function plasmaCrawlLoader.loadSync([...])

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2474 6c8d7289-2bf4-0310-a012-ef5d649a1542
2006-09-04 09:00:18 +00:00

130 lines
3.9 KiB
Java

package de.anomic.plasma.crawler;
import java.lang.reflect.Constructor;
import org.apache.commons.pool.KeyedPoolableObjectFactory;
import de.anomic.plasma.plasmaHTCache;
import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.plasma.crawler.http.CrawlWorker;
import de.anomic.server.logging.serverLog;
public final class plasmaCrawlerFactory implements KeyedPoolableObjectFactory {
private plasmaCrawlerPool thePool;
private final ThreadGroup theThreadGroup;
private final plasmaHTCache cacheManager;
private final serverLog theLog;
private final plasmaSwitchboard sb;
public plasmaCrawlerFactory(
ThreadGroup threadGroup,
plasmaSwitchboard theSb,
plasmaHTCache theCacheManager,
serverLog log
) {
super();
if (threadGroup == null)
throw new IllegalArgumentException("The threadgroup object must not be null.");
this.theThreadGroup = threadGroup;
this.cacheManager = theCacheManager;
this.sb = theSb;
this.theLog = log;
}
public void setPool(plasmaCrawlerPool pool) {
this.thePool = pool;
}
/**
* @see org.apache.commons.pool.PoolableObjectFactory#makeObject()
*/
public Object makeObject(Object key) throws Exception {
if (!(key instanceof String))
throw new IllegalArgumentException("The object key must be of type string.");
// getting the class name
String className = this.getClass().getPackage().getName() + "." + key + ".CrawlWorker";
// loading class by name
Class moduleClass = Class.forName(className);
// getting the constructor
Constructor classConstructor = moduleClass.getConstructor( new Class[] {
ThreadGroup.class,
plasmaCrawlerPool.class,
plasmaSwitchboard.class,
plasmaHTCache.class,
serverLog.class
} );
// instantiating class
CrawlWorker theCrawlWorker = (CrawlWorker) classConstructor.newInstance(new Object[] {
this.theThreadGroup,
this.thePool,
this.sb,
this.cacheManager,
this.theLog
});
// return the newly created object
return theCrawlWorker;
// return new plasmaCrawlWorker(
// this.theThreadGroup,
// this.thePool,
// this.sb,
// this.cacheManager,
// this.theLog);
}
/**
* @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang.Object)
*/
public void destroyObject(Object key, Object obj) {
if (obj == null) return;
if (obj instanceof CrawlWorker) {
CrawlWorker theWorker = (CrawlWorker) obj;
synchronized(theWorker) {
theWorker.destroyed = true;
theWorker.setName(plasmaCrawlWorker.threadBaseName + "_destroyed");
theWorker.setStopped(true);
theWorker.interrupt();
}
}
}
/**
* @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object)
*/
public boolean validateObject(Object key, Object obj) {
return true;
}
/**
* @param obj
*
*/
public void activateObject(Object key, Object obj) {
//log.debug(" activateObject...");
}
/**
* @param obj
*
*/
public void passivateObject(Object key, Object obj) {
//log.debug(" passivateObject..." + obj);
/*
if (obj instanceof plasmaCrawlWorker) {
plasmaCrawlWorker theWorker = (plasmaCrawlWorker) obj;
}
*/
}
}