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.jump;
026:
027: import java.lang.reflect.Field;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030: import com.sun.midp.midlet.MIDletStateHandler;
031: import com.sun.midp.midlet.MIDletSuite;
032: import com.sun.midp.jump.installer.TrustedMIDletSuiteInfo;
033:
034: /**
035: * Initialize the JUMP executive environment for using midp code.
036: */
037: public class JumpInit {
038:
039: public static void init() {
040:
041: // First, load the midp natives.
042: try {
043: String n = System.getProperty("sun.midp.library.name",
044: "midp");
045: // DEBUG: System.err.println("Loading DLL \"" + n + "\" ...");
046: System.loadLibrary(n);
047: } catch (UnsatisfiedLinkError err) {
048: }
049:
050: /**
051: * Path to MIDP working directory.
052: * Default is the property "sun.midp.home.path", the fallback is user.dir.
053: */
054: String userdir = System.getProperty("user.dir", ".");
055: String home = System.getProperty("sun.midp.home.path", userdir);
056: String profile = System.getProperty("microedition.profiles");
057: if (profile == null)
058: System.setProperty("microedition.profiles", "MIDP-2.1");
059:
060: if (!initMidpNativeStates(home)) {
061: throw new RuntimeException(
062: "MIDP native initialization failed");
063: }
064:
065: /**
066: * Making the runtime believe that this is a trusted midlet suite.
067: * This is needed because in midp's code, security check is often done
068: * by checking the executing midlet suite's security token.
069: * See com.sun.midp.jump.installer.TrustedMIDletSuiteInfo for more
070: * information.
071: **/
072:
073: final MIDletStateHandler handler = MIDletStateHandler
074: .getMidletStateHandler();
075: MIDletSuite suite = handler.getMIDletSuite();
076: if (suite == null) {
077: AccessController.doPrivileged(new PrivilegedAction() {
078: public Object run() {
079:
080: try {
081: Class clazz = MIDletStateHandler.class;
082: Field midletSuiteField = clazz
083: .getDeclaredField("midletSuite");
084:
085: midletSuiteField.setAccessible(true);
086:
087: midletSuiteField.set(handler,
088: new TrustedMIDletSuiteInfo());
089:
090: } catch (Exception e) {
091: e.printStackTrace();
092: }
093: return null;
094: }
095: });
096: }
097: }
098:
099: /**
100: * Performs native subsystem initialization.
101: * @param home path to the MIDP working directory.
102: */
103: static native boolean initMidpNativeStates(String home);
104: }
|