📄 stdpeergroup.java
字号:
if (LOG.isEnabledFor(Level.ERROR)) LOG.error("Skipping: " + classID + " Unsupported module descriptor : " + value.getClass().getName() ); modules.remove(classID); if (thisClassOnly != null) break; else continue; } if (theModule == null) { throw new PeerGroupException("Could not find a loadable implementation for : " + classID ); } modules.put(classID, theModule); } catch (Exception e) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Could not load module for class ID " + classID, e); } if (value instanceof ModuleClassID) { if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Will be missing from peer group: " + classID + " (" + e.getMessage() + ")."); } else { if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Will be missing from peer group: " + ((ModuleImplAdvertisement)value).getDescription() + " (" + e.getMessage() + ")."); } modules.remove(classID); } if (thisClassOnly != null) break; } } /** * The group does not care for start args, and does not come-up * with args to pass to its main app. So, until we decide on something * more useful, the args of the group's startApp are passed-on to the * group's main app. NB: both the apps init and startApp methods are * invoked. * * @return int Status. **/ public int startApp(String[] arg) { if ( !initialized) { if (LOG.isEnabledFor(Level.ERROR)) LOG.error("Group has not been initialized or init failed."); return -1; } // FIXME: maybe concurrent callers should be blocked until the // end of startApp(). That could mean forever, though. if (started == true) return 0; started = true; // Normaly does nothing, but we have to. super.startApp(arg); loadAllModules(applications, null, false); // Apps are non-privileged; int res = 0; Iterator appKeys = applications.keySet().iterator(); while (appKeys.hasNext()) { Object appKey = appKeys.next(); Module app = (Module) applications.get(appKey); int tmp = app.startApp(arg); if (tmp != 0) appKeys.remove(); else applications.put(appKey, app); res += tmp; } return res; } /** * {@inheritDoc} **/ public void stopApp() { Iterator modules = applications.values().iterator(); while (modules.hasNext()) { Module module = null; try { module = (Module) modules.next(); module.stopApp(); } catch (Exception any) { if (module != null) { if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Failed to stop application: " + module.getClass().getName(), any ); } } } applications.clear(); modules = protocols.values().iterator(); while (modules.hasNext()) { Module module = null; try { module = (Module) modules.next(); module.stopApp(); } catch (Exception any) { if (module != null) { if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Failed to stop protocol : " + module.getClass().getName(), any ); } } } protocols.clear(); if (cm != null) { cm.stop(); cm = null; } super.stopApp(); } /** * {@inheritDoc} * * <p/>This method loads and initializes all modules * described in the given implementation advertisement. Then, all modules * are placed in a list and the list is processed iteratively. During each * iteration, the {@link Module#startApp(String[])} method of each module * is invoked once. Iterations continue until no progress is being made or * the list is empty. * * <p/>The status returned by the {@link Module#startApp(String[])} method * of each module is considered as follows: * * <ul> * <li>{@link Module#START_OK}: The module is removed from the list of * modules to be started and its {@link Module#startApp(String[])} * method will not be invoked again. * </li> * * <li>{@link Module#START_AGAIN_PROGRESS}: The module remains in the * list of modules to be started and its {@link Module#startApp(String[])} * method will be invoked during the next iteration, if there is one. </li> * * <li>{@link Module#START_AGAIN_STALLED}: The module remains in the list * of modules to be started and its {@link Module#startApp(String[])} * method will be invoked during the next iteration if there is one. </li> * * <li>Any other value: The module failed to initialize. Its * {@link Module#startApp(String[])} method will not be invoked again.</li> * </ul> * * <p/>Iterations through the list stop when: * <ul> * <li>The list is empty: the group initialization proceeds.</li> * * <li>A complete iteration was performed and all modules returned * {@link Module#START_AGAIN_STALLED}: a {@link PeerGroupException} * is thrown.</li> * * <li>A number of complete iteration completed without any module * returning {@link Module#START_OK}: a {@link PeerGroupException} * is thrown. The number of complete iterations before that happens is * computed as 1 + the square of the number of modules currently in the * list.</li> * </ul> **/ protected synchronized void initFirst( PeerGroup parent, ID assignedID, Advertisement impl ) throws PeerGroupException { if (initialized == true) { if (LOG.isEnabledFor(Level.WARN)) LOG.warn("You cannot initialize a PeerGroup more than once !"); return; } // Set-up the minimal GenericPeerGroup super.initFirst(parent, assignedID, impl); // initialize cm before starting services. Do not refer to assignedID, as it could be // null, in which case the group ID has been generated automatically by super.initFirst() try { cm = new Cm( getHomeThreadGroup(), getStoreHome(), getPeerGroupID().getUniqueValue().toString(), Cm.DEFAULT_GC_MAX_INTERVAL, false ); } catch (Exception e) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("Error during creation of local store", e); } throw new PeerGroupException("Error during creation of local store", e); } // flush srdi for this group SrdiIndex.clearSrdi(this); ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) impl; /* * Build the list of modules disabled by config. */ ConfigParams conf = (ConfigParams) getConfigAdvertisement(); if (conf != null) { Iterator eachService = conf.getServiceParamsEntrySet().iterator(); while ( eachService.hasNext() ) { Entry anEntry = (Entry) eachService.next(); TextElement e = (TextElement) anEntry.getValue(); if (e.getChildren("isOff").hasMoreElements()) { disabledModules.add( anEntry.getKey() ); } } conf = null; } /* * Load all the modules from the advertisement */ StdPeerGroupParamAdv paramAdv = new StdPeerGroupParamAdv(implAdv.getParam()); // Applications are shelved until startApp() applications = paramAdv.getApps(); // FIXME: we should improve services-dependencies so that we do not // have to do that hand-filtering. But it will do for now. Map initServices = paramAdv.getServices(); protocols = paramAdv.getProtos(); // Load & init the designated modules; updates the "services" table. // First Load the Peer Info Service since other services need to register Monitors through it loadAllModules(initServices, peerinfoClassID, true); Module peerInfoModule = (Module) initServices.get(peerinfoClassID); if (peerInfoModule != null) { addService(peerinfoClassID, (Service) peerInfoModule); } // Next Load Endpoint Service since other services may look for it in their init(); loadAllModules(initServices, endpointClassID, true); // Check that the endpoint module is in the table (means that it was // in there and could be loaded and init'ed successfully). Module endp = (Module) initServices.get(endpointClassID); addService(endpointClassID, (Service) endp); initServices.remove(endpointClassID); // Done with that one. // Since we have an endpoint, load the protocols. They are // not necessarily services, just Modules, so they do not // get registered in the services table. // Wait until other services are loaded before starting the // protocols though. loadAllModules(protocols, null, true); // Get all the others services now. loadAllModules(initServices, null, true); Module m; Iterator allKeys = initServices.keySet().iterator(); while (allKeys.hasNext()) { ModuleClassID classID = (ModuleClassID) allKeys.next(); m = (Module) initServices.get(classID); if (m instanceof Service) { addService(classID, (Service) m); } else { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Suspicious service: " + classID + " is not an instance of Service."); LOG.warn("It will not be registered as a service."); } } } // Make sure all the required services are loaded. try { checkServices(); } catch (ServiceNotFoundException e) { LOG.error( "Missing peer group service", e ); throw new PeerGroupException( "Missing peer group service", e ); } catch ( Throwable e) { LOG.error( "Unhandled Throwable", e ); throw new PeerGroupException( "Unhandled Throwable", e ); } // Make a list of all the things we need to start. // There is an a-priori order, but we'll iterate over the // list until all where able to complete their start phase // or no progress is made. Since we give to modules the opportunity // to pretend that they are making progress, we need to have a // safeguard: we will not iterate through the list more than N^2 + 1 // times without at least one module completing; N being the number // of modules still in the list. That should cover the worst case // scenario and still allow the process to eventually fail if it has // no chance of success. Map allStart = new HashMap( protocols.size() + initServices.size() + 1 ); allStart.put( endpointClassID, endp ); allStart.putAll( initServices ); allStart.putAll( protocols ); long modulesToGo = allStart.size(); long maxIterations = modulesToGo * modulesToGo + 1; boolean progress = true; while (progress && (modulesToGo > 0) && (maxIterations-- > 0)) { progress = false; Iterator eachModule = allStart.entrySet().iterator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -