webplugin.java
来自「Azureus is a powerful, full-featured, cr」· Java 代码 · 共 700 行 · 第 1/2 页
JAVA
700 行
/*
* File : WebPlugin.java
* Created : 23-Jan-2004
* By : parg
*
* Azureus - a Java Bittorrent client
*
* 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.
*
* 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 ( see the LICENSE file ).
*
* 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
*/
package org.gudy.azureus2.ui.webplugin;
/**
* @author parg
*
*/
import java.io.*;
import java.util.*;
import java.net.*;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.plugins.*;
import org.gudy.azureus2.plugins.logging.*;
import org.gudy.azureus2.plugins.ipfilter.*;
import org.gudy.azureus2.plugins.tracker.*;
import org.gudy.azureus2.plugins.tracker.web.*;
import org.gudy.azureus2.plugins.ui.*;
import org.gudy.azureus2.plugins.ui.config.*;
import org.gudy.azureus2.plugins.ui.model.*;
import com.aelitis.azureus.plugins.upnp.UPnPPlugin;
public class
WebPlugin
implements Plugin, TrackerWebPageGenerator
{
public static final String PROPERTIES_MIGRATED = "Properties Migrated";
public static final String CONFIG_MIGRATED = "Config Migrated";
public static final String CONFIG_PASSWORD_ENABLE = "Password Enable";
public final boolean CONFIG_PASSWORD_ENABLE_DEFAULT = false;
public static final String CONFIG_USER = "User";
public final String CONFIG_USER_DEFAULT = "";
public static final String CONFIG_PASSWORD = "Password";
public final byte[] CONFIG_PASSWORD_DEFAULT = {};
public static final String CONFIG_PORT = "Port";
public int CONFIG_PORT_DEFAULT = 8089;
public static final String CONFIG_PROTOCOL = "Protocol";
public final String CONFIG_PROTOCOL_DEFAULT = "HTTP";
public static final String CONFIG_UPNP_ENABLE = "UPnP Enable";
public final boolean CONFIG_UPNP_ENABLE_DEFAULT = true;
public static final String CONFIG_HOME_PAGE = "Home Page";
public final String CONFIG_HOME_PAGE_DEFAULT = "index.html";
public static final String CONFIG_ROOT_DIR = "Root Dir";
public final String CONFIG_ROOT_DIR_DEFAULT = "";
public static final String CONFIG_ROOT_RESOURCE = "Root Resource";
public String CONFIG_ROOT_RESOURCE_DEFAULT = "";
public static final String CONFIG_MODE = "Mode";
public static final String CONFIG_MODE_FULL = "full";
public final String CONFIG_MODE_DEFAULT = CONFIG_MODE_FULL;
public static final String CONFIG_ACCESS = "Access";
public final String CONFIG_ACCESS_DEFAULT = "all";
protected static final String NL = "\r\n";
protected static final String[] welcome_pages = {"index.html", "index.htm", "index.php", "index.tmpl" };
protected static File[] welcome_files;
protected PluginInterface plugin_interface;
protected LoggerChannel log;
protected Tracker tracker;
protected BasicPluginConfigModel config_model;
protected String home_page;
protected String file_root;
protected String resource_root;
protected boolean ip_range_all = false;
protected IPRange ip_range;
public
WebPlugin()
{
}
public
WebPlugin(
Properties defaults )
{
Integer i = (Integer)defaults.get(CONFIG_PORT);
if ( i != null ){
CONFIG_PORT_DEFAULT = i.intValue();
}
String s = (String)defaults.get( CONFIG_ROOT_RESOURCE );
if( s != null ){
CONFIG_ROOT_RESOURCE_DEFAULT = s;
}
}
public void
initialize(
PluginInterface _plugin_interface )
throws PluginException
{
plugin_interface = _plugin_interface;
log = plugin_interface.getLogger().getChannel("WebPlugin");
UIManager ui_manager = plugin_interface.getUIManager();
final BasicPluginViewModel model = ui_manager.createBasicPluginViewModel( plugin_interface.getPluginName());
model.getStatus().setText( "Running" );
model.getActivity().setVisible( false );
model.getProgress().setVisible( false );
log.addListener(
new LoggerChannelListener()
{
public void
messageLogged(
int type,
String message )
{
model.getLogArea().appendText( message+"\n");
}
public void
messageLogged(
String str,
Throwable error )
{
model.getLogArea().appendText( error.toString()+"\n");
}
});
PluginConfig plugin_config = plugin_interface.getPluginconfig();
config_model = ui_manager.createBasicPluginConfigModel( "plugins", "plugins." + plugin_interface.getPluginID());
boolean save_needed = false;
if ( !plugin_config.getPluginBooleanParameter( CONFIG_MIGRATED, false )){
plugin_config.setPluginParameter( CONFIG_MIGRATED, true );
save_needed = true;
plugin_config.setPluginParameter(
CONFIG_PASSWORD_ENABLE,
plugin_config.getBooleanParameter(
"Tracker Password Enable Web", CONFIG_PASSWORD_ENABLE_DEFAULT ));
plugin_config.setPluginParameter(
CONFIG_USER,
plugin_config.getStringParameter(
"Tracker Username", CONFIG_USER_DEFAULT ));
plugin_config.setPluginParameter(
CONFIG_PASSWORD,
plugin_config.getByteParameter(
"Tracker Password", CONFIG_PASSWORD_DEFAULT ));
}
if ( !plugin_config.getPluginBooleanParameter( PROPERTIES_MIGRATED, false )){
plugin_config.setPluginParameter( PROPERTIES_MIGRATED, true );
Properties props = plugin_interface.getPluginProperties();
// make sure we've got an old properties file too
if ( props.getProperty( "port", "" ).length() > 0 ){
save_needed = true;
String prop_port = props.getProperty( "port", ""+CONFIG_PORT_DEFAULT );
String prop_protocol = props.getProperty( "protocol", CONFIG_PROTOCOL_DEFAULT );
String prop_home = props.getProperty( "homepage", CONFIG_HOME_PAGE_DEFAULT );
String prop_rootdir = props.getProperty( "rootdir", CONFIG_ROOT_DIR_DEFAULT );
String prop_rootres = props.getProperty( "rootresource", CONFIG_ROOT_RESOURCE_DEFAULT );
String prop_mode = props.getProperty( "mode", CONFIG_MODE_DEFAULT );
String prop_access = props.getProperty( "access", CONFIG_ACCESS_DEFAULT );
int prop_port_int = CONFIG_PORT_DEFAULT;
try{
prop_port_int = Integer.parseInt( prop_port );
}catch( Throwable e ){
}
plugin_config.setPluginParameter(CONFIG_PORT, prop_port_int );
plugin_config.setPluginParameter(CONFIG_PROTOCOL, prop_protocol );
plugin_config.setPluginParameter(CONFIG_HOME_PAGE, prop_home );
plugin_config.setPluginParameter(CONFIG_ROOT_DIR, prop_rootdir );
plugin_config.setPluginParameter(CONFIG_ROOT_RESOURCE, prop_rootres );
plugin_config.setPluginParameter(CONFIG_MODE, prop_mode );
plugin_config.setPluginParameter(CONFIG_ACCESS, prop_access );
File props_file = new File( plugin_interface.getPluginDirectoryName(), "plugin.properties" );
PrintWriter pw = null;
try{
File backup = new File( plugin_interface.getPluginDirectoryName(), "plugin.properties.bak" );
props_file.renameTo( backup );
pw = new PrintWriter( new FileWriter( props_file ));
pw.println( "plugin.class=" + props.getProperty( "plugin.class" ));
pw.println( "plugin.name=" + props.getProperty( "plugin.name" ));
pw.println( "plugin.version=" + props.getProperty( "plugin.version" ));
pw.println( "plugin.id=" + props.getProperty( "plugin.id" ));
pw.println( "" );
pw.println( "# configuration has been migrated to plugin config - see view->config->plugins" );
pw.println( "# in the SWT user interface" );
log.logAlert( LoggerChannel.LT_INFORMATION,
plugin_interface.getPluginName() + " - plugin.properties settings migrated to plugin configuration." );
}catch( Throwable e ){
Debug.printStackTrace( e );
log.logAlert( LoggerChannel.LT_ERROR,
plugin_interface.getPluginName() + " - plugin.properties settings migration failed." );
}finally{
if ( pw != null ){
pw.close();
}
}
}
}
if ( save_needed ){
plugin_config.save();
}
config_model.addLabelParameter2( "webui.restart.info" );
IntParameter param_port = config_model.addIntParameter2( CONFIG_PORT, "webui.port", CONFIG_PORT_DEFAULT );
StringListParameter param_protocol =
config_model.addStringListParameter2(
CONFIG_PROTOCOL, "webui.protocol", new String[]{ "http", "https" }, CONFIG_PROTOCOL_DEFAULT );
final BooleanParameter upnp_enable =
config_model.addBooleanParameter2(
CONFIG_UPNP_ENABLE,
"webui.upnpenable",
CONFIG_UPNP_ENABLE_DEFAULT );
StringParameter param_home = config_model.addStringParameter2( CONFIG_HOME_PAGE, "webui.homepage", CONFIG_HOME_PAGE_DEFAULT );
StringParameter param_rootdir = config_model.addStringParameter2( CONFIG_ROOT_DIR, "webui.rootdir", CONFIG_ROOT_DIR_DEFAULT );
StringParameter param_rootres = config_model.addStringParameter2( CONFIG_ROOT_RESOURCE, "webui.rootres", CONFIG_ROOT_RESOURCE_DEFAULT );
config_model.addLabelParameter2( "webui.mode.info" );
StringListParameter param_mode =
config_model.addStringListParameter2(
CONFIG_MODE, "webui.mode", new String[]{ "full", "view" }, CONFIG_MODE_DEFAULT );
config_model.addLabelParameter2( "webui.access.info" );
StringParameter param_access = config_model.addStringParameter2( CONFIG_ACCESS, "webui.access", CONFIG_ACCESS_DEFAULT );
final BooleanParameter pw_enable =
config_model.addBooleanParameter2(
CONFIG_PASSWORD_ENABLE,
"webui.passwordenable",
CONFIG_PASSWORD_ENABLE_DEFAULT );
final StringParameter user_name =
config_model.addStringParameter2(
CONFIG_USER,
"webui.user",
CONFIG_USER_DEFAULT );
final PasswordParameter password =
config_model.addPasswordParameter2(
CONFIG_PASSWORD,
"webui.password",
PasswordParameter.ET_SHA1,
CONFIG_PASSWORD_DEFAULT );
pw_enable.addEnabledOnSelection( user_name );
pw_enable.addEnabledOnSelection( password );
tracker = plugin_interface.getTracker();
home_page = param_home.getValue().trim();
if ( home_page.length() == 0 ){
home_page = null;
}else if (!home_page.startsWith("/" )){
home_page = "/" + home_page;
}
resource_root = param_rootres.getValue().trim();
if ( resource_root.length() == 0 ){
resource_root = null;
}else if ( resource_root.startsWith("/" )){
resource_root = resource_root.substring(1);
}
String root_dir = param_rootdir.getValue().trim();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?