midhttpexecutionserver.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 660 行 · 第 1/2 页

SVN-BASE
660
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.cldc.communication.midp;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.net.InetAddress;import java.net.URL;import java.net.URLClassLoader;import java.net.UnknownHostException;import java.util.ResourceBundle;import java.util.StringTokenizer;import java.util.Vector;import com.sun.cldc.communication.Server;import com.sun.cldc.communication.TestProvider;import com.sun.tck.midp.javatest.MidBundle;import com.sun.tck.midp.javatest.MidTestProvider;public class MIDHttpExecutionServer extends BaseServer implements Server {    private MidTestProvider testProvider;    private String mainClass;    private String jarSourceDir;    private ContentHandler appHandler;    private SuiteSigner suiteSigner;    private File signerJar;    private File handlerJar;    protected String testRoot;    private String serverRoot;    private String extraURI;    private String extraHtmlURI;    private String expectedRequest = "getNextApp";    private ContentTypeHandler ctHandler;    private boolean last_getNextApp = true;    private static final String TCK_ROOT = "test.root=";    private static final String SERVER_ROOT = "server.root=";    private static final String HANDLER = "handler=";    private static final String HANDLER_JAR = "handlerJar=";    private static final String SIGNER = "signer=";    private static final String SIGNER_JAR = "signerJar=";    private static final String SIGNER_ARGS = "signerArgs=";    private static final String EXTRA_CONTENT_TYPES = "extraContentTypes=";    public MIDHttpExecutionServer() {        port = 80;        testRoot = "/test";        serverRoot = "/";        extraURI = "getNextApp.jad";        extraHtmlURI = "getNextApp.html";        done = true;        appHandler = new DefaultContentHandler();        ctHandler = new ContentTypeHandler();        try {            host = InetAddress.getLocalHost().getHostName();        } catch (UnknownHostException uhe) {            host = "localhost";        }    }    /**     * Initialize the server with necessary parameters.     * "test.root=bar"     * "http.port=42"     */    public void init(String[] arg) {        String [] str_args = null;        for (int i=0; arg!=null && i<arg.length; i++) {            if (arg[i].startsWith(TCK_ROOT) && arg[i].length() > TCK_ROOT.length()) {                testRoot = arg[i].substring(TCK_ROOT.length());                continue;            } else if (arg[i].startsWith(SERVER_ROOT) && arg[i].length() > SERVER_ROOT.length()) {                serverRoot = arg[i].substring(SERVER_ROOT.length());                continue;            } else if (arg[i].startsWith(HANDLER_JAR) && arg[i].length() > HANDLER_JAR.length()) {                String handlerPath = arg[i].substring(HANDLER_JAR.length());                handlerJar = new File(handlerPath);                if (!handlerJar.exists()) {                    throw new IllegalArgumentException(i18n.getString("error.handlerJarNotFound")                                                       + handlerPath);                } else if (!handlerJar.isFile()) {                    throw new IllegalArgumentException(i18n.getString("error.handlerJarIsNotFile")                                                       + handlerPath);                }                continue;            } else if (arg[i].startsWith(HANDLER) && arg[i].length() > HANDLER.length()) {                try {                    verboseln("Instantiating handler: '" +                               arg[i].substring(HANDLER.length()) + "'");                    ClassLoader loader = null;                    if (handlerJar != null) { // load from user jar                        // need class loader of this class in order to load                        // ContentHandler interface                        loader = new URLClassLoader( new URL[] { handlerJar.toURL() },                                                     this.getClass().getClassLoader());                    } else // load from standard jars or classpath                        loader = this.getClass().getClassLoader();                    Class handlerClass = Class.forName(arg[i].substring(HANDLER.length()),                                                       true, loader);                    appHandler = (ContentHandler)handlerClass.newInstance();                    continue;                } catch (java.net.MalformedURLException mue) {                    throw new IllegalArgumentException(i18n.getString("error.malfHandlerURL")                                                       + mue.toString());                } catch (ClassNotFoundException cnfe) {                    throw new IllegalArgumentException(i18n.getString("error.handlerNotFound")                                                        + cnfe.toString());                } catch (InstantiationException ie) {                    throw new IllegalArgumentException(i18n.getString("error.handlerCreate")                                                       + ie.toString());                } catch (IllegalAccessException iae) {                    throw new IllegalArgumentException(i18n.getString("error.handlerArgs")                                                        + iae.toString());                }            } else if (arg[i].startsWith(SIGNER_JAR) && arg[i].length() > SIGNER_JAR.length()) {                String signerPath = arg[i].substring(SIGNER_JAR.length());                signerJar = new File(signerPath);                if (!signerJar.exists()) {                    throw new IllegalArgumentException(i18n.getString("error.signerJarNotFound")                                                       + signerPath);                } else if (!signerJar.isFile()) {                    throw new IllegalArgumentException(i18n.getString("error.signerJarIsNotFile")                                                       + signerPath);                }                continue;            } else if (arg[i].startsWith(SIGNER) && arg[i].length() > SIGNER.length()) {                try {                    verboseln("Instantiating signer: '"+arg[i].substring(SIGNER.length())+"'");                    ClassLoader loader = null;                    if (signerJar != null) { // load from user jar                        // need class loader of this class in order to load                        // SuiteSigner interface                        loader = new URLClassLoader( new URL[] { signerJar.toURL() },                                                     this.getClass().getClassLoader());                    } else // load from standard jars or classpath                        loader = this.getClass().getClassLoader();                    Class signerClass = Class.forName(arg[i].substring(SIGNER.length()),                                                       true, loader);                    suiteSigner = (SuiteSigner)signerClass.newInstance();                    continue;                } catch (java.net.MalformedURLException mue) {                    throw new IllegalArgumentException(i18n.getString("error.malformedURL")                                                       + mue.toString());                } catch (ClassNotFoundException cnfe) {                    throw new IllegalArgumentException(i18n.getString("error.signerNotFound")                                                       + cnfe.toString());                } catch (InstantiationException ie) {                    throw new IllegalArgumentException(i18n.getString("error.signerCreate")                                                       + ie.toString());                } catch (IllegalAccessException iae) {                    throw new IllegalArgumentException(i18n.getString("error.signerArgs")                                                       + iae.toString());                }            } else if (arg[i].startsWith(SIGNER_ARGS) && arg[i].length() > SIGNER_ARGS.length()) {                StringTokenizer st = new StringTokenizer(arg[i].substring(SIGNER_ARGS.length()));                Vector args = new Vector();                while (st.hasMoreTokens()) {                    args.add(st.nextToken());                }                Object [] obj_args = args.toArray();                str_args = new String[obj_args.length];                for(int k=0; k<str_args.length; k++) str_args[k] = (String)obj_args[k];            } else if (arg[i].startsWith(EXTRA_CONTENT_TYPES)) {                     if (arg[i].length() > EXTRA_CONTENT_TYPES.length()) {                        try {                            ctHandler.registerExtensions(arg[i].substring(EXTRA_CONTENT_TYPES.length()));                        } catch (IllegalArgumentException e) {                            throw new IllegalArgumentException(i18n.getString("error.invalidContentTypes")                                    + e.getMessage());                        }                    }            } else                super.processArg(arg[i]);        }        if (!testRoot.endsWith("/")) {            testRoot = testRoot + "/";        }        if(suiteSigner != null && str_args != null) {            verboseln("Initializing signer");            suiteSigner.init(str_args);            verboseln("Initializing signer done");        }        runners = new BaseHttpServer[handlerCount];        for (int i = 0; i < handlerCount; i++) {            runners[i] = createRequestHandler();            runners[i].setName("ExecutionRequestHandler_"+i);            ((BaseHttpServer)runners[i]).setParams(verbose, host, port, retry);        }    }    protected BaseHttpServer createRequestHandler() {        return new RequestHandler();    }        /**     * Set test provider. There is only one test provider      * per server. Next call to setTestProvider removes the previous      * one. Null argument causes removal of current test provider.     */    public void setTestProvider(TestProvider tp) {        try {            testProvider = (MidTestProvider)tp;	} catch (ClassCastException cce) {	    throw new IllegalArgumentException(	            i18n.getString("error.providerNotSuitable"));	}        mainClass = tp.getAppMainClass();        if (mainClass == null) {            throw new NullPointerException(                    i18n.getString("error.providerNotDefined"));        }        jarSourceDir = tp.getJarSourceDirectory();        if (jarSourceDir == null) {            throw new NullPointerException(                    i18n.getString("error.jarSourceNotDefined"));        }    }    /**     * Returns current test provider.     */    public TestProvider getTestProvider() {        return testProvider;    }    protected void createJadFile(String dir, String file, byte[] content)            throws IOException {        FileOutputStream fos = null;        try {            fos = new FileOutputStream(new File(dir, file));            fos.write(content);            fos.flush();        } finally {            if (fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    protected byte[] getFileContent(String dir, String file) {        try {            InputStream fis = new BufferedInputStream(                    new FileInputStream(new File(dir, file)));            ByteArrayOutputStream bos = new ByteArrayOutputStream();            int b = -1;            while ((b = fis.read()) >= 0) {                bos.write(b);            }            bos.flush();            bos.close();            fis.close();            return bos.toByteArray();        } catch (IOException ioe) {            verbosetrace(ioe);            throw new IllegalArgumentException(                    i18n.getString("error.readSignedJad"));        }    }    public synchronized void start() {        if (testProvider == null) {            throw new NullPointerException(                    i18n.getString("error.providerNotSet"));        }        last_getNextApp = false;        super.start();    }    public synchronized void stop() {        if (testProvider != null) {            synchronized (testProvider) {                testProvider.notifyAll();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?