executelockscdcscript.java.svn-base

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

SVN-BASE
123
字号
/* * $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.javatest;import com.sun.javatest.ResourceTable;import com.sun.javatest.Status;import com.sun.javatest.TestDescription;import com.sun.javatest.TestEnvironment;import com.sun.javatest.util.StringArray;import com.sun.tck.javatest.TCKScript;/** * An extension to the {@link TCKScript base TCK Script} for CDC, * provides "executeLocks" functionality. Allows to use simple named * locks to serialize access to arbitrary resources that require * exclusive use. * <p> * For example, some tests might adjust security environment for their * testing needs via <code>SecurityManager</code>, and running such * security-sensitive tests concurrently would lead to interference * between tests, which is not desired. Marking such tests with a * named lock, e.g., "SecurityManager" would allow to run the tests * one by one to avoid interference. * <p> * <b>Note:</b> This Script should not be used in cases when test * bundling is involved, e.g., in CLDC/MIDP cases. */public class ExecuteLocksCdcScript extends TCKScript {    /**     * The name of <code>TestDescription</code>'s parameter to get     * the lock names. The lock names should be separated by spaces.     */    public static final String EXECUTE_LOCKS_PARAMETER = "executeLocks";    /**     * Creates a Script instance.     */    public ExecuteLocksCdcScript() {    }    public Status run(String[] args, TestDescription td, TestEnvironment env) {        String[] locks = StringArray.split(                td.getParameter(EXECUTE_LOCKS_PARAMETER));        try {            boolean ok = lockExecution(locks);            if (!ok) {                return Status.error("Timeout waiting to acquire internal "                        + "locks on: " + StringArray.join(locks));            }            return super.run(args, td, env);        } catch (InterruptedException e) {            return Status.error(                    "timeout waiting to acquire internal locks on: "                    + StringArray.join(locks));        } finally {            unlockExecution(locks);        }    }    /**     * Tries to acquire a set of named locks. To avoid deadlocks, the     * locks are acquired in a canonical order (alphabetical by name).     *     * @param locks     *                A list of names identifying locks to be     *                acquired.     * @return <code>true</code> if and only if all the locks were     *         successfully acquired     * @throws InterruptedException     *                 if the method was interrupted while waiting for     *                 the locks to become available.     */    protected boolean lockExecution(String[] locks)            throws InterruptedException {        if (locks.length == 0) {            return true;        }        return lockTable.acquire(locks, Integer.MAX_VALUE);    }    /**     * Releases a set of previously acquired locks. The named locks are     * only released if currently owned by the same thread that     * acquired them.     *     * @param locks     *                The names of the locks to be released.     */    protected void unlockExecution(String[] locks) {        if (locks.length > 0) {            lockTable.release(locks);        }    }    private static ResourceTable lockTable = new ResourceTable();}

⌨️ 快捷键说明

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