]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/id/jxta/Instantiator.java
cb6f438d8edb2b212a6ebf6a8c22ba463d1d412d
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / id / jxta / 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.id.jxta;
58
59
60 import net.jxta.codat.CodatID;
61 import net.jxta.id.ID;
62 import net.jxta.peer.PeerID;
63 import net.jxta.peergroup.PeerGroupID;
64 import net.jxta.pipe.PipeID;
65 import net.jxta.platform.ModuleClassID;
66 import net.jxta.platform.ModuleSpecID;
67
68 import java.io.IOException;
69 import java.io.InputStream;
70 import java.net.MalformedURLException;
71 import java.net.URI;
72 import java.net.URISyntaxException;
73 import java.net.URL;
74 import java.net.UnknownServiceException;
75 import java.security.ProviderException;
76
77
78 /**
79  * Instantiator for the 'jxta' ID Format.
80  *
81  * @see net.jxta.id.ID
82  * @see net.jxta.id.IDFactory
83  * @see net.jxta.id.jxta.IDFormat
84  */
85 final class Instantiator implements net.jxta.id.IDFactory.URIInstantiator {
86
87     /**
88      * This table maps the textual values of the well known ids to the
89      * singleton classes which match those textual names.
90      */
91     final static Object[][] wellKnownIDs = {
92         { net.jxta.id.ID.nullID.getUniqueValue(), net.jxta.id.ID.nullID}
93                 ,
94
95         { net.jxta.peergroup.PeerGroupID.worldPeerGroupID.getUniqueValue(), net.jxta.peergroup.PeerGroupID.worldPeerGroupID}
96                 ,
97
98         {
99             net.jxta.peergroup.PeerGroupID.defaultNetPeerGroupID.getUniqueValue()
100                     ,
101             net.jxta.peergroup.PeerGroupID.defaultNetPeerGroupID}
102     };
103
104     /**
105      * {@inheritDoc}
106      */
107     public String getSupportedIDFormat() {
108         return IDFormat.JXTAFormat;
109     }
110
111     /**
112      * {@inheritDoc}
113      */
114     @Deprecated
115     public ID fromURL(URL source) throws MalformedURLException, UnknownServiceException {
116
117         // check the protocol
118         if (!ID.URIEncodingName.equalsIgnoreCase(source.getProtocol())) {
119             throw new UnknownServiceException("URI protocol type was not as expected.");
120         }
121
122         String encoded = source.getFile();
123
124         int colonAt = encoded.indexOf(':');
125
126         // There's a colon right?
127         if (-1 == colonAt) {
128             throw new UnknownServiceException("URN namespace was missing.");
129         }
130
131         // check the namespace
132         if (!ID.URNNamespace.equalsIgnoreCase(encoded.substring(0, colonAt))) {
133             throw new UnknownServiceException("URN namespace was not as expected.");
134         }
135
136         // skip the namespace portion and the colon
137         encoded = encoded.substring(colonAt + 1);
138
139         int dashAt = encoded.indexOf('-');
140
141         // there's a dash, right?
142         if (-1 == dashAt) {
143             throw new UnknownServiceException("JXTA ID Format was missing.");
144         }
145
146         if (!encoded.substring(0, dashAt).equals(getSupportedIDFormat())) {
147             throw new UnknownServiceException("JXTA ID Format was not as expected.");
148         }
149
150         for (Object[] wellKnownID : wellKnownIDs) {
151             if (encoded.equalsIgnoreCase(wellKnownID[0].toString())) {
152                 return (ID) wellKnownID[1];
153             }
154         }
155
156         throw new MalformedURLException("unrecognized id");
157     }
158
159     /**
160      * {@inheritDoc}
161      */
162     public CodatID newCodatID(PeerGroupID groupID) {
163         throw new ProviderException("unsupported id type");
164     }
165
166     /**
167      * {@inheritDoc}
168      */
169     public CodatID newCodatID(PeerGroupID groupID, byte[] seed) {
170         throw new ProviderException("unsupported id type");
171     }
172
173     /**
174      * {@inheritDoc}
175      */
176     public CodatID newCodatID(PeerGroupID groupID, InputStream in) throws IOException {
177         throw new ProviderException("unsupported id type");
178     }
179
180     /**
181      * {@inheritDoc}
182      */
183     public CodatID newCodatID(PeerGroupID groupID, byte[] seed, InputStream in) throws IOException {
184         throw new ProviderException("unsupported id type");
185     }
186
187     /**
188      * {@inheritDoc}
189      */
190     public PeerID newPeerID(PeerGroupID groupID) {
191         throw new ProviderException("unsupported id type");
192     }
193
194     /**
195      * {@inheritDoc}
196      */
197     public PeerID newPeerID(PeerGroupID groupID, byte[] seed) {
198         throw new ProviderException("unsupported id type");
199     }
200
201     /**
202      * {@inheritDoc}
203      */
204     public net.jxta.peergroup.PeerGroupID newPeerGroupID() {
205         throw new ProviderException("unsupported id type");
206     }
207
208     /**
209      * {@inheritDoc}
210      */
211     public net.jxta.peergroup.PeerGroupID newPeerGroupID(byte[] seed) {
212         throw new ProviderException("unsupported id type");
213     }
214
215     /**
216      * {@inheritDoc}
217      */
218     public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent) {
219         throw new ProviderException("unsupported id type");
220     }
221
222     /**
223      * {@inheritDoc}
224      */
225     public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent, byte[] seed) {
226         throw new ProviderException("unsupported id type");
227     }
228
229     /**
230      * {@inheritDoc}
231      */
232     public PipeID newPipeID(PeerGroupID groupID) {
233         throw new ProviderException("unsupported id type");
234     }
235
236     /**
237      * {@inheritDoc}
238      */
239     public PipeID newPipeID(PeerGroupID groupID, byte[] seed) {
240         throw new ProviderException("unsupported id type");
241     }
242
243     /**
244      * {@inheritDoc}
245      */
246     public ModuleClassID newModuleClassID() {
247         throw new ProviderException("unsupported id type");
248     }
249
250     /**
251      * {@inheritDoc}
252      */
253     public ModuleClassID newModuleClassID(ModuleClassID classID) {
254         throw new ProviderException("unsupported id type");
255     }
256
257     /**
258      * {@inheritDoc}
259      */
260     public ModuleSpecID newModuleSpecID(ModuleClassID classID) {
261         throw new ProviderException("unsupported id type");
262     }
263
264     /**
265      * {@inheritDoc}
266      */
267     public ID fromURI(URI source) throws URISyntaxException {
268
269         // check the protocol
270         if (!ID.URIEncodingName.equalsIgnoreCase(source.getScheme())) {
271             throw new URISyntaxException(source.toString(), "URI scheme was not as expected.");
272         }
273
274         String decoded = source.getSchemeSpecificPart();
275
276         int colonAt = decoded.indexOf(':');
277
278         // There's a colon right?
279         if (-1 == colonAt) {
280             throw new URISyntaxException(source.toString(), "URN namespace was missing.");
281         }
282
283         // check the namespace
284         if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(decoded.substring(0, colonAt))) {
285             throw new URISyntaxException(source.toString()
286                     ,
287                     "URN namespace was not as expected. (" + net.jxta.id.ID.URNNamespace + "!=" + decoded.substring(0, colonAt)
288                     + ")");
289         }
290
291         // skip the namespace portion and the colon
292         decoded = decoded.substring(colonAt + 1);
293
294         return fromURNNamespaceSpecificPart(decoded);
295     }
296
297     /**
298      * {@inheritDoc}
299      */
300     public ID fromURNNamespaceSpecificPart(String source) throws URISyntaxException {
301         int dashAt = source.indexOf('-');
302
303         // there's a dash, right?
304         if (-1 == dashAt) {
305             throw new URISyntaxException(source, "URN jxta namespace IDFormat was missing.");
306         }
307
308         if (!source.substring(0, dashAt).equals(getSupportedIDFormat())) {
309             throw new URISyntaxException(source, "JXTA ID Format was not as expected.");
310         }
311
312         for (Object[] wellKnownID : wellKnownIDs) {
313             if (source.equalsIgnoreCase(wellKnownID[0].toString())) {
314                 return (ID) wellKnownID[1];
315             }
316         }
317
318         throw new URISyntaxException(source, "unrecognized id");
319     }
320 }