2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
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.
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.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
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.
48 * ====================================================================
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.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.id.jxta;
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;
68 import java.io.IOException;
69 import java.io.InputStream;
70 import java.net.MalformedURLException;
72 import java.net.URISyntaxException;
74 import java.net.UnknownServiceException;
75 import java.security.ProviderException;
79 * Instantiator for the 'jxta' ID Format.
82 * @see net.jxta.id.IDFactory
83 * @see net.jxta.id.jxta.IDFormat
85 final class Instantiator implements net.jxta.id.IDFactory.URIInstantiator {
88 * This table maps the textual values of the well known ids to the
89 * singleton classes which match those textual names.
91 final static Object[][] wellKnownIDs = {
92 { net.jxta.id.ID.nullID.getUniqueValue(), net.jxta.id.ID.nullID}
95 { net.jxta.peergroup.PeerGroupID.worldPeerGroupID.getUniqueValue(), net.jxta.peergroup.PeerGroupID.worldPeerGroupID}
99 net.jxta.peergroup.PeerGroupID.defaultNetPeerGroupID.getUniqueValue()
101 net.jxta.peergroup.PeerGroupID.defaultNetPeerGroupID}
107 public String getSupportedIDFormat() {
108 return IDFormat.JXTAFormat;
115 public ID fromURL(URL source) throws MalformedURLException, UnknownServiceException {
117 // check the protocol
118 if (!ID.URIEncodingName.equalsIgnoreCase(source.getProtocol())) {
119 throw new UnknownServiceException("URI protocol type was not as expected.");
122 String encoded = source.getFile();
124 int colonAt = encoded.indexOf(':');
126 // There's a colon right?
128 throw new UnknownServiceException("URN namespace was missing.");
131 // check the namespace
132 if (!ID.URNNamespace.equalsIgnoreCase(encoded.substring(0, colonAt))) {
133 throw new UnknownServiceException("URN namespace was not as expected.");
136 // skip the namespace portion and the colon
137 encoded = encoded.substring(colonAt + 1);
139 int dashAt = encoded.indexOf('-');
141 // there's a dash, right?
143 throw new UnknownServiceException("JXTA ID Format was missing.");
146 if (!encoded.substring(0, dashAt).equals(getSupportedIDFormat())) {
147 throw new UnknownServiceException("JXTA ID Format was not as expected.");
150 for (Object[] wellKnownID : wellKnownIDs) {
151 if (encoded.equalsIgnoreCase(wellKnownID[0].toString())) {
152 return (ID) wellKnownID[1];
156 throw new MalformedURLException("unrecognized id");
162 public CodatID newCodatID(PeerGroupID groupID) {
163 throw new ProviderException("unsupported id type");
169 public CodatID newCodatID(PeerGroupID groupID, byte[] seed) {
170 throw new ProviderException("unsupported id type");
176 public CodatID newCodatID(PeerGroupID groupID, InputStream in) throws IOException {
177 throw new ProviderException("unsupported id type");
183 public CodatID newCodatID(PeerGroupID groupID, byte[] seed, InputStream in) throws IOException {
184 throw new ProviderException("unsupported id type");
190 public PeerID newPeerID(PeerGroupID groupID) {
191 throw new ProviderException("unsupported id type");
197 public PeerID newPeerID(PeerGroupID groupID, byte[] seed) {
198 throw new ProviderException("unsupported id type");
204 public net.jxta.peergroup.PeerGroupID newPeerGroupID() {
205 throw new ProviderException("unsupported id type");
211 public net.jxta.peergroup.PeerGroupID newPeerGroupID(byte[] seed) {
212 throw new ProviderException("unsupported id type");
218 public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent) {
219 throw new ProviderException("unsupported id type");
225 public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent, byte[] seed) {
226 throw new ProviderException("unsupported id type");
232 public PipeID newPipeID(PeerGroupID groupID) {
233 throw new ProviderException("unsupported id type");
239 public PipeID newPipeID(PeerGroupID groupID, byte[] seed) {
240 throw new ProviderException("unsupported id type");
246 public ModuleClassID newModuleClassID() {
247 throw new ProviderException("unsupported id type");
253 public ModuleClassID newModuleClassID(ModuleClassID classID) {
254 throw new ProviderException("unsupported id type");
260 public ModuleSpecID newModuleSpecID(ModuleClassID classID) {
261 throw new ProviderException("unsupported id type");
267 public ID fromURI(URI source) throws URISyntaxException {
269 // check the protocol
270 if (!ID.URIEncodingName.equalsIgnoreCase(source.getScheme())) {
271 throw new URISyntaxException(source.toString(), "URI scheme was not as expected.");
274 String decoded = source.getSchemeSpecificPart();
276 int colonAt = decoded.indexOf(':');
278 // There's a colon right?
280 throw new URISyntaxException(source.toString(), "URN namespace was missing.");
283 // check the namespace
284 if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(decoded.substring(0, colonAt))) {
285 throw new URISyntaxException(source.toString()
287 "URN namespace was not as expected. (" + net.jxta.id.ID.URNNamespace + "!=" + decoded.substring(0, colonAt)
291 // skip the namespace portion and the colon
292 decoded = decoded.substring(colonAt + 1);
294 return fromURNNamespaceSpecificPart(decoded);
300 public ID fromURNNamespaceSpecificPart(String source) throws URISyntaxException {
301 int dashAt = source.indexOf('-');
303 // there's a dash, right?
305 throw new URISyntaxException(source, "URN jxta namespace IDFormat was missing.");
308 if (!source.substring(0, dashAt).equals(getSupportedIDFormat())) {
309 throw new URISyntaxException(source, "JXTA ID Format was not as expected.");
312 for (Object[] wellKnownID : wellKnownIDs) {
313 if (source.equalsIgnoreCase(wellKnownID[0].toString())) {
314 return (ID) wellKnownID[1];
318 throw new URISyntaxException(source, "unrecognized id");