001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.midp.main;
026:
027: import com.sun.midp.midlet.*;
028: import com.sun.midp.lcdui.*;
029: import com.sun.midp.midletsuite.*;
030: import com.sun.midp.configurator.Constants;
031: import com.sun.midp.i18n.ResourceConstants;
032: import com.sun.midp.installer.InternalMIDletSuiteImpl;
033: import com.sun.midp.log.*;
034:
035: /**
036: * The class presents abstract MIDlet suite loader with routines to prepare
037: * runtime environment for CLDC a suite execution.
038: */
039: abstract class CldcMIDletSuiteLoader extends AbstractMIDletSuiteLoader {
040: /** Event producer to send MIDlet state events to the AMS isolate. */
041: protected MIDletControllerEventProducer midletControllerEventProducer;
042:
043: /** Listener for MIDlet related events (state changes, etc). */
044: protected MIDletEventListener midletEventListener;
045:
046: /**
047: * Provides interface for display foreground notification,
048: * functionality that can not be publicly added to a javax package.
049: */
050: protected ForegroundEventListener foregroundEventListener;
051:
052: /** Core initialization of a MIDlet suite loader */
053: protected void init() {
054: isolateId = MIDletSuiteUtils.getIsolateId();
055: amsIsolateId = MIDletSuiteUtils.getAmsIsolateId();
056:
057: // Hint VM of startup beginning: system init phase
058: MIDletSuiteUtils.vmBeginStartUp(isolateId);
059:
060: super .init();
061: }
062:
063: /**
064: * Creates all needed objects of a MIDlet suite environment, but
065: * only initialization that is done, will be to pass other created objects,
066: * and the current and AMS isolate IDs. It is mostly event-related
067: * objects, however subclasses can extend the environment with more
068: * specific parts
069: */
070: protected void createSuiteEnvironment() {
071: midletControllerEventProducer = new MIDletControllerEventProducer(
072: eventQueue, amsIsolateId, isolateId);
073:
074: foregroundController = new CldcForegroundController(
075: midletControllerEventProducer);
076:
077: // creates display container, needs foregroundController
078: super .createSuiteEnvironment();
079:
080: foregroundEventListener = new ForegroundEventListener(
081: eventQueue, displayContainer);
082:
083: midletStateHandler = MIDletStateHandler.getMidletStateHandler();
084:
085: MIDletStateListener midletStateListener = new CldcMIDletStateListener(
086: internalSecurityToken, displayContainer,
087: midletControllerEventProducer);
088:
089: midletStateHandler.initMIDletStateHandler(
090: internalSecurityToken, midletStateListener,
091: new CldcMIDletLoader(internalSecurityToken),
092: new CldcPlatformRequest(internalSecurityToken));
093:
094: midletEventListener = new MIDletEventListener(
095: internalSecurityToken, midletStateHandler, eventQueue);
096: }
097:
098: /**
099: * Does all initialization for already created objects of a MIDlet suite
100: * environment. Subclasses can also extend the initialization with
101: * various global system initializations needed for all suites.
102: */
103: protected void initSuiteEnvironment() {
104: super .initSuiteEnvironment();
105: }
106:
107: /**
108: * Starts MIDlet suite in the prepared environment
109: * Overrides super method to hint VM of system startup
110: * phase is ended
111: *
112: * @throws Exception can be thrown during execution
113: */
114: protected void startSuite() throws Exception {
115: // Hint VM of startup finish: system init phase
116: MIDletSuiteUtils.vmEndStartUp(isolateId);
117: super .startSuite();
118: }
119:
120: /**
121: * Creates MIDlet suite instance by suite ID
122: *
123: * @return MIDlet suite to load
124: *
125: * @throws Exception in the case MIDlet suite can not be
126: * created because of a security reasons or some problems
127: * related to suite storage
128: */
129: protected MIDletSuite createMIDletSuite() throws Exception {
130: MIDletSuiteStorage storage;
131: MIDletSuite suite = null;
132:
133: if (suiteId == MIDletSuite.INTERNAL_SUITE_ID) {
134: // assume a class name of a MIDlet in the classpath
135: suite = InternalMIDletSuiteImpl.create(midletDisplayName,
136: suiteId);
137: } else {
138: storage = MIDletSuiteStorage
139: .getMIDletSuiteStorage(internalSecurityToken);
140:
141: suite = storage.getMIDletSuite(suiteId, false);
142: Logging.initLogSettings(suiteId);
143: }
144:
145: return suite;
146: }
147:
148: protected void closeSuite() {
149: super .closeSuite();
150:
151: // shutdown any preempting
152: if (displayEventHandler != null) {
153: displayEventHandler.donePreempting(null);
154: }
155: }
156:
157: /**
158: * Gets error code by exception type
159: *
160: * @param t exception instance
161: * @return error code
162: */
163: static protected int getErrorCode(Throwable t) {
164: if (t instanceof ClassNotFoundException) {
165: return Constants.MIDLET_CLASS_NOT_FOUND;
166: } else if (t instanceof InstantiationException) {
167: return Constants.MIDLET_INSTANTIATION_EXCEPTION;
168: } else if (t instanceof IllegalAccessException) {
169: return Constants.MIDLET_ILLEGAL_ACCESS_EXCEPTION;
170: } else if (t instanceof OutOfMemoryError) {
171: return Constants.MIDLET_OUT_OF_MEM_ERROR;
172: } else if (t instanceof MIDletSuiteLockedException) {
173: return Constants.MIDLET_INSTALLER_RUNNING;
174: } else {
175: return Constants.MIDLET_CONSTRUCTOR_FAILED;
176: }
177: }
178:
179: /**
180: * Gets AMS error message ID by generic error code
181: *
182: * @param errorCode generic error code
183: * @return AMS error ID
184: */
185: static protected int getErrorMessageId(int errorCode) {
186: switch (errorCode) {
187: case Constants.MIDLET_SUITE_DISABLED:
188: return ResourceConstants.AMS_MIDLETSUITELDR_MIDLETSUITE_DISABLED;
189: case Constants.MIDLET_SUITE_NOT_FOUND:
190: return ResourceConstants.AMS_MIDLETSUITELDR_MIDLETSUITE_NOTFOUND;
191: case Constants.MIDLET_CLASS_NOT_FOUND:
192: return ResourceConstants.AMS_MIDLETSUITELDR_CANT_LAUNCH_MISSING_CLASS;
193: case Constants.MIDLET_INSTANTIATION_EXCEPTION:
194: return ResourceConstants.AMS_MIDLETSUITELDR_CANT_LAUNCH_ILL_OPERATION;
195: case Constants.MIDLET_ILLEGAL_ACCESS_EXCEPTION:
196: return ResourceConstants.AMS_MIDLETSUITELDR_CANT_LAUNCH_ILL_OPERATION;
197: case Constants.MIDLET_OUT_OF_MEM_ERROR:
198: return ResourceConstants.AMS_MIDLETSUITELDR_QUIT_OUT_OF_MEMORY;
199: default:
200: return ResourceConstants.AMS_MIDLETSUITELDR_UNEXPECTEDLY_QUIT;
201: }
202: }
203:
204: /**
205: * Handles exception occurred during MIDlet suite execution.
206: * @param t exception instance
207: */
208: public void handleException(Throwable t) {
209: t.printStackTrace();
210: int errorCode = getErrorCode(t);
211:
212: reportError(errorCode, t.getMessage());
213: }
214: }
|