Archiviert
13
0

Add the error reporter to the background compiler.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-11-20 00:26:16 +01:00
Ursprung d5aa1cde51
Commit f8af92eb5b
3 geänderte Dateien mit 62 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -90,13 +90,14 @@ public class ProtocolLibrary extends JavaPlugin {
logger = getLoggerSafely(); logger = getLoggerSafely();
// Add global parameters // Add global parameters
DetailedErrorReporter reporter = new DetailedErrorReporter(this); DetailedErrorReporter detailedReporter = new DetailedErrorReporter(this);
updater = new Updater(this, logger, "protocollib", getFile(), "protocol.info"); updater = new Updater(this, logger, "protocollib", getFile(), "protocol.info");
reporter = detailedReporter;
try { try {
config = new ProtocolConfig(this); config = new ProtocolConfig(this);
} catch (Exception e) { } catch (Exception e) {
reporter.reportWarning(this, "Cannot load configuration", e); detailedReporter.reportWarning(this, "Cannot load configuration", e);
// Load it again // Load it again
deleteConfig(); deleteConfig();
@ -105,18 +106,18 @@ public class ProtocolLibrary extends JavaPlugin {
try { try {
unhookTask = new DelayedSingleTask(this); unhookTask = new DelayedSingleTask(this);
protocolManager = new PacketFilterManager(getClassLoader(), getServer(), unhookTask, reporter); protocolManager = new PacketFilterManager(getClassLoader(), getServer(), unhookTask, detailedReporter);
reporter.addGlobalParameter("manager", protocolManager); detailedReporter.addGlobalParameter("manager", protocolManager);
// Initialize command handlers // Initialize command handlers
commandProtocol = new CommandProtocol(reporter, this, updater, config); commandProtocol = new CommandProtocol(detailedReporter, this, updater, config);
commandPacket = new CommandPacket(reporter, this, logger, protocolManager); commandPacket = new CommandPacket(detailedReporter, this, logger, protocolManager);
// Send logging information to player listeners too // Send logging information to player listeners too
broadcastUsers(PERMISSION_INFO); broadcastUsers(PERMISSION_INFO);
} catch (Throwable e) { } catch (Throwable e) {
reporter.reportDetailed(this, "Cannot load ProtocolLib.", e, protocolManager); detailedReporter.reportDetailed(this, "Cannot load ProtocolLib.", e, protocolManager);
disablePlugin(); disablePlugin();
} }
} }
@ -167,8 +168,12 @@ public class ProtocolLibrary extends JavaPlugin {
// Initialize background compiler // Initialize background compiler
if (backgroundCompiler == null && config.isBackgroundCompilerEnabled()) { if (backgroundCompiler == null && config.isBackgroundCompilerEnabled()) {
backgroundCompiler = new BackgroundCompiler(getClassLoader()); backgroundCompiler = new BackgroundCompiler(getClassLoader(), reporter);
BackgroundCompiler.setInstance(backgroundCompiler); BackgroundCompiler.setInstance(backgroundCompiler);
logger.info("Started structure compiler thread.");
} else {
logger.info("Structure compiler thread has been disabled.");
} }
// Set up command handlers // Set up command handlers

Datei anzeigen

@ -170,7 +170,7 @@ public class DetailedErrorReporter implements ErrorReporter {
if (pluginReference.get() != null) { if (pluginReference.get() != null) {
Plugin plugin = pluginReference.get(); Plugin plugin = pluginReference.get();
writer.println("Version:"); writer.println("Version:");
writer.println(addPrefix(plugin.toString() + "-" + plugin.getDescription().getVersion(), SECOND_LEVEL_PREFIX)); writer.println(addPrefix(plugin.toString(), SECOND_LEVEL_PREFIX));
} }
// Add the server version too // Add the server version too

Datei anzeigen

@ -22,11 +22,16 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable;
import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/** /**
* Compiles structure modifiers on a background thread. * Compiles structure modifiers on a background thread.
@ -37,6 +42,11 @@ import com.comphenix.protocol.reflect.StructureModifier;
*/ */
public class BackgroundCompiler { public class BackgroundCompiler {
/**
* The default format for the name of new worker threads.
*/
public static final String THREAD_FORMAT = "ProtocolLib-StructureCompiler %s";
// How long to wait for a shutdown // How long to wait for a shutdown
public static final int SHUTDOWN_DELAY_MS = 2000; public static final int SHUTDOWN_DELAY_MS = 2000;
@ -48,6 +58,7 @@ public class BackgroundCompiler {
private boolean shuttingDown; private boolean shuttingDown;
private ExecutorService executor; private ExecutorService executor;
private ErrorReporter reporter;
/** /**
* Retrieves the current background compiler. * Retrieves the current background compiler.
@ -67,24 +78,38 @@ public class BackgroundCompiler {
/** /**
* Initialize a background compiler. * Initialize a background compiler.
* <p>
* Uses the default {@link #THREAD_FORMAT} to name worker threads.
* @param loader - class loader from Bukkit. * @param loader - class loader from Bukkit.
* @param reporter - current error reporter.
*/ */
public BackgroundCompiler(ClassLoader loader) { public BackgroundCompiler(ClassLoader loader, ErrorReporter reporter) {
this(loader, Executors.newSingleThreadExecutor()); ThreadFactory factory = new ThreadFactoryBuilder().
setDaemon(true).
setNameFormat(THREAD_FORMAT).
build();
initializeCompiler(loader, reporter, Executors.newSingleThreadExecutor(factory));
} }
/** /**
* Initialize a background compiler utilizing the given thread pool. * Initialize a background compiler utilizing the given thread pool.
* @param loader - class loader from Bukkit. * @param loader - class loader from Bukkit.
* @param reporter - current error reporter.
* @param executor - thread pool we'll use. * @param executor - thread pool we'll use.
*/ */
public BackgroundCompiler(ClassLoader loader, ExecutorService executor) { public BackgroundCompiler(ClassLoader loader, ErrorReporter reporter, ExecutorService executor) {
initializeCompiler(loader, reporter, executor);
}
// Avoid "Constructor call must be the first statement".
private void initializeCompiler(ClassLoader loader, @Nullable ErrorReporter reporter, ExecutorService executor) {
if (loader == null) if (loader == null)
throw new IllegalArgumentException("loader cannot be NULL"); throw new IllegalArgumentException("loader cannot be NULL");
if (executor == null) if (executor == null)
throw new IllegalArgumentException("executor cannot be NULL"); throw new IllegalArgumentException("executor cannot be NULL");
this.compiler = new StructureCompiler(loader); this.compiler = new StructureCompiler(loader);
this.reporter = reporter;
this.executor = executor; this.executor = executor;
this.enabled = true; this.enabled = true;
} }
@ -129,15 +154,30 @@ public class BackgroundCompiler {
executor.submit(new Callable<Object>() { executor.submit(new Callable<Object>() {
@Override @Override
public Object call() throws Exception { public Object call() throws Exception {
StructureModifier<TKey> modifier = uncompiled; StructureModifier<TKey> modifier = uncompiled;
// Do our compilation // Do our compilation
try {
modifier = compiler.compile(modifier); modifier = compiler.compile(modifier);
listener.onCompiled(modifier); listener.onCompiled(modifier);
} catch (Throwable e) {
// Disable future compilations!
setEnabled(false);
// Inform about this error as best as we can
if (reporter != null) {
reporter.reportDetailed(BackgroundCompiler.this,
"Cannot compile structure. Disabing compiler.", e, uncompiled);
} else {
System.err.println("Exception occured in structure compiler: ");
e.printStackTrace();
}
}
// We'll also return the new structure modifier // We'll also return the new structure modifier
return modifier; return modifier;
} }
}); });
} catch (RejectedExecutionException e) { } catch (RejectedExecutionException e) {