]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/id/binaryID/PeerGroupBinaryID.java
59477bd5adcc5905518a33e05597cbf0599de3f3
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / id / binaryID / PeerGroupBinaryID.java
1 /*
2  * Copyright (c) 2001-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 package net.jxta.impl.id.binaryID;
57
58
59 import java.net.URI;
60 import java.util.logging.Level;
61 import net.jxta.logging.Logging;
62 import java.util.logging.Logger;
63 import net.jxta.id.ID;
64
65
66 /**
67  * This class implements a PeerGroup ID. Each peer group is assigned a unique
68  * peer id.BinaryID id are used to implement peer group id. Because this id is
69  * built with BinaryID, pulling the parent group requires a little work. The
70  * parent group is the first id, with the second following, separated by a
71  * dash '-' character.<p>
72  *
73  * @author Daniel Brookshier <a HREF="mailto:turbogeek@cluck.com">turbogeek@cluck.com</a>
74  * @see net.jxta.id.ID
75  * @see net.jxta.id.IDFactory
76  * @see net.jxta.peergroup.PeerGroupID
77  */
78 public final class PeerGroupBinaryID extends net.jxta.peergroup.PeerGroupID {
79
80     /**
81      * LOG object for this class.
82      */
83     private final static transient Logger LOG = Logger.getLogger(PeerGroupBinaryID.class.getName());
84
85     /**
86      * This is the id string used in the XML of the id. The format is TX0..Xn where T is the type and X0 through Xn are the base64 encoded id.
87      */
88     protected String id;
89
90     /**
91      * Constructor for creating a new PeerGroupID with a unique ID and a parent.<p>
92      * <p/>
93      * Note that only the ID for the parent is obtained and not the
94      * parent and the grandparent.
95      *
96      * @param parent         Parent peer group.
97      * @param data           data byte array to be used as the id.
98      * @param lengthIncluded If true, the first byte in the data array is the length of the remaining bytes.
99      */
100     public PeerGroupBinaryID(net.jxta.peergroup.PeerGroupID parent, byte[] data, boolean lengthIncluded) {
101         this();
102
103         String parentStr = IDFormat.childGroup(parent);
104
105         if (parentStr != null) {
106             id = BinaryIDFactory.newBinaryID(BinaryID.flagPeerGroupID, data, lengthIncluded).getID() + "."
107                     + parentStr.replace('-', '.');
108         } else {
109             id = BinaryIDFactory.newBinaryID(BinaryID.flagPeerGroupID, data, lengthIncluded).getID();
110         }
111     }
112
113     /**
114      * Creates a ID from a string. Note that the ID is not currently validated.
115      *
116      * @param id Value of ID.
117      */
118
119     protected PeerGroupBinaryID(String id) {
120         super();
121         this.id = id;
122     }
123
124     /**
125      * Constructor for creating a new PeerGroupID with a unique ID and a parent.
126      *
127      * @param data           DOCUMENT ME!
128      * @param lengthIncluded DOCUMENT ME!
129      */
130     public PeerGroupBinaryID(byte[] data, boolean lengthIncluded) {
131         this();
132         id = BinaryIDFactory.newBinaryID(BinaryID.flagPeerGroupID, data, lengthIncluded).getID();
133     }
134
135     /**
136      * Constructor for creating a new PeerGroupID. Note that this creates an
137      * invalid ID but is required for serialization.
138      */
139     public PeerGroupBinaryID() {
140         super();
141     }
142
143     /**
144      * Constructor. Intializes contents from provided ID. This PeerGroupID has
145      * no parent.
146      *
147      * @param id the ID data
148      */
149     public PeerGroupBinaryID(BinaryID id) {
150         super();
151         this.id = id.getID();
152     }
153
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public boolean equals(Object target) {
159         if (this == target) {
160             return true;
161         }
162         
163         return target instanceof PeerGroupBinaryID && getUniqueValue().equals(((PeerGroupBinaryID) target).getUniqueValue());
164     }
165
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public int hashCode() {
171         return getUniqueValue().hashCode();
172     }
173
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public String getIDFormat() {
179         return IDFormat.INSTANTIATOR.getSupportedIDFormat();
180     }
181
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public Object getUniqueValue() {
187         if (null == id) { 
188             return ID.nullID.getUniqueValue();
189         }
190
191         return getIDFormat() + "-" + id;
192     }
193
194     /**
195      * {@inheritDoc}
196      */
197     public net.jxta.id.ID getPeerGroupID() {
198         // convert to the generic world PGID as necessary
199         return IDFormat.translateToWellKnown(this);
200     }
201
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public net.jxta.peergroup.PeerGroupID getParentPeerGroupID() {
207         net.jxta.peergroup.PeerGroupID result = null;
208
209         try {
210             if (id == null) {
211                 result = (net.jxta.peergroup.PeerGroupID) net.jxta.id.ID.nullID;
212             }
213             String idd = id;
214             int parentStart = idd.indexOf('.');
215
216             if (parentStart != -1) {
217                 idd = idd.substring(parentStart + 1);
218             } else {
219                 result = null;
220             }
221             URI url = new URI("urn:jxta:" + idd.replace('.', '-'));
222             net.jxta.peergroup.PeerGroupID peerGroupID = (net.jxta.peergroup.PeerGroupID) net.jxta.id.IDFactory.fromURI(url);
223
224             result = (net.jxta.peergroup.PeerGroupID) IDFormat.translateToWellKnown(peerGroupID);
225         } catch (Exception e) {
226             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
227                 LOG.warning("cannot convert sub group. ID value = " + id);
228             }
229             result = null;
230
231         }
232         // LOG.error("getParentPeerGroupID():"+result);
233         return result;
234     }
235
236     /**
237      * returns the coded ID without the binaryid tag.
238      *
239      * @return The coded ID without the binaryid tag.
240      */
241     protected String getID() {
242         return id;
243     }
244 }