📄 modulemanager.java
字号:
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: ModuleManager.java,v 1.2 2002/03/04 21:43:01 echtcherbina Exp $
*/
package net.jxta.impl.util;
import java.util.*;
import java.io.*;
import java.net.*;
import net.jxta.platform.*;
import net.jxta.discovery.*;
import net.jxta.service.*;
import net.jxta.document.*;
import net.jxta.protocol.*;
import net.jxta.peergroup.*;
import net.jxta.id.*;
/**
* Module Manager.
*
* This class allows to manage modules to be loaded, started and stopped
* within a PeerGroup. Modules that are loaded using the ModuleManager do not need
* to be listed within the PeerGroup advertisement, nor do they have to have
* published their ModuleSpec and ModuleImpl advertisements: the ModuleManager
* takes care of this task. However, other peers which may want to load the Module
* will also have to use its own loader (or the ModuleManager itself, of course):
* the ModuleManager only manages Modules on the local peer.
*
* The Module Manager allows, as an option, to use an application provided class loader.
* The default class loader is the PeerGroup class loader.
*
* The following exemple shows how to use the ModuleManager:
*
*
* <pre>
* // Get the peergroup
* PeerGroup group = getMyPeerGroup();
* // Get the ModuleManager
* ModuleManager moduleManager = ModuleManager.getModuleManager (group);
*
* // Is the Module already loaded ?
* Module module = moduleManager.lookupModule ("SampleModule");
* if (module == null) {
* // SampleModue is not loaded yet. Load it now.
* module = moduleManager.loadModule ( "SampleModule", "net.jxta.app.SampleModule.SampleModule");
* }
*
* // Start SampleModule
* moduleManager.startModule ("SampleModule", moduleArgs);
* </pre>
**/
public class ModuleManager {
static private Hashtable managers = null;
static private long LOCAL_ADV_TTL = 5 * 60 * 1000; // 5 minutes is more than sufficient
static private long REMOTE_ADV_TTL = 0; // We do not allow remote access of the advertisements.
private Hashtable modules = new Hashtable();
private PeerGroup group = null;
/**
* ModuleManagerLoader interface.
* This interface is used by the application in order to provide its own
* class loader instead of using the standard PeerGroup loader.
*
**/
public interface ModuleManagerLoader {
/**
* This method is invoked by the ModuleManager when it is time to load
* the class associated to the module. The name of the module is provided,
* which allows the application provided loader to be able to load a variety
* of modules, if that is necessary for the application. Note that the ModuleManager
* assumes that the module which is loaded by the provided loader is not started:
* loading and starting a module are two different operations for the ModuleManager.
*
* @param moduleName is the symbolic name of the Module.
* @return Module the object that has been loaded.
**/
public Module loadModule (String moduleName);
}
/**
* Private constructor that allows to create an instance of the Module Manager for each
* PeerGroup.
*
* @param group the PeerGroup for which the ModuleManager needs to allocated a new instance
* of itself.
**/
private ModuleManager (PeerGroup group) {
this.group = group;
}
/**
* startModule
*
* This method is invoked by the application to start a previously loaded
* module.
*
* @param moduleName is the symbolic name of the module.
* @param args is an array of String containing optional arguments for the module. This
* array is passed directely to the startApp (String[] ) method of the Module.
**/
public void startModule (String moduleName, String[] args) {
ModuleDesc moduleDesc = (ModuleDesc) modules.get (moduleName);
if (moduleDesc == null) {
// Cannot find such a module
return;
}
moduleDesc.startApp (args);
}
/**
* stopModule
*
* This method is invoked by the application to stop a running module.
*
* @param moduleName is the symbolic name of the module.
**/
public void stopModule (String moduleName) {
ModuleDesc moduleDesc = (ModuleDesc) modules.get (moduleName);
if (moduleDesc == null) {
// Cannot find such a module
return;
}
moduleDesc.stopApp ();
}
/**
* getModuleManager
*
* This method is used in order to get the instance of the ModuleManager for a given
* PeerGroup. getModuleManager will create a new instance automatically if there is no
* instance for the given PeerGroup.
*
* @param group the PeerGroup for which the ModuleManager is asked.
* @return the ModuleManager instance for the given PeerGroup.
**/
static public ModuleManager getModuleManager(PeerGroup group) {
if (managers == null) {
// This is the first time the ModuleManager is invoked. Create
// the Hashtable
managers = new Hashtable();
}
ModuleManager manager = null;
manager = (ModuleManager) managers.get (group.getPeerGroupID());
if (manager == null) {
manager = new ModuleManager(group);
managers.put (group.getPeerGroupID(), manager);
}
return manager;
}
private synchronized boolean registerModule (String moduleName, Module module) {
ModuleDesc moduleDesc = (ModuleDesc) modules.get (module);
if (moduleDesc != null) {
// There is already a module registered to that name.
return false;
}
moduleDesc = new ModuleDesc (module);
modules.put (moduleName, moduleDesc);
return true;
}
private synchronized Module unregisterModule (String moduleName) {
ModuleDesc moduleDesc = (ModuleDesc) modules.get (moduleName);
if (moduleDesc == null) {
// There is already a module registered to that name.
return null;
}
modules.remove (moduleName);
return moduleDesc.module;
}
/**
* lookupModule
*
* Get the Module from its symbolic name.
*
* @param moduleName symbolic name of the Module
* @return the Module for the given name. null is returned if there is no module
* of the given name.
**/
public synchronized Module lookupModule (String moduleName) {
ModuleDesc moduleDesc = (ModuleDesc) modules.get (moduleName);
if (moduleDesc == null) {
// There is not any module registered to that name.
return null;
}
return moduleDesc.module;
}
/**
* loadModule
*
* Loads a Module. A class loaded is provided by the application.
* If the module has already been loaded, the existing Module is returned.
*
* @param moduleName symbolic name of the Module
* @param loader application provided class loader
* @return the Module for the given name. null is returned if the module could not be
* loaded
**/
public synchronized Module loadModule (String moduleName, ModuleManagerLoader loader) {
// First check if the module is already loaded and registered
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -