📄 pluginlauncherimpl.java
字号:
/*
* Created on 25-Jul-2005
* Created by Paul Gardner
* Copyright (C) 2005, 2006 Aelitis, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package org.gudy.azureus2.pluginsimpl.local.launch;
import java.io.*;
import java.net.*;
import java.util.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.util.Constants;
import org.gudy.azureus2.core3.util.SystemProperties;
import org.gudy.azureus2.plugins.LaunchablePlugin;
import org.gudy.azureus2.plugins.Plugin;
import org.gudy.azureus2.plugins.logging.LoggerChannel;
import org.gudy.azureus2.plugins.logging.LoggerChannelListener;
import org.gudy.azureus2.pluginsimpl.PluginUtils;
import com.aelitis.azureus.core.AzureusCore;
import com.aelitis.azureus.core.AzureusCoreFactory;
public class
PluginLauncherImpl
{
private static Map preloaded_plugins = new HashMap();
public static void
launch(
String[] args )
{
// This *has* to be done first as it sets system properties that are read and cached by Java
COConfigurationManager.preInitialise();
// try and infer the application name. this is only required on OSX as the app name
// is a component of the "application path" used to find plugins etc.
if ( Constants.isOSX ){
/* example class path
/Applications/Utilities/Azureus.app/Contents/Resources/
Java/swt.jar:/Applications/Utilities/Azureus.app/Contents/Resources/
Java/swt-pi.jar:/Applications/Utilities/Azureus.app/Contents/Resources/
Java/Azureus2.jar:/System/Library/Java
*/
String classpath = System.getProperty("java.class.path");
if ( classpath == null ){
System.out.println( "classpath is null!!!!" );
}else{
int dot_pos = classpath.indexOf( ".app/Contents" );
if ( dot_pos == -1 ){
// System.out.println( "can't find .app/Contents" );
}else{
int start_pos = dot_pos;
while( start_pos >= 0 && classpath.charAt(start_pos) != '/' ){
start_pos--;
}
String app_name = classpath.substring( start_pos+1, dot_pos );
SystemProperties.setApplicationName( app_name );
}
}
}
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 );
}
}
private static LaunchablePlugin[]
findLaunchablePlugins(
LoggerChannelListener listener )
{
// CAREFUL - this is called BEFORE any AZ initialisation has been performed and must
// therefore NOT use anything that relies on this (such as logging, debug....)
List res = new ArrayList();
File app_dir = getApplicationFile("plugins");
if ( !( app_dir.exists()) && app_dir.isDirectory()){
listener.messageLogged( LoggerChannel.LT_ERROR, "Application dir '" + app_dir + "' not found" );
return( new LaunchablePlugin[0] );
}
File[] plugins = app_dir.listFiles();
if ( plugins == null || plugins.length == 0 ){
listener.messageLogged( LoggerChannel.LT_ERROR, "Application dir '" + app_dir + "' empty" );
return( new LaunchablePlugin[0] );
}
for ( int i=0;i<plugins.length;i++ ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -