midexportbundler.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 264 行

JAVA
264
字号
/* * $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.tck.cldc.communication.GenericTestBundle;import com.sun.tck.cldc.communication.TestBundle;import com.sun.tck.cldc.javatest.TestBuilder;import com.sun.tck.cldc.javatest.util.UTFConverter;import com.sun.tck.j2me.utils.FileUtils;import com.sun.tck.midp.javatest.MidBundle;import com.sun.tck.midp.javatest.MidExportBuilder;import com.sun.tck.midp.policy.PermissionSet;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import java.util.Properties;/** * * Test bundler for MIDP export mode. */public class MidExportBundler extends ExportBundler {        /**     * The name of file which is created in the export directory and defines     * the Java Security Policy that should be implemented in device running     * exported tests.     */    public static final String policyFileName = "java.policy";        /**     * Creates instance of <code>ExportBundler</code> with specified parameters.     *     * @param   untrusted   boolean value defining whether the exported bundles     *      should be created for untrusted security mode (JAD files are not     *      signed).     * @param   tdm     <code>TestDescriptionModifier</code> for this test run.     * @param   testExportInfo  <code>TestExportInfo</code> instance containing     *      all test export related parameters for this test run.     * @param   midAgentArgs    Arguments for MIDletAgent.     */    public MidExportBundler(boolean untrusted, TestDescriptionModifier tdm,            TestExportInfo testExportInfo, String[] midAgentArgs) {        super(tdm,  testExportInfo);        setUntrusted(untrusted);        setMidAgentArgs(midAgentArgs);    }        /**     * Sets the security policy to use with exported tests.     *     * Exported tests should be run in specific security environment.     * Test suite specify the security policy to be used with exported tests     * by invoking this method. The specified permissions argument is the     * set of permissions which should be bounded to untrusted security     * domain on the device.     *     * @param   permissions new security policy.     */    public void setSecurityPolicy(PermissionSet permissions) {        this.permissions = permissions;    }        /**     * Overrides the <code>TestBundler.createTestBuilder()</code> method.     * Creates <code>MidExportBuilder</code> instance.     *     * @return  Initialized <code>MidExportBuilder</code> instance.     */    protected TestBuilder createTestBuilder(String jarSourceDirectory,            int jarFileSizeLimit, String testPath) {                MidExportBuilder builder = new MidExportBuilder(jarSourceDirectory,                jarFileSizeLimit, testPath, testExportInfo,                getTestProvider().getAppMainClass());        builder.setUntrusted(isUntrustedMode());        return builder;    }        /**     * Creates <code>MidBundle</code> instance with keeping JAR files.     */    protected GenericTestBundle createBundle() {        // Do not delete JARs        return new MidBundle(true);    }    /**     * Overrides super implementation. This implementation doesn't add     * <code>agent.dat</code> entry to newly generated JAR file for current     * bundle. It's left for last moment when all client args will be known.     */    protected boolean initBundle() {        boolean status = super.initBundle();                if (status && getMidAgentArgs() != null) {            status = getTestBuilder().addByteArray("mid_agent.dat",                    UTFConverter.stringsToBytes(getMidAgentArgs()));        }        return status;    }        /**     * Overrides notification on successful dispatching all tests.     * This method is invoked after all tests have been exported and it's the     * time for exporting the things related to whole test suite.     * <p>     * This method invokes {@link #exportBundle(TestBundle bundle)} method     * for each exported bundle and also exports files which don't belong to     * any bundle.     */    protected void allTestsDispatched() {        super.allTestsDispatched();        if (permissions != null) {            exportSecurityPolicy();        }    }    String formatLinksToBundleFiles(String bundleId, String jarUrl,            String jadUrl) {        return bundleId + " [<A HREF=\"" + jadUrl + "\">" + bundleId                 + " JAD</A>] [<A HREF=\"" + jarUrl + "\">" + bundleId                + " JAR</A>]";    }        /**     * Adds signer related properties to properties generated by ExportBundler.     */    Properties getBuildProperties(TestBundle bundle, File bundleDir,            Properties bundleProps) {        Properties buildProps = super.getBuildProperties(bundle, bundleDir,                bundleProps);        if (!this.isUntrustedMode()) {            buildProps.setProperty("trusted", "true");            // Copy signer JAR to lib directory            File signerJarSrc = new File(testExportInfo.getSignerJar());            File signerJarDest = new File(bundleDir, "lib" + File.separator                    + signerJarSrc.getName());            File destDir = signerJarDest.getParentFile();            if (destDir != null && !destDir.exists()) {                destDir.mkdirs();            }            try {                FileUtils.copyFile(signerJarSrc, signerJarDest, true);            } catch (IOException e) {                throw new RuntimeException("Cannot copy signer jar file.", e);            }            buildProps.setProperty("signer.jar",                    "lib" + File.separator + signerJarDest.getName());            buildProps.setProperty("signer.class",                    testExportInfo.getSignerClass());            String argsLine = "";            String[] signerArgs = processSignerArgs(                    testExportInfo.getSignerArgs(), bundleDir);            for (int i = 0; i < signerArgs.length; i++) {                argsLine += " -arg " + signerArgs[i];            }            buildProps.setProperty("signer.args", argsLine.trim());        }        return buildProps;    }    String getApplicationDescriptorName(TestBundle bundle) {        return bundle.getApp().replace(".jar", ".jad");    }    /**     * Signer args may contain filenames which exist only on the javatest host.     * But the exported tests must be able to be used on any host.     *     * This method detects the filenames in the signer args and copies the files     * to the export directory.      *     * @return  The args with updated filenames     */    private String[] processSignerArgs(String[] args, File bundleDir) {        String[] newArgs = new String[args.length];                File destDir = new File(bundleDir, "lib");        if (!destDir.exists()) {            destDir.mkdirs();        }        for (int i = 0; i < args.length; i++) {            File file = new File(args[i]);            if (file.canRead()) {                String name = file.getName();                try {                    FileUtils.copyFileToDir(file, destDir, true);                } catch (IOException e) {                    throw new RuntimeException("Failed to copy file "                            + file.getPath() + " to directory "                            + destDir.getPath());                }                newArgs[i] = bundleDir.toURI().relativize(destDir.toURI())                        + name;            } else {                newArgs[i] = args[i];            }        }        return newArgs;    }        /**     * Writes the java.policy to be used with exported tests.     * This file will contain the definition of untrusted security domain     * with the set of permissions set in interview.     */    private void exportSecurityPolicy() {        PrintWriter pw = null;        File policyFile = new File(getTestProvider().getJarSourceDirectory(),                policyFileName);                try {            pw = new PrintWriter(new FileWriter(policyFile));            pw.println("domain: untrusted");            Enumeration perms = permissions.getGranted();            if (perms.hasMoreElements()) {                pw.print("allow: " + (String) perms.nextElement());                while (perms.hasMoreElements()) {                    String permName = (String) perms.nextElement();                    pw.print(", " + permName);                }                pw.println();            }        } catch (IOException e) {            throw new RuntimeException("Can not write security policy file", e);        } finally {            if (pw != null) {                pw.close();            }        }    }        private PermissionSet permissions;    }

⌨️ 快捷键说明

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