home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Runtime.java < prev    next >
Text File  |  1997-05-21  |  20KB  |  500 lines

  1. /*
  2.  * @(#)Runtime.java    1.26 97/02/24
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.lang;
  24.  
  25. import java.io.*;
  26. import java.util.StringTokenizer;
  27.  
  28. /*
  29.  * Every Java application has a single instance of class 
  30.  * <code>Runtime</code> that allows the application to interface with 
  31.  * the environment in which the application is running. The current 
  32.  * runtime can be obtained from the <code>getRuntime</code> method. 
  33.  * <p>
  34.  * An application cannot create its own instance of this class. 
  35.  *
  36.  * @author  unascribed
  37.  * @version 1.26, 02/24/97
  38.  * @see     java.lang.Runtime#getRuntime()
  39.  * @since   JDK1.0
  40.  */
  41. public class Runtime {
  42.     private static Runtime currentRuntime = new Runtime();
  43.       
  44.     /**
  45.      * Returns the runtime object associated with the current Java application.
  46.      *
  47.      * @return  the <code>Runtime</code> object associated with the current
  48.      *          Java application.
  49.      * @since   JDK1.0
  50.      */
  51.     public static Runtime getRuntime() { 
  52.     return currentRuntime;
  53.     }
  54.     
  55.     /** Don't let anyone else instantiate this class */
  56.     private Runtime() {}
  57.  
  58.     /* Helper for exit
  59.      */
  60. //#ifdef __SYMC__
  61.     private native synchronized void exitInternal(int status);
  62. //#else
  63. //  private native void exitInternal(int status);
  64. //#endif
  65.  
  66.     /**
  67.      * Terminates the currently running Java Virtual Machine. This 
  68.      * method never returns normally. 
  69.      * <p>
  70.      * If there is a security manager, its <code>checkExit</code> method 
  71.      * is called with the status as its argument. This may result in a 
  72.      * security exception. 
  73.      * <p>
  74.      * The argument serves as a status code; by convention, a nonzero 
  75.      * status code indicates abnormal termination. 
  76.      *
  77.      * @param      status   exit status.
  78.      * @exception  SecurityException  if the current thread cannot exit with
  79.      *               the specified status.
  80.      * @see        java.lang.SecurityException
  81.      * @see        java.lang.SecurityManager#checkExit(int)
  82.      * @since      JDK1.0
  83.      */
  84.     public void exit(int status) {
  85.     SecurityManager security = System.getSecurityManager();
  86.     if (security != null) {
  87.         security.checkExit(status);
  88.     }
  89.     exitInternal(status);
  90.     }
  91.  
  92.     /**
  93.      * Enable or disable finalization on exit; doing so specifies that the
  94.      * finalizers of all objects that have finalizers that have not yet been
  95.      * automatically invoked are to be run before the Java runtime exits.
  96.      * By default, finalization on exit is disabled.  An invocation of
  97.      * the runFinalizersOnExit method is permitted only if the caller is
  98.      * allowed to exit, and is otherwise rejected by the security manager.
  99.      * @see Runtime#gc
  100.      * @see Runtime#exit
  101.      * @since   JDK1.1
  102.      */
  103.     public static void runFinalizersOnExit(boolean value) {
  104.     SecurityManager security = System.getSecurityManager();
  105.     if (security != null) {
  106.         try { security.checkExit(0); }
  107.         catch (SecurityException e) {
  108.         throw new SecurityException("runFinalizersOnExit");
  109.         }
  110.     }
  111.     runFinalizersOnExit0(value);
  112.     }
  113.  
  114.     /*
  115.      * Private variable holding the boolean determining whether to finalize
  116.      * on exit.  The default value of the variable is false.  See the comment
  117.      * on Runtime.runFinalizersOnExit for constraints on modifying this.
  118.      */
  119.     private static native void runFinalizersOnExit0(boolean value);
  120.  
  121.     /* Helper for exec
  122.      */
  123.     private native Process execInternal(String cmdarray[], String envp[]) 
  124.      throws IOException;
  125.  
  126.     /**
  127.      * Executes the specified string command in a separate process. 
  128.      * <p>
  129.      * The <code>command</code> argument is parsed into tokens and then 
  130.      * executed as a command in a separate process. This method has 
  131.      * exactly the same effect as <code>exec(command, null)</code>. 
  132.      *
  133.      * @param      command   a specified system command.
  134.      * @return     a <code>Process</code> object for managing the subprocess.
  135.      * @exception  SecurityException  if the current thread cannot create a
  136.      *             subprocess.
  137.      * @see        java.lang.Runtime#exec(java.lang.String, java.lang.String[])
  138.      * @since      JDK1.0
  139.      */
  140.     public Process exec(String command) throws IOException {
  141.     return exec(command, null);
  142.     }
  143.  
  144.     /**
  145.      * Executes the specified string command in a separate process with the 
  146.      * specified environment. 
  147.      * <p>
  148.      * This method breaks the <code>command</code> string into tokens and 
  149.      * creates a new array <code>cmdarray</code> containing the tokens; it 
  150.      * then performs the call <code>exec(cmdarray, envp)</code>. 
  151.      *
  152.      * @param      command   a specified system command.
  153.      * @param      envp      array containing environment in format
  154.      *                       <i>name</i>=<i>value</i>
  155.      * @return     a <code>Process</code> object for managing the subprocess.
  156.      * @exception  SecurityException  if the current thread cannot create a
  157.      *               subprocess.
  158.      * @see        java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  159.      * @since      JDK1.0
  160.      */
  161.     public Process exec(String command, String envp[]) throws IOException {
  162.     int count = 0;
  163.     String cmdarray[];
  164.      StringTokenizer st;
  165.  
  166.     st = new StringTokenizer(command);
  167.      count = st.countTokens();
  168.  
  169.     cmdarray = new String[count];
  170.     st = new StringTokenizer(command);
  171.     count = 0;
  172.      while (st.hasMoreTokens()) {
  173.          cmdarray[count++] = st.nextToken();
  174.      }
  175.     SecurityManager security = System.getSecurityManager();
  176.     if (security != null) {
  177.         security.checkExec(cmdarray[0]);
  178.     }
  179.     return execInternal(cmdarray, envp);
  180.     }
  181.  
  182.     /**
  183.      * Executes the specified command and arguments in a separate process.
  184.      * <p>
  185.      * The command specified by the tokens in <code>cmdarray</code> is 
  186.      * executed as a command in a separate process. This has exactly the 
  187.      * same effect as <code>exec(cmdarray, null)</code>. 
  188.      *
  189.      * @param      cmdarray   array containing the command to call and
  190.      *                        its arguments.
  191.      * @return     a <code>Process</code> object for managing the subprocess.
  192.      * @exception  SecurityException  if the current thread cannot create a
  193.      *               subprocess.
  194.      * @see        java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  195.      * @since      JDK1.0
  196.      */
  197.     public Process exec(String cmdarray[]) throws IOException {
  198.     return exec(cmdarray, null);
  199.     }
  200.  
  201.     /**
  202.      * Executes the specified command and arguments in a separate process
  203.      * with the specified environment. 
  204.      * <p>
  205.      * If there is a security manager, its <code>checkExec</code> method 
  206.      * is called with the first component of the array 
  207.      * <code>cmdarray</code> as its argument. This may result in a security 
  208.      * exception. 
  209.      * <p>
  210.      * Given an array of strings <code>cmdarray</code>, representing the 
  211.      * tokens of a command line, and an array of strings <code>envp</code>, 
  212.      * representing an "environment" that defines system 
  213.      * properties, this method creates a new process in which to execute 
  214.      * the specified command. 
  215.      *
  216.      * @param      cmdarray   array containing the command to call and
  217.      *                        its arguments.
  218.      * @param      envp       array containing environment in format
  219.      *                        <i>name</i>=<i>value</i>.
  220.      * @return     a <code>Process</code> object for managing the subprocess.
  221.      * @exception  SecurityException  if the current thread cannot create a
  222.      *               subprocess.
  223.      * @see     java.lang.SecurityException
  224.      * @see     java.lang.SecurityManager#checkExec(java.lang.String)
  225.      * @since   JDK1.0
  226.      */
  227.     public Process exec(String cmdarray[], String envp[]) throws IOException {
  228.     SecurityManager security = System.getSecurityManager();
  229.     if (security != null) {
  230.         security.checkExec(cmdarray[0]);
  231.     }
  232.     return execInternal(cmdarray, envp);
  233.     }
  234.  
  235.     /**
  236.      * Returns the amount of free memory in the system. The value returned
  237.      * by this method is always less than the value returned by the
  238.      * <code>totalMemory</code> method. Calling the <code>gc</code> method may
  239.      * result in increasing the value returned by <code>freeMemory.</code>
  240.      *
  241.      * @return  an approximation to the total amount of memory currently
  242.      *          available for future allocated objects, measured in bytes.
  243.      * @since   JDK1.0
  244.      */
  245.     public native long freeMemory();
  246.  
  247.     /**
  248.      * Returns the total amount of memory in the Java Virtual Machine. 
  249.      *
  250.      * @return  the total amount of memory currently available for allocating
  251.      *          objects, measured in bytes.
  252.      * @since   JDK1.0
  253.      */
  254.     public native long totalMemory();
  255.  
  256.     /**
  257.      * Runs the garbage collector.
  258.      * Calling this method suggests that the Java Virtual Machine expend 
  259.      * effort toward recycling unused objects in order to make the memory 
  260.      * they currently occupy available for quick reuse. When control 
  261.      * returns from the method call, the Java Virtual Machine has made 
  262.      * its best effort to recycle all unused objects. 
  263.      * <p>
  264.      * The name <code>gc</code> stands for "garbage 
  265.      * collector". The Java Virtual Machine performs this recycling 
  266.      * process automatically as needed even if the <code>gc</code> method 
  267.      * is not invoked explicitly. 
  268.      *
  269.      * @since   JDK1.0
  270.      */
  271.     public native void gc();
  272.  
  273.     /**
  274.      * Runs the finalization methods of any objects pending finalization.
  275.      * Calling this method suggests that the Java Virtual Machine expend 
  276.      * effort toward running the <code>finalize</code> methods of objects 
  277.      * that have been found to be discarded but whose <code>finalize</code> 
  278.      * methods have not yet been run. When control returns from the 
  279.      * method call, the Java Virtual Machine has made a best effort to 
  280.      * complete all outstanding finalizations. 
  281.      * <p>
  282.      * The Java Virtual Machine performs the finalization process 
  283.      * automatically as needed if the <code>runFinalization</code> method 
  284.      * is not invoked explicitly. 
  285.      *
  286.      * @see     java.lang.Object#finalize()
  287.      * @since   JDK1.0
  288.      */
  289.     public native void runFinalization();
  290.  
  291.     /**
  292.      * Enables/Disables tracing of instructions.
  293.      * If the <code>boolean</code> argument is <code>true</code>, this 
  294.      * method asks the Java Virtual Machine to print out a detailed trace 
  295.      * of each instruction in the Java Virtual Machine as it is executed. 
  296.      * The virtual machine may ignore this request if it does not support 
  297.      * this feature. The destination of the trace output is system 
  298.      * dependent. 
  299.      * <p>
  300.      * If the <code>boolean</code> argument is <code>false</code>, this 
  301.      * method causes the Java Virtual Machine to stop performing the 
  302.      * detailed instruction trace it is performing. 
  303.      *
  304.      * @param   on   <code>true</code> to enable instruction tracing;
  305.      *               <code>false</code> to disable this feature.
  306.      * @since   JDK1.0
  307.      */
  308.     public native void traceInstructions(boolean on);
  309.  
  310.     /**
  311.      * Enables/Disables tracing of method calls.
  312.      * If the <code>boolean</code> argument is <code>true</code>, this 
  313.      * method asks the Java Virtual Machine to print out a detailed trace 
  314.      * of each method in the Java Virtual Machine as it is called. The 
  315.      * virtual machine may ignore this request if it does not support 
  316.      * this feature. The destination of the trace output is system dependent. 
  317.      * <p>
  318.      * If the <code>boolean</code> argument is <code>false</code>, this 
  319.      * method causes the Java Virtual Machine to stop performing the 
  320.      * detailed method trace it is performing. 
  321.      *
  322.      * @param   on   <code>true</code> to enable instruction tracing;
  323.      *               <code>false</code> to disable this feature.
  324.      * @since   JDK1.0
  325.      */
  326.     public native void traceMethodCalls(boolean on);
  327.  
  328.     /**
  329.      * Initializes the linker and returns the search path for shared libraries.
  330.      */
  331.     private synchronized native String initializeLinkerInternal();
  332.     private native String buildLibName(String pathname, String filename);
  333.  
  334.     /* Helper for load and loadLibrary */
  335.     private native int loadFileInternal(String filename);
  336.  
  337.     /** The paths searched for libraries */
  338.     private String paths[];
  339.  
  340.     private void initializeLinker() {
  341.     String ldpath = initializeLinkerInternal();
  342.     char c = System.getProperty("path.separator").charAt(0);
  343.     int ldlen = ldpath.length();
  344.     int i, j, n;
  345.     // Count the separators in the path
  346.     i = ldpath.indexOf(c);
  347.     n = 0;
  348.     while (i >= 0) {
  349.         n++;
  350.         i = ldpath.indexOf(c, i+1);
  351.     }
  352.  
  353.     // allocate the array of paths - n :'s = n + 1 path elements
  354.     paths = new String[n + 1];
  355.  
  356.     // Fill the array with paths from the ldpath
  357.     n = i = 0;
  358.     j = ldpath.indexOf(c);
  359.     while (j >= 0) {
  360.         if (j - i > 0) {
  361.         paths[n++] = ldpath.substring(i, j);
  362.         } else if (j - i == 0) { 
  363.         paths[n++] = ".";
  364.         }
  365.         i = j + 1;
  366.         j = ldpath.indexOf(c, i);
  367.     }
  368.     paths[n] = ldpath.substring(i, ldlen);
  369.     }
  370.  
  371.     /**
  372.      * Loads the specified filename as a dynamic library. The filename 
  373.      * argument must be a complete pathname. 
  374.      * From <code>java_g</code> it will automagically insert "_g" before the
  375.      * ".so" (for example
  376.      * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>).
  377.      * <p>
  378.      * If there is a security manager, its <code>checkLink</code> method 
  379.      * is called with the <code>filename</code> as its argument. This may 
  380.      * result in a security exception. 
  381.      *
  382.      * @param      filename   the file to load.
  383.      * @exception  SecurityException     if the current thread cannot load the
  384.      *               specified dynamic library.
  385.      * @exception  UnsatisfiedLinkError  if the file does not exist.
  386.      * @see        java.lang.Runtime#getRuntime()
  387.      * @see        java.lang.SecurityException
  388.      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
  389.      * @since      JDK1.0
  390.      */
  391.     public synchronized void load(String filename) {
  392.     SecurityManager security = System.getSecurityManager();
  393.     int ret;
  394.     if (security != null) {
  395.         security.checkLink(filename);
  396.     }
  397.     ret = loadFileInternal(filename);
  398.     if (ret == -1) {
  399.         throw new OutOfMemoryError();
  400.     } else if (ret == 0) {
  401.         throw new UnsatisfiedLinkError(filename);
  402.     }   /* else load was successful; return */
  403.     }
  404.  
  405.     /**
  406.      * Loads the dynamic library with the specified library name. The 
  407.      * mapping from a library name to a specific filename is done in a 
  408.      * system-specific manner. 
  409.      * <p>
  410.      * First, if there is a security manager, its <code>checkLink</code> 
  411.      * method is called with the <code>filename</code> as its argument. 
  412.      * This may result in a security exception. 
  413.      * <p>
  414.      * If this method is called more than once with the same library 
  415.      * name, the second and subsequent calls are ignored. 
  416.      *
  417.      * @param      libname   the name of the library.
  418.      * @exception  SecurityException     if the current thread cannot load the
  419.      *               specified dynamic library.
  420.      * @exception  UnsatisfiedLinkError  if the library does not exist.
  421.      * @see        java.lang.SecurityException
  422.      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
  423.      * @since      JDK1.0
  424.      */
  425.     public synchronized void loadLibrary(String libname) {
  426.     SecurityManager security = System.getSecurityManager();
  427.     if (security != null) {
  428.         security.checkLink(libname);
  429.     }
  430.         if (paths == null) {
  431.             initializeLinker();
  432.     }
  433.     for (int i = 0 ; i < paths.length ; i++) {
  434.         int ret;
  435.         String tempname = buildLibName(paths[i], libname);
  436.         ret = loadFileInternal(tempname);
  437.         if (ret == -1) {
  438.         throw new OutOfMemoryError();
  439.         } else if (ret == 1) {    // Loaded or found it already loaded
  440.         return;
  441.         }
  442.     }
  443.     // Oops, it failed
  444.         throw new UnsatisfiedLinkError("no " + libname + 
  445.                        " in shared library path");
  446.     }
  447.  
  448.     /**
  449.      * Creates a localized version of an input stream. This method takes 
  450.      * an <code>InputStream</code> and returns an <code>InputStream</code> 
  451.      * equivalent to the argument in all respects except that it is 
  452.      * localized: as characters in the local character set are read from 
  453.      * the stream, they are automatically converted from the local 
  454.      * character set to Unicode. 
  455.      * <p>
  456.      * If the argument is already a localized stream, it may be returned 
  457.      * as the result. 
  458.      *
  459.      * @deprecated As of JDK 1.1, the preferred way translate a byte
  460.      * stream in the local encoding into a character stream in Unicode is via
  461.      * the <code>InputStreamReader</code> and <code>BufferedReader</code>
  462.      * classes.
  463.      *
  464.      * @return     a localized input stream.
  465.      * @see        java.io.InputStream
  466.      * @see        java.io.BufferedReader#BufferedReader(java.io.Reader)
  467.      * @see        java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
  468.      */
  469.     public InputStream getLocalizedInputStream(InputStream in) {
  470.     return in;
  471.     }
  472.  
  473.     /**
  474.      * Creates a localized version of an output stream. This method 
  475.      * takes an <code>OutputStream</code> and returns an 
  476.      * <code>OutputStream</code> equivalent to the argument in all respects 
  477.      * except that it is localized: as Unicode characters are written to 
  478.      * the stream, they are automatically converted to the local 
  479.      * character set. 
  480.      * <p>
  481.      * If the argument is already a localized stream, it may be returned 
  482.      * as the result. 
  483.      *
  484.      * @deprecated As of JDK 1.1, the preferred way to translate a
  485.      * Unicode character stream into a byte stream in the local encoding is via
  486.      * the <code>OutputStreamWriter</code>, <code>BufferedWriter</code>, and
  487.      * <code>PrintWriter</code> classes.
  488.      *
  489.      * @return     a localized output stream.
  490.      * @see        java.io.OutputStream
  491.      * @see        java.io.BufferedWriter#BufferedWriter(java.io.Writer)
  492.      * @see        java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  493.      * @see        java.io.PrintWriter#PrintWriter(java.io.OutputStream)
  494.      */
  495.     public OutputStream getLocalizedOutputStream(OutputStream out) {
  496.     return out;
  497.     }
  498.  
  499. }
  500.