📄 internalconnector.java
字号:
/* * @(#)InternalConnector.java 1.13 01/08/21 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package com.sun.midp.io;import javax.microedition.io.*;import com.sun.cldc.io.*;import com.sun.midp.Configuration;import com.sun.midp.io.j2me.http.Protocol;import java.io.IOException;/** * This class provides access to the internal only connection * types needed by the implementations of the supported subsystems. * If bypasses the javax.microedition. */public class InternalConnector { /* * Implementation note. the open parameter is used to form a class name * in the form: * <p> * <code>com.sun.midp.io.{platform}.{protocol}.Protocol</code> or * <code>com.sun.cldc.io.{platform}.{protocol}.Protocol</code> * <p> * The platform name is derived from the system by looking for the * system property "j2me.platform". * If this property key is not found or the associated class is not * present then one of two default * directories are used. These are called "j2me" and "j2se". If the * property "j2me.configuration" * is non-null then "j2me" is used, otherwise "j2se" is assumed. * <p> * The system property "microedition.platform" can be used to * change the root of the class space * used to lookup the classes. * <p> * The protocol name is derived from the parameter string describing * the target of the * connection. This takes the from: * <p> * <code> {protocol}:[{target}][ {parms}] </code> * <p> * The protocol name is used to find the appropriate class and * stripped from the target * name before being passed to open() method of a new installation * of the class. */ /** * The platform name */ private static String platform; /** * True if we are running on a J2ME system (defaults to false) */ private static boolean j2me; /* * The roots of the Protocol classes. * Each is tried in turn by open*. */ /** root for MIDP io packages */ private static String midpRoot; /** root for CLDC io packagaes. */ private static String cldcRoot; /** * If enableAllProtocols is true, then open(...) * will allow applications to use non-MIDP protocols. */ private static boolean enableAllProtocols; /** * Class initializer */ static { String prop; /* Work out if we are running on a J2ME system */ if (Configuration.getProperty("microedition.configuration") != null) { j2me = true; } /* Setup the platform name */ platform = Configuration.getProperty("microedition.platform"); if (platform == null) { platform = j2me ? "j2me" : "j2se"; } /* Setup the MIDP and CLDC root prefixes. */ midpRoot = "com.sun.midp.io." + platform; cldcRoot = "com.sun.cldc.io." + platform; /* Initialize enableAllProtocols from System property. */ prop = Configuration .getProperty("com.sun.midp.io.enable_extra_protocols"); if (prop != null) { enableAllProtocols = prop.equals("true"); } } /** * Prevent instantiation */ private InternalConnector() {} /** * Create and open a Connection. This method is internal to * the MIPD implementation to allow * any of the MIDP and CLDC protocols can be instantiated. * * @param name The URL for the connection * @param mode The access mode * @param timeouts A flag to indicate that the called wants * timeout exceptions * @return A new Connection object * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException if the requested connection * cannot be make, or the protocol type does not exist. * @exception IOException If some other kind of I/O error occurs. */ public static Connection openInternal(String name, int mode, boolean timeouts) throws IOException { /* Lookup the protocol using the MIDP root. */ try { return openPrim(name, mode, timeouts, midpRoot); } catch (ClassNotFoundException x) { } /* Drop back to the default classes of CLDC root */ try { return openPrim(name, mode, timeouts, cldcRoot); } catch (ClassNotFoundException x) { } throw new ConnectionNotFoundException( "The requested protocol does not exist " + name); } /** * Create and open a Connection. * * @param name The URL for the connection * @param mode The access mode * @param timeouts A flag to indicate that the called * wants timeout exceptions * @return A new Connection object * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException if the requested connection * cannot be make, or the protocol type does not exist. * @exception IOException If some other kind of I/O error occurs. */ public static Connection open(String name, int mode, boolean timeouts) throws IOException { /* Lookup the protocol using the MIDP root. */ try { return openPrim(name, mode, timeouts, midpRoot); } catch (ClassNotFoundException x) { } if (enableAllProtocols) { /* Drop back to the default classes of CLDC root */ try { return openPrim(name, mode, timeouts, cldcRoot); } catch (ClassNotFoundException x) { } } throw new ConnectionNotFoundException( "The requested protocol does not exist "+name); } /** * Create and open a Connection * * @param name The URL for the connection * @param mode The access mode * @param timeouts A flag to indicate that the called wants * timeout exceptions * @param root MIDP or CLDC root path for I/O packages * @return A new Connection object * * @exception ClassNotFoundException If the protocol cannot be found. * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException If the connection cannot be found. * @exception IOException If some other kind of I/O error occurs. * @exception IOException If an I/O error occurs * @exception IllegalArgumentException If a parameter is invalid */ private static Connection openPrim(String name, int mode, boolean timeouts, String root) throws IOException, ClassNotFoundException { /* Test for null argument */ if (name == null) { throw new IllegalArgumentException("Null URL"); } /* Look for : as in "http:", "file:", or whatever */ int colon = name.indexOf(':'); /* Test for null argument */ if (colon < 1) { throw new IllegalArgumentException("no ':' in URL"); } try { String protocol; /* Strip off the protocol name */ protocol = name.substring(0, colon); /* Strip the protocol name from the rest of the string */ name = name.substring(colon+1); /* "file" is always an alias for "storage" */ if (protocol.equals("file")) { protocol = "storage"; } /* Use specific http protocol class. */ if (protocol.equals("http")) { ConnectionBaseInterface hc = (ConnectionBaseInterface) new com.sun.midp.io.j2me.http.Protocol(); return hc.openPrim(name, mode, timeouts); } /* Require the enable extra protocols parameter for https use. */ if (protocol.equals("https") && ! enableAllProtocols) { throw new ClassNotFoundException("https is not enabled"); } /* * Using the platform and protocol names lookup a * class to implement the connection. */ Class clazz = Class.forName(root+"."+protocol+".Protocol"); /* Construct a new instance */ ConnectionBaseInterface uc = (ConnectionBaseInterface) clazz.newInstance(); /* Open the connection, and return it */ return uc.openPrim(name, mode, timeouts); } catch (InstantiationException x) { throw new IOException(x.toString()); } catch (IllegalAccessException x) { throw new IOException(x.toString()); } catch (ClassCastException x) { throw new IOException(x.toString()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -