passiveagenttestsuiteservice.java.svn-base

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

SVN-BASE
230
字号
/* * $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.passiveAgentService;import java.io.IOException;import java.io.OutputStream;import java.io.InputStream;import com.sun.javatest.TestEnvironment;import com.sun.javatest.Harness;import com.sun.javatest.agent.AgentMain;import com.sun.javatest.agent.Agent;import com.sun.javatest.agent.Connection;import com.sun.javatest.agent.ConnectionFactory;import com.sun.tck.j2me.javatest.TestSuiteService;import com.sun.tck.j2me.utils.Utils;/** * A class to start and stop passive agent for TestSuite. */public class PassiveAgentTestSuiteService extends TestSuiteService {    private boolean verbose = false;    private boolean autoStart = true;    private int concurrency = 1;    private Thread agentMainRunner;    private int passivePort;    private String errMessage;    /**     * Name of JavaTest environment variable which controls whether passive agent prints     * verbose messages.     */    public static final String VERBOSE = "remoteVerbose";    /**     * The default name for this service for registration with the service manager.     */    public static final String SERVICE_NAME="service.passiveAgent";        /**     * Name of JavaTest environment variable which controls whether the passive     * agent should automatic start.     */    public static final String AUTO_START = "passiveAgent.autoStart";        /**     * Name of JavaTest environment variable for passive port of passive agent.     */    public static final String PASSIVE_PORT = "passivePort";        public PassiveAgentTestSuiteService(Harness harness) {        super(harness);    }        /**     * Read parameters for passive agent from TestEnvironment.     */    private void loadParameters() {        TestEnvironment env = getTestEnvironment();        verbose = Utils.getBooleanFromEnv(env, VERBOSE, false);        autoStart = Utils.getBooleanFromEnv(env, AUTO_START, true);        concurrency = getHarness().getParameters().getConcurrency();        passivePort = Utils.getIntFromEnv(env, PASSIVE_PORT, 1908);    }        /**     * Start the passive agent in a new thread. Passive agent be started     * if value of JavaTest environment variable AUTO_START is     * not set or is set to "true".     */    public synchronized void start() throws TestSuiteServiceFault {        loadParameters();        if (passivePort == 0) {            throw new TestSuiteServiceFault(i18n, "ts.passivePortZero");        }                    if (!autoStart) {            return;        }        // We need to decorate the default connection factory,        // so that factory's nextConnection() won't throw a Fault when        // we try to close the factory. Otherwise, error messages        // are going to be printed to the stdout and we don't want        // those useless messages to confuse users.        class DistributedTestsAgent extends AgentMain {            protected ConnectionFactory createConnectionFactory()                    throws Fault {                final ConnectionFactory baseConnFactory                        = super.createConnectionFactory();                return new ConnectionFactory() {                    private volatile boolean closing = false;                    public void close() throws                            com.sun.javatest.agent.ConnectionFactory.Fault {                        closing = true;                        baseConnFactory.close();                    }                    public Connection nextConnection()                        throws com.sun.javatest.agent.ConnectionFactory.Fault {                        try {                            return baseConnFactory.nextConnection();                        } catch (                            com.sun.javatest.agent.ConnectionFactory.Fault f) {                            if (!closing) {                                throw f;                            } else {                                // swallow the exception, since we're closing,                                // and return the dummy connection.                                return new Connection() {                                    public void waitUntilClosed(int timeout)                                            throws InterruptedException {                                        return;                                    }                                    public boolean isClosed() {                                        return true;                                    }                                    public void close() throws IOException {                                        // do nothing                                    }                                    public OutputStream getOutputStream() {                                        return null;                                    }                                    public InputStream getInputStream() {                                        return null;                                    }                                    public String getName() {                                        return "Closed Connection";                                    }                                };                            }                        }                    }                };            }        }        // Agent should be executed in separate thread        agentMainRunner = new Thread() {            public void run() {                DistributedTestsAgent am = new DistributedTestsAgent();                try {                    // We set verbose property directly, since there is                    // no other way to turn it off via command line arguments.                    Agent.tracing = verbose;                    String[] params;                    if (verbose)  {                        params = new String[]{"-passive", "-passivePort",                                String.valueOf(passivePort), "-concurrency",                                String.valueOf(concurrency), "-trace"};                    } else {                        params = new String[]{"-passive", "-passivePort",                                String.valueOf(passivePort), "-concurrency",                                String.valueOf(concurrency)};                    }                    am.run(params);                } catch (AgentMain.BadArgs e) {                    errMessage = "Error: Bad arguments for AgentMain "                        + e.toString();                } catch (AgentMain.Fault e) {                    String[] msgs = e.getMessages();                    StringBuffer sb = new StringBuffer();                    for (int i = 0; i < msgs.length; i++) {                        sb.append(" ");                        sb.append(msgs[i]);                    }                    errMessage = "Error: general fault in AgentMain "                            + sb.toString();                } catch (IllegalArgumentException e) {                    errMessage = "Error: invalid port for AgentMain "                            + e.toString();                }            }        };        // starting passive agent for distributed tests        agentMainRunner.start();        try {            Thread.sleep(5000);        } catch (InterruptedException ignore) {            // Silently ignore        }        if (!agentMainRunner.isAlive()) {            throw new TestSuiteServiceFault(i18n, "ts.passiveAgent", errMessage);        }    }                /**     * Try to stop passive agent by interrupting the agent thread.     */    public synchronized void stop() {               if (agentMainRunner != null) {            agentMainRunner.interrupt();        }    }}

⌨️ 快捷键说明

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