| 1 | // System.java - System-specific info.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.lang;
|
|---|
| 12 |
|
|---|
| 13 | import java.io.FileDescriptor;
|
|---|
| 14 | import java.io.FileInputStream;
|
|---|
| 15 | import java.io.FileOutputStream;
|
|---|
| 16 | import java.io.FilterInputStream;
|
|---|
| 17 | import java.io.InputStream;
|
|---|
| 18 | import java.io.PrintStream;
|
|---|
| 19 | import java.io.BufferedInputStream;
|
|---|
| 20 | import java.io.BufferedOutputStream;
|
|---|
| 21 | import java.util.Properties;
|
|---|
| 22 | import java.util.PropertyPermission;
|
|---|
| 23 | import java.util.TimeZone;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * @author Tom Tromey <[email protected]>
|
|---|
| 27 | * @date August 27, 1998
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 31 | * "The Java Language Specification", ISBN 0-201-63451-1
|
|---|
| 32 | * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
|
|---|
| 33 | * Status: 1.1. Some 1.2 methods missing. Properties code not fully
|
|---|
| 34 | * implemented.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | public final class System
|
|---|
| 38 | {
|
|---|
| 39 | public static native void arraycopy (Object src, int srcOffset,
|
|---|
| 40 | Object dst, int dstOffset,
|
|---|
| 41 | int count);
|
|---|
| 42 |
|
|---|
| 43 | public static native long currentTimeMillis ();
|
|---|
| 44 |
|
|---|
| 45 | // FIXME: When merging with Classpath, remember to remove the call to
|
|---|
| 46 | // getDefaultTimeZoneId from java.util.Timezone.
|
|---|
| 47 | private static native String getSystemTimeZone ();
|
|---|
| 48 |
|
|---|
| 49 | // Get the System Timezone as reported by the OS. It should be in
|
|---|
| 50 | // the form PST8PDT so we'll need to parse it and check that it's valid.
|
|---|
| 51 | // The result is used to set the user.timezone property in init_properties.
|
|---|
| 52 | // FIXME: Using the code from Classpath for generating the System
|
|---|
| 53 | // Timezone IMO is suboptimal because it ignores whether the rules for
|
|---|
| 54 | // DST match up.
|
|---|
| 55 | private static String getDefaultTimeZoneId ()
|
|---|
| 56 | {
|
|---|
| 57 | String sysTimeZoneId = getSystemTimeZone ();
|
|---|
| 58 |
|
|---|
| 59 | // Check if this is a valid timezone. Make sure the IDs match
|
|---|
| 60 | // since getTimeZone returns GMT if no match is found.
|
|---|
| 61 | TimeZone tz = TimeZone.getTimeZone (sysTimeZoneId);
|
|---|
| 62 | if (tz.getID ().equals (sysTimeZoneId))
|
|---|
| 63 | return sysTimeZoneId;
|
|---|
| 64 |
|
|---|
| 65 | // Check if the base part of sysTimeZoneId is a valid timezone that
|
|---|
| 66 | // matches with daylight usage and rawOffset. Make sure the IDs match
|
|---|
| 67 | // since getTimeZone returns GMT if no match is found.
|
|---|
| 68 | // First find start of GMT offset info and any Daylight zone name.
|
|---|
| 69 | int startGMToffset = 0;
|
|---|
| 70 | int sysTimeZoneIdLength = sysTimeZoneId.length();
|
|---|
| 71 | for (int i = 0; i < sysTimeZoneIdLength && startGMToffset == 0; i++)
|
|---|
| 72 | {
|
|---|
| 73 | if (Character.isDigit (sysTimeZoneId.charAt (i)))
|
|---|
| 74 | startGMToffset = i;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | int startDaylightZoneName = 0;
|
|---|
| 78 | boolean usesDaylight = false;
|
|---|
| 79 | for (int i = sysTimeZoneIdLength - 1;
|
|---|
| 80 | i >= 0 && !Character.isDigit (sysTimeZoneId.charAt (i)); --i)
|
|---|
| 81 | {
|
|---|
| 82 | startDaylightZoneName = i;
|
|---|
| 83 | }
|
|---|
| 84 | if (startDaylightZoneName > 0)
|
|---|
| 85 | usesDaylight = true;
|
|---|
| 86 |
|
|---|
| 87 | int GMToffset = Integer.parseInt (startDaylightZoneName == 0 ?
|
|---|
| 88 | sysTimeZoneId.substring (startGMToffset) :
|
|---|
| 89 | sysTimeZoneId.substring (startGMToffset, startDaylightZoneName));
|
|---|
| 90 |
|
|---|
| 91 | // Offset could be in hours or seconds. Convert to millis.
|
|---|
| 92 | if (GMToffset < 24)
|
|---|
| 93 | GMToffset *= 60 * 60;
|
|---|
| 94 | GMToffset *= -1000;
|
|---|
| 95 |
|
|---|
| 96 | String tzBasename = sysTimeZoneId.substring (0, startGMToffset);
|
|---|
| 97 | tz = TimeZone.getTimeZone (tzBasename);
|
|---|
| 98 | if (tz.getID ().equals (tzBasename) && tz.getRawOffset () == GMToffset)
|
|---|
| 99 | {
|
|---|
| 100 | boolean tzUsesDaylight = tz.useDaylightTime ();
|
|---|
| 101 | if (usesDaylight && tzUsesDaylight || !usesDaylight && !tzUsesDaylight)
|
|---|
| 102 | return tzBasename;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // If no match, see if a valid timezone has the same attributes as this
|
|---|
| 106 | // and then use it instead.
|
|---|
| 107 | String[] IDs = TimeZone.getAvailableIDs (GMToffset);
|
|---|
| 108 | for (int i = 0; i < IDs.length; ++i)
|
|---|
| 109 | {
|
|---|
| 110 | // FIXME: The daylight savings rules may not match the rules
|
|---|
| 111 | // for the desired zone.
|
|---|
| 112 | boolean IDusesDaylight =
|
|---|
| 113 | TimeZone.getTimeZone (IDs[i]).useDaylightTime ();
|
|---|
| 114 | if (usesDaylight && IDusesDaylight || !usesDaylight && !IDusesDaylight)
|
|---|
| 115 | return IDs[i];
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // If all else fails, return null.
|
|---|
| 119 | return null;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | public static void exit (int status)
|
|---|
| 123 | {
|
|---|
| 124 | Runtime.getRuntime().exit(status);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | public static void gc ()
|
|---|
| 128 | {
|
|---|
| 129 | Runtime.getRuntime().gc();
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | // Marked deprecated in 1.1. We implement what the JCL book says.
|
|---|
| 133 | public static String getenv (String name)
|
|---|
| 134 | {
|
|---|
| 135 | throw new Error ();
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | private static native void init_properties ();
|
|---|
| 139 |
|
|---|
| 140 | public static Properties getProperties ()
|
|---|
| 141 | {
|
|---|
| 142 | if (secman != null)
|
|---|
| 143 | secman.checkPropertiesAccess();
|
|---|
| 144 | if (properties == null)
|
|---|
| 145 | init_properties ();
|
|---|
| 146 | return properties;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | public static String getProperty (String property)
|
|---|
| 150 | {
|
|---|
| 151 | if (secman != null)
|
|---|
| 152 | secman.checkPropertyAccess(property);
|
|---|
| 153 | if (properties == null)
|
|---|
| 154 | init_properties ();
|
|---|
| 155 | return properties.getProperty(property);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | public static String getProperty (String property, String defval)
|
|---|
| 159 | {
|
|---|
| 160 | if (secman != null)
|
|---|
| 161 | secman.checkPropertyAccess(property);
|
|---|
| 162 | if (properties == null)
|
|---|
| 163 | init_properties ();
|
|---|
| 164 | return properties.getProperty(property, defval);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | public static SecurityManager getSecurityManager ()
|
|---|
| 168 | {
|
|---|
| 169 | return secman;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | public static native int identityHashCode (Object obj);
|
|---|
| 173 |
|
|---|
| 174 | public static void load (String pathname)
|
|---|
| 175 | {
|
|---|
| 176 | Runtime.getRuntime().load(pathname);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | public static void loadLibrary (String libname)
|
|---|
| 180 | {
|
|---|
| 181 | Runtime.getRuntime().loadLibrary(libname);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | public static void runFinalization ()
|
|---|
| 185 | {
|
|---|
| 186 | Runtime.getRuntime().runFinalization();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | // Marked as deprecated in 1.2.
|
|---|
| 190 | public static void runFinalizersOnExit (boolean run)
|
|---|
| 191 | {
|
|---|
| 192 | Runtime.getRuntime().runFinalizersOnExit(run);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | private static void checkSetIO ()
|
|---|
| 196 | {
|
|---|
| 197 | // In 1.1, we are supposed to call checkExec, but the argument is
|
|---|
| 198 | // not specified. In 1.2, we are supposed to use checkPermission,
|
|---|
| 199 | // which doesn't exist in 1.1.
|
|---|
| 200 | if (secman != null)
|
|---|
| 201 | secman.checkExec("");
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | public static native void setErr (PrintStream newErr);
|
|---|
| 205 | public static native void setIn (InputStream newIn);
|
|---|
| 206 | public static native void setOut (PrintStream newOut);
|
|---|
| 207 |
|
|---|
| 208 | public static void setProperties (Properties props)
|
|---|
| 209 | {
|
|---|
| 210 | if (secman != null)
|
|---|
| 211 | secman.checkPropertiesAccess();
|
|---|
| 212 | synchronized (System.class)
|
|---|
| 213 | {
|
|---|
| 214 | properties = props;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | public static String setProperty (String key, String value)
|
|---|
| 219 | {
|
|---|
| 220 | if (secman != null)
|
|---|
| 221 | secman.checkPermission (new PropertyPermission (key, "write"));
|
|---|
| 222 | if (properties == null)
|
|---|
| 223 | init_properties ();
|
|---|
| 224 | return (String) properties.setProperty (key, value);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | // TODO 1.2.
|
|---|
| 228 | // public static String mapLibraryName (String libname);
|
|---|
| 229 |
|
|---|
| 230 | public static void setSecurityManager (SecurityManager s)
|
|---|
| 231 | {
|
|---|
| 232 | if (secman != null)
|
|---|
| 233 | secman.checkPermission(new RuntimePermission("setSecurityManager"));
|
|---|
| 234 | secman = s;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | // Public data.
|
|---|
| 238 | public static final InputStream in = new BufferedInputStream (new FileInputStream (FileDescriptor.in));
|
|---|
| 239 |
|
|---|
| 240 | public static final PrintStream out = new PrintStream (new BufferedOutputStream (new FileOutputStream (FileDescriptor.out)), true);
|
|---|
| 241 |
|
|---|
| 242 | public static final PrintStream err = new PrintStream (new BufferedOutputStream (new FileOutputStream (FileDescriptor.err)), true);
|
|---|
| 243 |
|
|---|
| 244 | // Don't allow System objects to be made.
|
|---|
| 245 | private System ()
|
|---|
| 246 | {
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | // Private data.
|
|---|
| 250 | private static SecurityManager secman = null;
|
|---|
| 251 | private static Properties properties = null;
|
|---|
| 252 | }
|
|---|