src/utils/reorder/tools/MaxTime.java
author mcimadamore
Fri, 31 Aug 2018 18:01:47 +0100
changeset 51612 bdac20c6c8dd
parent 47216 71c04702a3d5
permissions -rw-r--r--
8210226: Add support for multiple project folders to idea.sh Summary: Overhaul templating logic for idea.sh; add support for -o option Reviewed-by: erikj, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/** Run an application (from a jar file) and terminate it after a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  number of seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class MaxTime {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static boolean debugFlag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static void debug(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
          "Usage:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
          "  java  MaxTime  <jarfile> <timeout>\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
          "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
          "  <jarfile> is a jar file specifying an application to run\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
          "  <timeout> is an integer number of seconds to allow the app to run." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int timeoutPeriod = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        String mainClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (args.length != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Identify the timeout value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            timeoutPeriod = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Identify the application's main class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            mainClass = new JarFile(args[0]).getManifest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                .getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.err.println("Can't find " + args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Set the exit timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        final int timeout = timeoutPeriod * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    debug("Before timeout!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    Thread.sleep(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    debug("After timeout!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                debug("Exit caused by timeout!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // Start up the app.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            ClassLoader cl = new URLClassLoader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                   new URL[] {new URL("file:"+args[0])});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Class clazz = cl.loadClass(mainClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            String[] objs = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Method mainMethod = clazz.getMethod("main", new Class[]{objs.getClass()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            mainMethod.invoke(null, new Object[]{objs});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.exit(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}