use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class PluginInitializer method loadPlugins.
public List loadPlugins(Core core, boolean bSkipAlreadyLoaded, boolean load_external_plugins, boolean loading_for_startup, boolean initialise_plugins) {
if (bSkipAlreadyLoaded) {
// discard any failed ones
List pis;
synchronized (s_plugin_interfaces) {
pis = new ArrayList(s_plugin_interfaces);
}
for (int i = 0; i < pis.size(); i++) {
PluginInterfaceImpl pi = (PluginInterfaceImpl) pis.get(i);
Plugin p = pi.getPlugin();
if (p instanceof FailedPlugin) {
unloadPlugin(pi);
}
}
}
List pluginLoaded = new ArrayList();
PluginManagerImpl.setStartDetails(core);
getRootClassLoader();
// first do explicit plugins
File user_dir = FileUtil.getUserFile("plugins");
File app_dir = FileUtil.getApplicationFile("plugins");
int user_plugins = 0;
int app_plugins = 0;
if (user_dir.exists() && user_dir.isDirectory()) {
user_plugins = user_dir.listFiles().length;
}
if (app_dir.exists() && app_dir.isDirectory()) {
app_plugins = app_dir.listFiles().length;
}
if (load_external_plugins) {
pluginLoaded.addAll(loadPluginsFromDir(user_dir, 0, user_plugins + app_plugins, bSkipAlreadyLoaded, loading_for_startup, initialise_plugins));
if (!user_dir.equals(app_dir)) {
pluginLoaded.addAll(loadPluginsFromDir(app_dir, user_plugins, user_plugins + app_plugins, bSkipAlreadyLoaded, loading_for_startup, initialise_plugins));
}
} else {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "Loading of external plugins skipped"));
}
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Loading built-in plugins"));
PluginManagerDefaults def = PluginManager.getDefaults();
for (int i = 0; i < builtin_plugins.length; i++) {
if (def.isDefaultPluginEnabled(builtin_plugins[i][0])) {
try {
loading_builtin = true;
// lazyness here, for builtin we use static load method with default plugin interface
// if we need to improve on this then we'll have to move to a system more akin to
// the dir-loaded plugins
Class cla = root_class_loader.loadClass(builtin_plugins[i][1]);
Method load_method = cla.getMethod("load", new Class[] { PluginInterface.class });
load_method.invoke(null, new Object[] { getDefaultInterfaceSupport() });
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Built-in plugin '" + builtin_plugins[i][0] + "' ok"));
} catch (NoSuchMethodException e) {
} catch (Throwable e) {
if (builtin_plugins[i][4].equalsIgnoreCase("true")) {
Debug.printStackTrace(e);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Load of built in plugin '" + builtin_plugins[i][2] + "' fails", e));
}
} finally {
loading_builtin = false;
}
} else {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Built-in plugin '" + builtin_plugins[i][2] + "' is disabled"));
}
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Loading dynamically registered plugins"));
for (int i = 0; i < registration_queue.size(); i++) {
Object entry = registration_queue.get(i);
Class cla;
String id;
if (entry instanceof Class) {
cla = (Class) entry;
id = cla.getName();
} else {
Object[] x = (Object[]) entry;
Plugin plugin = (Plugin) x[0];
cla = plugin.getClass();
id = (String) x[1];
}
try {
// lazyness here, for dynamic we use static load method with default plugin interface
// if we need to improve on this then we'll have to move to a system more akin to
// the dir-loaded plugins
Method load_method = cla.getMethod("load", new Class[] { PluginInterface.class });
load_method.invoke(null, new Object[] { getDefaultInterfaceSupport() });
} catch (NoSuchMethodException e) {
} catch (Throwable e) {
Debug.printStackTrace(e);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Load of dynamic plugin '" + id + "' fails", e));
}
}
return pluginLoaded;
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class PluginInitializer method checkJDKVersion.
public static void checkJDKVersion(String name, Properties props, boolean alert_on_fail) throws PluginException {
String required_jdk = (String) props.get("plugin.jdk.min_version");
if (required_jdk != null) {
String actual_jdk = Constants.JAVA_VERSION;
required_jdk = normaliseJDK(required_jdk);
actual_jdk = normaliseJDK(actual_jdk);
if (required_jdk.length() == 0 || actual_jdk.length() == 0) {
return;
}
if (Constants.compareVersions(actual_jdk, required_jdk) < 0) {
String msg = "Plugin " + (name.length() > 0 ? (name + " ") : "") + "requires Java version " + required_jdk + " or higher";
if (alert_on_fail) {
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR, msg));
}
throw (new PluginException(msg));
}
}
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class PluginInitializer method initialisePlugins.
public void initialisePlugins() {
try {
addInitThread();
final LinkedList<Runnable> initQueue = new LinkedList<>();
for (int i = 0; i < loaded_pi_list.size(); i++) {
final int idx = i;
initQueue.add(new Runnable() {
@Override
public void run() {
try {
List l = (List) loaded_pi_list.get(idx);
if (l.size() > 0) {
PluginInterfaceImpl plugin_interface = (PluginInterfaceImpl) l.get(0);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Initializing plugin '" + plugin_interface.getPluginName() + "'"));
initialisePlugin(l);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Initialization of plugin '" + plugin_interface.getPluginName() + "' complete"));
}
} catch (Throwable e) {
// already handled
}
// some plugins try and steal the logger stdout redirects.
// re-establish them if needed
Logger.doRedirects();
}
});
}
// now do built in ones
initQueue.add(new Runnable() {
@Override
public void run() {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Initializing built-in plugins"));
}
});
final PluginManagerDefaults def = PluginManager.getDefaults();
for (int i = 0; i < builtin_plugins.length; i++) {
final int idx = i;
initQueue.add(new Runnable() {
@Override
public void run() {
if (def.isDefaultPluginEnabled(builtin_plugins[idx][0])) {
String id = builtin_plugins[idx][2];
String key = builtin_plugins[idx][3];
try {
Class cla = root_class_loader.loadClass(builtin_plugins[idx][1]);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Initializing built-in plugin '" + builtin_plugins[idx][2] + "'"));
initializePluginFromClass(cla, id, key, "true".equals(builtin_plugins[idx][5]), true, true);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Initialization of built in plugin '" + builtin_plugins[idx][2] + "' complete"));
} catch (Throwable e) {
try {
// replace it with a "broken" plugin instance
initializePluginFromClass(FailedPlugin.class, id, key, false, false, true);
} catch (Throwable f) {
}
if (builtin_plugins[idx][4].equalsIgnoreCase("true")) {
Debug.printStackTrace(e);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Initialization of built in plugin '" + builtin_plugins[idx][2] + "' fails", e));
}
}
} else {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Built-in plugin '" + builtin_plugins[idx][2] + "' is disabled"));
}
}
});
}
initQueue.add(new Runnable() {
@Override
public void run() {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Initializing dynamically registered plugins"));
}
});
for (int i = 0; i < registration_queue.size(); i++) {
final int idx = i;
initQueue.add(new Runnable() {
@Override
public void run() {
try {
Object entry = registration_queue.get(idx);
if (entry instanceof Class) {
Class cla = (Class) entry;
singleton.initializePluginFromClass(cla, INTERNAL_PLUGIN_ID, cla.getName(), false, true, true);
} else {
Object[] x = (Object[]) entry;
Plugin plugin = (Plugin) x[0];
singleton.initializePluginFromInstance(plugin, (String) x[1], (String) x[2]);
}
} catch (PluginException e) {
}
}
});
}
AEThread2 secondaryInitializer = new AEThread2("2nd PluginInitializer Thread", true) {
@Override
public void run() {
try {
addInitThread();
while (true) {
Runnable toRun;
synchronized (initQueue) {
if (initQueue.isEmpty()) {
break;
}
toRun = (Runnable) initQueue.remove(0);
}
try {
toRun.run();
} catch (Throwable e) {
Debug.out(e);
}
}
} finally {
removeInitThread();
}
}
};
secondaryInitializer.start();
while (true) {
Runnable toRun;
synchronized (initQueue) {
if (initQueue.isEmpty()) {
break;
}
toRun = (Runnable) initQueue.remove(0);
}
try {
toRun.run();
} catch (Throwable e) {
Debug.out(e);
}
}
secondaryInitializer.join();
registration_queue.clear();
plugins_initialised = true;
fireEvent(PluginEvent.PEV_ALL_PLUGINS_INITIALISED);
} finally {
removeInitThread();
}
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class LoggerChannelImpl method logAlert.
// Alert Functions
// ===============
protected void logAlert(int alert_type, String message, boolean repeatable) {
// output as log message to any listeners
for (int i = 0; i < listeners.size(); i++) {
try {
((LoggerChannelListener) listeners.get(i)).messageLogged(alert_type, addTimeStamp(message));
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
if (!no_output) {
int at;
switch(alert_type) {
case LoggerChannel.LT_INFORMATION:
{
at = LogAlert.AT_INFORMATION;
break;
}
case LoggerChannel.LT_WARNING:
{
at = LogAlert.AT_WARNING;
break;
}
default:
{
at = LogAlert.AT_ERROR;
break;
}
}
com.biglybt.core.logging.Logger.log(new LogAlert(repeatable, at, message));
}
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class LoggerImpl method addAlertListener.
@Override
public void addAlertListener(final LoggerAlertListener listener) {
ILogAlertListener lg_listener = new ILogAlertListener() {
@Override
public void alertRaised(LogAlert alert) {
if (alert.err == null) {
int type;
if (alert.entryType == LogAlert.AT_INFORMATION) {
type = LoggerChannel.LT_INFORMATION;
} else if (alert.entryType == LogAlert.AT_WARNING) {
type = LoggerChannel.LT_WARNING;
} else {
type = LoggerChannel.LT_ERROR;
}
listener.alertLogged(type, alert.text, alert.repeatable);
} else
listener.alertLogged(alert.text, alert.err, alert.repeatable);
}
};
alert_listeners_map.put(listener, lg_listener);
com.biglybt.core.logging.Logger.addListener(lg_listener);
}
Aggregations