2 * Copyright (c) 2004-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.impl.protocol;
60 import java.util.Enumeration;
62 import net.jxta.document.Advertisement;
63 import net.jxta.document.AdvertisementFactory;
64 import net.jxta.document.Attributable;
65 import net.jxta.document.Attribute;
66 import net.jxta.document.Document;
67 import net.jxta.document.Element;
68 import net.jxta.document.ExtendableAdvertisement;
69 import net.jxta.document.MimeMediaType;
70 import net.jxta.document.StructuredDocument;
71 import net.jxta.document.XMLElement;
72 import net.jxta.id.ID;
74 import java.util.logging.Level;
75 import net.jxta.logging.Logging;
76 import java.util.logging.Logger;
80 * Contains parameters for configuration of the Reference Implemenation
88 public final class DiscoveryConfigAdv extends ExtendableAdvertisement {
91 * Instantiator for DiscoveryConfigAdv
93 public static class Instantiator implements AdvertisementFactory.Instantiator {
98 public String getAdvertisementType() {
105 public Advertisement newInstance() {
106 return new DiscoveryConfigAdv();
112 public Advertisement newInstance(Element root) {
113 return new DiscoveryConfigAdv(root);
120 private static final Logger LOG = Logger.getLogger(DiscoveryConfigAdv.class.getName());
125 private static final String advType = "jxta:DiscoConfigAdv";
127 private static final String FORWARD_ALWAYS_REPLICA = "forwardAlwaysReplica";
128 private static final String FORWARD_BELOW_TRESHOLD = "forwardBelowThreshold";
129 private static final String LOCAL_ONLY = "localOnly";
131 private static final String[] fields = {};
134 * If true, the discovery service will always forward queries to the replica peer
135 * even if there are local responses, unless the replica peer is the local peer.
137 private boolean forwardAlwaysReplica = false;
140 * If true, the discovery service will always forward queries if the number of local
141 * responses is below the specified threshold. The threshold may be reduced by the number
142 * of local responses before forwarding. NOTE: not yet implemented.
144 private boolean forwardBelowTreshold = false;
147 * localOnly discovery.
149 private boolean localOnly = false;
152 * Returns the identifying type of this Advertisement.
154 * <p/><b>Note:</b> This is a static method. It cannot be used to determine
155 * the runtime type of an advertisment. ie.
157 * Advertisement adv = module.getSomeAdv();
158 * String advType = adv.getAdvertisementType();
161 * <p/><b>This is wrong and does not work the way you might expect.</b>
162 * This call is not polymorphic and calls
163 * Advertiement.getAdvertisementType() no matter what the real type of the
166 * @return String the type of advertisement
168 public static String getAdvertisementType() {
173 * Use the Instantiator through the factory
175 DiscoveryConfigAdv() {}
178 * Use the Instantiator through the factory
179 * @param root the element
181 DiscoveryConfigAdv(Element root) {
182 if (!XMLElement.class.isInstance(root)) {
183 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
186 XMLElement doc = (XMLElement) root;
188 String doctype = doc.getName();
190 String typedoctype = "";
191 Attribute itsType = doc.getAttribute("type");
193 if (null != itsType) {
194 typedoctype = itsType.getValue();
197 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
198 throw new IllegalArgumentException(
199 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
202 Enumeration eachAttr = doc.getAttributes();
204 while (eachAttr.hasMoreElements()) {
205 Attribute aDiscoAttr = (Attribute) eachAttr.nextElement();
206 String name = aDiscoAttr.getName();
207 boolean flag = Boolean.valueOf(aDiscoAttr.getValue().trim());
209 if (FORWARD_ALWAYS_REPLICA.equals(name)) {
210 forwardAlwaysReplica = flag;
211 } else if (FORWARD_BELOW_TRESHOLD.equals(name)) {
212 forwardBelowTreshold = flag;
213 } else if (LOCAL_ONLY.equals(name)) {
216 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
217 LOG.warning("Unhandled Attribute: " + name);
224 * Make a safe clone of this DiscoveryConfigAdv.
226 * @return Object A copy of this DiscoveryConfigAdv
229 public DiscoveryConfigAdv clone() throws CloneNotSupportedException {
231 throw new CloneNotSupportedException("Not implemented");
238 public String getAdvType() {
239 return getAdvertisementType();
246 public final String getBaseAdvType() {
247 return getAdvertisementType();
262 public Document getDocument(MimeMediaType encodeAs) {
263 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
265 if (adv instanceof Attributable) {
266 Attributable attrDoc = (Attributable) adv;
268 // Do not output if false. It is the default value.
269 if (forwardAlwaysReplica) {
270 attrDoc.addAttribute(FORWARD_ALWAYS_REPLICA, Boolean.toString(forwardAlwaysReplica));
272 if (forwardBelowTreshold) {
273 attrDoc.addAttribute(FORWARD_BELOW_TRESHOLD, Boolean.toString(forwardBelowTreshold));
276 attrDoc.addAttribute(FORWARD_BELOW_TRESHOLD, Boolean.toString(localOnly));
286 public String[] getIndexFields() {
291 * True if this discovery service will forward queries to the replica peer in all cases, rather
292 * than only in the absence of local responses.
294 * @return The current setting.
296 public boolean getForwardAlwaysReplica() {
297 return forwardAlwaysReplica;
301 * Specifies if this discovery service will forward queries to the replica peer in all cases, rather than only in the absence
302 * of local responses.
304 * @param newvalue The new setting.
306 public void setForwardAlwaysReplica(boolean newvalue) {
307 forwardAlwaysReplica = newvalue;
311 * True if this discovery service will forward queries when the number of local responses
312 * is below the specified treshold, rather than only in the absence of local responses.
314 * @return The current setting.
316 public boolean getForwardBelowTreshold() {
317 return forwardBelowTreshold;
321 * Specifies if this discovery service will forward queries when the number of local responses is below the specified
322 * treshold, rather than only in the absence of local responses.
324 * @param newvalue The new setting.
326 public void setForwardBelowTreshold(boolean newvalue) {
327 forwardBelowTreshold = newvalue;
331 * True if this discovery service performs only local discovery.
333 * @return The current setting.
335 public boolean getLocalOnly() {
340 * Specifies if this discovery service will perform local only discovery.
342 * @param newvalue The new setting.
344 public void setLocalOnly(boolean newvalue) {
345 localOnly = newvalue;