001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.cldchi.jvm;
028:
029: public class JVM {
030: /**
031: * If this flag is defined and the romization is successful, class
032: * files are removed from the JAR file(s) after the romization
033: * process. This parameter is ignored for source romization.
034: */
035: public static final int REMOVE_CLASSES_FROM_JAR = (1 << 1);
036:
037: /**
038: * Returned by getAppImageProgress() to indicate that the last image
039: * creation process has was cancelled before it was completed.
040: */
041: public static final int STATUS_CANCELLED = -3;
042:
043: /**
044: * Returned by getAppImageProgress() to indicate that the last image
045: * creation process has failed before it was completed.
046: */
047: public static final int STATUS_FAILED = -2;
048:
049: /**
050: * Returned by getAppImageProgress() to indicate that no image
051: * creation process has ever been started since the VM was bootstraped.
052: */
053: public static final int STATUS_VIRGIN = -1;
054:
055: /**
056: * Any value returned by getAppImageProgress() that.s greater or equal to
057: * STATUS_START, but lower than STATUS_SUCCEEDED, means that the
058: * image creation is still taking place.
059: */
060: public static final int STATUS_START = 0;
061:
062: /**
063: * Returned by getAppImageProgress() to indicate that the last image
064: * creation process has succeeded.
065: */
066: public static final int STATUS_SUCCEEDED = 100;
067:
068: /**
069: * Returned by verifyJar() to indicate no classes verification
070: * has ever been started since VM didn't find any classes in JAR.
071: */
072: public static final int STATUS_VERIFY_NOTHING = 1;
073:
074: /**
075: * Returned by verifyJar() to indicate all JAR classes were
076: * successfully verified.
077: */
078: public static final int STATUS_VERIFY_SUCCEEDED = 2;
079:
080: /**
081: * Returned by verifyJar() to indicate JAR classes verification
082: * failed by some reason.
083: */
084: public static final int STATUS_VERIFY_FAILED = 3;
085:
086: /**
087: * Creates an application image file. It loads the Java classes
088: * from the <code>jarFile</code> into the heap, verify the class
089: * contents, and write the classes to an Application Image file as
090: * specified by <code>binFile</code>. This function is typically
091: * executed by the Application Management Software (AMS)
092: * immediately after a JAR file is downloaded to the device. <p>
093: *
094: * This function must be called with a clean VM state -- i.e., if a
095: * Java application is executing, you must exit the Java application
096: * before running the Converter. <p>
097: *
098: * In MVM mode, this method should not be called only from within
099: * a clean Isolate. <p>
100: *
101: * <b>Interaction with classpath and shared libraries: </b>
102: * In the context of the VM (or current Isolate), the classpath
103: * may be specified to additional shared libraries. These shared
104: * libraries are loaded first, before jarFile is loaded. All shared
105: * libraries specified on the classpath must be binary image files
106: * and must be be JAR files.
107: *
108: * Note that if the image creation process was cancelled, no exception
109: * is thrown. A subsequent call to getAppImageProgress() will return
110: * STATUS_CANCELLED.
111: *
112: * @param jarFile specifies the JAR file to be converted.
113: *
114: * @param binFile specifies the name of the app image file to be
115: * written into
116: *
117: * @exception Error if another instance of the converter is
118: * already running.
119: *
120: * @exception OutOfMemoryError if the VM ran out of memory during
121: * the image creation process.
122: */
123: private static void createAppImage(char jarFile[], char binFile[],
124: int flags) throws Error {
125: startAppImage(jarFile, binFile, flags);
126: for (;;) {
127: if (!createAppImage0()) {
128: break;
129: }
130: }
131: }
132:
133: public static void createAppImage(String jarFile, String binFile,
134: int flags) throws Error {
135: createAppImage(jarFile.toCharArray(), binFile.toCharArray(),
136: flags);
137: }
138:
139: public native static int getAppImageProgress();
140:
141: /**
142: * If an image creation process is underway, cancel it. This will
143: * force createAppImage() to delete all temporary files, as well as
144: * the output image file, and return immediately. A future call to
145: * getAppImageProgress() will return STATUS_CANCELLED.
146: *
147: * If an image creation process is not underway, this method has no
148: * effect.
149: */
150: public native static void cancelImageCreation();
151:
152: private static native void startAppImage(char jarFile[],
153: char binFile[], int flags) throws Error;
154:
155: /**
156: * Returns true if the image creation process has completed or
157: * been concelled.
158: */
159: private native static boolean createAppImage0();
160:
161: /**
162: * This method is used by the source romizer to create ROMImage.cpp.
163: *
164: * @exception Error if the romization process fails for any reason.
165: */
166: private native static void createSysImage() throws Error;
167:
168: /**
169: * This method is used to load binary library into the VM.
170: * It allows to call native function implementations from this library.
171: *
172: * @param libName name of the library WITHOUT EXTENSION. It was made to make
173: * java code platform-independent.
174: *
175: * @exception Error if the VM fails to load the library with this name.
176: */
177: public native static void loadLibrary(String libName) throws Error;
178:
179: /**
180: * Copy an array from the specified source array, beginning at the
181: * specified position, to the specified position of the destination array.
182: * <p>
183: * Impose the following restrictions on the input arguments:
184: * <ul>
185: * <li><code>dst</code> is not <code>null</code>.
186: * <li><code>src</code> is not <code>null</code>.
187: * <li>The <code>srcOffset</code> argument is not negative.
188: * <li>The <code>dstOffset</code> argument is not negative.
189: * <li>The <code>length</code> argument is not negative.
190: * <li><code>srcOffset+length</code> is not greater than
191: * <code>src.length</code>, the length of the source array.
192: * <li><code>dstOffset+length</code> is not greater than
193: * <code>dst.length</code>, the length of the destination array.
194: * <li>any actual component of the source array from position
195: * <code>srcOffset</code> through <code>srcOffset+length-1</code>
196: * can be converted to the component type of the destination array
197: * </ul>
198: * <p>
199: * The caller is responsible that these restrictions are not violated.
200: * If any of the restrictions above is violated, the behavior is undefined.
201: *
202: * @param src the source array.
203: * @param srcOffset start position in the source array.
204: * @param dst the destination array.
205: * @param dstOffset start position in the destination data.
206: * @param length the number of array elements to be copied.
207: */
208: public static void unchecked_byte_arraycopy(byte[] src,
209: int srcOffset, byte[] dst, int dstOffset, int length) {
210: System.arraycopy(src, srcOffset, dst, dstOffset, length);
211: }
212:
213: public static void unchecked_char_arraycopy(char[] src,
214: int srcOffset, char[] dst, int dstOffset, int length) {
215: System.arraycopy(src, srcOffset, dst, dstOffset, length);
216: }
217:
218: public static void unchecked_int_arraycopy(int[] src,
219: int srcOffset, int[] dst, int dstOffset, int length) {
220: System.arraycopy(src, srcOffset, dst, dstOffset, length);
221: }
222:
223: public static void unchecked_long_arraycopy(long[] src,
224: int srcOffset, long[] dst, int dstOffset, int length) {
225: System.arraycopy(src, srcOffset, dst, dstOffset, length);
226: }
227:
228: public static void unchecked_obj_arraycopy(Object[] src,
229: int srcOffset, Object[] dst, int dstOffset, int length) {
230: System.arraycopy(src, srcOffset, dst, dstOffset, length);
231: }
232:
233: /**
234: * Verifies all classes of the given JAR package within the current
235: * VM instance. The JAR path should be included into classpath(s) of
236: * the VM.
237: *
238: * @param jar specifies the JAR file to be verified.
239: * @param chunkSize amount of bytecode to be verified with a single
240: * native call, however not less than one class will be
241: * verified with a single call.
242: * @return status of the JAR classes verification, it can be one of the
243: * following values STATUS_VERIFY_NOTHING, STATUS_VERIFY_SUCCEEDED or
244: * STATUS_VERIFY_FAILED
245: */
246: public static int verifyJar(String jar, int chunkSize) {
247:
248: int nextChunkID = 0;
249: int status = STATUS_VERIFY_NOTHING;
250: try {
251: do {
252: nextChunkID = verifyNextChunk(jar, nextChunkID,
253: chunkSize);
254: Thread.yield();
255: } while (nextChunkID > 0);
256: // OK, just all files verified
257: if (nextChunkID == 0) {
258: status = STATUS_VERIFY_SUCCEEDED;
259: }
260: } catch (Throwable t) {
261: //do we need it?
262: t.printStackTrace();
263: status = STATUS_VERIFY_FAILED;
264: }
265:
266: return status;
267: }
268:
269: private static native int verifyNextChunk(String jar,
270: int nextChunkID, int chunkSize);
271:
272: }
|