commtestsuiteservice.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 187 行

SVN-BASE
187
字号
/* * $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.tck.j2me.services.commService;import java.io.File;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import com.sun.javatest.TestEnvironment;import com.sun.javatest.Harness;import com.sun.tck.j2me.javatest.TestSuiteService;import com.sun.tck.j2me.services.Service;import com.sun.tck.j2me.services.ServiceFault;import com.sun.tck.j2me.communication.CommunicationServer;import com.sun.tck.j2me.services.commService.CommToJ2MEService;import com.sun.tck.j2me.utils.Utils;import java.util.ArrayList;import java.util.List;/** * A class that starts and stops CommService for ServiceBasedTestSuite. */public class CommTestSuiteService extends TestSuiteService {    /**     * The default name for this service for registration with the service manager.     */    public static final String SERVICE_NAME="service.commService";        /**     * JavaTest environment variable name for CommService jar file name.     */    public static final String COMMSERVICE_JAR = "commService.server.jar";    /**     * JavaTest environment variable name for CommService classpath.      */    public static final String COMMSERVICE_PATH = "commService.server.path";        /**     * JavaTest environment variable name for CommService server class name.     */    public static final String COMMSERVICE_SERVER = "commService.server";        /**     * @param harness the Harness where the service will be running      * @param loader The TestSuite's class loader returned by TestSuite.getClassLoader().      *        The class loader will load CommService server class.       */    public CommTestSuiteService(Harness harness, ClassLoader loader) {            super(harness, loader);    }            private void loadCommServiceEntries() throws TestSuiteServiceFault {        TestEnvironment env = getTestEnvironment();        String[] envData = null;        String serverClassName;        String[] serverArgs;        URLClassLoader loader;        List<URL> list = null;        try {            list = Utils.getPathFromEnv(env, COMMSERVICE_PATH);        } catch (Exception e) {            // Ignore            list = new ArrayList<URL>();        }        File f1 = null;        try {            f1 = Utils.getJarFromEnv(env, COMMSERVICE_JAR);            list.add(f1.toURI().toURL());        } catch (TestEnvironment.Fault fault) {            throw new TestSuiteServiceFault(i18n, "dts.lookupError", COMMSERVICE_JAR);        } catch (IllegalArgumentException e) {            throw new TestSuiteServiceFault(i18n, "dts.cannotReadFile", e.getMessage());        } catch (NullPointerException e) {            // COMMSERVICE_JAR is not required if the COMMSERVICE_PATH defines            // at least one file.            if (list.size() == 0) {                throw new TestSuiteServiceFault(i18n, "dts.jarUndefined", COMMSERVICE_JAR);            }        } catch (MalformedURLException e) {            throw new TestSuiteServiceFault(i18n, "dts.commService.server.jar.NotFound", f1.getAbsoluteFile());        }        loader = new URLClassLoader(list.toArray(new URL[list.size()]), getClassLoader());        // commService.server        try {            envData = env.lookup(COMMSERVICE_SERVER);            if (envData == null || envData.length == 0) {                throw new TestSuiteServiceFault(i18n, "dts.commService.server.NotDefined");            }        } catch (TestEnvironment.Fault tef) {            throw new TestSuiteServiceFault(i18n, "dts.commService.server.NotDefined");        }        serverClassName = envData[0];        serverArgs = new String[0];        if (envData.length > 1) {            serverArgs = new String[envData.length - 1];            System.arraycopy(envData, 1, serverArgs, 0, envData.length - 1);        }                Class serverClass;        CommunicationServer server;        try {            serverClass = loader.loadClass(serverClassName);        } catch (ClassNotFoundException e) {            throw new TestSuiteServiceFault(                    i18n, "dts.commServerClassNotFound", serverClassName);        }        if (!CommunicationServer.class.isAssignableFrom(serverClass)) {            throw new TestSuiteServiceFault(                    i18n, "dts.commServerNotImplemented", serverClass.toString());        }	try {	    server = (CommunicationServer) serverClass.newInstance();	} catch (InstantiationException e) {	    throw new TestSuiteServiceFault(i18n, "ts.cantInstantiate",			    new Object[] { serverClass.getName(), e });	} catch (IllegalAccessException e) {	    throw new TestSuiteServiceFault(i18n, "ts.illegalAccess",			    new Object[] { serverClass.getName(), e });	} catch (NoClassDefFoundError e) {            throw new TestSuiteServiceFault(i18n, "ts.cantLoadClass",                    new Object[] {e.getMessage(), e});        }        server.init(serverArgs);        CommToJ2MEService commService = CommToJ2MEService.getInstance();        // assuming commService is not running        assert commService.getState() != Service.STARTED;        // every test run our commService will start with empty handler list        commService.removeAllHandlers();        commService.setCommServer(server);            }                /**     * Starts CommService     */    public synchronized void start() throws ServiceFault {        loadCommServiceEntries();        CommToJ2MEService.getInstance().start();    }        /**     * Stops CommService     */    public synchronized void stop() {        try {            CommToJ2MEService.getInstance().stop();        } catch (ServiceFault f) {            // silently ignore        }    }        }

⌨️ 快捷键说明

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