serverexecutionservice.java.svn-base

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

SVN-BASE
770
字号
/* * $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.execution.server;import static com.sun.tck.j2me.execution.queue.EventConstants.AGENT_REGISTERED;import static com.sun.tck.j2me.execution.queue.EventConstants.AGENT_UNREGISTERED;import static com.sun.tck.j2me.execution.queue.EventConstants.NO_AGENTS_REGISTERED;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import com.sun.javatest.Status;import com.sun.tck.j2me.execution.baseclient.BaseUtils;import com.sun.tck.j2me.execution.baseclient.Constants;import com.sun.tck.j2me.execution.baseclient.ExecutionRequest;import com.sun.tck.j2me.execution.baseclient.Response;import com.sun.tck.j2me.execution.client.RegistrationTicket;import com.sun.tck.j2me.execution.client.Utils;import com.sun.tck.j2me.execution.queue.EventRegistry;import com.sun.tck.j2me.execution.queue.IncomingQueue;import com.sun.tck.j2me.execution.queue.NonBlockingOutgoingQueue;import com.sun.tck.j2me.execution.queue.OutgoingQueue;import com.sun.tck.j2me.execution.queue.Request;import com.sun.tck.j2me.execution.queue.RequestImpl;import com.sun.tck.j2me.execution.queue.TestInfo;import com.sun.tck.j2me.execution.queue.EventRegistry.Event;import com.sun.tck.j2me.execution.queue.EventRegistry.EventImpl;import com.sun.tck.j2me.execution.queue.EventRegistry.Listener;import com.sun.tck.j2me.services.LifeCycleAware;import com.sun.tck.j2me.services.ServiceFault;import com.sun.tck.j2me.services.commService.CommToJ2MEService;import com.sun.tck.j2me.services.commService.CommunicationHandler;/** * It is implementation of the ServerExecutionService that works via * CommunicationServer Interface (CSI). CSI should be configured separately. * When the ServerExecutionService is created is registered as * {@link CommunicationHandler} in the {@link CommToJ2MEService} using own name * as recipientServiceName. * <p> * Prior using of the ServerExecutionService it should be configured. * The following methods should be invoked prior running: * <ul> *   <li>{@link #setIncomingQueue(IncomingQueue)}: The {@link IncomingQueue} *       provides the {@link Request} instances for execution.</li> *   <li>{@link #setOutgoingQueue(OutgoingQueue)}: The completed *       {@link Request} instances are written to the provided *       {@link OutgoingQueue}. The ServerExecutionService can be considered *       as component, which reads {@link Request} instances from *       {@link IncomingQueue}, when agent requests tests for execution, and *       writes the {@link Request} instances with status, when the completion *       status is received from the agent. It allows to integrate the *       ServerExecutionService into custom TestRunner.</li> *   <li>{@link #setRegistrationHandler(RegistrationHandler)}: *       The provided {@link RegistrationHandler} instance is used for *       registering/unregistering of the agents. Also responsibility of the *       {@link RegistrationHandler} is maintaining of the list of the *       registered (active) agents and dispatch proactive commands to the *       agents.</li> *   <li>{@link #setResourceProvider(ResourceProvider)}: The *       {@link ResourceProvider} is required only if the agent is going to *       download the resources from the server side. If the *       {@link ResourceProvider} is not set, then null is returned for all *       getResource requests.</li> *  </ul> * The following methods provides finer customization of the * ServerExecutionService. The have the default setting, which are applicable * for most use cases: *  <ul> *   <li>{@link #setExecDataCreator(ExecDataCreator)}: The default instance is *       {@link DefaultExecDataCreator} with *       "<code>-mapArgs -loadRemote $execClass $execArgs</code>" specification. *   </li> *   <li>{@link #setEventRegistry(EventRegistry)}. If the {@link EventRegistry} *       is not set, the ServerExecutionService does not listen or emit any *       events. In this case the ServerExecutionService will not report tests *       failures, when messages about connection Agent registration events are *       emitted.</li> *  </ul> * TODO I18n * <p> * TODO Mapping on the Server side and reporting of the AgentProperties. */public class ServerExecutionService implements CommunicationHandler, LifeCycleAware {    /**     * Name of the default execution section.     */    public static final String EXECUTE = "execute";    private static final long DEFAULT_DELAY = 5000;    private static final String DEFAULT_NAME = "ServerExecutionService";    private static final long QUEUE_WAIT_PERIOD = 100L;    private String serviceName;    private Map<String, ServerProactiveCommand>            commandsInProcess = Collections.synchronizedMap(                    new HashMap<String, ServerProactiveCommand>());    private ProactiveCommandsManager agentsSynchronizer = new ProactiveCommandsManager();    /**     * This interface is used for creation of the ExecutionRequest for the given     * TestInfo. It works similar to JavaTest script. It uses specification string     * associated with DefaultExecDataCreator and parses it into string list using     * TestEnvironment of the given TestInfo.     */    public interface ExecDataCreator {        public ExecutionRequest createRequest(Request request, TestInfo info);    }    /**     * The handler for the command.     */    private interface ResponseHandler {        /**         * process the given request. The response is written in out.         * @param request given request         * @param out stream for writing of the response.         * @return 0 if the request is successfully processed of number of         *         milliseconds for CAN_NOT_PROCESS response.         * @throws IOException         */        public long handleResponse(String connectionID, Response request,                                   DataOutputStream out)                                   throws IOException, ProactiveCommandException;    }    private static HashMap<String, ResponseHandler> handlers = new HashMap<String, ResponseHandler>();    private RegistrationHandler registrator;    private ResourceProvider resourceProvider;    private TestResultManager executor;    private CommToJ2MEService communication;    private IncomingQueue in;    private OutgoingQueue out;    private long idle_timeout = 60000L; // 1 minute    /**     * Creates a ServerExecutionService with the default name and "execute"     * section name.     */    public ServerExecutionService() {        this(DEFAULT_NAME, new TestResultManager());    }    /**     * Creates a ServerExecutionService with the given name and the given     * section name.     * @param name name of the ServerExecutionService. This name is used as     * recipientServiceName during registration in the {@link CommToJ2MEService}.     * @param sectionName name of the section in {@link TestInfo} instances to     * report results.     */    public ServerExecutionService(String name, String sectionName) {        this(name, new TestResultManager(sectionName));    }    /**     * Creates a ServerExecutionService with the given name and TestResultManager.     * @param name name of the ServerExecutionService. This name is used as     * recipientServiceName during registration in the {@link CommToJ2MEService}.     * @param resultsManager Given resultsManager for the maintaining of test     * results.     */    ServerExecutionService(String name, TestResultManager resultsManager) {        serviceName = (name == null) ? DEFAULT_NAME : name;        executor = ((resultsManager == null) ? new TestResultManager()                         : resultsManager);        handlers.put(Constants.REGISTER, register_handler);        handlers.put(Constants.UPDATE_REGISTRATION, update_registration_handler);        handlers.put(Constants.UNREGISTER, unregister_handler);        handlers.put(Constants.GET_NEXT_TEST, get_next_test_handler);        handlers.put(Constants.SEND_LOG, send_log_handler);        handlers.put(Constants.SEND_STATUS, send_status_handler);        handlers.put(Constants.GET_RESOURCE, get_resource_handler);        handlers.put(Constants.RESPONSE_DATA, response_data_handler);        handlers.put(Constants.PROACTIVE_COMMAND_NOT_SUPPORTED,                     proactive_commands_not_supported);        CommToJ2MEService.getInstance().setCommHandler(this, serviceName);        setRegistrationHandler(new AgentMonitoringService(                EventRegistry.getInstance(EventRegistry.DEFAULT_REGISTRY)));    }    private void initRegistry() {        if (registry == null) {            return;        }        registry.registerListener(AGENT_UNREGISTERED, new Listener() {            public void eventFired(String type, Event event) {                if (event.getSource() != DEFAULT_NAME) {                    agentUnregistered((String)event.getProperties().get("agent"));                }            }        });        registry.registerListener(NO_AGENTS_REGISTERED, new Listener() {            public void eventFired(String type, Event event) {                Object val;                if (((val = event.getProperties().get("since")) != null)) {                    long since = (Long)val;                    if ((System.currentTimeMillis() - since) > idle_timeout) {                        log.finest("reportFatalError:" + NO_AGENTS_REGISTERED                                + " " + (System.currentTimeMillis() - since));                        reportFatalError(NO_AGENTS_REGISTERED);                    }                }            }        });    }    private IncomingQueue getInQueue() {        return in;    }

⌨️ 快捷键说明

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