⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdpeergroup.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            int tmp =  app.startApp(arg);
            if (tmp != 0) applications.remove(appKey);
            else applications.put(appKey, app);
            res += tmp;

        }
        return res;
    }

    public void stopApp() {
        Enumeration modules = applications.elements();
        if (modules != null) {
            while (modules.hasMoreElements()) {
                Module module = null;
                try {
                    module = (Module) modules.nextElement();
                    module.stopApp();
                } catch (Exception any) {
                    if (module != null) {
                        if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Failed to stop application: "
                                 + module.getClass().getName());
                    }
                }
            }
        }
        applications = null; // get rid of the table.
        modules = protocols.elements();
        if (modules != null) {
            while (modules.hasMoreElements()) {
                Module module = null;
                try {
                    module = (Module) modules.nextElement();
                    module.stopApp();
                } catch (Exception any) {
                    if (module != null) {
                        if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Failed to stop application: "
                                 + module.getClass().getName());
                    }
                }
            }
        }
        protocols = null; // get rid of the table.
        super.stopApp();
    }

    public void init(PeerGroup parent, ID assignedID, Advertisement impl)
        throws PeerGroupException
    {
        ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) impl;

	if (initialized == true) {
	    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("You cannot initialize a PeerGroup more"
                     +" than once !");
	    return;
	}
	initialized = true;

        // Set-up the minimal GenericPeerGroup
        super.init(parent, assignedID, implAdv);


        /*
         * Build the list of modules disabled by config.
         */
        PeerAdvertisement conf = (PeerAdvertisement) getConfigAdvertisement();
        if (conf != null) {
            Hashtable serviceParams = conf.getServiceParams();
            Enumeration keys = serviceParams.keys();
            while (keys.hasMoreElements()) {
                ID key = (ID) keys.nextElement();
                TextElement e = (TextElement) serviceParams.get(key);
                if (e.getChildren("isOff").hasMoreElements()) {
                    disabledModules.addElement(key);
                }
            }
            conf = null;
            serviceParams = 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.
        Hashtable services = paramAdv.getServices();
        protocols = paramAdv.getProtos();

        // Load & init the designated modules; updates the "services" table.
        loadAllModules(services, endpointClassID);

        // 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) services.get(endpointClassID);

        if (endp != null) {
            addService(endpointClassID, (Service) endp);
            endp.startApp(null);
            services.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);
            Enumeration allProtos = protocols.elements();
            while (allProtos.hasMoreElements()) {
                Module proto = (Module) allProtos.nextElement();
            }
        } else {
            if (parent != null) {
                // Otherwise, attach our parent's endpoint.
                addService(endpointClassID, parent.getEndpointService());

                // Do the magic wand thing on the peer adv.
                // Using the parent's endpoint instead of having one is a
                // legacy thing that we might remove in the near future.)
                PeerAdvertisement peerAdv = getPeerAdvertisement();
                PeerAdvertisement pPeerAdv = parent.getPeerAdvertisement();

                peerAdv.putServiceParam(endpointClassID,
                                        pPeerAdv.getServiceParam(endpointClassID));
            }
            protocols.clear();
        }

        // Get all the others services now.
        loadAllModules(services, null);

        Module m;
        Enumeration allKeys = services.keys();
        while (allKeys.hasMoreElements()) {
            ModuleClassID classID = (ModuleClassID) allKeys.nextElement();
            m = (Module) services.get(classID);
            if (m instanceof Service) {
                addService(classID, (Service) m);
            } else {
                if (LOG.isEnabledFor(Priority.WARN)) {
                    LOG.warn("Suspicious service: " + classID.toString());
                    LOG.warn("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 (Exception e) {
            throw new PeerGroupException(e.getMessage());
        }

        // Now startApp() them all.
        Enumeration allModules = services.elements();
        while (allModules.hasMoreElements()) {
            m = (Module) allModules.nextElement();
            m.startApp(null);
        }
        // startApp the protocols if they're there.
        allModules = protocols.elements();
        while (allModules.hasMoreElements()) {
            m = (Module) allModules.nextElement();
            m.startApp(null);
        }

        /*
         * Publish a few things that have not been published in this
         * group yet.
         */
        DiscoveryService disco = getDiscoveryService();
        if (disco != null) {
            // It should work but if it does not we can survive.
            try {
                // Discovery service adv could not be published localy,
                // since at that time there was no local discovery to
                // publish to. FIXME: this is realy a cherry on the cake.
                // no-one realy cares
                disco.publish(disco.getImplAdvertisement(),
                              DiscoveryService.ADV,
                              DEFAULT_LIFETIME,
                              DEFAULT_EXPIRATION);

                // Try to publish our impl adv within this group. (it was published
                // in the parent automatically when loaded.
                disco.publish(implAdv,
                              DiscoveryService.ADV,
                              DEFAULT_LIFETIME,
                              DEFAULT_EXPIRATION);
            } catch(Exception nevermind) {
                if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Failed to publish Impl adv within group.");
            }
        }
    }

    /**
     * Returns the all purpose peer group implementation advertisement.
     * This defines a peergroup implementation that can be used for
     * many purposes, and from whih one may derive slightly different
     * peergroup implementations.
     * This definition is always the same and has a well known ModuleSpecID.
     * It includes the basic service, no protocols and the shell for main
     * application.
     * The user must remember to change the specID if the set of services
     * protocols or applications is altered before use.
     *
     * @return ModuleImplAdvertisement The new peergroup impl adv.
     */
    public ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement()
        throws Exception {

        // Build it only the first time; then clone it.
        if (allPurposeImplAdv != null)
            return (ModuleImplAdvertisement) allPurposeImplAdv.clone();

        // Grab the platform.
        RefPeerGroup platf = this;
        while (platf.getParentGroup() != null) platf = platf.getParentGroup();

        // grab its impl adv.
        ModuleImplAdvertisement implAdv =
            (ModuleImplAdvertisement) platf.getImplAdvertisement();

        // re-id it
        implAdv.setModuleSpecID(PeerGroup.allPurposePeerGroupSpecID);
        implAdv.setDescription("General Purpose Peer Group Implementation");

        // Alter it: mostly same services but...

        // class is *This* class, not Platform.
        implAdv.setCode(StdPeerGroup.class.getName());

        TextElement paramElement = (TextElement) implAdv.getParam();
        StdPeerGroupParamAdv paramAdv = new StdPeerGroupParamAdv(paramElement);

        // No protos
        paramAdv.setProtos(null);

        // No endpoint service.
        Hashtable services = paramAdv.getServices();
        services.remove(PeerGroup.endpointClassID);
        paramAdv.setServices(services);

        // Main app is the shell
        // Build a ModuleImplAdv for the shell
        ModuleImplAdvertisement newAppAdv = (ModuleImplAdvertisement)
            AdvertisementFactory.newAdvertisement(
                ModuleImplAdvertisement.getAdvertisementType());


        // The shell's spec id is a canned one.
        newAppAdv.setModuleSpecID(PeerGroup.refShellSpecID);

        // Same compat than the group.
        newAppAdv.setCompat(implAdv.getCompat());
        newAppAdv.setUri(implAdv.getUri());
        newAppAdv.setProvider(implAdv.getProvider());

        // Make up a description
        newAppAdv.setDescription("JXTA Shell reference implementation");

        // Tack in the class name
        newAppAdv.setCode("net.jxta.impl.shell.bin.Shell.Shell");

        // Put that in a new table of Apps and replace the entry in
        // paramAdv
        Hashtable newApps = new Hashtable();
        newApps.put(PeerGroup.applicationClassID, newAppAdv);
        paramAdv.setApps(newApps);

        // Pour our newParamAdv in netPgImplAdv
        paramElement = (TextElement)
            paramAdv.getDocument(new MimeMediaType("text", "xml"));

        implAdv.setParam(paramElement);

        allPurposeImplAdv = implAdv;

        return implAdv;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -