📄 modulemanager.java
字号:
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 each = group.getDiscoveryService().getLocalAdvertisements(DiscoveryService.ADV, "Name", moduleName); if (!each.hasMoreElements()) { // No advertisement. return null; } ModuleClassAdvertisement mcAdv = null; while (each.hasMoreElements()) { try { mcAdv = (ModuleClassAdvertisement) each.nextElement(); break; } catch (Exception ez1) {// ignored } } // Revover the Module Specification Advertisement each = group.getDiscoveryService().getLocalAdvertisements(DiscoveryService.ADV, "Name", moduleName); if (!each.hasMoreElements()) { return null; } ModuleSpecAdvertisement mSpecAdv = null; while (each.hasMoreElements()) { try { mSpecAdv = (ModuleSpecAdvertisement) each.nextElement(); break; } catch (Exception ez1) {// ignored } } module = group.loadModule(mcAdv.getModuleClassID(), mSpecAdv.getModuleSpecID(), PeerGroup.Here); if (module != null) { registerModule(moduleName, module); } return module; } catch (Exception ez2) { return null; } } /** * Description of the Method * * @param moduleName Description of the Parameter * @param moduleSpecURI Description of the Parameter * @param moduleCode Description of the Parameter * @param moduleCodeURI Description of the Parameter * @param localTTL Description of the Parameter * @param remoteTTL Description of the Parameter * @return Description of the Return Value */ 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, 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, 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()); miadv.setUri(pgImpl.getUri()); // 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, localTTL, remoteTTL); } catch (Exception ex) { return false; } return true; } // FIXME this method should be refactored /** * Creates a Module Class, Spec, and Impl advertisements, and adds the service * Advertisement as part of the Module Impl Advertisement, and publishes the advertisements * in local cache * * @param group group * @param moduleName module name * @param description module description * @param moduleSpecURI module spec uri * @param moduleCode module code * @param moduleCodeURI module code uri * @param mcID module class id * @param msID module spec id * @param code module code * @param serviceAdv service advertisement * @param localTTL local cache lifetime in ms * @param remoteTTL remote cache lifetime in ms * @exception IOException if an io error occurs */ public void createServiceAdvertisement(PeerGroup group, String moduleName, String description, String moduleSpecURI, String moduleCode, String moduleCodeURI, ModuleClassID mcID, ModuleSpecID msID, String code, Advertisement serviceAdv, long localTTL, long remoteTTL) throws IOException { DiscoveryService discovery = group.getDiscoveryService(); // Create module class advertisement ModuleClassAdvertisement mcadv = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType()); mcadv.setModuleClassID(mcID); mcadv.setName(moduleName); mcadv.setDescription(description); // Module spec advertisement ModuleSpecAdvertisement mdspec = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(ModuleSpecAdvertisement.getAdvertisementType()); mdspec.setModuleSpecID(msID); mdspec.setName(moduleName); mdspec.setSpecURI(moduleSpecURI); // Module implementation advertisement ModuleImplAdvertisement miadv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType()); miadv.setModuleSpecID(mdspec.getModuleSpecID()); miadv.setDescription(description); if (moduleCodeURI != null) { miadv.setUri(moduleCodeURI); } if (moduleCode != null) { miadv.setCode(moduleCode); } // 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()); miadv.setCode(code); Element pEl = (Element) serviceAdv.getDocument(MimeMediaType.XMLUTF8); StructuredDocument svcParm = StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Parm"); StructuredDocumentUtils.copyElements(svcParm, svcParm, pEl); miadv.setParam(svcParm); // publish the advertisements discovery.publish(mcadv, localTTL, remoteTTL); discovery.publish(mdspec, localTTL, remoteTTL); discovery.publish(miadv, localTTL, remoteTTL); } /** * Retreives a Service Advertisement from a module impl advertisement * @param group peer group * @param mia ModuleImplAdvertisement * @param advertismentType service advertisment string Type * @return The service Advertisement * @exception IOException if an io error occurs */ public Advertisement getServiceAdvertisement(PeerGroup group, ModuleImplAdvertisement mia, String advertismentType) throws IOException { Element param = mia.getParam(); Element pel = null; if (param != null) { Enumeration list = param.getChildren(advertismentType); if (list.hasMoreElements()) { pel = (Element) list.nextElement(); } } Advertisement adv = AdvertisementFactory.newAdvertisement((TextElement) pel); return adv; } /** * Description of the Class */ private class ModuleDesc { /** * Description of the Field */ protected Module module = null; private boolean started = false; private boolean stopped = true; /** *Constructor for the ModuleDesc object * * @param module Description of the Parameter */ public ModuleDesc(Module module) { this.module = module; } /** * Description of the Method * * @param args Description of the Parameter */ public void startApp(String[] args) { if (module == null) { return; } if (started) { // Already started - nothing to do return; } module.startApp(args); started = true; stopped = false; } /** * Description of the Method */ public void stopApp() { if (module == null) { return; } if (stopped) { // Already stopped - nothing to do return; } module.stopApp(); stopped = true; started = false; } } /** * 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -