Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » lang » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package java.lang;
   18   
   19   import java.io.BufferedInputStream;
   20   import java.io.FileDescriptor;
   21   import java.io.FileInputStream;
   22   import java.io.FileOutputStream;
   23   import java.io.BufferedOutputStream;
   24   import java.io.InputStream;
   25   import java.io.PrintStream;
   26   import java.io.IOException;
   27   import java.security.SecurityPermission;
   28   import java.util.Map;
   29   import java.util.Properties;
   30   import java.util.PropertyPermission;
   31   import java.nio.channels.spi.SelectorProvider;
   32   import java.nio.channels.Channel;
   33   
   34   import org.apache.harmony.lang.RuntimePermissionCollection;
   35   import org.apache.harmony.vm.VMStack;
   36   import org.apache.harmony.luni.platform.Environment;
   37   //import org.apache.harmony.drlvm.VMHelper;
   38   //import org.apache.harmony.drlvm.gc_gen.GCHelper;
   39   
   40   /**
   41    * @com.intel.drl.spec_ref 
   42    * 
   43    * @author Roman S. Bushmanov
   44    */
   45   public final class System {
   46   
   47       /**
   48        * This class can not be instantiated.
   49        */
   50       private System() {
   51       }
   52   
   53       static String getPropertyUnsecure(String key) {
   54           return getPropertiesUnsecure().getProperty(key);
   55       }
   56   
   57       /**
   58        * @com.intel.drl.spec_ref
   59        */
   60       public static final PrintStream err = createErr();
   61   
   62       /**
   63        * @com.intel.drl.spec_ref
   64        */
   65       public static final InputStream in = createIn();
   66   
   67       /**
   68        * @com.intel.drl.spec_ref
   69        */
   70       public static final PrintStream out = createOut();
   71   
   72       /**
   73        * Current system security manager
   74        */
   75       private static SecurityManager securityManager = null;
   76   
   77       /**
   78        * Current system properties
   79        */
   80       private static Properties systemProperties = null;
   81   
   82       /**
   83        * @com.intel.drl.spec_ref
   84        */
   85       public static void arraycopy(Object src, int srcPos, Object dest,
   86                                    int destPos, int length) {
   87           VMMemoryManager.arrayCopy(src, srcPos, dest, destPos, length);
   88       }
   89   
   90       /**
   91        * @com.intel.drl.spec_ref
   92        */
   93       public static long currentTimeMillis() {
   94           return VMExecutionEngine.currentTimeMillis();
   95       }
   96   
   97       /**
   98        * @com.intel.drl.spec_ref
   99        */
  100       public static void exit(int status) {
  101           Runtime.getRuntime().exit(status);
  102       }
  103   
  104       /**
  105        * @com.intel.drl.spec_ref
  106        */
  107       public static void gc() {
  108           Runtime.getRuntime().gc();
  109       }
  110   
  111       /**
  112        * @com.intel.drl.spec_ref
  113        */
  114       public static String getenv(String name) {
  115           if (name == null) {
  116               throw new NullPointerException("name should not be null");
  117           }
  118           SecurityManager sm = securityManager;
  119           if (sm != null) {
  120               sm.checkPermission(new RuntimePermission("getenv." + name));
  121           }
  122           return Environment.getenv(name);
  123       }
  124   
  125       /**
  126        * @com.intel.drl.spec_ref
  127        */
  128       public static Map<String, String> getenv() {
  129           SecurityManager sm = securityManager;
  130           if (sm != null) {
  131               sm.checkPermission(RuntimePermissionCollection.GETENV_PERMISSION);
  132           }
  133           return Environment.getenv();
  134       }
  135   
  136       /**
  137        * @com.intel.drl.spec_ref
  138        */
  139       public static Properties getProperties() {
  140           if (securityManager != null) {
  141               securityManager.checkPropertiesAccess();
  142           }
  143           return getPropertiesUnsecure();
  144       }
  145   
  146       /**
  147        * @com.intel.drl.spec_ref
  148        */
  149       public static String getProperty(String key) {
  150           return getProperty(key, null);
  151       }
  152   
  153       /**
  154        * @com.intel.drl.spec_ref
  155        */
  156       public static String getProperty(String key, String def) {
  157           SecurityManager sm = securityManager;
  158           if (sm != null) {
  159               sm.checkPropertyAccess(key);
  160           } else if (key.length() == 0) {
  161               throw new IllegalArgumentException("key is empty");
  162           }
  163           Properties props = getPropertiesUnsecure();
  164           return props.getProperty(key, def);
  165       }
  166       
  167       /**
  168        * @com.intel.drl.spec_ref
  169        */
  170       public static String clearProperty(String key){
  171           SecurityManager sm = securityManager;
  172           if (sm != null) {
  173               sm.checkPermission(new PropertyPermission(key, "write"));
  174           } else if (key.length() == 0) {
  175               throw new IllegalArgumentException("key is empty");
  176           }
  177           Properties props = getPropertiesUnsecure();
  178           return (String)props.remove(key);
  179       }
  180   
  181       /**
  182        * @com.intel.drl.spec_ref
  183        */
  184       public static SecurityManager getSecurityManager() {
  185           return securityManager;
  186       }
  187   
  188       /**
  189        * @com.intel.drl.spec_ref
  190        */
  191       public static int identityHashCode(Object object) {
  192   //	if (VMHelper.isVMMagicPackageSupported()) {
  193   //                return GCHelper.get_hashcode(object);
  194   //        } else {
  195   //                return VMMemoryManager.getIdentityHashCode(object);
  196   //        } 
  197           return VMMemoryManager.getIdentityHashCode(object);
  198       }
  199   
  200       /**
  201        * @com.intel.drl.spec_ref
  202        */
  203       public static Channel inheritedChannel() throws IOException{
  204       	//XXX:does it mean the permission of the "access to the channel"?
  205       	//If YES then this checkPermission must be removed because it should be presented into java.nio.channels.spi.SelectorProvider.inheritedChannel()
  206       	//If NO  then some other permission name (which one?) should be used here
  207       	//and the corresponding constant should be placed within org.apache.harmony.lang.RuntimePermission class: 
  208           if (securityManager != null) {
  209           	securityManager.checkPermission(new RuntimePermission("inheritedChannel")); //see java.nio.channels.spi.SelectorProvider.inheritedChannel() spec
  210           }
  211           
  212           return SelectorProvider.provider().inheritedChannel();
  213       }
  214   
  215       /**
  216        * @com.intel.drl.spec_ref
  217        */
  218       public static void load(String filename) {
  219           Runtime.getRuntime().load0(
  220                   filename,
  221                   VMClassRegistry.getClassLoader(VMStack.getCallerClass(0)), 
  222                   true);
  223       }
  224   
  225       public static void loadLibrary(String libname) {
  226           Runtime.getRuntime().loadLibrary0(
  227                   libname,
  228                   VMClassRegistry.getClassLoader(VMStack.getCallerClass(0)), 
  229                   true);
  230       }
  231   
  232       /**
  233        * @com.intel.drl.spec_ref
  234        */
  235       public static String mapLibraryName(String libname) {
  236           if (libname == null) {
  237               throw new NullPointerException("libname should not be empty");
  238           }
  239           return VMExecutionEngine.mapLibraryName(libname);
  240       }
  241     
  242       /**
  243        * @com.intel.drl.spec_ref
  244        */
  245       public static long nanoTime() {
  246           return VMExecutionEngine.nanoTime();
  247       }
  248   
  249       /**
  250        * @com.intel.drl.spec_ref
  251        */
  252       public static void runFinalization() {
  253           Runtime.getRuntime().runFinalization();
  254       }
  255   
  256       /**
  257        * @com.intel.drl.spec_ref
  258        * @deprecated
  259        */
  260       public static void runFinalizersOnExit(boolean value) {
  261           Runtime.runFinalizersOnExit(value);
  262       }
  263   
  264       /**
  265        * @com.intel.drl.spec_ref
  266        */
  267       public static void setErr(PrintStream err) {
  268           SecurityManager sm = securityManager;
  269           if (sm != null) {
  270               sm.checkPermission(RuntimePermissionCollection.SET_IO_PERMISSION);
  271           }
  272           setErrUnsecure(err);
  273       }
  274   
  275       /**
  276        * @com.intel.drl.spec_ref
  277        */
  278       public static void setIn(InputStream in) {
  279           SecurityManager sm = securityManager;
  280           if (sm != null) {
  281               sm.checkPermission(RuntimePermissionCollection.SET_IO_PERMISSION);
  282           }
  283           setInUnsecure(in);
  284       }
  285   
  286       /**
  287        * @com.intel.drl.spec_ref
  288        */
  289       public static void setOut(PrintStream out) {
  290           SecurityManager sm = securityManager;
  291           if (sm != null) {
  292               sm.checkPermission(RuntimePermissionCollection.SET_IO_PERMISSION);
  293           }
  294           setOutUnsecure(out);
  295       }
  296   
  297       /**
  298        * @com.intel.drl.spec_ref
  299        */
  300       public static void setProperties(Properties props) {
  301           SecurityManager sm = securityManager;
  302           if (sm != null) {
  303               sm.checkPropertiesAccess();
  304           }
  305           systemProperties = props;
  306       }
  307   
  308       /**
  309        * @com.intel.drl.spec_ref
  310        */
  311       public static String setProperty(String key, String value) {
  312           if (key.length() == 0) {
  313               throw new IllegalArgumentException("key is empty");
  314           }
  315           SecurityManager sm = securityManager;
  316           if (sm != null) {
  317               sm.checkPermission(new PropertyPermission(key, "write"));
  318           }
  319           Properties props = getPropertiesUnsecure();
  320           return (String)props.setProperty(key, value);
  321       }
  322   
  323       /**
  324        * @com.intel.drl.spec_ref
  325        */
  326       public static synchronized void setSecurityManager(SecurityManager sm) {
  327           if (securityManager != null) {
  328               securityManager
  329                   .checkPermission(RuntimePermissionCollection.SET_SECURITY_MANAGER_PERMISSION);
  330           }
  331   
  332           if (sm != null) {
  333               // before the new manager assumed office, make a pass through 
  334               // the common operations and let it load needed classes (if any),
  335               // to avoid infinite recursion later on 
  336               try {
  337                   sm.checkPermission(new SecurityPermission("getProperty.package.access")); 
  338               } catch (Exception ignore) {}
  339               try {
  340                   sm.checkPackageAccess("java.lang"); 
  341               } catch (Exception ignore) {}
  342           }
  343   
  344           securityManager = sm;
  345       }
  346   
  347       /**
  348        * Constructs a system <code>err</code> stream. This method is used only
  349        * for initialization of <code>err</code> field
  350        */
  351       private static PrintStream createErr() {
  352           return new PrintStream(new BufferedOutputStream(new FileOutputStream(
  353                   FileDescriptor.err)), true);
  354       }
  355   
  356       /**
  357        * Constructs a system <code>in</code> stream. This method is used only
  358        * for initialization of <code>in</code> field
  359        */
  360       private static InputStream createIn() {
  361           return new BufferedInputStream(new FileInputStream(FileDescriptor.in));
  362       }
  363   
  364       /**
  365        * Constructs a system <code>out</code> stream. This method is used only
  366        * for initialization of <code>out</code> field
  367        */
  368       private static PrintStream createOut() {
  369           return new PrintStream(new BufferedOutputStream(new FileOutputStream(
  370                   FileDescriptor.out)), true);
  371       }
  372   
  373       /**
  374        * Returns system properties without security checks. Initializes the system
  375        * properties if it isn't done yet.
  376        */
  377       private static Properties getPropertiesUnsecure() {
  378           Properties sp = systemProperties;
  379           if (sp == null) {
  380               systemProperties = sp = VMExecutionEngine.getProperties();
  381           }
  382           return sp;
  383       }
  384   
  385       /**
  386        * Initiaies the VM shutdown sequence.
  387        */
  388       static void execShutdownSequence() {
  389           Runtime.getRuntime().execShutdownSequence();
  390       }
  391   
  392       /**
  393        * Sets the value of <code>err</code> field without any security checks
  394        */
  395       private static native void setErrUnsecure(PrintStream err);
  396   
  397       /**
  398        * Sets the value of <code>in</code> field without any security checks
  399        */
  400       private static native void setInUnsecure(InputStream in);
  401   
  402       /**
  403        * Sets the value of <code>out</code> field without any security checks
  404        */
  405       private static native void setOutUnsecure(PrintStream out);
  406   
  407       /**
  408        *  Helps to throw an arbitrary throwable without mentioning within 
  409        *  <code>throw</code> clause and so bypass 
  410        *  exception checking by a compiler.
  411        *  
  412        *  @see java.lang.Class#newInstance()
  413        */
  414       native static void rethrow(Throwable tr);
  415   }
  416   

Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » lang » [javadoc | source]