]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/id/binaryID/Instantiator.java
f4934674005661eb16fcc4473cdd08aee3843f8e
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / id / binaryID / Instantiator.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
57 package net.jxta.impl.id.binaryID;
58
59
60 import java.io.InputStream;
61 import java.net.URI;
62 import java.net.URL;
63 import java.security.SecureRandom;
64 import java.util.Random;
65 import java.util.logging.Level;
66 import java.util.logging.Logger;
67
68 import java.io.IOException;
69 import java.net.MalformedURLException;
70 import java.net.URISyntaxException;
71 import java.net.UnknownServiceException;
72
73 import net.jxta.peergroup.PeerGroupID;
74
75
76 /**
77  * ID Factory for the binary ID type. All identifiers in this type are prefixed by "binaryid".
78  *
79  * @author Daniel Brookshier <a HREF="mailto:turbogeek@cluck.com">turbogeek@cluck.com</a>
80  */
81
82 public final class Instantiator implements net.jxta.id.IDFactory.URIInstantiator {
83
84     /**
85      * LOG object for this class.
86      */
87     private final static transient Logger LOG = Logger.getLogger(Instantiator.class.getName());
88  
89     /**
90      * Our ID Format
91      */
92     final static String BinaryIDEncoded = "binaryid";
93
94     /**
95      * Random generator used for ID creation where a seed (idValue) is not provided.
96      */
97     private static final Random randNumGenerator = new SecureRandom();
98
99     /**
100      * {@inheritDoc}
101      */
102     public String getSupportedIDFormat() {
103         return BinaryIDEncoded;
104     }
105
106     /**
107      * {@inheritDoc}
108      */
109     public net.jxta.id.ID fromURL(URL source) throws MalformedURLException, UnknownServiceException {
110
111         // check the protocol
112         if (!net.jxta.id.ID.URIEncodingName.equalsIgnoreCase(source.getProtocol())) {
113             throw new UnknownServiceException("URI protocol type was not as expected.");
114         }
115
116         String encoded = source.getFile();
117
118         int colonAt = encoded.indexOf(':');
119
120         // There's a colon right?
121         if (-1 == colonAt) {
122             throw new UnknownServiceException("URN namespace was missing.");
123         }
124
125         // check the namespace
126         if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(encoded.substring(0, colonAt))) {
127             throw new UnknownServiceException("URN namespace was not as expected.");
128         }
129
130         // skip the namespace portion and the colon
131         encoded = encoded.substring(colonAt + 1);
132
133         try {
134             return fromURNNamespaceSpecificPart(encoded);
135         } catch (URISyntaxException failed) {
136             MalformedURLException failure = new MalformedURLException("Failure parsing URL");
137
138             failure.initCause(failed);
139             
140             throw failure;
141         }
142     }
143         
144     /**
145      * {@inheritDoc}
146      */
147     public net.jxta.id.ID fromURI(URI source) throws URISyntaxException {
148         
149         // check the protocol
150         if (!net.jxta.id.ID.URIEncodingName.equalsIgnoreCase(source.getScheme())) {
151             throw new URISyntaxException(source.toString(), "URI scheme was not as expected.");
152         }
153         
154         String decoded = source.getSchemeSpecificPart();
155         
156         int colonAt = decoded.indexOf(':');
157         
158         // There's a colon right?
159         if (-1 == colonAt) {
160             throw new URISyntaxException(source.toString(), "URN namespace was missing.");
161         }
162         
163         // check the namespace
164         if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(decoded.substring(0, colonAt))) {
165             throw new URISyntaxException(source.toString()
166                     ,
167                     "URN namespace was not as expected. (" + net.jxta.id.ID.URNNamespace + "!=" + decoded.substring(0, colonAt)
168                     + ")");
169         }
170         
171         // skip the namespace portion and the colon
172         decoded = decoded.substring(colonAt + 1);
173         
174         return fromURNNamespaceSpecificPart(decoded);
175     }
176     
177     /**
178      * {@inheritDoc}
179      */
180     public net.jxta.id.ID fromURNNamespaceSpecificPart(String encoded) throws URISyntaxException {
181         int dashAt = encoded.indexOf('-');
182
183         // there's a dash, right?
184         if (-1 == dashAt) {
185             throw new URISyntaxException(encoded, "URN Encodingtype was missing.");
186         }
187
188         if (!encoded.substring(0, dashAt).equals(BinaryIDEncoded)) {
189             throw new URISyntaxException(encoded
190                     ,
191                     "JXTA id format was not as expected. Should have been BinaryIDEncoded found:" + encoded.substring(0, dashAt));
192         }
193
194         // skip the dash
195         encoded = encoded.substring(dashAt + 1);
196         // check that the length is long enough
197         if (encoded.length() < 1) {
198             throw new URISyntaxException(encoded, "URN does not contain enough chars. Must have at least one byte");
199         }
200         BinaryID id = new BinaryID(encoded);
201         net.jxta.id.ID result = null;
202
203         switch (id.type()) {
204
205         case BinaryID.flagCodatID:
206             result = new CodatBinaryID(encoded);
207             break;
208
209         case BinaryID.flagPeerGroupID:
210             result = new PeerGroupBinaryID(encoded);
211             if (PeerGroupID.worldPeerGroupID.equals(result)) {
212                 result = net.jxta.peergroup.PeerGroupID.worldPeerGroupID;
213             }
214             break;
215
216         case BinaryID.flagPeerID:
217             result = new PeerBinaryID(encoded);
218             break;
219
220         case BinaryID.flagPipeID:
221             result = new PipeBinaryID(encoded);
222             break;
223
224         case BinaryID.flagModuleClassID:
225             result = new ModuleClassBinaryID(encoded);
226             break;
227
228         case BinaryID.flagModuleSpecID:
229             result = new ModuleSpecBinaryID(encoded);
230             break;
231
232         default:
233             throw new URISyntaxException(encoded, "jxta ID type not recognized");
234         }
235
236         return result;
237     }
238
239     /**
240      * Utility to create a random array of bits to be used when a random value is required.
241      */
242     private byte[] randomID() {
243         byte[] randBuf16 = new byte[16];
244
245         randNumGenerator.nextBytes(randBuf16);
246
247         return randBuf16;
248     }
249
250     /**
251      * {@inheritDoc}
252      *
253      * @throws UnsupportedOperationException This form is not supported. Use CODAT from UUID package instead.
254      */
255     public net.jxta.codat.CodatID newCodatID(final net.jxta.peergroup.PeerGroupID groupID) {
256         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
257
258         return new net.jxta.impl.id.binaryID.CodatBinaryID(parentGroupID, randomID(), false);
259         // throw new UnsupportedOperationException("This form is not supported. Use CODAT from UUID package instead.");
260     }
261
262     /**
263      * {@inheritDoc}
264      *
265      * @throws UnsupportedOperationException This form is not supported. Use CODAT from UUID package instead.
266      */
267     public net.jxta.codat.CodatID newCodatID(final net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
268         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
269
270         return new net.jxta.impl.id.binaryID.CodatBinaryID(parentGroupID, seed, false);
271     }
272
273     /**
274      * {@inheritDoc}
275      *
276      * @throws UnsupportedOperationException This form is not supported. Use CODAT from UUID package instead.
277      */
278     public net.jxta.codat.CodatID newCodatID(final net.jxta.peergroup.PeerGroupID groupID, InputStream in) throws IOException {
279         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
280
281         return new net.jxta.impl.id.binaryID.CodatBinaryID(parentGroupID, randomID(), false);
282     }
283
284     /**
285      * {@inheritDoc}
286      *
287      * @throws UnsupportedOperationException This form is not supported. Use CODAT from UUID package instead.
288      */
289     public net.jxta.codat.CodatID newCodatID(final net.jxta.peergroup.PeerGroupID groupID, byte[] idValue, InputStream in) throws IOException {
290         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
291
292         return new net.jxta.impl.id.binaryID.CodatBinaryID(parentGroupID, idValue, false);
293     }
294
295     /**
296      * {@inheritDoc}
297      */
298     public net.jxta.peer.PeerID newPeerID(final net.jxta.peergroup.PeerGroupID groupID) {
299         LOG.log(Level.SEVERE, "random peer created", new RuntimeException());
300         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
301
302         return new net.jxta.impl.id.binaryID.PeerBinaryID(parentGroupID, randomID(), false);
303     }
304
305     /**
306      * {@inheritDoc}
307      */
308     public net.jxta.peer.PeerID newPeerID(final net.jxta.peergroup.PeerGroupID groupID, byte[] idValue) {
309         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
310
311         return new net.jxta.impl.id.binaryID.PeerBinaryID(parentGroupID, idValue, false);
312     }
313
314     /**
315      * {@inheritDoc}
316      */
317     public net.jxta.peergroup.PeerGroupID newPeerGroupID() {
318         return net.jxta.id.IDFactory.newPeerGroupID(randomID());
319     }
320
321     /**
322      * {@inheritDoc}
323      */
324     public net.jxta.peergroup.PeerGroupID newPeerGroupID(byte[] idValue) {
325         return new PeerGroupBinaryID(idValue, false);
326     }
327
328     /**
329      * {@inheritDoc}
330      */
331     public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent) {
332         LOG.log(Level.SEVERE, "random peergroup created", new RuntimeException());
333         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(parent);
334
335         return net.jxta.id.IDFactory.newPeerGroupID(parentGroupID, randomID());
336     }
337
338     /**
339      * {@inheritDoc}
340      */
341     public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent, byte[] idValue) {
342         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(parent);
343
344         return new PeerGroupBinaryID(parentGroupID, idValue, false);
345     }
346
347     /**
348      * {@inheritDoc}
349      */
350     public net.jxta.pipe.PipeID newPipeID(final net.jxta.peergroup.PeerGroupID groupID) {
351         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
352
353         return net.jxta.id.IDFactory.newPipeID(parentGroupID, randomID());
354     }
355
356     /**
357      * {@inheritDoc}
358      */
359     public net.jxta.pipe.PipeID newPipeID(final net.jxta.peergroup.PeerGroupID groupID, byte[] idValue) {
360         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
361
362         return new PipeBinaryID(peerGroupID, idValue, false);
363     }
364
365     /**
366      * {@inheritDoc}
367      *
368      * @throws UnsupportedOperationException This form is not supported because a binary ID is meant to be created with a random ID.
369      */
370     public net.jxta.platform.ModuleClassID newModuleClassID() {
371         throw new UnsupportedOperationException(
372                 "This form is not supported because a binary ID is meant to be created with a random ID. Use UUID package instead.");
373     }
374
375     /**
376      * {@inheritDoc}
377      *
378      * @throws UnsupportedOperationException This form is not supported because a binary ID is meant to be created with a random ID.
379      */
380     public net.jxta.platform.ModuleClassID newModuleClassID(final net.jxta.platform.ModuleClassID classID) {
381         throw new UnsupportedOperationException(
382                 "This form is not supported because a binary ID is meant to be created with a random ID. Use UUID package instead.");
383     }
384
385     /**
386      * {@inheritDoc}
387      *
388      * @throws UnsupportedOperationException This form is not supported because a binary ID is meant to be created with a random ID. Use UUID instead.
389      */
390     public net.jxta.platform.ModuleSpecID newModuleSpecID(final net.jxta.platform.ModuleClassID classID) {
391         throw new UnsupportedOperationException(
392                 "This form is not supported because a binary ID is meant to be created with a random ID. Use UUID package instead.");
393     }
394
395 }