View Javadoc

1   package base.jdbs.network.util;
2   
3   /*
4   *  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
5   *  reserved.
6   *
7   *  Redistribution and use in source and binary forms, with or without
8   *  modification, are permitted provided that the following conditions
9   *  are met:
10  *
11  *  1. Redistributions of source code must retain the above copyright
12  *  notice, this list of conditions and the following disclaimer.
13  *
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *  notice, this list of conditions and the following discalimer in
16  *  the documentation and/or other materials provided with the
17  *  distribution.
18  *
19  *  3. The end-user documentation included with the redistribution,
20  *  if any, must include the following acknowledgment:
21  *  "This product includes software developed by the
22  *  Sun Microsystems, Inc. for Project JXTA."
23  *  Alternately, this acknowledgment may appear in the software itself,
24  *  if and wherever such third-party acknowledgments normally appear.
25  *
26  *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
27  *  must not be used to endorse or promote products derived from this
28  *  software without prior written permission. For written
29  *  permission, please contact Project JXTA at http://www.jxta.org.
30  *
31  *  5. Products derived from this software may not be called "JXTA",
32  *  nor may "JXTA" appear in their name, without prior written
33  *  permission of Sun.
34  *
35  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  *  SUCH DAMAGE.
47  *  ====================================================================
48  *
49  *  This software consists of voluntary contributions made by many
50  *  individuals on behalf of Project JXTA.  For more
51  *  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  *  $Id: PipeUtil.java,v 1.1 2006/05/22 18:29:41 junior-skunk Exp $
57  */
58  
59  import java.io.IOException;
60  import java.util.ArrayList;
61  import java.util.Enumeration;
62  import java.util.List;
63  
64  import net.jxta.discovery.DiscoveryListener;
65  import net.jxta.discovery.DiscoveryService;
66  import net.jxta.document.AdvertisementFactory;
67  import net.jxta.id.IDFactory;
68  import net.jxta.peergroup.PeerGroup;
69  import net.jxta.pipe.PipeID;
70  import net.jxta.protocol.PipeAdvertisement;
71  
72  /***
73   *
74   * @version $Id: PipeUtil.java,v 1.1 2006/05/22 18:29:41 junior-skunk Exp $
75   *
76   * @author james todd [gonzo at jxta dot org]
77   */
78  
79  public class PipeUtil {
80  
81  //    private static final Object LOCK = new Object();
82  
83      public static final byte[] GROUP_CHAT_PIPE_ID = {
84          (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1,
85          (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1,
86          (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1,
87          (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1
88      };
89  
90      public static PipeAdvertisement getAdv(PeerGroup pg, String name,
91          String type, byte[] pipeId) {
92          return getAdv(pg, name, type, pipeId, false);
93      }
94  
95      public static PipeAdvertisement getAdv(PeerGroup pg, String name,
96          String type, byte[] pipeId, boolean remote) {
97          PipeAdvertisement pa = searchLocal(pg, name);
98  
99          if (pa == null) {
100             pa = createAdv(pg, name, type, pipeId);
101 
102             publish(pg, pa, remote);
103         }
104 
105         return pa;
106     }
107 
108     public static List getAdvs(PeerGroup pg, String name) {
109         List p = new ArrayList();
110 
111         try {
112             for (Enumeration pas = pg.getDiscoveryService().
113                 getLocalAdvertisements(DiscoveryService.ADV,
114                     PipeAdvertisement.NameTag, name);
115                 pas.hasMoreElements(); ) {
116                 Object o = pas.nextElement();
117 
118                 if (o instanceof PipeAdvertisement) {
119                     p.add((PipeAdvertisement)o);
120                 }
121             }
122         } catch (IOException ioe) {}
123 
124         return p;
125     }
126 
127     public static PipeAdvertisement searchLocal(PeerGroup pg, String name) {
128         PipeAdvertisement pa = null;
129 
130         try {
131             for (Enumeration pas = pg.getDiscoveryService().
132                 getLocalAdvertisements(DiscoveryService.ADV,
133                     PipeAdvertisement.NameTag, name);
134                 pas.hasMoreElements(); ) {
135                 pa = (PipeAdvertisement)pas.nextElement();
136 
137                 if (pa.getName().equals(name)) {
138                     break;
139                 } else {
140                     pa = null;
141                 }
142             }
143         } catch (IOException ioe) {}
144 
145         return pa;
146     }
147 
148     public static void discoverAdvs(PeerGroup pg, String name,
149         DiscoveryListener listener) {
150         DiscoveryService s = pg.getDiscoveryService();
151 
152         s.getRemoteAdvertisements(null, DiscoveryService.ADV,
153             name != null ? PipeAdvertisement.NameTag : null,
154             name, 10, listener);
155     }
156 
157     public static PipeAdvertisement createAdv(PeerGroup pg, String name,
158         String type) {
159         return createAdv(pg, name, type, null);
160     }
161 
162     public static PipeAdvertisement createAdv(PeerGroup pg, String name,
163         String type, byte[] id) {
164         PipeAdvertisement pa = (PipeAdvertisement)AdvertisementFactory.
165             newAdvertisement(PipeAdvertisement.getAdvertisementType());
166 
167         pa.setPipeID(id != null ?
168             (PipeID)IDFactory.newPipeID(pg.getPeerGroupID(), id) :
169             (PipeID)IDFactory.newPipeID(pg.getPeerGroupID()));
170         pa.setName(name);
171         pa.setType(type);
172 
173         return pa;
174     }
175 
176     public static void publish(PeerGroup pg, PipeAdvertisement pa) {
177         publish(pg, pa, false);
178     }
179 
180     public static void publish(PeerGroup pg, PipeAdvertisement pa,
181         boolean remote) {
182         DiscoveryService ds = pg.getDiscoveryService();
183 
184         try {
185             ds.publish(pa);
186         } catch (IOException ioe) {}
187 
188         if (remote) {
189             // xxx: no remote publishing
190             // ds.remotePublish(pa, DiscoveryService.ADV);
191         }
192     }
193 }