home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / Runtime.java < prev    next >
Text File  |  1996-05-03  |  9KB  |  288 lines

  1. /*
  2.  * @(#)Runtime.java    1.15 96/03/27 Frank Yellin
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.io.*;
  23. import java.util.StringTokenizer;
  24.  
  25.  
  26. public class Runtime {
  27.     private static Runtime currentRuntime = new Runtime();
  28.       
  29.  
  30.     /**
  31.      * Returns the runtime.
  32.      */
  33.     public static Runtime getRuntime() { 
  34.     return currentRuntime;
  35.     }
  36.     
  37.     /** Don't let anyone else instantiate this class */
  38.     private Runtime() {}
  39.  
  40.     /* Helper for exit
  41.      */
  42.     private native void exitInternal(int status);
  43.  
  44.     /**
  45.      * Exits the virtual machine with an exit code. This method does
  46.      * not return, use with caution.
  47.      * @param status exit status, 0 if successful, other values indicate
  48.      *        various error types. 
  49.      */
  50.     public void exit(int status) {
  51.     SecurityManager security = System.getSecurityManager();
  52.     if (security != null) {
  53.         security.checkExit(status);
  54.     }
  55.     exitInternal(status);
  56.     }
  57.  
  58.     /* Helper for exec
  59.      */
  60.     private native Process execInternal(String cmdarray[], String envp[]) 
  61.      throws IOException;
  62.  
  63.     /**
  64.      * Executes the system command specified in the parameter.
  65.      * Returns a Process which has methods for optaining the stdin,
  66.      * stdout, and stderr of the subprocess.  This method fails if
  67.      * executed by untrusted code.
  68.      *
  69.      * @param command a specified system command
  70.      * @return an instance of class Process
  71.      */
  72.     public Process exec(String command) throws IOException {
  73.     return exec(command, null);
  74.     }
  75.  
  76.     /**
  77.      * Executes the system command specified in the parameter.
  78.      * Returns a Process which has methods for optaining the stdin,
  79.      * stdout, and stderr of the subprocess.  This method fails if
  80.      * executed by untrusted code.
  81.      *
  82.      * @param command a specified system command
  83.      * @return an instance of class Process
  84.      */
  85.     public Process exec(String command, String envp[]) throws IOException {
  86.     int count = 0;
  87.     String cmdarray[];
  88.      StringTokenizer st;
  89.  
  90.     st = new StringTokenizer(command);
  91.      count = st.countTokens();
  92.  
  93.     cmdarray = new String[count];
  94.     st = new StringTokenizer(command);
  95.     count = 0;
  96.      while (st.hasMoreTokens()) {
  97.          cmdarray[count++] = st.nextToken();
  98.      }
  99.     SecurityManager security = System.getSecurityManager();
  100.     if (security != null) {
  101.         security.checkExec(cmdarray[0]);
  102.     }
  103.     return execInternal(cmdarray, envp);
  104.     }
  105.  
  106.     /**
  107.      * Executes the system command specified by cmdarray[0] with arguments
  108.      * specified by the strings in the rest of the array.
  109.      * Returns a Process which has methods for optaining the stdin,
  110.      * stdout, and stderr of the subprocess.  This method fails if
  111.      * executed by untrusted code.
  112.      *
  113.      * @param an array containing the command to call and its arguments
  114.      * @param envp array containing environment in format name=value
  115.      * @return an instance of class Process
  116.      */
  117.  
  118.     public Process exec(String cmdarray[]) throws IOException {
  119.     return exec(cmdarray, null);
  120.     }
  121.  
  122.     /**
  123.      * Executes the system command specified by cmdarray[0] with arguments
  124.      * specified by the strings in the rest of the array.
  125.      * Returns a Process which has methods for optaining the stdin,
  126.      * stdout, and stderr of the subprocess.  This method fails if
  127.      * executed by untrusted code.
  128.      *
  129.      * @param an array containing the command to call and its arguments
  130.      * @param envp array containing environment in format name=value
  131.      * @return an instance of class Process
  132.      */
  133.  
  134.     public Process exec(String cmdarray[], String envp[]) throws IOException {
  135.     SecurityManager security = System.getSecurityManager();
  136.     if (security != null) {
  137.         security.checkExec(cmdarray[0]);
  138.     }
  139.     return execInternal(cmdarray, envp);
  140.     }
  141.  
  142.  
  143.     /**
  144.      * Returns the number of free bytes in system memory. This number
  145.      * is not always accurate because it is just an estimation of the available
  146.      * memory.  More memory may be freed by calling System.gc() .
  147.      */
  148.     public native long freeMemory();
  149.  
  150.     /**
  151.      * Returns the total number of bytes in system memory. 
  152.      */
  153.     public native long totalMemory();
  154.  
  155.     /**
  156.      * Runs the garbage collector.
  157.      */
  158.     public native void gc();
  159.  
  160.     /**
  161.      * Runs the finalization methods of any objects pending finalization.
  162.      * Usually you will not need to call this method since finalization
  163.      * methods will be called asynchronously by the finalization thread.
  164.      * However, under some circumstances (like running out of a finalized
  165.      * resource) it can be useful to run finalization methods synchronously.
  166.      */
  167.     public native void runFinalization();
  168.  
  169.     /**
  170.      * Enables/Disables tracing of instructions.
  171.      * @param on    start tracing if true
  172.      */
  173.     public native void traceInstructions(boolean on);
  174.  
  175.     /**
  176.      * Enables/Disables tracing of method calls.
  177.      * @param on    start tracing if true
  178.      */
  179.     public native void traceMethodCalls(boolean on);
  180.  
  181.     /**
  182.      * Initializes the linker and returns the search path for shared libraries.
  183.      */
  184.     private synchronized native String initializeLinkerInternal();
  185.     private native String buildLibName(String pathname, String filename);
  186.  
  187.     /* Helper for load and loadLibrary */
  188.     private native boolean loadFileInternal(String filename);
  189.  
  190.  
  191.     /** The paths searched for libraries */
  192.     private String paths[];
  193.  
  194.     private void initializeLinker() {
  195.     String ldpath = initializeLinkerInternal();
  196.     char c = System.getProperty("path.separator").charAt(0);
  197.     int ldlen = ldpath.length();
  198.     int i, j, n;
  199.     // Count the separators in the path
  200.     i = ldpath.indexOf(c);
  201.     n = 0;
  202.     while (i >= 0) {
  203.         n++;
  204.         i = ldpath.indexOf(c, i+1);
  205.     }
  206.  
  207.     // allocate the array of paths - n :'s = n + 1 path elements
  208.     paths = new String[n + 1];
  209.  
  210.     // Fill the array with paths from the ldpath
  211.     n = i = 0;
  212.     j = ldpath.indexOf(c);
  213.     while (j >= 0) {
  214.         if (j - i > 0) {
  215.         paths[n++] = ldpath.substring(i, j);
  216.         } else if (j - i == 0) { 
  217.         paths[n++] = ".";
  218.         }
  219.         i = j + 1;
  220.         j = ldpath.indexOf(c, i);
  221.     }
  222.     paths[n] = ldpath.substring(i, ldlen);
  223.     }
  224.     
  225.  
  226.     /**
  227.      * Loads a dynamic library, given a complete path name. If you use this
  228.      * from java_g it will automagically insert "_g" before the ".so".
  229.      *
  230.      * Example: <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>
  231.      * @param filename the file to load
  232.      * @exception UnsatisfiedLinkError If the file does not exist.
  233.      * @see #getRuntime
  234.      */
  235.     public synchronized void load(String filename) {
  236.     SecurityManager security = System.getSecurityManager();
  237.     if (security != null) {
  238.         security.checkLink(filename);
  239.     }
  240.         if (!loadFileInternal(filename)) {
  241.         throw new UnsatisfiedLinkError(filename);
  242.     }
  243.     }
  244.  
  245.     /**
  246.      * Loads a dynamic library with the specified library name. The 
  247.      * call to LoadLibrary() should be made in the static 
  248.      * initializer of the first class that is loaded. Linking in the 
  249.      * same library more than once is ignored.
  250.      * @param libname the name of the library
  251.      * @exception UnsatisfiedLinkError If the library does not exist. 
  252.      */
  253.     public synchronized void loadLibrary(String libname) {
  254.     SecurityManager security = System.getSecurityManager();
  255.     if (security != null) {
  256.         security.checkLink(libname);
  257.     }
  258.         if (paths == null) {
  259.             initializeLinker();
  260.     }
  261.     for (int i = 0 ; i < paths.length ; i++) {
  262.         String tempname = buildLibName(paths[i], libname);
  263.         if (loadFileInternal(tempname)) {
  264.             return;
  265.         }
  266.     }
  267.     // Oops, it failed
  268.         throw new UnsatisfiedLinkError("no " + libname + 
  269.                        " in shared library path");
  270.     }
  271.  
  272.     /**
  273.      * Localize an input stream. A localized input stream will automatically
  274.      * translate the input from the local format to UNICODE. 
  275.      */
  276.     public InputStream getLocalizedInputStream(InputStream in) {
  277.     return in;
  278.     }
  279.  
  280.     /**
  281.      * Localize an output stream. A localized output stream will automatically
  282.      * translate the output from UNICODE to the local format.
  283.      */
  284.     public OutputStream getLocalizedOutputStream(OutputStream out) {
  285.     return out;
  286.     }
  287. }
  288.