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

📄 peeradvertisement.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/*
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 *
 * $Id: PeerAdvertisement.java,v 1.2 2002/03/04 20:19:21 echtcherbina Exp $
 */

package net.jxta.protocol;

import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.document.Advertisement;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentUtils;
import net.jxta.document.Element;

import java.net.URL;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import java.util.Hashtable;
import java.util.Enumeration;

/**
 * This type of advertisement is used in two different fashions:
 * One is generated when instantiating a group on a peer and contains all the
 * parameters that services need to publish. It is then published within the
 * the group.
 * The other is generated by configuration menus and serves to pass to all
 * services their optional user-driven configuration. Some of which may
 * be published as part of the above process. A configuration PeerAdvertisement
 * is not itself published.
 *
 */

public abstract class PeerAdvertisement extends Advertisement
    implements Cloneable {


    /*
     * FIXME: [20011001 jice@jxta.org]
     * Idealy Advertisements should be immutable, but then they become too
     * cumbersome to construct. Therefore we would need an immutable class
     * and a mutable subclass, and provide one or the other depending on some
     * yet to defined privilege or something like that...later.
     *
     * Currently what we do is to make it properly clonable, so that
     * copies can be safely modified independantly. The cost is still small
     * because most members are actually immutable and thus implement a trivial
     * clone() method. To be safe with future modifications of the code we do not
     * assume that classes other than String are indeed immutable. We just rely
     * on theyr clone() method being as efficient as can be.
     */

    private PeerID pid = null;
    private PeerGroupID gid = null;
    private String name = null;
    private String description = null;
    private String debugLevel = null;

    // A table of structured documents to be interpreted by each service.
    // For cost and convenience reasons the elements in this table are
    // not cloned when the table is set or returned. For safe operation these
    // elements should be immutable, but we're helpless if they are not.
    private Hashtable serviceParams = new Hashtable();

    /**
     * Make a safe clone of this PeerAdvertisement.
     *
     * @return Object an object of class PeerAdvertisement that is a deep-enough
     * copy of this one.
     */
    public Object clone() {
        // IDs are know to be immutable but that could change. clone() them
        // for safety; their clone method costs nothing.

        // Shallow clone the params table.
        // The individual elements do not need cloning because we never allow
        // them to be altered. Instead accessor methods always return clones.
        // However, there are mutators to add/remove elements to/from the
        // table, which is far less expensive than cloning the whole table
        // contents out and in. Since this table is never very big,
        // it is cheaper to just clone it whole now than playing games.

        try {
            PeerAdvertisement result = (PeerAdvertisement) super.clone();
            result.serviceParams = (Hashtable) serviceParams.clone();
            return result;
        } catch (CloneNotSupportedException impossible) {
            return null;
        }
    }
     
    /**
     * returns the advertisement type
     * @return string type
     *
     * @since JXTA 1.0
     */

    public static String getAdvertisementType() {
        return "jxta:PA" ;
    }

    /**
     * returns the name of the peer.
     * @return String name of the peer.
     *
     * @since JXTA 1.0
     */

    public String getName() {
        return name;
    }

    /**
     * sets the name of the peer.
     * @param name name of the peer.
     *
     * @since JXTA 1.0
     */

    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the id of the peer.
     * @return PeerID the peer id
     *
     * @since JXTA 1.0
     */

    public PeerID getPeerID() {
        return (pid == null ? null : (PeerID) pid.clone());
    }

    /**
     * Sets the id of the peer.
     * @param id The id of the peer.
     *
     * @since JXTA 1.0
     */

    public void setPeerID(PeerID pid) {
        this.pid = (pid == null ? null : (PeerID) pid.clone());
    }

    /**
     * Returns the id of the peergroup this peer advertisement is for.
     *
     * @return PeerGroupID the peergroup id
     *
     * @since JXTA 1.0
     */

    public PeerGroupID getPeerGroupID() {
        return (gid == null ? null : (PeerGroupID) gid.clone());
    }

    /**
     * Returns the id of the peergroup this peer advertisement is for.
     *
     * @param gid The id of the peer.
     *
     * @since JXTA 1.0
     */

    public void setPeerGroupID(PeerGroupID gid) {
        this.gid = (gid == null ? null : (PeerGroupID) gid.clone());
    }

    /**
     * Returns a unique ID for that peer X group intersection. This is for indexing
     * purposes only.
     *
     * We return a composite ID that represents this peer is this group
     * rather than in the platform, which is what the regular peerId shows.
     *
     * May-be one day we'll want to name a peer differently
     * in each group, exactly in this way. In the meantime we still need
     * it to uniquely identify this adv.
     *
     * @return ID the composite ID
     *
     * @since JXTA 1.0
     */

    public ID getID() {

        // If it is incomplete, there's no meaninfull ID that we can return.
        if (gid == null || pid == null) return null;

        String peer;
        // That's tricky; we're not realy supposed to do that...

        // Get the grp unique string of hex. Clip the two type bytes
        // at the end.
        if ( gid.equals(PeerGroupID.defaultNetPeerGroupID) 
        || gid.equals( PeerGroupID.worldPeerGroupID) ) {
                
         peer = pid.getUniqueValue().toString();

        } else {
         String grp = gid.getUniqueValue().toString();
         grp = grp.substring(0, grp.length() - 2);

        // Get the peer unique string whih starts with the platform's unique
        // string.
         peer = pid.getUniqueValue().toString();
        // Replace the platform's unique portion with this group's id.
         peer = grp + peer.substring(grp.length());
        }
        
        // Forge a URL form for this chimaera and build a new PeerID out of it.
        try {
            return IDFactory.fromURL(new URL("urn", "", "jxta:" + peer));
        } catch (MalformedURLException iDontMakeMistakes) {
            // Fall through,  iDontMakeMistakes sometimes makes mistakes :)
            //iDontMakeMistakes.printStackTrace();
        } catch (UnknownServiceException ever) {
            // Fall through
            // ever.printStackTrace();
        }
        // May be if we had an "internal error exception" we should throw it.
        return null;
    }

    /** 
     * returns the description
     * @return String the description
     * 
     * @since JXTA 1.0     
     */

    public String getDescription() {
        return description;
    }
 
    /** 
     * sets the description
     * @param description the description
     * 
     * @since JXTA 1.0     
     */

    public void setDescription(String description) {
        this.description = description;
    }

    /** 
     * returns the debugLevel
     * @return String the debugLevel
     * 
     * @since JXTA 1.0     
     */

    public String getDebugLevel() {
        return debugLevel;
    }
 
    /** 
     * sets the debugLevel
     * @param description the debugLevel
     * 
     * @since JXTA 1.0     
     */

    public void setDebugLevel(String debugLevel) {
        this.debugLevel = debugLevel;
    }

    /**
     * sets the sets of parameters for all services.
     * This method first makes a deep copy, in order to protect the active
     * information from uncontrolled sharing.  This quite an expensive
     * operation. If only a few of the parameters need to be
     * added, it is wise to use putServiceParam() instead.
     *
     * @param params The whole set of parameters.
     *
     * @since JXTA 1.0     
     */
    public void setServiceParams(Hashtable params) {
        if (params == null) {
            serviceParams = new Hashtable();
            return;
        }

        Hashtable copy = new Hashtable();

        Enumeration keys = params.keys();
        while (keys.hasMoreElements()) {
            ID key = (ID) keys.nextElement();
            Element e = (Element) params.get(key);
            Element newDoc = StructuredDocumentUtils.copyAsDocument(e);
            copy.put(key.clone(), newDoc);
        }

        serviceParams = copy;
    }

    /**
     * Returns the sets of parameters for all services.
     * This method returns a deep copy, in order to protect the real
     * information from uncontrolled sharing while keeping it shared as
     * long as it is safe. This quite an expensive operation. If only a few
     * parameters need to be accessed, it is wise to use getServiceParam()
     * instead.
     *
     * @param params The whole set of parameters.
     *
     * @since JXTA 1.0     
     */
    public Hashtable getServiceParams() {
        Hashtable copy = new Hashtable();

        Enumeration keys = serviceParams.keys();
        while (keys.hasMoreElements()) {
            ID key = (ID) keys.nextElement();
            Element e = (Element) serviceParams.get(key);
            Element newDoc = StructuredDocumentUtils.copyAsDocument(e);
            copy.put(key.clone(), newDoc);
        }
        return copy;
    }

    /**
     * Puts a service parameter in the service parameters table
     * under the given key. The key is of a subclass of ID; usually a
     * ModuleClassID. This method makes a deep copy of the given element
     * into an independent document.
     *
     * @param key The key.
     * @param param The parameter, as an element. What is stored is a copy as
     * a standalone StructuredDocument which type is the element's name.
     *
     * @since JXTA 1.0     
     */
    public void putServiceParam(ID key, Element param) {
        if (param == null) {
            serviceParams.remove(key);
            return;
        }
        Element newDoc = StructuredDocumentUtils.copyAsDocument(param);
        serviceParams.put(key.clone(), newDoc);
    }

    /**
     * Returns the parameter element that matches the given key from the
     * service parameters table. The key is of a subclass of ID; usually a
     * ModuleClassID.
     *
     * @param key The key.
     *
     * @return StructuredDocument The matching parameter document or null if
     * none matched. The document type id "Param".
     *
     * @since JXTA 1.0     
     */
    public StructuredDocument getServiceParam(ID key) {
        Element param = (Element) serviceParams.get(key);
        if (param == null) return null;

        StructuredDocument newDoc =
            StructuredDocumentUtils.copyAsDocument(param);
        return newDoc;
    }

    /**
     * Removes and returns the parameter element that matches the given key
     * from the service parameters table. The key is of a subclass of ID;
     * usually a ModuleClassID.
     *
     * @param key The key.
     *
     * @return Element the removed parameter element or null if not found.
     * This is actually a StructureDocument of type "Param".
     *
     * @since JXTA 1.0     
     */
    public StructuredDocument removeServiceParam(ID key) {
        Element param = (Element) serviceParams.remove(key);
        if (param == null) return null;

        // It sound silly to clone it, but remember that we could be sharing
        // this element with a clone of ours, so we have the duty to still
        // protect it.

        StructuredDocument newDoc =
            StructuredDocumentUtils.copyAsDocument(param);
        return newDoc;
    }
}

⌨️ 快捷键说明

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