]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/RelayConfigAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / RelayConfigAdv.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 net.jxta.document.Advertisement;
61 import net.jxta.document.AdvertisementFactory;
62 import net.jxta.document.Attributable;
63 import net.jxta.document.Attribute;
64 import net.jxta.document.Document;
65 import net.jxta.document.Element;
66 import net.jxta.document.ExtendableAdvertisement;
67 import net.jxta.document.MimeMediaType;
68 import net.jxta.document.StructuredDocument;
69 import net.jxta.document.XMLElement;
70 import net.jxta.endpoint.EndpointAddress;
71 import net.jxta.id.ID;
72 import net.jxta.impl.util.TimeUtils;
73 import java.util.logging.Level;
74 import net.jxta.logging.Logging;
75 import java.util.logging.Logger;
76
77 import java.net.URI;
78 import java.util.Enumeration;
79 import java.util.HashSet;
80 import java.util.Set;
81
82
83 /**
84  * Contains parameters for configuration of the Reference Implemenation
85  * Relay Service.
86  * <p/>
87  * <p/><pre><code>
88  * <p/>
89  * </code></pre>
90  */
91 public final class RelayConfigAdv extends ExtendableAdvertisement implements Cloneable {
92
93     /**
94      * Log4J Logger
95      */
96     private static final Logger LOG = Logger.getLogger(RelayConfigAdv.class.getName());
97
98     /**
99      * Our DOCTYPE
100      */
101     private static final String advType = "jxta:RelayConfig";
102
103     private static final String RELAY_CLIENT_ATTR = "client";
104     private static final String RELAY_SERVER_ATTR = "server";
105
106     private static final String RELAY_CLIENT_ELEMENT = "client";
107     private static final String RELAY_CLIENT_SERVERS_ATTR = "maxRelays";
108     private static final String RELAY_CLIENT_LEASE_ATTR = "maxLease";
109     private static final String RELAY_CLIENT_POLL_ATTR = "messengerPollInterval";
110
111     private static final String RELAY_CLIENT_SEEDS_ELEMENT = "seeds";
112     private static final String USE_ONLY_SEEDS_ATTR = "useOnlySeeds";
113
114     private static final String SEED_RELAY_ADDR_ELEMENT = "addr";
115     private static final String SEED_RELAY_ADDR_SEEDING_ATTR = "seeding";
116
117     private static final String RELAY_SERVER_ELEMENT = "server";
118     private static final String RELAY_SERVER_CLIENTS_ATTR = "maxClients";
119     private static final String RELAY_SERVER_QUEUE_ATTR = "clientQueue";
120     private static final String RELAY_SERVER_LEASE_ATTR = "leaseDuration";
121     private static final String RELAY_SERVER_STALL_ATTR = "stallTimeout";
122     private static final String RELAY_SERVER_ANNOUNCE_ATTR = "announceInterval";
123     private static final String ACL_URI = "acl";
124
125     private static final String[] fields = {};
126
127     /**
128      * Are we configured as a relay client?
129      */
130     private boolean clientEnabled = false;
131
132     /**
133      * Max Relays
134      */
135     private int maxRelays = -1;
136
137     /**
138      * Max clients lease in relative milliseconds.
139      */
140     private long maxClientLeaseDuration = -1;
141
142     /**
143      * Messenger poll interval in relative milliseconds.
144      */
145     private long messengerPollInterval = -1;
146
147     /**
148      * Use only seeded relays.
149      */
150     private boolean useOnlySeeds = false;
151
152     /**
153      * Seed Relays
154      * <p/>
155      * <ul>
156      * <li>Elements are {@link net.jxta.endpoint.EndpointAddress}</li>
157      * </ul>
158      */
159     private Set<EndpointAddress> seedRelays = new HashSet<EndpointAddress>();
160
161     /**
162      * The list of seeding resources.
163      * <p/>
164      * <p/><ul>
165      * <li>The values are {@link java.net.URI}.</li>
166      * </ul>
167      */
168     private Set<URI> seedingURIs = new HashSet<URI>();
169
170     /**
171      * Are we configured as a relay server?
172      */
173     private boolean serverEnabled = false;
174
175     /**
176      * Max Clients
177      */
178     private int maxClients = -1;
179
180     /**
181      */
182     private int maxClientMessageQueue = -1;
183
184     /**
185      * Max Lease offered by server in relative milliseconds.
186      */
187     private long maxServerLeaseDuration = -1;
188
189     /**
190      * Stall timeout in relative milliseconds.
191      */
192     private long stallTimeout = -1;
193
194     /**
195      * Announce interval in relative milliseconds.
196      */
197     private long announceInterval = -1;
198
199     /**
200      * Access control URI
201      */
202     private URI aclURI = null;
203
204     /**
205      * Instantiator for RelayConfigAdv
206      */
207     public static class Instantiator implements AdvertisementFactory.Instantiator {
208
209         /**
210          * {@inheritDoc}
211          */
212         public String getAdvertisementType() {
213             return advType;
214         }
215
216         /**
217          * {@inheritDoc}
218          */
219         public Advertisement newInstance() {
220             return new RelayConfigAdv();
221         }
222
223         /**
224          * {@inheritDoc}
225          */
226         public Advertisement newInstance(Element root) {
227             return new RelayConfigAdv(root);
228         }
229     }
230
231     /**
232      * Returns the identifying type of this Advertisement.
233      * <p/>
234      * <p/><b>Note:</b> This is a static method. It cannot be used to determine
235      * the runtime type of an advertisment. ie.
236      * </p><code><pre>
237      *      Advertisement adv = module.getSomeAdv();
238      *      String advType = adv.getAdvertisementType();
239      *  </pre></code>
240      * <p/>
241      * <p/><b>This is wrong and does not work the way you might expect.</b>
242      * This call is not polymorphic and calls
243      * Advertiement.getAdvertisementType() no matter what the real type of the
244      * advertisment.
245      *
246      * @return String the type of advertisement
247      */
248     public static String getAdvertisementType() {
249         return advType;
250     }
251
252     private RelayConfigAdv() {}
253
254     private RelayConfigAdv(Element root) {
255         if (!XMLElement.class.isInstance(root)) {
256             throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
257         }
258
259         XMLElement doc = (XMLElement) root;
260
261         String doctype = doc.getName();
262
263         String typedoctype = "";
264         Attribute itsType = doc.getAttribute("type");
265
266         if (null != itsType) {
267             typedoctype = itsType.getValue();
268         }
269
270         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
271             throw new IllegalArgumentException(
272                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
273         }
274
275         Enumeration eachAttr = doc.getAttributes();
276
277         while (eachAttr.hasMoreElements()) {
278             Attribute aRelayAttr = (Attribute) eachAttr.nextElement();
279
280             if (super.handleAttribute(aRelayAttr)) {
281                 // nothing to do
282                 ;
283             } else if (RELAY_CLIENT_ATTR.equals(aRelayAttr.getName())) {
284                 clientEnabled = Boolean.valueOf(aRelayAttr.getValue().trim());
285             } else if (RELAY_SERVER_ATTR.equals(aRelayAttr.getName())) {
286                 serverEnabled = Boolean.valueOf(aRelayAttr.getValue().trim());
287             } else {
288                 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
289                     LOG.warning("Unhandled Attribute: " + aRelayAttr.getName());
290                 }
291             }
292         }
293
294         Enumeration elements = doc.getChildren();
295
296         while (elements.hasMoreElements()) {
297             XMLElement elem = (XMLElement) elements.nextElement();
298
299             if (!handleElement(elem)) {
300                 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
301                     LOG.warning("Unhandled Element: " + elem.toString());
302                 }
303             }
304         }
305
306         // Sanity Check!!!
307         if ((-1 != maxRelays) && (maxRelays <= 0)) {
308             throw new IllegalArgumentException("Max relays must not be negative or zero.");
309         }
310
311         if ((-1 != maxClientLeaseDuration) && (maxClientLeaseDuration <= 0)) {
312             throw new IllegalArgumentException("Max lease duration must not be negative or zero.");
313         }
314
315         if ((-1 != messengerPollInterval) && (messengerPollInterval <= 0)) {
316             throw new IllegalArgumentException("Messenger poll interval must not be negative or zero.");
317         }
318
319         if (useOnlySeeds && clientEnabled && seedRelays.isEmpty() && seedingURIs.isEmpty()) {
320             throw new IllegalArgumentException("Cannot specify 'useOnlySeeds' and no seed relays");
321         }
322
323         if ((-1 != maxClients) && (maxClients <= 0)) {
324             throw new IllegalArgumentException("Max clients must not be negative or zero.");
325         }
326
327         if ((-1 != maxClientMessageQueue) && (maxClientMessageQueue <= 0)) {
328             throw new IllegalArgumentException("Max client queue must not be negative or zero.");
329         }
330
331         if ((-1 != maxServerLeaseDuration) && (maxServerLeaseDuration <= 0)) {
332             throw new IllegalArgumentException("Max lease duration must not be negative or zero.");
333         }
334
335         if ((-1 != stallTimeout) && (stallTimeout <= 0)) {
336             throw new IllegalArgumentException("Client stall timeout duration must not be negative or zero.");
337         }
338
339         if ((-1 != announceInterval) && (announceInterval <= 0)) {
340             throw new IllegalArgumentException("Announce interval must not be negative or zero.");
341         }
342     }
343
344     /**
345      * {@inheritDoc}
346      */
347     @Override
348     public RelayConfigAdv clone() {
349
350         RelayConfigAdv result;
351         
352         try {
353             result = (RelayConfigAdv) super.clone();
354         } catch (CloneNotSupportedException impossible) {
355             throw new Error("Object.clone() threw CloneNotSupportedException", impossible);
356         }  
357
358         result.setAnnounceInterval(getAnnounceInterval());
359         result.setClientEnabled(isClientEnabled());
360         result.setClientLeaseDuration(getClientLeaseDuration());
361         result.setClientMessageQueueSize(getClientMessageQueueSize());
362         result.setMaxClients(getMaxClients());
363         result.setMaxRelays(getMaxRelays());
364         result.setMessengerPollInterval(getMessengerPollInterval());
365         result.setServerEnabled(isServerEnabled());
366         result.setServerLeaseDuration(getServerLeaseDuration());
367         result.setStallTimeout(getStallTimeout());
368         result.setUseOnlySeeds(getUseOnlySeeds());
369         
370         return result;
371     }
372
373     /**
374      * {@inheritDoc}
375      */
376     @Override
377     public String getAdvType() {
378         return getAdvertisementType();
379     }
380
381     /**
382      * {@inheritDoc}
383      */
384     @Override
385     public final String getBaseAdvType() {
386         return getAdvertisementType();
387     }
388
389     /**
390      * {@inheritDoc}
391      */
392     @Override
393     public ID getID() {
394         return ID.nullID;
395     }
396
397     /**
398      * {@inheritDoc}
399      */
400     @Override
401     protected boolean handleElement(Element raw) {
402
403         if (super.handleElement(raw)) {
404             return true;
405         }
406
407         XMLElement elem = (XMLElement) raw;
408
409         if (RELAY_CLIENT_ELEMENT.equals(elem.getName())) {
410             Enumeration eachAttr = elem.getAttributes();
411
412             while (eachAttr.hasMoreElements()) {
413                 Attribute aRelayAttr = (Attribute) eachAttr.nextElement();
414
415                 if (RELAY_CLIENT_SERVERS_ATTR.equals(aRelayAttr.getName())) {
416                     maxRelays = Integer.parseInt(aRelayAttr.getValue().trim());
417                 } else if (RELAY_CLIENT_LEASE_ATTR.equals(aRelayAttr.getName())) {
418                     maxClientLeaseDuration = Long.parseLong(aRelayAttr.getValue().trim());
419                 } else if (RELAY_CLIENT_POLL_ATTR.equals(aRelayAttr.getName())) {
420                     messengerPollInterval = Long.parseLong(aRelayAttr.getValue().trim());
421                 } else {
422                     if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
423                         LOG.warning("Unhandled Attribute: " + aRelayAttr.getName());
424                     }
425                 }
426             }
427
428             Enumeration elements = elem.getChildren();
429
430             while (elements.hasMoreElements()) {
431                 XMLElement seedsElem = (XMLElement) elements.nextElement();
432
433                 if (RELAY_CLIENT_SEEDS_ELEMENT.equals(seedsElem.getName())) {
434                     Enumeration eachSeedsAttr = seedsElem.getAttributes();
435
436                     while (eachSeedsAttr.hasMoreElements()) {
437                         Attribute aRelayAttr = (Attribute) eachSeedsAttr.nextElement();
438
439                         if (USE_ONLY_SEEDS_ATTR.equals(aRelayAttr.getName())) {
440                             useOnlySeeds = Boolean.valueOf(aRelayAttr.getValue().trim());
441                         } else {
442                             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
443                                 LOG.warning("Unhandled Attribute: " + aRelayAttr.getName());
444                             }
445                         }
446                     }
447
448                     Enumeration addrElements = seedsElem.getChildren();
449
450                     while (addrElements.hasMoreElements()) {
451                         XMLElement addrElem = (XMLElement) addrElements.nextElement();
452
453                         if (SEED_RELAY_ADDR_ELEMENT.equals(addrElem.getName())) {
454                             String endpAddrString = addrElem.getTextValue();
455
456                             if (null != endpAddrString) {
457                                 URI endpURI = URI.create(endpAddrString.trim());
458
459                                 Attribute seedingAttr = addrElem.getAttribute(SEED_RELAY_ADDR_SEEDING_ATTR);
460
461                                 if ((null != seedingAttr) && Boolean.valueOf(seedingAttr.getValue().trim())) {
462                                     seedingURIs.add(endpURI);
463                                 } else {
464                                     seedRelays.add(new EndpointAddress(endpURI));
465                                 }
466                             }
467                         } else {
468                             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
469                                 LOG.warning("Unhandled Element: " + elem.toString());
470                             }
471                         }
472                     }
473                 } else {
474                     if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
475                         LOG.warning("Unhandled Element: " + elem.toString());
476                     }
477                 }
478             }
479
480             return true;
481         } else if (RELAY_SERVER_ELEMENT.equals(elem.getName())) {
482             Enumeration eachAttr = elem.getAttributes();
483
484             while (eachAttr.hasMoreElements()) {
485                 Attribute aRelayAttr = (Attribute) eachAttr.nextElement();
486
487                 if (RELAY_SERVER_CLIENTS_ATTR.equals(aRelayAttr.getName())) {
488                     maxClients = Integer.parseInt(aRelayAttr.getValue().trim());
489                 } else if (RELAY_SERVER_QUEUE_ATTR.equals(aRelayAttr.getName())) {
490                     maxClientMessageQueue = Integer.parseInt(aRelayAttr.getValue().trim());
491                 } else if (RELAY_SERVER_LEASE_ATTR.equals(aRelayAttr.getName())) {
492                     maxServerLeaseDuration = Long.parseLong(aRelayAttr.getValue().trim());
493                 } else if (RELAY_SERVER_STALL_ATTR.equals(aRelayAttr.getName())) {
494                     stallTimeout = Long.parseLong(aRelayAttr.getValue().trim());
495                 } else if (RELAY_SERVER_ANNOUNCE_ATTR.equals(aRelayAttr.getName())) {
496                     announceInterval = Long.parseLong(aRelayAttr.getValue().trim());
497                 } else {
498                     if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
499                         LOG.warning("Unhandled Attribute: " + aRelayAttr.getName());
500                     }
501                 }
502             }
503
504             return true;
505         } else if (ACL_URI.equals(elem.getName())) {
506             String addrElement = elem.getTextValue();
507
508             if (null != addrElement) {
509                 aclURI = URI.create(addrElement.trim());
510             }
511
512             return true;
513         }
514
515         return false;
516     }
517
518     /**
519      * {@inheritDoc}
520      */
521     @Override
522     public Document getDocument(MimeMediaType encodeAs) {
523         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
524
525         if (!(adv instanceof Attributable)) {
526             throw new IllegalStateException("Only Attributable documents are supported.");
527         }
528
529         if ((-1 != maxRelays) && (maxRelays <= 0)) {
530             throw new IllegalStateException("Max relays must not be negative or zero.");
531         }
532
533         if ((-1 != maxClientLeaseDuration) && (maxClientLeaseDuration <= 0)) {
534             throw new IllegalStateException("Max lease duration must not be negative or zero.");
535         }
536
537         if ((-1 != messengerPollInterval) && (messengerPollInterval <= 0)) {
538             throw new IllegalStateException("Messenger poll interval must not be negative or zero.");
539         }
540
541         if (useOnlySeeds && clientEnabled && seedRelays.isEmpty() && seedingURIs.isEmpty()) {
542             throw new IllegalStateException("Cannot specify 'useOnlySeeds' and no seed relays");
543         }
544
545         if ((-1 != maxClients) && (maxClients <= 0)) {
546             throw new IllegalStateException("Max clients must not be negative or zero.");
547         }
548
549         if ((-1 != maxClientMessageQueue) && (maxClientMessageQueue <= 0)) {
550             throw new IllegalStateException("Max client queue must not be negative or zero.");
551         }
552
553         if ((-1 != maxServerLeaseDuration) && (maxServerLeaseDuration <= 0)) {
554             throw new IllegalStateException("Max lease duration must not be negative or zero.");
555         }
556
557         if ((-1 != stallTimeout) && (stallTimeout <= 0)) {
558             throw new IllegalStateException("Client stall timeout duration must not be negative or zero.");
559         }
560
561         if ((-1 != announceInterval) && (announceInterval <= 0)) {
562             throw new IllegalStateException("Announce interval must not be negative or zero.");
563         }
564
565         Attributable attrDoc = (Attributable) adv;
566
567         if (clientEnabled) {
568             attrDoc.addAttribute(RELAY_CLIENT_ATTR, Boolean.TRUE.toString());
569         }
570
571         if (serverEnabled) {
572             attrDoc.addAttribute(RELAY_SERVER_ATTR, Boolean.TRUE.toString());
573         }
574
575         Element clientElem = adv.createElement(RELAY_CLIENT_ELEMENT);
576
577         adv.appendChild(clientElem);
578
579         Attributable attrElem = (Attributable) clientElem;
580
581         if (-1 != maxRelays) {
582             attrElem.addAttribute(RELAY_CLIENT_SERVERS_ATTR, Integer.toString(maxRelays));
583         }
584
585         if (-1 != maxClientLeaseDuration) {
586             attrElem.addAttribute(RELAY_CLIENT_LEASE_ATTR, Long.toString(maxClientLeaseDuration));
587         }
588
589         if (-1 != messengerPollInterval) {
590             attrElem.addAttribute(RELAY_CLIENT_POLL_ATTR, Long.toString(messengerPollInterval));
591         }
592
593         if (!seedRelays.isEmpty() || !seedingURIs.isEmpty()) {
594             Element seedsElem = adv.createElement(RELAY_CLIENT_SEEDS_ELEMENT);
595
596             clientElem.appendChild(seedsElem);
597
598             attrElem = (Attributable) seedsElem;
599
600             if (useOnlySeeds) {
601                 attrElem.addAttribute(USE_ONLY_SEEDS_ATTR, Boolean.TRUE.toString());
602             }
603
604             for (EndpointAddress seedRelay : seedRelays) {
605                 Element addrElement = adv.createElement(SEED_RELAY_ADDR_ELEMENT, seedRelay.toString());
606
607                 seedsElem.appendChild(addrElement);
608             }
609
610             for (URI seedingURI : seedingURIs) {
611                 Element addrElement = adv.createElement(SEED_RELAY_ADDR_ELEMENT, seedingURI.toString());
612
613                 seedsElem.appendChild(addrElement);
614
615                 ((Attributable) addrElement).addAttribute(SEED_RELAY_ADDR_SEEDING_ATTR, Boolean.TRUE.toString());
616             }
617         }
618
619         Element serverElem = adv.createElement(RELAY_SERVER_ELEMENT);
620
621         adv.appendChild(serverElem);
622
623         attrElem = (Attributable) serverElem;
624
625         if (-1 != maxClients) {
626             attrElem.addAttribute(RELAY_SERVER_CLIENTS_ATTR, Integer.toString(maxClients));
627         }
628
629         if (-1 != maxClientMessageQueue) {
630             attrElem.addAttribute(RELAY_SERVER_QUEUE_ATTR, Integer.toString(maxClientMessageQueue));
631         }
632
633         if (-1 != maxServerLeaseDuration) {
634             attrElem.addAttribute(RELAY_SERVER_LEASE_ATTR, Long.toString(maxServerLeaseDuration));
635         }
636
637         if (-1 != stallTimeout) {
638             attrElem.addAttribute(RELAY_SERVER_STALL_ATTR, Long.toString(stallTimeout));
639         }
640
641         if (-1 != announceInterval) {
642             attrElem.addAttribute(RELAY_SERVER_ANNOUNCE_ATTR, Long.toString(announceInterval));
643         }
644         if (aclURI != null) {
645             Element acl = adv.createElement(ACL_URI, aclURI.toString());
646
647             adv.appendChild(acl);
648         }
649         return adv;
650     }
651
652     /**
653      * {@inheritDoc}
654      */
655     @Override
656     public String[] getIndexFields() {
657         return fields;
658     }
659
660     /**
661      * If true then this peer will act as a relay client.
662      *
663      * @return If true then this peer will act as a relay client.
664      */
665     public boolean isClientEnabled() {
666         return clientEnabled;
667     }
668
669     /**
670      * If true then this peer will act as a relay client.
671      *
672      * @param enabled If true then this peer will act as a relay client.
673      */
674     public void setClientEnabled(boolean enabled) {
675         clientEnabled = enabled;
676     }
677
678     /**
679      * Return the maximum number of relay clients.
680      *
681      * @return The maximum number of relay clients or <code>-1</code> for
682      *         default value.
683      */
684     public int getMaxRelays() {
685         return maxRelays;
686     }
687
688     /**
689      * Sets the maximum number of relay clients.
690      *
691      * @param newvalue The maximum number of relay clients or <code>-1</code>
692      *                 for default value or <code>-1</code> for default value.
693      */
694     public void setMaxRelays(int newvalue) {
695         if ((-1 != newvalue) && (newvalue <= 0)) {
696             throw new IllegalArgumentException("Max Relays must be > 0");
697         }
698
699         maxRelays = newvalue;
700     }
701
702     /**
703      * The interval in relative milliseconds of leases accepted by clients.
704      *
705      * @return The interval in relative milliseconds of leases accepted by
706      *         clients or <code>-1</code> for default value.
707      */
708     public long getClientLeaseDuration() {
709         return maxClientLeaseDuration;
710     }
711
712     /**
713      * Sets interval in relative milliseconds of leases accepted by clients.
714      *
715      * @param newvalue The interval in relative milliseconds of leases accepted
716      *                 by clients or <code>-1</code> for default value.
717      */
718     public void setClientLeaseDuration(long newvalue) {
719         if ((-1 != newvalue) && (newvalue <= 0)) {
720             throw new IllegalArgumentException("Lease Duration must be > 0");
721         }
722
723         maxClientLeaseDuration = newvalue;
724     }
725
726     /**
727      * The interval in relative milliseconds of at which clients will poll for
728      * messengers
729      *
730      * @return The interval in relative milliseconds of at which clients will
731      *         poll for messengers or <code>-1</code> for default value.
732      */
733     public long getMessengerPollInterval() {
734         return messengerPollInterval;
735     }
736
737     /**
738      * Sets interval in relative milliseconds of at which clients will poll for
739      * messengers.
740      *
741      * @param newvalue The interval in relative milliseconds of at which clients
742      *                 will poll for messengers or <code>-1</code> for default value.
743      */
744     public void setMessengerPollInterval(long newvalue) {
745         if ((-1 != newvalue) && (newvalue <= 0)) {
746             throw new IllegalArgumentException("Poll interval must be > 0");
747         }
748
749         messengerPollInterval = newvalue;
750     }
751
752     /**
753      * If true then this peer will use only seed rendezvous when configured as
754      * an edge peer.
755      *
756      * @return If true then this peer will use only seed rendezvous when configured as
757      *         an edge peer.
758      */
759     public boolean getUseOnlySeeds() {
760         return useOnlySeeds;
761     }
762
763     /**
764      * Set whether this peer will use only seed rendezvous when configured as
765      * an edge peer.
766      *
767      * @param onlySeeds If true then this peer will use only seed rendezvous when configured as
768      *                  an edge peer.
769      */
770     public void setUseOnlySeeds(boolean onlySeeds) {
771         useOnlySeeds = onlySeeds;
772     }
773
774     public EndpointAddress[] getSeedRelays() {
775         return seedRelays.toArray(new EndpointAddress[seedRelays.size()]);
776     }
777
778     public void addSeedRelay(EndpointAddress addr) {
779         if (null == addr) {
780             throw new IllegalArgumentException("addr may not be null");
781         }
782
783         seedRelays.add(addr);
784     }
785
786     public void addSeedRelay(String addr) {
787         if (null == addr) {
788             throw new IllegalArgumentException("addr may not be null");
789         }
790
791         seedRelays.add(new EndpointAddress(addr));
792     }
793
794     public boolean removeSeedRelay(EndpointAddress addr) {
795         if (null == addr) {
796             throw new IllegalArgumentException("addr may not be null");
797         }
798
799         return seedRelays.remove(addr);
800     }
801
802     public void clearSeedRelays() {
803         seedRelays.clear();
804     }
805
806     public URI[] getSeedingURIs() {
807         return seedingURIs.toArray(new URI[seedingURIs.size()]);
808     }
809
810     public void addSeedingURI(URI addr) {
811         if (null == addr) {
812             throw new IllegalArgumentException("addr may not be null");
813         }
814
815         seedingURIs.add(addr);
816     }
817
818     public void addSeedingURI(String addr) {
819         if (null == addr) {
820             throw new IllegalArgumentException("addr may not be null");
821         }
822
823         seedingURIs.add(URI.create(addr));
824     }
825
826     public boolean removeSeedingURI(URI addr) {
827         if (null == addr) {
828             throw new IllegalArgumentException("addr may not be null");
829         }
830
831         return seedingURIs.remove(addr);
832     }
833
834     public void clearSeedingURIs() {
835         seedingURIs.clear();
836     }
837
838     /**
839      * If true then this peer will act as a relay server.
840      *
841      * @return If true then this peer will act as a relay server.
842      */
843     public boolean isServerEnabled() {
844         return serverEnabled;
845     }
846
847     /**
848      * If true then this peer will act as a relay server.
849      *
850      * @param enabled If true then this peer will act as a relay server.
851      */
852     public void setServerEnabled(boolean enabled) {
853         serverEnabled = enabled;
854     }
855
856     /**
857      * Return the maximum number of relay clients.
858      *
859      * @return The maximum number of relay clients or <code>-1</code> for
860      *         default value.
861      */
862     public int getMaxClients() {
863         return maxClients;
864     }
865
866     /**
867      * Sets he maximum number of relay clients.
868      *
869      * @param newvalue The maximum number of relay clients or <code>-1</code>
870      *                 for default value.
871      */
872     public void setMaxClients(int newvalue) {
873         if ((-1 != newvalue) && (newvalue <= 0)) {
874             throw new IllegalArgumentException("Max Clients must be > 0");
875         }
876
877         maxClients = newvalue;
878     }
879
880     /**
881      * Return the client message queue length size.
882      *
883      * @return The client message queue length size or <code>-1</code> for default value.
884      */
885     public int getClientMessageQueueSize() {
886         return maxClientMessageQueue;
887     }
888
889     /**
890      * Sets the client message queue length size.
891      *
892      * @param newvalue The client message queue length size or <code>-1</code>
893      *                 for default value.
894      */
895     public void setClientMessageQueueSize(int newvalue) {
896         if ((-1 != newvalue) && (newvalue <= 0)) {
897             throw new IllegalArgumentException("Client Message Queue Size must be > 0");
898         }
899
900         maxClientMessageQueue = newvalue;
901     }
902
903     /**
904      * The interval in relative milliseconds of leases offered by servers.
905      *
906      * @return The interval in relative milliseconds of leases offered by servers.
907      */
908     public long getServerLeaseDuration() {
909         return maxServerLeaseDuration;
910     }
911
912     /**
913      * Sets interval in relative milliseconds of leases  offered by servers.
914      *
915      * @param newvalue The interval in relative milliseconds of leases offered
916      *                 by servers or <code>-1</code> for default value.
917      */
918     public void setServerLeaseDuration(long newvalue) {
919         if ((-1 != newvalue) && (newvalue <= 0)) {
920             throw new IllegalArgumentException("Lease Duration must be >= 0");
921         }
922
923         maxServerLeaseDuration = newvalue;
924     }
925
926     /**
927      * The interval in relative milliseconds after which a client is assumed to
928      * no longer be connected if it fails to request messages.
929      *
930      * @return The interval in relative milliseconds after which a client is
931      *         assumed to no longer be connected if it fails to request messages or
932      *         <code>-1</code> for default value.
933      */
934     public long getStallTimeout() {
935         return stallTimeout;
936     }
937
938     /**
939      * Sets interval in relative milliseconds after which a client is assumed to
940      * no longer be connected if it fails to request messages.
941      *
942      * @param newvalue The interval in relative milliseconds after which a
943      *                 client is  assumed to no longer be connected if it fails to request
944      *                 messages or <code>-1</code> for default value.
945      */
946     public void setStallTimeout(long newvalue) {
947         if ((-1 != newvalue) && (newvalue <= 0)) {
948             throw new IllegalArgumentException("Stall timeout must be > 0");
949         }
950
951         stallTimeout = newvalue;
952     }
953
954     /**
955      * The interval in relative milliseconds at which relay server will
956      * announce its presence.
957      *
958      * @return The interval in relative milliseconds at which relay server will
959      *         broadcast its presence or <code>-1</code> for default value.
960      */
961     public long getAnnounceInterval() {
962         return announceInterval;
963     }
964
965     /**
966      * Sets interval in relative milliseconds at which relay server will
967      * announce its presence  or <code>-1</code> for default value.
968      *
969      * @param newvalue The interval in relative milliseconds at which relay server will
970      *                 announce its presence.
971      */
972     public void setAnnounceInterval(long newvalue) {
973         if ((-1 != newvalue) && (newvalue <= 0)) {
974             throw new IllegalArgumentException("Announce Interval must be > 0");
975         }
976
977         announceInterval = newvalue;
978     }
979
980     /**
981      * Return ACL URI if set
982      *
983      * @return ACL URI if set, null otherwise
984      */
985     public URI getAclUri() {
986         return aclURI;
987     }
988
989     /**
990      * Sets ACL URI
991      *
992      * @param uri URI if set, null otherwise
993      */
994     public void setAclUri(URI uri) {
995         aclURI = uri;
996     }
997 }