📄 configparams.java
字号:
/* * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved. * * The Sun Project JXTA(TM) Software License * * 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 Sun Microsystems, Inc. for JXTA(TM) technology." * 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. * * JXTA is a registered trademark of Sun Microsystems, Inc. in the United * States and other countries. * * Please see the license information page at : * <http://www.jxta.org/project/www/license.html> for instructions on use of * the license in source files. * * ==================================================================== * * 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. */package net.jxta.protocol;import net.jxta.document.*;import net.jxta.id.ID;import net.jxta.id.IDFactory;import java.util.logging.Level;import net.jxta.logging.Logging;import java.util.logging.Logger;import java.net.URI;import java.net.URISyntaxException;import java.util.*;import java.util.concurrent.atomic.AtomicInteger;/** * A container for collections of configuration parameters. Configuration * parameters are stored in a Map which is keyed by {@code JXTA ID}s and whose * values are {@code Advertisement}s. */public abstract class ConfigParams extends ExtendableAdvertisement implements Cloneable { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(ConfigParams.class.getName()); private static final String SVC_TAG = "Svc"; private static final String MCID_TAG = "MCID"; private static final String PARAM_TAG = "Parm"; /** * A table of structured documents to be interpreted by each service. * For safe operation these elements should be immutable, but we're helpless * if they are not. */ private final Map<ID, StructuredDocument> params = new HashMap<ID, StructuredDocument>(); /** * A map of advertisements to be interpreted by each service. * For safe operation we clone the advertisements when they are added to the * map and only ever return clones of the advertisements. */ private final Map<ID, Advertisement> ads = new HashMap<ID, Advertisement>(); /** * The ids of the advertisements and/or params which have been explicitly * marked as disabled. */ private final Set<ID> disabled = new HashSet<ID>(); /** * Counts the changes made to this object. The API increments it every time * some change is not proven to be idempotent. We rely on implementations to * increment modCount every time something is changed without going through * the API. */ protected final transient AtomicInteger modCount = new AtomicInteger(0); /** * Returns the identifying type of this Advertisement. * * @return String the type of advertisement */ public static String getAdvertisementType() { return "jxta:CP"; } /** * Default Constructor. We want all ConfigParams derived advertisements to * pretty print. */ protected ConfigParams() { super(true); } /** * {@inheritDoc} */ @Override public ConfigParams clone() { try { ConfigParams result = (ConfigParams) super.clone(); for (Map.Entry<ID, StructuredDocument> anEntry : params.entrySet()) { result.params.put(anEntry.getKey(), StructuredDocumentUtils.copyAsDocument(anEntry.getValue())); } for (Map.Entry<ID, Advertisement> anEntry : ads.entrySet()) { result.ads.put(anEntry.getKey(), anEntry.getValue().clone()); } result.disabled.addAll(disabled); return result; } catch (CloneNotSupportedException impossible) { throw new Error("Object.clone() threw CloneNotSupportedException", impossible); } } /** * {@inheritDoc} */ @Override public boolean equals(Object other) { if(this == other) { return true; } if(other instanceof ConfigParams) { ConfigParams likeMe = (ConfigParams) other; boolean ep = params.equals(likeMe.params); boolean ea = ads.equals(likeMe.ads); boolean ed = disabled.equals(likeMe.disabled); return ep && ea && ed; } return false; } /** * {@inheritDoc} */ @Override public final String getBaseAdvType() { return getAdvertisementType(); } /** * {@inheritDoc} */ @Override protected boolean handleElement(Element raw) { if (super.handleElement(raw)) { return true; } XMLElement elem = (XMLElement) raw; if (SVC_TAG.equals(elem.getName())) { Attribute disabledAttr = elem.getAttribute("disabled"); boolean isDisabled = (null != disabledAttr) && Boolean.parseBoolean(disabledAttr.getValue()); Enumeration<XMLElement> elems = elem.getChildren(); ID key = null; XMLElement param = null; while (elems.hasMoreElements()) { XMLElement e = elems.nextElement(); if (MCID_TAG.equals(e.getName())) { try { URI mcid = new URI(e.getTextValue()); key = IDFactory.fromURI(mcid); } catch (URISyntaxException badID) { throw new IllegalArgumentException("Bad ID in advertisement: " + e.getTextValue()); } } else if (PARAM_TAG.equals(e.getName())) { param = e; } else { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Unrecognized <Svc> tag : " + e.getName()); } } } if (key != null && param != null) { if(!isDisabled) { // Backwards compatibility support. Enumeration<XMLElement> isOff = param.getChildren("isOff"); isDisabled = isOff.hasMoreElements(); } putServiceParam(key, param); if(isDisabled) { disabled.add(key); } } else { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Incomplete Service Param : id=" + key + " param=" + param); } return false; } return true; } return false; } /** * Return the advertisement as a document. * * @param adv the document to add elements to. * @return true if elements were added otherwise false. */ public boolean addDocumentElements(StructuredDocument adv) { for (Map.Entry<ID, StructuredDocument> anEntry : params.entrySet()) { ID anID = anEntry.getKey(); StructuredDocument aDoc = anEntry.getValue(); Element s = adv.createElement(SVC_TAG); adv.appendChild(s); if(disabled.contains(anID)) { ((Attributable)s).addAttribute("disabled", "true"); } Element e = adv.createElement(MCID_TAG, anID.toString()); s.appendChild(e); StructuredDocumentUtils.copyElements(adv, s, aDoc, PARAM_TAG); } for (Map.Entry<ID, Advertisement> anEntry : ads.entrySet()) { ID anID = anEntry.getKey(); Advertisement anAdv = anEntry.getValue(); Element s = adv.createElement(SVC_TAG); adv.appendChild(s); if(disabled.contains(anID)) { ((Attributable)s).addAttribute("disabled", "true"); } Element e = adv.createElement(MCID_TAG, anID.toString());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -