]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/DiscoveryConfigAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / DiscoveryConfigAdv.java
1 /*
2  * Copyright (c) 2004-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.protocol;
58
59
60 import java.util.Enumeration;
61
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;
73
74 import java.util.logging.Level;
75 import net.jxta.logging.Logging;
76 import java.util.logging.Logger;
77
78
79 /**
80  * Contains parameters for configuration of the Reference Implemenation
81  * Rendezvous Service.
82  *
83  * <p/><pre><code>
84  *
85  * </code></pre>
86  *
87  */
88 public final class DiscoveryConfigAdv extends ExtendableAdvertisement {
89
90     /**
91      *  Instantiator for DiscoveryConfigAdv
92      */
93     public static class Instantiator implements AdvertisementFactory.Instantiator {
94
95         /**
96          * {@inheritDoc}
97          */
98         public String getAdvertisementType() {
99             return advType;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public Advertisement newInstance() {
106             return new DiscoveryConfigAdv();
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         public Advertisement newInstance(Element root) {
113             return new DiscoveryConfigAdv(root);
114         }
115     }
116
117     /**
118      *  Log4J Logger
119      */
120     private static final Logger LOG = Logger.getLogger(DiscoveryConfigAdv.class.getName());
121
122     /**
123      *  Our DOCTYPE
124      */
125     private static final String advType = "jxta:DiscoConfigAdv";
126
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";
130
131     private static final String[] fields = {};
132
133     /**
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.
136      */
137     private boolean forwardAlwaysReplica = false;
138
139     /**
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.
143      */
144     private boolean forwardBelowTreshold = false;
145
146     /**
147      * localOnly discovery.
148      */
149     private boolean localOnly = false;
150
151     /**
152      *  Returns the identifying type of this Advertisement.
153      *
154      *  <p/><b>Note:</b> This is a static method. It cannot be used to determine
155      *  the runtime type of an advertisment. ie.
156      *  </p><code><pre>
157      *      Advertisement adv = module.getSomeAdv();
158      *      String advType = adv.getAdvertisementType();
159      *  </pre></code>
160      *
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
164      *  advertisment.
165      *
166      * @return String the type of advertisement
167      */
168     public static String getAdvertisementType() {
169         return advType;
170     }
171
172     /**
173      *  Use the Instantiator through the factory
174      */
175     DiscoveryConfigAdv() {}
176
177     /**
178      *  Use the Instantiator through the factory
179      * @param root the element
180      */
181     DiscoveryConfigAdv(Element root) {
182         if (!XMLElement.class.isInstance(root)) {
183             throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
184         }
185
186         XMLElement doc = (XMLElement) root;
187
188         String doctype = doc.getName();
189
190         String typedoctype = "";
191         Attribute itsType = doc.getAttribute("type");
192
193         if (null != itsType) {
194             typedoctype = itsType.getValue();
195         }
196
197         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
198             throw new IllegalArgumentException(
199                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
200         }
201
202         Enumeration eachAttr = doc.getAttributes();
203
204         while (eachAttr.hasMoreElements()) {
205             Attribute aDiscoAttr = (Attribute) eachAttr.nextElement();
206             String name = aDiscoAttr.getName();
207             boolean flag = Boolean.valueOf(aDiscoAttr.getValue().trim());
208
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)) {
214                 localOnly = flag;
215             } else {
216                 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
217                     LOG.warning("Unhandled Attribute: " + name);
218                 }
219             }
220         }
221     }
222
223     /**
224      * Make a safe clone of this DiscoveryConfigAdv.
225      *
226      * @return Object A copy of this DiscoveryConfigAdv
227      */
228     @Override
229     public DiscoveryConfigAdv clone() throws CloneNotSupportedException {
230
231         throw new CloneNotSupportedException("Not implemented");
232     }
233
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public String getAdvType() {
239         return getAdvertisementType();
240     }
241
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public final String getBaseAdvType() {
247         return getAdvertisementType();
248     }
249
250     /**
251      *  {@inheritDoc}
252      */
253     @Override
254     public ID getID() {
255         return ID.nullID;
256     }
257
258     /**
259      *  {@inheritDoc}
260      */
261     @Override
262     public Document getDocument(MimeMediaType encodeAs) {
263         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
264
265         if (adv instanceof Attributable) {
266             Attributable attrDoc = (Attributable) adv;
267
268             // Do not output if false. It is the default value.
269             if (forwardAlwaysReplica) {
270                 attrDoc.addAttribute(FORWARD_ALWAYS_REPLICA, Boolean.toString(forwardAlwaysReplica));
271             }
272             if (forwardBelowTreshold) {
273                 attrDoc.addAttribute(FORWARD_BELOW_TRESHOLD, Boolean.toString(forwardBelowTreshold));
274             }
275             if (localOnly) {
276                 attrDoc.addAttribute(FORWARD_BELOW_TRESHOLD, Boolean.toString(localOnly));
277             }
278         }
279         return adv;
280     }
281
282     /**
283      *  {@inheritDoc}
284      */
285     @Override
286     public String[] getIndexFields() {
287         return fields;
288     }
289
290     /**
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.
293      *
294      * @return The current setting.
295      */
296     public boolean getForwardAlwaysReplica() {
297         return forwardAlwaysReplica;
298     }
299
300     /**
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.
303      *
304      * @param newvalue The new setting.
305      */
306     public void setForwardAlwaysReplica(boolean newvalue) {
307         forwardAlwaysReplica = newvalue;
308     }
309
310     /**
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.
313      *
314      * @return The current setting.
315      */
316     public boolean getForwardBelowTreshold() {
317         return forwardBelowTreshold;
318     }
319
320     /**
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.
323      *
324      * @param newvalue The new setting.
325      */
326     public void setForwardBelowTreshold(boolean newvalue) {
327         forwardBelowTreshold = newvalue;
328     }
329
330     /**
331      * True if this discovery service performs only local discovery.
332      *
333      * @return The current setting.
334      */
335     public boolean getLocalOnly() {
336         return localOnly;
337     }
338
339     /**
340      * Specifies if this discovery service will perform local only discovery.
341      *
342      * @param newvalue The new setting.
343      */
344     public void setLocalOnly(boolean newvalue) {
345         localOnly = newvalue;
346     }
347 }