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: /**
018: * @author Evgueni Brevnov, Roman S. Bushmanov
019: * @version $Revision: 1.1.2.1.4.4 $
020: */package java.lang;
021:
022: import java.util.Properties;
023: import java.util.Vector;
024:
025: /**
026: * Provides the methods to interact with VM Execution Engine that are used by
027: * different classes from the <code>java.lang</code> package, such as System,
028: * Runtime.
029: * <p>
030: * This class must be implemented according to the common policy for porting
031: * interfaces - see the porting interface overview for more detailes.
032: *
033: * @api2vm
034: */
035: final class VMExecutionEngine {
036:
037: /**
038: * keeps Runnable objects of the shutdown sequence
039: */
040: private static Vector<Runnable> shutdownActions = new Vector<Runnable>();
041:
042: /**
043: * This class is not supposed to be instantiated.
044: */
045: private VMExecutionEngine() {
046: }
047:
048: /**
049: * Terminates a Virtual Machine with possible invocation of finilization
050: * methods. This method is used by the {@link Runtime#exit(int)
051: * Runtime.exit(int status)} and {@link Runtime#halt(int)
052: * Runtime.halt(int satus)} methods implementation. When it is called by the
053: * <code>Runtime.exit(int status)</code> method all shutdown hook threads
054: * should have been already finished. The needFinilization argument must
055: * be true if uninvoked finilizers should be called on VM exit. The
056: * implementation simply calls the
057: * {@link VMExecutionEngine#exit(int, boolean, Runnable[])
058: * VMExecutionEngine.exit(int status, boolean needFinalization,
059: * Runnable[] shutdownSequence)} method with an array of
060: * <code>Runnable</code> objects which were registered before by means of
061: * the {@link VMExecutionEngine#registerShutdownAction(Runnable)
062: * VMExecutionEngine.registerShutdownAction(Runnable action)} method.
063: *
064: * @param status exit status
065: * @param needFinalize specifies that finalization must be performed. If
066: * true then it perfoms finalization of all not finalized objects
067: * that have finalizers
068: * @api2vm
069: */
070: static void exit(int status, boolean needFinalization) {
071: exit(status, needFinalization, shutdownActions
072: .toArray(new Runnable[0]));
073: }
074:
075: /**
076: * Call to this method forces VM to
077: * <ol>
078: * <li> Execute uninvoked finilizers if needFinalization is true </li>
079: * <li> Forcibly stop all running non-system threads
080: * <li> Sequentially execute the <code>run</code> method for each object
081: * of the shutdownSequence array. The execution starts from the last
082: * element of the array. No threads will be created to perform
083: * execution. If uncatched exception occurs it's stack trace is
084: * printed to the error stream and the next element of the array if
085: * any will be executed
086: * <li> Exit
087: * </ol>
088: *
089: * @param status exit status
090: * @param needFinalization indicates that finilization should be performed
091: * @param shutdownSequence array of shutdown actions
092: * @api2vm
093: */
094: private static native void exit(int status,
095: boolean needFinalization, Runnable[] shutdownSequence);
096:
097: /**
098: * This method provides an information about the assertion status specified
099: * via command line options.
100: * <p>
101: *
102: * @see java.lang.Class#desiredAssertionStatus()
103: *
104: * @param clss the class to be initialized with the assertion status. Note,
105: * assertion status is applicable to top-level classes only, therefore
106: * any member/local class passed is a subject to conversion to corresponding
107: * top-level declaring class. Also, <code>null</code> argument can be used to
108: * check if any assertion was specified through command line options.
109: * @param recursive controls whether this method should check exact match
110: * with name of the class, or check (super)packages recursively
111: * (most specific one has precedence).
112: * @param defaultStatus if no specific package setting found,
113: * this value may override command-line defaults. This parameter is
114: * actual only when <code>recursive == true</code>.
115: * @see java.lang.ClassLoader#setDefaultAssertionStatus(boolean)
116: * @return 0 - unspecified, < 0 - false, > 0 - true
117: * @api2vm
118: */
119: static native int getAssertionStatus(Class clss, boolean recursive,
120: int defaultStatus);
121:
122: /**
123: * This method satisfies the requirements of the specification for the
124: * {@link Runtime#availableProcessors() Runtime.availableProcessors()}
125: * method.
126: * @api2vm
127: */
128: static native int getAvailableProcessors();
129:
130: /**
131: * This method satisfies the requirements of the specification for the
132: * {@link System#getProperties() System.getProperties()} method.
133: * <p>
134: * Additionally a class library implementation may relay on existance of
135: * the following properties "vm.boot.class.path" & "vm.boot.library.path".
136: * The "vm.boot.class.path" property can be used to load classes and
137: * resources which reside in the bootstrap sequence of the VM.
138: * The "vm.boot.library.path" property can be used to find libraries that
139: * should be obtatined by classes which were loaded by bootstrap class loader.
140: * @api2vm
141: */
142: static native Properties getProperties();
143:
144: /**
145: * Adds the specified action to the list of shutdown actions. The
146: * {@link Runnable#run() Runnable.run()} method of the specified action
147: * object is executed after all non-system threads have been stopped. Last
148: * registered action is executed before previously registered actions. Each
149: * action should not create threads inside. It is expected that registered
150: * actions doesn't requre a lot of time to complete.
151: * <p>
152: * Typicily one may use this method to close open files, connections etc. on
153: * Virtual Machine exit
154: * @param action action which should be performed on VM exit
155: * @api2vm
156: */
157: public static void registerShutdownAction(Runnable action) {
158: shutdownActions.add(action);
159: }
160:
161: /**
162: * This method satisfies the requirements of the specification for the
163: * {@link Runtime#traceInstructions(boolean)
164: * Runtime.traceInstructions(boolean on)} method.
165: * @api2vm
166: */
167: static native void traceInstructions(boolean enable);
168:
169: /**
170: * This method satisfies the requirements of the specification for the
171: * {@link Runtime#traceMethodCalls(boolean)
172: * Runtime.traceMethodCalls(boolean on)} method.
173: * @api2vm
174: */
175: static native void traceMethodCalls(boolean enable);
176:
177: /**
178: * Returns the current system time in milliseconds since
179: * the Unix epoch (midnight, 1 Jan, 1970).
180: * @api2vm
181: */
182: static native long currentTimeMillis();
183:
184: /**
185: * Returns the current value of a system timer with the best accuracy
186: * the OS can provide, in nanoseconds.
187: * @api2vm
188: */
189: static native long nanoTime();
190:
191: /**
192: * Returns platform-specific name of the specified library.
193: * @api2vm
194: */
195: static native String mapLibraryName(String libname);
196: }
|