001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: *
024: */
025: package com.sun.cdc.io;
026:
027: import java.io.*;
028: import javax.microedition.io.*;
029: import sun.security.action.GetPropertyAction;
030:
031: public class InternalConnectorImpl implements InternalConnector {
032: protected ClassLoader protocolClassLoader;
033: protected String classRoot;
034:
035: protected String getClassRoot() {
036: if (classRoot != null) {
037: return classRoot;
038: }
039:
040: String profileTemp = null;
041:
042: try {
043: /*
044: * Check to see if there is a property override for the dynamic
045: * building of class root.
046: */
047: classRoot = (String) java.security.AccessController
048: .doPrivileged(new GetPropertyAction(
049: "javax.microedition.io.Connector.protocolpath"));
050: } catch (Throwable t) {
051: // do nothing
052: }
053:
054: if (classRoot == null) {
055: classRoot = "com.sun.cdc.io";
056: }
057:
058: return classRoot;
059: }
060:
061: protected ClassLoader getProtocolClassLoader() {
062: if (protocolClassLoader != null) {
063: return protocolClassLoader;
064: }
065:
066: protocolClassLoader = this .getClass().getClassLoader();
067: return protocolClassLoader;
068: }
069:
070: /**
071: * Create and open a Connection.
072: *
073: * @param string The URL for the connection
074: * @param mode The access mode
075: * @param timeouts A flag to indicate that the caller
076: * wants timeout exceptions
077: * @param platform Platform name
078: * @return A new Connection object
079: *
080: * @exception ClassNotFoundException If the protocol cannot be found.
081: * @exception IllegalArgumentException If a parameter is invalid.
082: * @exception ConnectionNotFoundException If the target of the
083: * name cannot be found, or if the requested protocol type
084: * is not supported.
085: * @exception IOException If some other kind of I/O error occurs.
086: * @exception IllegalArgumentException If a parameter is invalid.
087: */
088:
089: public Connection open(String name, int mode, boolean timeouts)
090: throws IOException {
091: /* Test for null argument */
092: if (name == null) {
093: throw new IllegalArgumentException("Null URL");
094: }
095:
096: /* Look for : as in "http:", "file:", or whatever */
097: int colon = name.indexOf(':');
098:
099: /* Test for null argument */
100: if (colon < 1) {
101: throw new IllegalArgumentException("no ':' in URL");
102: }
103: try {
104: String protocol;
105:
106: /* Strip off the protocol name */
107: protocol = name.substring(0, colon);
108:
109: /* Strip off the rest of the string */
110: name = name.substring(colon + 1);
111:
112: /*
113: * Convert all the '-' characters in the protocol
114: * name to '_' characters (dashes are not allowed
115: * in class names). This operation creates garbage
116: * only if the protocol name actually contains dashes
117: */
118: protocol = protocol.replace('-', '_');
119:
120: String className = (String) java.security.AccessController
121: .doPrivileged(new GetPropertyAction("j2me."
122: + protocol + ".Protocol"));
123: /*
124: * Use the platform and protocol names to look up
125: * a class to implement the connection
126: */
127: if (className == null) {
128: className = getClassRoot() + "." + "j2me" + "."
129: + protocol + ".Protocol";
130: }
131: Class clazz = Class.forName(className, true,
132: getProtocolClassLoader());
133:
134: /* Construct a new instance of the protocol */
135: ConnectionBaseInterface uc = (ConnectionBaseInterface) clazz
136: .newInstance();
137:
138: /* Open the connection, and return it */
139: return uc.openPrim(name, mode, timeouts);
140: } catch (InstantiationException x) {
141: throw new IOException(x.toString());
142: } catch (IllegalAccessException x) {
143: throw new IOException(x.toString());
144: } catch (ClassCastException x) {
145: throw new IOException(x.toString());
146: } catch (ClassNotFoundException x) {
147: throw new ConnectionNotFoundException(
148: "The requested protocol does not exist " + name);
149: }
150: }
151: }
|