return;
// This *has* to be done first as it sets system properties that are read and cached by Java
COConfigurationManager.preInitialise();
final LoggerChannelListener listener =
new LoggerChannelListener()
{
public void
messageLogged(
int type,
String content )
{
log( content, false );
}
public void
messageLogged(
String str,
Throwable error )
{
log( str, true );
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter( sw );
error.printStackTrace( pw );
pw.flush();
log( sw.toString(), true );
}
protected synchronized void
log(
String str,
boolean stdout )
{
File log_file = getApplicationFile("launch.log");
PrintWriter pw = null;
try{
pw = new PrintWriter(new FileWriter( log_file, true ));
if ( str.endsWith( "\n" )){
if ( stdout ){
System.err.print( "PluginLauncher: " + str );
}
pw.print( str );
}else{
if ( stdout ){
System.err.println( "PluginLauncher: " + str );
}
pw.println( str );
}
}catch( Throwable e ){
}finally{
if ( pw != null ){
pw.close();
}
}
}
};
LaunchablePlugin[] launchables = findLaunchablePlugins(listener);
if ( launchables.length == 0 ){
listener.messageLogged( LoggerChannel.LT_ERROR, "No launchable plugins found" );
return;
}else if ( launchables.length > 1 ){
listener.messageLogged( LoggerChannel.LT_ERROR, "Multiple launchable plugins found, running first" );
}
try{
// set default details for restarter
SystemProperties.setApplicationEntryPoint( "org.gudy.azureus2.plugins.PluginLauncher" );
launchables[0].setDefaults( args );
// see if we're a secondary instance
if ( PluginSingleInstanceHandler.process( listener, args )){
return;
}
// we have to run the core startup on a separate thread and then effectively pass "this thread"
// through to the launchable "process" method
Thread core_thread =
new Thread( "PluginLauncher" )
{
public void
run()
{
try{
// give 'process' call below some time to start up
Thread.sleep(500);
AzureusCore azureus_core = AzureusCoreFactory.create();
azureus_core.start();
}catch( Throwable e ){
listener.messageLogged( "PluginLauncher: launch fails", e );
}
}
};
core_thread.setDaemon( true );
core_thread.start();
boolean restart = false;
boolean process_succeeded = false;
try{
restart = launchables[0].process();
process_succeeded = true;
}finally{
try{
if ( restart ){
AzureusCoreFactory.getSingleton().restart();
}else{
AzureusCoreFactory.getSingleton().stop();
}
}catch( Throwable e ){
// only report this exception if we're not already failing
if ( process_succeeded ){
throw( e );
}
}
}
}catch( Throwable e ){
listener.messageLogged( "PluginLauncher: launch fails", e );
}
}