📄 genericpeergroup.java
字号:
if ((null != implAdv.getCode()) && (null != implAdv.getUri())) { try { // Good one. Try it. Class<Module> clazz; try { clazz = (Class<Module>) loader.findClass(implAdv.getModuleSpecID()); } catch (ClassNotFoundException notLoaded) { clazz = (Class<Module>) loader.defineClass(implAdv); } if (null == clazz) { throw new ClassNotFoundException("Cannot load class (" + implAdv.getCode() + ") : " + assigned); } newMod = clazz.newInstance(); newMod.init(privileged ? this : (PeerGroup) getInterface(), assigned, implAdv); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info( "Loaded" + (privileged ? " privileged" : "") + " module : " + implAdv.getDescription() + " (" + implAdv.getCode() + ")"); } } catch (Exception ex) { try { newMod.stopApp(); } catch (Throwable ignored) { // If this does not work, nothing needs to be done. } throw new PeerGroupException("Could not load module for : " + assigned + " (" + implAdv.getDescription() + ")", ex); } } else { String error; if (null == implAdv.getCode()) { error = "ModuleImpAdvertisement missing Code element"; } else if (null == implAdv.getUri()) { error = "ModuleImpAdvertisement missing URI element"; } else { error = "ModuleImpAdvertisement missing both Code and URI elements"; } throw new PeerGroupException("Can not load module : " + error + " for" + assigned); } // Publish or renew the lease of this adv since we're using it. try { if (discovery != null) { discovery.publish(implAdv, DEFAULT_LIFETIME, DEFAULT_EXPIRATION); } } catch (Exception ignored) {// ignored } // If we reached this point we're done. return newMod; } /** * {@inheritDoc} */ public Module loadModule(ID assigned, ModuleSpecID specID, int where) { return loadModule(assigned, specID, where, false); } /** * Load a module from a ModuleSpecID * <p/> * Advertisement is sought, compatibility is checked on all candidates and * load is attempted. The first one that is compatible and loads * successfully is initialized and returned. * * @param assignedID Id to be assigned to that module (usually its ClassID). * @param specID The specID of this module. * @param where May be one of: {@code Here}, {@code FromParent}, or * {@code Both}, meaning that the implementation advertisement will be * searched in this group, its parent or both. As a general guideline, the * implementation advertisements of a group should be searched in its * prospective parent (that is Here), the implementation advertisements of a * group standard service should be searched in the same group than where * this group's advertisement was found (that is, FromParent), while * applications may be sought more freely (Both). * @param privileged If {@code true} then the module is provided the true * group obj instead of just an interface to the group object. This is * normally used only for the group's defined services and applications. * @return Module the new module, or {@code null} if no usable implementation was found. */ protected Module loadModule(ID assignedID, ModuleSpecID specID, int where, boolean privileged) { List<Advertisement> allModuleImplAdvs = new ArrayList<Advertisement>(); ModuleImplAdvertisement loadedImplAdv = loader.findModuleImplAdvertisement(specID); if(null != loadedImplAdv) { // We already have a module defined for this spec id. Use that. allModuleImplAdvs.add(loadedImplAdv); } else { // Search for a module to use. boolean fromHere = (where == Here || where == Both); boolean fromParent = (where == FromParent || where == Both); if (fromHere && (null != discovery)) { Collection<Advertisement> here = discoverSome(discovery, DiscoveryService.ADV, "MSID", specID.toString(), 120, ModuleImplAdvertisement.class); allModuleImplAdvs.addAll(here); } if (fromParent && (null != getParentGroup()) && (null != parentGroup.getDiscoveryService())) { Collection<Advertisement> parent = discoverSome(parentGroup.getDiscoveryService(), DiscoveryService.ADV, "MSID", specID.toString(), 120, ModuleImplAdvertisement.class); allModuleImplAdvs.addAll(parent); } } Throwable recentFailure = null; for (Advertisement eachAdv : allModuleImplAdvs) { if( !(eachAdv instanceof ModuleImplAdvertisement) ) { continue; } ModuleImplAdvertisement foundImpl = (ModuleImplAdvertisement) eachAdv; try { // First check that the MSID is really the one we're looking for. // It could have appeared somewhere else in the adv than where // we're looking, and discovery doesn't know the difference. if (!specID.equals(foundImpl.getModuleSpecID())) { continue; } Module newMod = loadModule(assignedID, foundImpl, privileged); // If we reach that point, the module is good. return newMod; } catch (ProtocolNotSupportedException failed) { // Incompatible implementation. if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Incompatbile impl adv"); } } catch (PeerGroupException failed) { // Initialization failure. if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Initialization failed", failed); } } catch (Throwable e) { recentFailure = e; if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Not a usable impl adv: ", e); } } } // Throw an exception if there was a recent failure. if (null != recentFailure) { if (recentFailure instanceof Error) { throw (Error) recentFailure; } else if (recentFailure instanceof RuntimeException) { throw (RuntimeException) recentFailure; } else { throw new UndeclaredThrowableException(recentFailure); } } if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Could not find a loadable implementation for SpecID: " + specID); } return null; } /** * {@inheritDoc} */ public ConfigParams getConfigAdvertisement() { return configAdvertisement; } /** * Sets the configuration advertisement for this peer group. * * @param config The configuration advertisement which will be used for * this peer group or {@code null} if no configuration advertisement is to * be used. */ protected void setConfigAdvertisement(ConfigParams config) { configAdvertisement = config; } /** * Adds configuration parameters for the specified group. The configuration * parameters remain cached until either the specified group is started or * the parameters are replaced. * * @param groupid The group for who's params are being provided. * @param params The parameters to be provided to the peer group when it is * created. */ public static void setGroupConfigAdvertisement(ID groupid, ConfigParams params) { if( null != params) { group_configs.put(groupid, params); } else { group_configs.remove(groupid); } } /* * Now comes the implementation of the public API, including the * API mandated by the Service interface. */ /** * {@inheritDoc} * <p/> * It is not recommended to overload this method. Instead, subclassers * should overload either or both of * {@link #initFirst(PeerGroup,ID,Advertisement)} and {@link #initLast()}. * If this method is to be overloaded, the overloading method must * invoke <code>super.init</code>. * <p/> * This method invokes <code>initFirst</code> * with identical parameters. <code>initLast</initLast> does not take * parameters since the relevant information can be obtained from the * group following completion of the <code>initFirst</code> phase. * The resulting values may be different from the parameters to * <code>initFirst</code> since <code>initFirst</code> may * be overLoaded and the overloading method may modify these parameters * when calling <code>super.initFirst</code>. (See * {@link net.jxta.impl.peergroup.Platform} for an example of such a case). * <p/> * Upon completion, the group object is marked as completely initialized * in all cases. Once a group object is completely initialized, it becomes * sensitive to reference counting. * <p/> * In the future this method may become final. */ public void init(PeerGroup homeGroup, ID assignedID, Advertisement impl) throws PeerGroupException { try { initFirst(homeGroup, assignedID, impl); initLast(); } finally { // This must be done in all cases. initComplete = true; } } /** * Performs all initialization steps that need to be performed * before any subclass initialization is performed. * <p/> * Classes that override this method should always call * <code>super.initFirst()</code> <strong>before</strong> doing * any of their own work. * * @param homeGroup The group that serves as a parent to this group. * @param assignedID The unique ID assigned to this module. For * group this is the group ID or <code>null</code> if a group ID * has not yet been assigned. If null is passed, GenericPeerGroup * will generate a new group ID. * @param impl The ModuleImplAdvertisement which defines this * group's implementation. * @throws PeerGroupException if a group initialization error occurs */ protected void initFirst(PeerGroup homeGroup, ID assignedID, Advertisement impl) throws PeerGroupException { this.implAdvertisement = (ModuleImplAdvertisement) impl; this.parentGroup = homeGroup; if (null != parentGroup) { jxtaHome = parentGroup.getStoreHome(); } // Set the peer configuration before we start. if((null != assignedID) && (null == getConfigAdvertisement())) { setConfigAdvertisement(group_configs.remove(assignedID)); } try { // FIXME 20030919 bondolo@jxta.org This setup doesnt give us any // capability to use seed material or parent group. if (null == assignedID) { if ("cbid".equals(IDFactory.getDefaultIDFormat())) { throw new IllegalStateException("Cannot generate group id for cbid group"); } else { assignedID = IDFactory.newPeerGroupID(); } } else { if (parentGroup != null) { DiscoveryService disco = parentGroup.getDiscoveryService(); if (null != disco) { Enumeration found = disco.getLocalAdvertisements(DiscoveryService.GROUP, "GID", assignedID.toString()); if (found.hasMoreElements()) { peerGroupAdvertisement = (PeerGroupAdvertisement) found.nextElement(); } } } } if (!(assignedID instanceof PeerGroupID)) { throw new PeerGroupException("assignedID must be a peer group ID"); } peerAdvertisement.setPeerGroupID((PeerGroupID) assignedID); // // make sure the parent group is the required group // if (null != peerAdvertisement.getPeerGroupID().getParentPeerGroupID()) { // if (null == parentGroup) { // throw new PeerGroupException("Group requires parent group : " + peerAdvertisement.getPeerGroupID().getParentPeerGroupID()); // } else if (!parentGroup.getPeerGroupID().equals(peerAdvertisement.getPeerGroupID().getParentPeerGroupID())) { // throw new PeerGroupException("Group requires parent group : " + peerAdvertisement.getPeerGroupID().getParentPeerGroupID() + ". Provided parent was : " + parentGroup.getPeerGroupID()); // } // } // Do our part of the PeerAdv construction. if ((configAdvertisement != null) && (configAdvertisement instanceof PlatformConfig)) { PlatformConfig platformConfig = (PlatformConfig) configAdvertisement; // Normally there will be a peer ID and a peer name in the config. PeerID configPID = platformConfig.getPeerID(); if ((null == configPID) || (ID.nullID == configPID)) { if ("cbid".equals(IDFactory.getDefaultIDFormat())) { // Get our peer-defined parameters in the configAdv XMLElement param = (XMLElement) platformConfig.getServiceParam(PeerGroup.membershipClassID); if (null == param) { throw new IllegalArgumentException(PSEConfigAdv.getAdvertisementType() + " could not be located"); } Advertisement paramsAdv = null; try { paramsAdv = AdvertisementFactory.newAdvertisement(param); } catch (NoSuchElementException noadv) {// ignored } if (!(paramsAdv instanceof PSEConfigAdv)) { throw new IllegalArgumentException( "Provided Advertisement was not a " + PSEConfigAdv.getAdvertisementType()); } PSEConfigAdv config = (PSEConfigAdv) paramsAdv; Certificate clientRoot = config.getCertificate(); byte[] pub_der = clientRoot.getPublicKey().getEncoded(); platformConfig.setPeerID(IDFactory.newPeerID((PeerGroupID) assignedID, pub_der)); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -