📄 modulemanager.java
字号:
Module module = lookupModule (moduleName);
if (module != null) {
return module;
}
module = loader.loadModule (moduleName);
if (module != null) {
// Since this module is not started by the standard
// JXTA PeerGroup, we need to initialize ourself.
// Note that the ID and the ModuleImplAdvertisement is
// then set to null, which is fine, since that has been
// the decision from the application to actually not use
// the standard PeerGroup Module loading scheme.
try {
module.init (group, null, null);
} catch (Exception e) {
// Init failed, the module cannot be initialized
return null;
}
registerModule (moduleName, module);
}
return module;
}
/**
* loadModule
*
* Loads a Module. The default PeerGroup class loader will be used. The class
* must be within the CLASSPATH of the platform.
* If the module has already been loaded, the existing Module is returned.
*
* @param moduleName symbolic name of the Module
* @param moduleCode the name of the class to be loaded.
* @return the Module for the given name. null is returned if the module could not be
* loaded
**/
public synchronized Module loadModule (String moduleName,
String moduleCode) {
// First check if the module is already loaded and registered
Module module = lookupModule (moduleName);
if (module != null) {
return module;
}
if ( !createModuleAdvs (moduleName,
null,
moduleCode,
null,
LOCAL_ADV_TTL,
REMOTE_ADV_TTL) ) {
// Creation of the module advertisement has failed.
return null;
}
// Get the module. This should always work since the advertisements have
// just been created.
module = loadModule (moduleName);
if (module == null) {
// There is really nothing more we can do here.
return null;
}
return module;
}
private synchronized Module loadModule (String moduleName) {
// First check if the module is already loaded and registered
Module module = lookupModule (moduleName);
if (module != null) {
return module;
}
try {
// Recover the ModuleClassAdvertisement
Enumeration enum =
group.getDiscoveryService().getLocalAdvertisements (DiscoveryService.ADV,
"Name",
moduleName);
if ((enum == null) || (!enum.hasMoreElements())) {
// No advertisement.
return null;
}
ModuleClassAdvertisement mcAdv = null;
while (enum.hasMoreElements()) {
try{
mcAdv = (ModuleClassAdvertisement) enum.nextElement();
break;
} catch (Exception ez1) {
continue;
}
}
// Revover the Module Specification Advertisement
enum =
group.getDiscoveryService().getLocalAdvertisements (DiscoveryService.ADV,
"Name",
moduleName);
if ((enum == null) || (!enum.hasMoreElements())) {
return null;
}
ModuleSpecAdvertisement mSpecAdv = null;
while (enum.hasMoreElements()) {
try {
mSpecAdv = (ModuleSpecAdvertisement) enum.nextElement();
break;
} catch (Exception ez1) {
continue;
}
}
module = group.loadModule (mcAdv.getModuleClassID(),
mSpecAdv.getModuleSpecID(),
PeerGroup.Here);
if (module != null) {
registerModule (moduleName, module);
}
return module;
} catch (Exception ez2) {
return null;
}
}
private boolean createModuleAdvs (String moduleName,
String moduleSpecURI,
String moduleCode,
String moduleCodeURI,
long localTTL,
long remoteTTL) {
DiscoveryService disco = group.getDiscoveryService();
try {
// First create the Module class advertisement associated with the module
// We build the module class advertisement using the advertisement
// Factory class by passing it the type of the advertisement we
// want to construct. The Module class advertisement is to be used
// to simply advertise the existence of the module. This is a
// a very small advertisement that only advertise the existence
// of module. In order to access the module, a peer will
// have to discover the associated module spec advertisement.
ModuleClassAdvertisement mcadv = (ModuleClassAdvertisement)
AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType());
mcadv.setName(moduleName);
mcadv.setDescription("Created by ModuleManager: " + moduleName);
ModuleClassID mcID = IDFactory.newModuleClassID();
mcadv.setModuleClassID(mcID);
// Ok the Module Class advertisement was created, just publish
// it in my local cache and to my peergroup. This
// is the NetPeerGroup
disco.publish(mcadv,
DiscoveryService.ADV,
localTTL,
remoteTTL);
// Create the Module Spec advertisement associated with the module
// We build the module Spec Advertisement using the advertisement
// Factory class by passing in the type of the advertisement we
// want to construct. The Module Spec advertisement will contain
// all the information necessary for a client to contact the module
// for instance it will contain a pipe advertisement to
// be used to contact the module
ModuleSpecAdvertisement mdadv = (ModuleSpecAdvertisement)
AdvertisementFactory.newAdvertisement(
ModuleSpecAdvertisement.getAdvertisementType());
// ModuleManager does not allow to set up any customized
// information for the Module.
mdadv.setName(moduleName);
mdadv.setCreator("jxta.org");
mdadv.setModuleSpecID(IDFactory.newModuleSpecID(mcID));
if (moduleSpecURI != null) {
mdadv.setSpecURI(moduleSpecURI);
}
// Ok the Module advertisement was created, just publish
// it in my local cache and into the NetPeerGroup.
disco.publish(mdadv,
DiscoveryService.ADV,
localTTL,
remoteTTL);
// Create the Module Implementation advertisement
ModuleImplAdvertisement miadv = (ModuleImplAdvertisement)
AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
miadv.setModuleSpecID (mdadv.getModuleSpecID());
if (moduleCode != null) {
miadv.setCode (moduleCode);
}
if (moduleCodeURI != null) {
miadv.setUri (moduleCodeURI);
}
miadv.setDescription("Created by ModuleManager: " + moduleName);
// Steal the compat, provider, and uri from the
// group's own impl adv. We DO want them identical in
// this case.
ModuleImplAdvertisement pgImpl =
(ModuleImplAdvertisement) group.getImplAdvertisement();
miadv.setCompat (pgImpl.getCompat());
// Ok the Module Class advertisement was created, just publish
// it in my local cache and to my peergroup. This
// is the NetPeerGroup
disco.publish(miadv,
DiscoveryService.ADV,
localTTL,
remoteTTL);
} catch (Exception ex) {
return false;
}
return true;
}
private class ModuleDesc {
protected Module module = null;
private boolean started = false;
private boolean stopped = true;
public ModuleDesc (Module module) {
this.module = module;
}
public void startApp (String[] args) {
if (module == null) {
return;
}
if (started) {
// Already started - nothing to do
return;
}
module.startApp (args);
started = true;
stopped = false;
}
public void stopApp() {
if (module == null) {
return;
}
if (stopped) {
// Already stopped - nothing to do
return;
}
module.stopApp();
stopped = true;
started = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -