001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package java.lang;
018:
019: import java.io.BufferedInputStream;
020: import java.io.FileDescriptor;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.BufferedOutputStream;
024: import java.io.InputStream;
025: import java.io.PrintStream;
026: import java.io.IOException;
027: import java.security.SecurityPermission;
028: import java.util.Map;
029: import java.util.Properties;
030: import java.util.PropertyPermission;
031: import java.nio.channels.spi.SelectorProvider;
032: import java.nio.channels.Channel;
033:
034: import org.apache.harmony.lang.RuntimePermissionCollection;
035: import org.apache.harmony.vm.VMStack;
036: import org.apache.harmony.luni.platform.Environment;
037:
038: /**
039: * @com.intel.drl.spec_ref
040: *
041: * @author Roman S. Bushmanov
042: * @version $Revision: 1.1.2.2.4.3 $
043: */
044: public final class System {
045:
046: /**
047: * This class can not be instantiated.
048: */
049: private System() {
050: }
051:
052: static String getPropertyUnsecure(String key) {
053: return getPropertiesUnsecure().getProperty(key);
054: }
055:
056: /**
057: * @com.intel.drl.spec_ref
058: */
059: public static final PrintStream err = createErr();
060:
061: /**
062: * @com.intel.drl.spec_ref
063: */
064: public static final InputStream in = createIn();
065:
066: /**
067: * @com.intel.drl.spec_ref
068: */
069: public static final PrintStream out = createOut();
070:
071: /**
072: * Current system security manager
073: */
074: private static SecurityManager securityManager = null;
075:
076: /**
077: * Current system properties
078: */
079: private static Properties systemProperties = null;
080:
081: /**
082: * @com.intel.drl.spec_ref
083: */
084: public static void arraycopy(Object src, int srcPos, Object dest,
085: int destPos, int length) {
086: VMMemoryManager.arrayCopy(src, srcPos, dest, destPos, length);
087: }
088:
089: /**
090: * @com.intel.drl.spec_ref
091: */
092: public static long currentTimeMillis() {
093: return VMExecutionEngine.currentTimeMillis();
094: }
095:
096: /**
097: * @com.intel.drl.spec_ref
098: */
099: public static void exit(int status) {
100: Runtime.getRuntime().exit(status);
101: }
102:
103: /**
104: * @com.intel.drl.spec_ref
105: */
106: public static void gc() {
107: Runtime.getRuntime().gc();
108: }
109:
110: /**
111: * @com.intel.drl.spec_ref
112: */
113: public static String getenv(String name) {
114: if (name == null) {
115: throw new NullPointerException("name should not be null");
116: }
117: SecurityManager sm = securityManager;
118: if (sm != null) {
119: sm.checkPermission(new RuntimePermission("getenv." + name));
120: }
121: return Environment.getenv(name);
122: }
123:
124: /**
125: * @com.intel.drl.spec_ref
126: */
127: public static Map<String, String> getenv() {
128: SecurityManager sm = securityManager;
129: if (sm != null) {
130: sm
131: .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: return VMMemoryManager.getIdentityHashCode(object);
193: }
194:
195: /**
196: * @com.intel.drl.spec_ref
197: */
198: public static Channel inheritedChannel() throws IOException {
199: //XXX:does it mean the permission of the "access to the channel"?
200: //If YES then this checkPermission must be removed because it should be presented into java.nio.channels.spi.SelectorProvider.inheritedChannel()
201: //If NO then some other permission name (which one?) should be used here
202: //and the corresponding constant should be placed within org.apache.harmony.lang.RuntimePermission class:
203: if (securityManager != null) {
204: securityManager.checkPermission(new RuntimePermission(
205: "inheritedChannel")); //see java.nio.channels.spi.SelectorProvider.inheritedChannel() spec
206: }
207:
208: return SelectorProvider.provider().inheritedChannel();
209: }
210:
211: /**
212: * @com.intel.drl.spec_ref
213: */
214: public static void load(String filename) {
215: Runtime.getRuntime().load0(
216: filename,
217: VMClassRegistry.getClassLoader(VMStack
218: .getCallerClass(0)), true);
219: }
220:
221: public static void loadLibrary(String libname) {
222: Runtime.getRuntime().loadLibrary0(
223: libname,
224: VMClassRegistry.getClassLoader(VMStack
225: .getCallerClass(0)), true);
226: }
227:
228: /**
229: * @com.intel.drl.spec_ref
230: */
231: public static String mapLibraryName(String libname) {
232: if (libname == null) {
233: throw new NullPointerException(
234: "libname should not be empty");
235: }
236: return VMExecutionEngine.mapLibraryName(libname);
237: }
238:
239: /**
240: * @com.intel.drl.spec_ref
241: */
242: public static long nanoTime() {
243: return VMExecutionEngine.nanoTime();
244: }
245:
246: /**
247: * @com.intel.drl.spec_ref
248: */
249: public static void runFinalization() {
250: Runtime.getRuntime().runFinalization();
251: }
252:
253: /**
254: * @com.intel.drl.spec_ref
255: * @deprecated
256: */
257: public static void runFinalizersOnExit(boolean value) {
258: Runtime.runFinalizersOnExit(value);
259: }
260:
261: /**
262: * @com.intel.drl.spec_ref
263: */
264: public static void setErr(PrintStream err) {
265: SecurityManager sm = securityManager;
266: if (sm != null) {
267: sm
268: .checkPermission(RuntimePermissionCollection.SET_IO_PERMISSION);
269: }
270: setErrUnsecure(err);
271: }
272:
273: /**
274: * @com.intel.drl.spec_ref
275: */
276: public static void setIn(InputStream in) {
277: SecurityManager sm = securityManager;
278: if (sm != null) {
279: sm
280: .checkPermission(RuntimePermissionCollection.SET_IO_PERMISSION);
281: }
282: setInUnsecure(in);
283: }
284:
285: /**
286: * @com.intel.drl.spec_ref
287: */
288: public static void setOut(PrintStream out) {
289: SecurityManager sm = securityManager;
290: if (sm != null) {
291: sm
292: .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(
327: SecurityManager sm) {
328: if (securityManager != null) {
329: securityManager
330: .checkPermission(RuntimePermissionCollection.SET_SECURITY_MANAGER_PERMISSION);
331: }
332:
333: if (sm != null) {
334: // before the new manager assumed office, make a pass through
335: // the common operations and let it load needed classes (if any),
336: // to avoid infinite recursion later on
337: try {
338: sm.checkPermission(new SecurityPermission(
339: "getProperty.package.access"));
340: } catch (Exception ignore) {
341: }
342: try {
343: sm.checkPackageAccess("java.lang");
344: } catch (Exception ignore) {
345: }
346: }
347:
348: securityManager = sm;
349: }
350:
351: /**
352: * Constructs a system <code>err</code> stream. This method is used only
353: * for initialization of <code>err</code> field
354: */
355: private static PrintStream createErr() {
356: return new PrintStream(new BufferedOutputStream(
357: new FileOutputStream(FileDescriptor.err)), true);
358: }
359:
360: /**
361: * Constructs a system <code>in</code> stream. This method is used only
362: * for initialization of <code>in</code> field
363: */
364: private static InputStream createIn() {
365: return new BufferedInputStream(new FileInputStream(
366: FileDescriptor.in));
367: }
368:
369: /**
370: * Constructs a system <code>out</code> stream. This method is used only
371: * for initialization of <code>out</code> field
372: */
373: private static PrintStream createOut() {
374: return new PrintStream(new BufferedOutputStream(
375: new FileOutputStream(FileDescriptor.out)), true);
376: }
377:
378: /**
379: * Returns system properties without security checks. Initializes the system
380: * properties if it isn't done yet.
381: */
382: private static Properties getPropertiesUnsecure() {
383: Properties sp = systemProperties;
384: if (sp == null) {
385: systemProperties = sp = VMExecutionEngine.getProperties();
386: }
387: return sp;
388: }
389:
390: /**
391: * Initiaies the VM shutdown sequence.
392: */
393: static void execShutdownSequence() {
394: Runtime.getRuntime().execShutdownSequence();
395: }
396:
397: /**
398: * Sets the value of <code>err</code> field without any security checks
399: */
400: private static native void setErrUnsecure(PrintStream err);
401:
402: /**
403: * Sets the value of <code>in</code> field without any security checks
404: */
405: private static native void setInUnsecure(InputStream in);
406:
407: /**
408: * Sets the value of <code>out</code> field without any security checks
409: */
410: private static native void setOutUnsecure(PrintStream out);
411:
412: /**
413: * Helps to throw an arbitrary throwable without mentioning within
414: * <code>throw</code> clause and so bypass
415: * exception checking by a compiler.
416: *
417: * @see java.lang.Class#newInstance()
418: */
419: native static void rethrow(Throwable tr);
420: }
|