/**
* @(#)Main.java 1.11 02/09/05
*
* Copyright 2002 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*/
package com.sun.midp;
import com.sonyericsson.jsi.event.AsyncHandler;
import com.sonyericsson.ojex.ExtensionManagerImpl;
import com.sonyericsson.ui.RescId;
import com.sonyericsson.midp.Throttling;
import com.sonyericsson.midp.StandbyInUseException;
/**
* The first class loaded in VM by main.c to initialize internal security and
* perform a MIDP command.
*/
public class Main {
private static com.sonyericsson.jsi.System system;
/**
* Initializes internal security, performs then next
* command indicated in the command state, and sets up the next command
* to be performed. The command loop is in main.c.
*
* When done exitInternal is called with a special constant so
* main.c knows that this was the last method run, as opposed to the VM
* aborting.
* @param args not used, instead a {@link CommandState} object is obtained
* using a native method.
*/
public static void main(String args[]) {
CommandState state = new CommandState();
int errMsgId = 0;
int status = CommandProcessor.MAIN_EXIT;
// Fire up the async handler
try {
AsyncHandler.startAsyncHandler();
} catch (SecurityException e) {
}
// Just allocate the instance when we're sure we have the memory
getSystem();
// Initialize OJEX
ExtensionManagerImpl.initializeOJEx();
Throttling.initialize();
try {
restoreCommandState(state);
CommandProcessor.perform(state);
} catch (Throwable t) {
status = CommandProcessor.ERROR;
errMsgId = handleThrowable(t);
}
try {
saveCommandState(state);
} catch (Throwable t) {}
exit(status, errMsgId);
}
private static com.sonyericsson.jsi.System getSystem() {
if (system == null) {
system = com.sonyericsson.jsi.System.getInstance();
}
return system;
}
public static int getMsgIdForThrowable(Throwable t) {
int errMsgId = 0;
try {
if (t instanceof java.lang.ClassNotFoundException) {
errMsgId = RescId.JAVA_CORRUPT_APP_TXT;
} else if (t instanceof java.lang.OutOfMemoryError) {
errMsgId = RescId.JAVA_OUTOFMEMORY_TXT;
} else if (t instanceof java.io.EOFException
|| t instanceof java.io.UnsupportedEncodingException
|| t instanceof java.io.UTFDataFormatException) {
errMsgId = RescId.JAVA_DATA_ERROR_TXT;
} else if (t instanceof java.io.IOException) {
errMsgId = RescId.JAVA_NETW_FAILURE_TXT;
} else if (t instanceof javax.microedition.rms.RecordStoreFullException) {
errMsgId = RescId.JAVA_RMSOUTOFMEMORY_TXT;
} else if (t instanceof com.sonyericsson.midp.StandbyInUseException) {
errMsgId = RescId.JAVA_STANDBY_APPLICATION_RUNNING_TXT;
} else {
errMsgId = RescId.JAVA_DEFAULT_ERROR_TXT;
}
} catch (Throwable thrw) {}
return errMsgId;
}
/** Notify end user of unhandled exception */
static int handleThrowable(Throwable t) {
int errMsgId = 0;
// Print stack trace for debugging
try {
t.printStackTrace();
errMsgId = getMsgIdForThrowable(t);
} catch (Throwable e) {}
return errMsgId;
}
/**
* Save the command state.
*
* @param state current command state
*/
private static native void saveCommandState(CommandState state);
/**
* Restore the command state.
*
* @param state current command state
*/
private static native void restoreCommandState(CommandState state);
/**
* Exit the VM with an error code. Our private version of Runtime.exit.
* <p>
* This is needed because the MIDP version of Runtime.exit cannot tell
* if it is being called from a MIDlet or not, so it always throws an
* exception.
* <p>
*
* @param status Status code to return.
*/
private static void exit(int status, int errMsgId) {
try {
exitInternal(status, errMsgId);
} catch (Throwable t) {}
getSystem().exit(errMsgId == 0 ?
com.sonyericsson.jsi.System.EXIT_NORMAL :
com.sonyericsson.jsi.System.EXIT_ERROR);
}
private static native void exitInternal(int status, int errMsgId);
/** This class is not meant to be instatiated */
private Main() {}
}
|