Permalink
Browse files

Removed some unnecessary uses of java.lang.reflect api.

This improves code browsing and readability, making search by references
or call hierarchy IDE features more accurate.
  • Loading branch information...
luccioman committed Aug 24, 2017
1 parent da3dbf9 commit 4eba88f2ff1466d9fad1e52f52c792eb360e665b
@@ -35,21 +35,20 @@
import java.util.regex.Pattern;
import net.yacy.cora.protocol.Domains;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.data.Translator;
import net.yacy.data.WorkTables;
import net.yacy.http.YaCyHttpServer;
import net.yacy.kelondro.workflow.InstantBusyThread;
import net.yacy.peers.OnePeerPingBusyThread;
import net.yacy.peers.Seed;
import net.yacy.search.Switchboard;
import net.yacy.search.SwitchboardConstants;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
import net.yacy.server.http.HTTPDFileHandler;
import net.yacy.utils.translation.TranslatorXliff;
import net.yacy.utils.upnp.UPnPMappingType;
import net.yacy.utils.upnp.UPnP;
import net.yacy.utils.upnp.UPnPMappingType;
public class ConfigBasic {
@@ -58,7 +57,7 @@
private static final int NEXTSTEP_PEERNAME = 2;
private static final int NEXTSTEP_PEERPORT = 3;
private static final int NEXTSTEP_RECONNECT = 4;
public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) throws FileNotFoundException, IOException {
// return variable that accumulates replacements
@@ -81,7 +80,7 @@ public static serverObjects respond(final RequestHeader header, final serverObje
//boolean doPeerPing = false;
if ((sb.peers.mySeed().isVirgin()) || (sb.peers.mySeed().isJunior())) {
InstantBusyThread.oneTimeJob(sb.yc, "peerPing", 0);
new OnePeerPingBusyThread(sb.yc).start();
//doPeerPing = true;
}
@@ -33,7 +33,7 @@
import net.yacy.data.ymark.YMarkXBELImporter;
import net.yacy.document.Parser.Failure;
import net.yacy.kelondro.blob.Tables;
import net.yacy.kelondro.workflow.InstantBusyThread;
import net.yacy.kelondro.workflow.OneTimeBusyThread;
import net.yacy.search.Switchboard;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
@@ -71,7 +71,7 @@ public static serverObjects respond(final RequestHeader header, final serverObje
empty = true;
}
YMarkAutoTagger autoTagger = new YMarkAutoTagger(autoTaggingQueue, sb.loader, sb.tables.bookmarks, bmk_user, merge);
InstantBusyThread.oneTimeJob(autoTagger, 0);
OneTimeBusyThread.startFromRunnable(autoTagger, 0);
}
if(isAdmin && post.containsKey("table") && post.get("table").length() > 0) {
@@ -109,8 +109,8 @@ public static serverObjects respond(final RequestHeader header, final serverObje
MonitoredReader reader = new MonitoredReader(new InputStreamReader(stream, StandardCharsets.UTF_8), 1024*16, bytes.length);
if(post.get("importer").equals("html") && reader != null) {
final YMarkHTMLImporter htmlImporter = new YMarkHTMLImporter(reader, queueSize, root);
InstantBusyThread.oneTimeJob(htmlImporter, 0);
InstantBusyThread.oneTimeJob(htmlImporter.getConsumer(sb, bmk_user, autoTaggingQueue, autotag, empty, indexing, medialink), 0);
OneTimeBusyThread.startFromRunnable(htmlImporter, 0);
OneTimeBusyThread.startFromRunnable(htmlImporter.getConsumer(sb, bmk_user, autoTaggingQueue, autotag, empty, indexing, medialink), 0);
prop.put("status", "1");
} else if(post.get("importer").equals("xbel") && reader != null) {
final YMarkXBELImporter xbelImporter;
@@ -123,13 +123,13 @@ public static serverObjects respond(final RequestHeader header, final serverObje
prop.put("status", "0");
return prop;
}
InstantBusyThread.oneTimeJob(xbelImporter, 0);
InstantBusyThread.oneTimeJob(xbelImporter.getConsumer(sb, bmk_user, autoTaggingQueue, autotag, empty, indexing, medialink), 0);
OneTimeBusyThread.startFromRunnable(xbelImporter, 0);
OneTimeBusyThread.startFromRunnable(xbelImporter.getConsumer(sb, bmk_user, autoTaggingQueue, autotag, empty, indexing, medialink), 0);
prop.put("status", "1");
} else if(post.get("importer").equals("json") && reader != null) {
YMarkJSONImporter jsonImporter;
jsonImporter = new YMarkJSONImporter(reader, queueSize, root);
InstantBusyThread.oneTimeJob(jsonImporter, 0);
OneTimeBusyThread.startFromRunnable(jsonImporter, 0);
while ((bmk = jsonImporter.take()) != YMarkEntry.POISON) {
putBookmark(sb, bmk_user, bmk, autoTaggingQueue, autotag, empty, indexing, medialink);
}
@@ -207,8 +207,8 @@ public static serverObjects respond(final RequestHeader header, final serverObje
mreader.addChangeListener(sb.tables.bookmarks.getProgressListener("DMOZImporter"));
DMOZImporter.setDepth(6);
InstantBusyThread.oneTimeJob(DMOZImporter, 0);
InstantBusyThread.oneTimeJob(DMOZImporter.getConsumer(sb, bmk_user, autoTaggingQueue, autotag, empty, indexing, medialink), 0);
OneTimeBusyThread.startFromRunnable(DMOZImporter, 0);
OneTimeBusyThread.startFromRunnable(DMOZImporter.getConsumer(sb, bmk_user, autoTaggingQueue, autotag, empty, indexing, medialink), 0);
prop.put("status", "1");
} catch (final Exception e) {
@@ -8,7 +8,7 @@
import net.yacy.repository.FilterEngine;
import net.yacy.search.Switchboard;
public class ContentControlFilterUpdateThread {
public class ContentControlFilterUpdateThread implements Runnable {
private final Switchboard sb;
@@ -22,6 +22,7 @@ public ContentControlFilterUpdateThread(final Switchboard sb) {
}
@Override
public final void run() {
if (!this.locked) {
@@ -13,7 +13,7 @@
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.search.Switchboard;
public class SMWListSyncThread {
public class SMWListSyncThread implements Runnable {
private final Switchboard sb;
private Boolean locked = false;
@@ -55,6 +55,7 @@ private final String wikiurlify (String s) {
return ret;
}
@Override
public final void run() {
if (!this.locked) {
@@ -24,103 +24,50 @@
package net.yacy.kelondro.workflow;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.TreeMap;
import net.yacy.cora.util.ConcurrentLog;
public final class InstantBusyThread extends AbstractBusyThread implements BusyThread {
public abstract class InstantBusyThread extends AbstractBusyThread implements BusyThread {
private Method jobExecMethod, jobCountMethod, freememExecMethod;
private final Object environment;
private final Long handle;
private static final TreeMap<Long, String> jobs = new TreeMap<Long, String>();
public InstantBusyThread(
final Object env,
final String jobExec,
final String jobCount,
final String freemem,
final long idleSleep,
final long busySleep) {
/**
* @param idleSleep defines min idle sleep time that can be set via setIdleSleep()
* @param busySleep defines min busy sleep time that can be set via setBusySleep()
*/
public InstantBusyThread(final long idleSleep, final long busySleep) {
this("InstantBusyThread.job", idleSleep, busySleep);
}
/**
* @param jobName the job name used to monitor the thread
* @param idleSleep defines min idle sleep time that can be set via setIdleSleep()
* @param busySleep defines min busy sleep time that can be set via setBusySleep()
*/
public InstantBusyThread(final String jobName, final long idleSleep, final long busySleep) {
super(idleSleep, busySleep);
// jobExec is the name of a method of the object 'env' that executes the one-step-run
// jobCount is the name of a method that returns the size of the job
// freemem is the name of a method that tries to free memory and returns void
final Class<?> theClass = (env instanceof Class<?>) ? (Class<?>) env : env.getClass();
try {
this.jobExecMethod = theClass.getMethod(jobExec);
} catch (final NoSuchMethodException e) {
throw new RuntimeException("serverInstantThread, wrong declaration of jobExec: " + e.getMessage());
}
try {
if (jobCount == null)
this.jobCountMethod = null;
else
this.jobCountMethod = theClass.getMethod(jobCount);
} catch (final NoSuchMethodException e) {
throw new RuntimeException("serverInstantThread, wrong declaration of jobCount: " + e.getMessage());
}
try {
if (freemem == null)
this.freememExecMethod = null;
else
this.freememExecMethod = theClass.getMethod(freemem);
} catch (final NoSuchMethodException e) {
throw new RuntimeException("serverInstantThread, wrong declaration of freemem: " + e.getMessage());
}
this.environment = (env instanceof Class<?>) ? null : env;
setName("BusyThread " + theClass.getName() + "." + jobExec);
setName("BusyThread " + jobName);
this.handle = Long.valueOf(System.currentTimeMillis() + getName().hashCode());
}
@Override
public int getJobCount() {
if (this.jobCountMethod == null) return Integer.MAX_VALUE;
try {
final Object result = this.jobCountMethod.invoke(this.environment);
return (result instanceof Integer) ? ((Integer) result).intValue() : -1;
} catch (final IllegalAccessException e) {
return -1;
} catch (final IllegalArgumentException e) {
return -1;
} catch (final InvocationTargetException e) {
ConcurrentLog.severe("BUSYTHREAD", "invocation serverInstantThread of thread '" + getName() + "': " + e.getMessage(), e);
return -1;
}
}
@Override
public boolean job() throws Exception {
//System.out.println("started job " + this.handle + ": " + this.getName());
synchronized(jobs) {jobs.put(this.handle, getName());}
boolean jobHasDoneSomething = false;
try {
final Object result = this.jobExecMethod.invoke(this.environment);
if (result == null) jobHasDoneSomething = true;
else if (result instanceof Boolean) jobHasDoneSomething = ((Boolean) result).booleanValue();
} catch (final IllegalAccessException e) {
ConcurrentLog.severe("BUSYTHREAD", "Internal Error in serverInstantThread.job: " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "shutting down thread '" + getName() + "'");
terminate(false);
jobHasDoneSomething = jobImpl();
} catch (final IllegalArgumentException e) {
ConcurrentLog.severe("BUSYTHREAD", "Internal Error in serverInstantThread.job: " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "Internal Error in InstantBusyThread.job: " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "shutting down thread '" + getName() + "'");
terminate(false);
} catch (final InvocationTargetException e) {
final String targetException = e.getTargetException().getMessage();
ConcurrentLog.logException(e);
ConcurrentLog.logException(e.getCause());
ConcurrentLog.logException(e.getTargetException());
ConcurrentLog.severe("BUSYTHREAD", "Runtime Error in serverInstantThread.job, thread '" + getName() + "': " + e.getMessage() + "; target exception: " + targetException, e.getTargetException());
} catch (final OutOfMemoryError e) {
ConcurrentLog.severe("BUSYTHREAD", "OutOfMemory Error in serverInstantThread.job, thread '" + getName() + "': " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "OutOfMemory Error in InstantBusyThread.job, thread '" + getName() + "': " + e.getMessage());
ConcurrentLog.logException(e);
freemem();
} catch (final Exception e) {
@@ -130,46 +77,36 @@ public boolean job() throws Exception {
synchronized(jobs) {jobs.remove(this.handle);}
return jobHasDoneSomething;
}
/**
* The job's main logic implementation
* @return true if it has done something, false if it is idle and does not expect to work on more for a longer time
* @throws Exception when an unexpected error occurred
*/
public abstract boolean jobImpl() throws Exception;
@Override
public void freemem() {
if (this.freememExecMethod == null) return;
try {
this.freememExecMethod.invoke(this.environment);
} catch (final IllegalAccessException e) {
ConcurrentLog.severe("BUSYTHREAD", "Internal Error in serverInstantThread.freemem: " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "shutting down thread '" + getName() + "'");
terminate(false);
} catch (final IllegalArgumentException e) {
ConcurrentLog.severe("BUSYTHREAD", "Internal Error in serverInstantThread.freemem: " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "shutting down thread '" + getName() + "'");
terminate(false);
} catch (final InvocationTargetException e) {
final String targetException = e.getTargetException().getMessage();
if (targetException.indexOf("heap space",0) > 0) ConcurrentLog.logException(e.getTargetException());
ConcurrentLog.severe("BUSYTHREAD", "Runtime Error in serverInstantThread.freemem, thread '" + getName() + "': " + e.getMessage() + "; target exception: " + targetException, e.getTargetException());
ConcurrentLog.logException(e.getTargetException());
freememImpl();
} catch (final OutOfMemoryError e) {
ConcurrentLog.severe("BUSYTHREAD", "OutOfMemory Error in serverInstantThread.freemem, thread '" + getName() + "': " + e.getMessage());
ConcurrentLog.severe("BUSYTHREAD", "OutOfMemory Error in InstantBusyThread.freemem, thread '" + getName() + "': " + e.getMessage());
ConcurrentLog.logException(e);
}
}
@Override
public int getJobCount() {
return Integer.MAX_VALUE;
}
/**
* Called when an outOfMemoryCycle is performed.
*/
public void freememImpl() {
// Do nothing in this implementation, please override
}
public static BusyThread oneTimeJob(final Object env, final String jobExec, final long startupDelay) {
// start the job and execute it once as background process
final BusyThread thread = new InstantBusyThread(env, jobExec, null, null, Long.MIN_VALUE, Long.MIN_VALUE);
thread.setStartupSleep(startupDelay);
thread.setIdleSleep(-1);
thread.setBusySleep(-1);
thread.setMemPreReqisite(0);
thread.setLoadPreReqisite(Double.MAX_VALUE); // this is called during initialization phase and some code parts depend on it; therefore we cannot set a prerequisite that prevents the start of that thread
thread.start();
return thread;
}
public static WorkflowThread oneTimeJob(final Runnable thread, final long startupDelay) {
return oneTimeJob(thread, "run", startupDelay);
}
@Override
public void open() {
@@ -180,5 +117,35 @@ public void open() {
public synchronized void close() {
// Not implemented in this thread
}
/**
* Construct an InstantBusyThread instance from a runnable task.
*
* @param task
* the task to run as a job
* @param idleSleep
* defines min idle sleep time that can be set via setIdleSleep()
* @param busySleep
* defines min busy sleep time that can be set via setBusySleep()
* @return a InstantBusyThread instance
* @throws IllegalArgumentException
* when the task is null
*/
public static InstantBusyThread createFromRunnable(final Runnable task, final long idleSleep,
final long busySleep) {
if (task == null) {
throw new IllegalArgumentException("Runnable task must not be null");
}
InstantBusyThread busyThread = new InstantBusyThread(task.getClass().getName() + ".run", idleSleep, busySleep) {
@Override
public boolean jobImpl() throws Exception {
task.run();
return true;
}
};
return busyThread;
}
}
Oops, something went wrong.

0 comments on commit 4eba88f

Please sign in to comment.