Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: net/jxta/ext/config/Transport.java


1   /*
2    *  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
3    *  reserved.
4    *
5    *  Redistribution and use in source and binary forms, with or without
6    *  modification, are permitted provided that the following conditions
7    *  are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright
10   *  notice, this list of conditions and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright
13   *  notice, this list of conditions and the following disclaimer in
14   *  the documentation and/or other materials provided with the
15   *  distribution.
16   *
17   *  3. The end-user documentation included with the redistribution,
18   *  if any, must include the following acknowledgment:
19   *  "This product includes software developed by the
20   *  Sun Microsystems, Inc. for Project JXTA."
21   *  Alternately, this acknowledgment may appear in the software itself,
22   *  if and wherever such third-party acknowledgments normally appear.
23   *
24   *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
25   *  must not be used to endorse or promote products derived from this
26   *  software without prior written permission. For written
27   *  permission, please contact Project JXTA at http://www.jxta.org.
28   *
29   *  5. Products derived from this software may not be called "JXTA",
30   *  nor may "JXTA" appear in their name, without prior written
31   *  permission of Sun.
32   *
33   *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34   *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36   *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
37   *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39   *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40   *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41   *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42   *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43   *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44   *  SUCH DAMAGE.
45   *  ====================================================================
46   *
47   *  This software consists of voluntary contributions made by many
48   *  individuals on behalf of Project JXTA.  For more
49   *  information on Project JXTA, please see
50   *  <http://www.jxta.org/>.
51   *
52   *  This license is based on the BSD license adopted by the Apache Foundation.
53   *
54   *  $Id: Transport.java,v 1.9 2004/11/30 22:42:10 gonzo Exp $
55   */
56  package net.jxta.ext.config;
57  
58  import java.util.ArrayList;
59  import java.util.Collections;
60  import java.util.Iterator;
61  import java.util.List;
62  
63  /**
64   *  Transport configuration container base class.
65   *
66   *@author     james todd [gonzo at jxta dot org]
67   *@created    November 13, 2003
68   */
69  public abstract class Transport {
70      
71       public static class Scheme {
72          /**
73          *  Tcp transport: {@value}
74           */
75          public final static String TCP = "tcp";
76          /**
77          *  Http transport: {@value}
78           */
79          public final static String HTTP = "http";
80  
81          /**
82           *  Gets the schemes attribute of the Scheme class
83           *
84           * @return    The protocols value
85           */
86          public static List getSchemes() {
87              return schemes;
88          }
89  
90  
91          /**
92           *  Gets the valid attribute of the Scheme class
93           *
94           * @param  p  Description of the Parameter
95           * @return    The valid value
96           */
97          public static boolean isValid(String t) {
98              return getSchemes().contains(t);
99          }
100 
101 
102         private static List schemes = null;
103 
104         static {
105             schemes = new ArrayList();
106 
107             schemes.add(TCP);
108             schemes.add(HTTP);
109         }
110 
111 
112         /**
113          *  Constructor for the Scheme object
114          */
115         private Scheme() { }
116     }
117     
118     private String scheme = Protocol.DEFAULT;
119     private boolean isEnabled = true;
120     private boolean isIncoming = false;
121     private boolean isOutgoing = false;
122     private List addresses = null;
123     private List publicAddresses = null;
124     private boolean isProxyEnabled = false;
125     private ProxyAddress proxy = null;
126 
127     /**
128      *  Constructor for the Transport object
129      *
130      *@param  scheme  Description of the Parameter
131      */
132     public Transport(String scheme) {
133         setScheme(scheme);
134     }
135 
136 
137     /**
138      *  Gets the scheme attribute of the Transport object
139      *
140      *@return    The scheme value
141      */
142     public String getScheme() {
143         return this.scheme;
144     }
145 
146 
147     /**
148      *  Sets the scheme attribute of the Transport object
149      *
150      *@param  scheme  The new scheme value
151      */
152     public void setScheme(String scheme) {
153         if (! Scheme.isValid(scheme)) {
154             throw new IllegalArgumentException("invalid scheme: " + scheme);
155         }
156         
157             this.scheme = scheme;
158     }
159 
160 
161     /**
162      *  Gets the enabled attribute of the Transport object
163      *
164      *@return    The enabled value
165      */
166     public boolean isEnabled() {
167         return this.isEnabled;
168     }
169 
170 
171     /**
172      *  Sets the enabled attribute of the Transport object
173      *
174      *@param  isEnabled  The new enabled value
175      */
176     public void setEnabled(boolean isEnabled) {
177         this.isEnabled = isEnabled;
178     }
179 
180 
181     /**
182      *  Gets the incoming attribute of the Transport object
183      *
184      *@return    The incoming value
185      */
186     public boolean isIncoming() {
187         return this.isIncoming;
188     }
189 
190 
191     /**
192      *  Sets the incoming attribute of the Transport object
193      *
194      *@param  isIncoming  The new incoming value
195      */
196     public void setIncoming(boolean isIncoming) {
197         this.isIncoming = isIncoming;
198     }
199 
200 
201     /**
202      *  Gets the outgoing attribute of the Transport object
203      *
204      *@return    The outgoing value
205      */
206     public boolean isOutgoing() {
207         return this.isOutgoing;
208     }
209 
210 
211     /**
212      *  Sets the outgoing attribute of the Transport object
213      *
214      *@param  isOutgoing  The new outgoing value
215      */
216     public void setOutgoing(boolean isOutgoing) {
217         this.isOutgoing = isOutgoing;
218     }
219 
220 
221     /**
222      *  Gets the addresses attribute of the Transport object
223      *
224      *@return    The addresses value
225      */
226     public List getAddresses() {
227         return this.addresses != null ?
228                this.addresses : Collections.EMPTY_LIST;
229     }
230 
231     /**
232      *  Sets the address attribute of the Transport object
233      *
234      *@param  address  The new address value
235      */
236     public void setAddress(Address address) {
237         setAddresses(Collections.singletonList(address));
238     }
239 
240 
241     /**
242      *  Sets the addresses attribute of the Transport object
243      *
244      *@param  addresses  The new addresses value
245      */
246     public void setAddresses(List addresses) {
247         clearAddresses();
248         addAddresses(addresses);
249     }
250 
251     /**
252      *  Adds a feature to the Address attribute of the Transport object
253      *
254      *@param  address  The feature to be added to the Address attribute
255      */
256     public void addAddress(Address address) {
257         addAddresses(Collections.singletonList(address));
258     }
259 
260     /**
261      *  Adds a feature to the Addresses attribute of the Transport object
262      *
263      *@param  addresses  The feature to be added to the Addresses attribute
264      */
265     public void addAddresses(List addresses) {
266         if (addresses == null) {
267             addresses = Collections.EMPTY_LIST;
268         }
269 
270         if (this.addresses == null) {
271             this.addresses = new ArrayList();
272         }
273 
274         for (Iterator i = addresses.iterator(); i.hasNext(); ) {
275             Address a = (Address)i.next();
276 
277             if (a != null &&
278                 ! this.addresses.contains(a)) {
279                 this.addresses.add(a);
280             }
281         }
282     }
283 
284     /**
285      *  Description of the Method
286      *
287      *@param  address  Description of the Parameter
288      *@return          Removed address or <code>null</code> if absent
289      */
290     public Address removeAddress(Address address) {
291         Address a = null;
292 
293         if (this.addresses != null) {
294             int i = this.addresses.indexOf(address);
295 
296             if (i > -1) {
297                 a = (Address)this.addresses.remove(i);
298 
299                 if (this.addresses.size() == 0) {
300                     this.addresses = null;
301                 }
302             }
303         }
304 
305         return a;
306     }
307 
308 
309     /**
310      *  Description of the Method
311      */
312     public void clearAddresses() {
313         if (this.addresses != null) {
314             this.addresses.clear();
315 
316             this.addresses = null;
317         }
318     }
319 
320 
321     /**
322      *  Gets the publicAddresses attribute of the Transport object
323      *
324      *@return    The publicAddresses value
325      */
326     public List getPublicAddresses() {
327         return this.publicAddresses != null ?
328                this.publicAddresses : Collections.EMPTY_LIST;
329     }
330 
331 
332     /**
333      *  Adds a feature to the PublicAddress attribute of the Transport object
334      *
335      *@param  address  The feature to be added to the PublicAddress attribute
336      */
337     public void addPublicAddress(PublicAddress address) {
338         addPublicAddresses(Collections.singletonList(address));
339     }
340 
341 
342     /**
343      *  Adds a feature to the PublicAddresses attribute of the Transport object
344      *
345      *@param  addresses  The feature to be added to the PublicAddresses
346      *      attribute
347      */
348     public void addPublicAddresses(List addresses) {
349         if (addresses == null) {
350             addresses = Collections.EMPTY_LIST;
351         }
352 
353         if (this.publicAddresses == null) {
354             this.publicAddresses = new ArrayList();
355         }
356 
357         for (Iterator i = addresses.iterator(); i.hasNext(); ) {
358             PublicAddress pa = (PublicAddress)i.next();
359 
360             if (pa != null &&
361                 ! this.publicAddresses.contains(pa)) {
362                 this.publicAddresses.add(pa);
363             }
364         }
365     }
366 
367 
368     /**
369      *  Sets the publicAddress attribute of the Transport object
370      *
371      *@param  address  The new publicAddress value
372      */
373     public void setPublicAddress(PublicAddress address) {
374         setPublicAddresses(Collections.singletonList(address));
375     }
376 
377 
378     /**
379      *  Sets the publicAddresses attribute of the Transport object
380      *
381      *@param  addresses  The new publicAddresses value
382      */
383     public void setPublicAddresses(List addresses) {
384         clearPublicAddresses();
385         addPublicAddresses(addresses);
386     }
387 
388 
389     /**
390      *  Description of the Method
391      *
392      *@param  address  Description of the Parameter
393      *@return          Description of the Return Value
394      */
395     public Address removePublicAddress(PublicAddress address) {
396         Address a = null;
397 
398         if (this.publicAddresses != null) {
399             int i = this.publicAddresses.indexOf(address);
400 
401             if (i > -1) {
402                 a = (Address)this.publicAddresses.remove(i);
403             }
404 
405             if (this.publicAddresses.size() == 0) {
406                 this.publicAddresses = null;
407             }
408         }
409 
410         return a;
411     }
412 
413 
414     /**
415      *  Description of the Method
416      */
417     public void clearPublicAddresses() {
418         if (this.publicAddresses != null) {
419             this.publicAddresses.clear();
420 
421             this.publicAddresses = null;
422         }
423     }
424 
425 
426     /**
427      *  Gets the proxy attribute of the Transport object
428      *
429      *@return    The proxy value
430      */
431     public boolean isProxy() {
432         return this.isProxyEnabled;
433     }
434 
435 
436     /**
437      *  Sets the proxy attribute of the Transport object
438      *
439      *@param  isProxyEnabled  The new proxy value
440      */
441     public void setProxy(boolean isProxyEnabled) {
442         this.isProxyEnabled = isProxyEnabled;
443     }
444 
445 
446     /**
447      *  Gets the proxyAddress attribute of the Transport object
448      *
449      *@return    The proxyAddress value
450      */
451     public ProxyAddress getProxyAddress() {
452         return this.proxy;
453     }
454 
455 
456     /**
457      *  Sets the proxyAddress attribute of the Transport object
458      *
459      *@param  proxy                         The new proxyAddress value
460      *@exception  IllegalArgumentException  Description of the Exception
461      */
462      public void setProxyAddress(ProxyAddress proxy) {
463         this.proxy = proxy;
464     }
465 }
466