001: /*
002: * Copyright 1990-2006 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.jumpimpl.module.installer;
026:
027: import com.sun.jump.common.JUMPAppModel;
028: import com.sun.jump.module.installer.JUMPInstallerModuleFactory;
029: import com.sun.jump.module.installer.JUMPInstallerModule;
030: import java.util.Map;
031: import java.util.MissingResourceException;
032: import java.util.ResourceBundle;
033:
034: import java.io.File;
035: import java.lang.reflect.Method;
036: import java.util.Vector;
037: import java.util.HashMap;
038:
039: import sun.misc.MIDPConfig;
040:
041: /**
042: * Factory implementation methods for the installer module
043: */
044: public class InstallerFactoryImpl extends JUMPInstallerModuleFactory {
045:
046: private JUMPInstallerModule mainInstaller;
047: private JUMPInstallerModule xletInstaller;
048: private JUMPInstallerModule midletInstaller;
049: private Map configMap = new HashMap();
050:
051: private JUMPInstallerModule getMainInstaller() {
052: synchronized (InstallerFactoryImpl.class) {
053: if (mainInstaller == null) {
054: mainInstaller = new MAINInstallerImpl();
055: mainInstaller.load(configMap);
056: }
057: return mainInstaller;
058: }
059: }
060:
061: private JUMPInstallerModule getXletInstaller() {
062: synchronized (InstallerFactoryImpl.class) {
063: if (xletInstaller == null) {
064: xletInstaller = new XLETInstallerImpl();
065: xletInstaller.load(configMap);
066: }
067: return xletInstaller;
068: }
069: }
070:
071: static boolean midletInstallerInitialized = false;
072:
073: private JUMPInstallerModule getMidletInstaller() {
074: synchronized (InstallerFactoryImpl.class) {
075:
076: if (midletInstaller == null && !midletInstallerInitialized) {
077:
078: /**
079: * The MIDLET installer is part of the MIDP library, which
080: * is only accessible through the MIDPImplementationClassLoader.
081: * Hence the installer needs to be loaded by the midp implementation
082: * classloader via reflection. Otherwise, the full midp-cdc-jump
083: * rommization fails as well as runtime requiring midp classes to
084: * be in the same path as jump classes.
085: **/
086:
087: try {
088:
089: ClassLoader midpImplementationClassLoader = MIDPConfig
090: .getMIDPImplementationClassLoader();
091:
092: if (midpImplementationClassLoader == null) {
093:
094: /**
095: * Noone had a need to load midp classes yet.
096: * Create the classloader using the
097: * default location in which sun.misc.MIDPLauncher
098: * normally uses, unless jump.midp.classes.zip is set.
099: **/
100:
101: String midpFile = (String) configMap
102: .get("jump.midp.classes.zip");
103:
104: midpImplementationClassLoader = MIDPConfig
105: .newMIDPImplementationClassLoader(new String[] { midpFile });
106: }
107:
108: midletInstaller = (JUMPInstallerModule) loadAndInstantiate(
109: midpImplementationClassLoader,
110: "com.sun.midp.jump.installer.MIDLETInstallerImpl");
111: if (midletInstaller != null) {
112: midletInstaller.load(configMap);
113: }
114:
115: } catch (ClassNotFoundException e) {
116: // MIDPConfig or MIDLETInstallerImpl not found.
117: // Not a dual stack platform.
118: e.printStackTrace();
119: } catch (Exception e) {
120: // Other unexpected error? Let's report it.
121: e.printStackTrace();
122: }
123: midletInstallerInitialized = true;
124: }
125:
126: return midletInstaller;
127: }
128: }
129:
130: private static Object loadAndInstantiate(ClassLoader loader,
131: String className) throws ClassNotFoundException {
132:
133: try {
134: Class clazz = Class.forName(className, true, loader);
135: return clazz.newInstance();
136: } catch (InstantiationException e) {
137: e.printStackTrace();
138: } catch (IllegalAccessException e) {
139: e.printStackTrace();
140: }
141:
142: return null;
143: }
144:
145: /**
146: * resource bundle for the installer module
147: */
148: static protected ResourceBundle bundle = null;
149:
150: /**
151: * load this module with the given properties
152: * @param map properties of this module
153: */
154: public void load(Map map) {
155: this .configMap = map;
156: }
157:
158: /**
159: * unload the module
160: */
161: public void unload() {
162: }
163:
164: /**
165: * Returns a <code>JUMPInstallerModule</code> for the app model specified
166: * @param appModel the application model for which an appropriate
167: * installer module should be returned.
168: * @return installer module object
169: */
170: public JUMPInstallerModule getModule(JUMPAppModel appModel) {
171:
172: if (appModel == JUMPAppModel.MAIN) {
173: return getMainInstaller();
174: }
175:
176: if (appModel == JUMPAppModel.XLET) {
177: return getXletInstaller();
178: }
179:
180: if (appModel == JUMPAppModel.MIDLET) {
181: JUMPInstallerModule module = getMidletInstaller();
182: if (module == null)
183: throw new IllegalArgumentException(
184: "Can't find MIDLET installer");
185:
186: return module;
187: }
188:
189: throw new IllegalArgumentException(
190: "Illegal app model for installer.");
191: }
192:
193: /**
194: * Returns a <code>JUMPInstallerModule</code> for the mime type
195: * specified.
196: *
197: * @param mimeType mime type for which an appropriate
198: * installer module should be returned.
199: *
200: * @return This can return null if no installer module is available
201: *
202: */
203: public JUMPInstallerModule getModule(String mimeType) {
204:
205: // Note yet implemented
206: return null;
207: }
208:
209: /**
210: * Get all of the available installer modules
211: * @return a list of all registered installers in the system for all types
212: * of content.
213: */
214: public JUMPInstallerModule[] getAllInstallers() {
215: Vector vector = new Vector();
216: JUMPInstallerModule mainModule = getMainInstaller();
217: if (mainModule != null) {
218: vector.add(mainModule);
219: }
220: JUMPInstallerModule xletModule = getXletInstaller();
221: if (xletModule != null) {
222: vector.add(xletModule);
223: }
224: JUMPInstallerModule midletModule = getMidletInstaller();
225: if (midletModule != null) {
226: vector.add(midletModule);
227: }
228: return (JUMPInstallerModule[]) vector
229: .toArray(new JUMPInstallerModule[] {});
230: };
231:
232: /*
233: * For localization support
234: */
235: static ResourceBundle getResourceBundle() {
236: if (bundle == null) {
237: bundle = ResourceBundle
238: .getBundle("com.sun.jumpimpl.module.installer.resources.installer");
239: }
240:
241: return bundle;
242: }
243:
244: static String getString(String key) {
245: String value = null;
246:
247: try {
248: value = getResourceBundle().getString(key);
249: } catch (MissingResourceException e) {
250: System.out.println("Could not find key for " + key);
251: e.printStackTrace();
252: }
253:
254: return value;
255: }
256:
257: static int getInt(String key) {
258: return Integer.parseInt(getString(key));
259: }
260:
261: }
|