001: /*
002: *
003: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: /*
027: * @(#)CLDCmain.java 1.7 06/10/10
028: */
029: package com.sun.javax.microedition;
030:
031: import java.lang.reflect.Method;
032: import sun.misc.MIDletClassLoader;
033: import sun.misc.MIDPImplementationClassLoader;
034: import sun.misc.MIDPConfig;
035: import sun.misc.MemberFilter;
036: import javax.microedition.midlet.*;
037: import java.io.*;
038:
039: public class CLDCmain {
040:
041: /* Important parameters */
042: final static String MIDPimplProperty = "com.sun.javax.microedition.implementation";
043:
044: /*
045: * Some places we want a file name,
046: * some places we want a URL.
047: */
048: static String[] filenamesToURL(String[] filenames) {
049: String URLs[] = new String[filenames.length];
050: for (int i = 0; i < filenames.length; i++) {
051: java.io.File f = new File(filenames[i]);
052: String longname;
053: try {
054: longname = f.getCanonicalPath();
055: } catch (IOException e) {
056: throw new Error("IOException");
057: }
058: URLs[i] = longname;
059: }
060: return URLs;
061: }
062:
063: /* Debug methods */
064: static void printStrings(String strings[]) {
065: for (int i = 0; i < strings.length; i++) {
066: System.out.print(strings[i]);
067: System.out.print(' ');
068: }
069: System.out.println();
070: }
071:
072: static String[] split(String path) {
073: int nComponents = 1;
074: char separator = System.getProperty("path.separator", ":")
075: .charAt(0);
076: String components[];
077: int length = path.length();
078: int start;
079: int componentIndex;
080: for (int i = 0; i < length; i++) {
081: if (path.charAt(i) == separator)
082: nComponents += 1;
083: }
084: components = new String[nComponents];
085: start = 0;
086: componentIndex = 0;
087: /* could optimize here for the common case of nComponents == 1 */
088: for (int i = 0; i < length; i++) {
089: if (path.charAt(i) == separator) {
090: components[componentIndex] = path.substring(start, i);
091: componentIndex += 1;
092: start = i + 1;
093: }
094: }
095: /* and the last components is delimited by end of String */
096: components[componentIndex] = path.substring(start, length);
097:
098: return components;
099:
100: }
101:
102: static boolean hadError = false;
103:
104: static void reportError(String msg) {
105: System.err.println("Error: " + msg);
106: hadError = true;
107: }
108:
109: /*
110: * MAIN:
111: * Instantiate class loaders.
112: * Use class loaders to load named class.
113: * Use reflection to call its main, passing parameters.
114: * Args:
115: * -impl <path> -- list of JAR files to pass to the implementation class loader
116: * default is value of property "com.sun.javax.microedition.implementation"
117: * -classpath <path> -- list of JAR files to pass to the MIDlet class loader
118: * <name> -- name of the main class to run. Needs not be a MIDlet.
119: * all args following this are passed to the program.
120: */
121:
122: public static void main(String args[]) {
123: String midImplString = null;
124: String suiteString = null;
125: String midImplPath[];
126: String suitePath[] = null;
127: String testClassName = null;
128: int argCount = 0;
129:
130: /* iterate over arguments */
131: for (int i = 0; i < args.length; i++) {
132: String this Arg = args[i];
133: if (this Arg.equals("-impl")) {
134: if (midImplString != null)
135: reportError("Implementation path set twice");
136: midImplString = args[++i];
137: } else if (this Arg.equals("-classpath")) {
138: if (suiteString != null)
139: reportError("Application/test path set twice");
140: suiteString = args[++i];
141: } else {
142: testClassName = args[i];
143: argCount = i + 1;
144: break;
145: }
146: }
147: if (midImplString == null) {
148: midImplString = System.getProperty(MIDPimplProperty);
149: }
150: if (midImplString == null)
151: reportError("Implementation path not set");
152: if (suiteString == null)
153: reportError("Application/test path not set");
154: if (testClassName == null)
155: reportError("Application/test class name not set");
156: if (hadError) {
157: System.exit(1);
158: }
159: midImplPath = filenamesToURL(split(midImplString));
160: suitePath = filenamesToURL(split(suiteString));
161:
162: MIDPImplementationClassLoader midpImpl = MIDPConfig
163: .newMIDPImplementationClassLoader(midImplPath);
164: MIDletClassLoader midpSuiteLoader;
165: Class testClass;
166:
167: midpSuiteLoader = MIDPConfig.newMIDletClassLoader(suitePath);
168: if (midpSuiteLoader == null) {
169: System.err
170: .println("Could not instantiate MIDletClassLoader");
171: return;
172: }
173:
174: try {
175: testClass = midpSuiteLoader.loadClass(testClassName, true);
176: } catch (Exception e) {
177: System.err.println("Instantiating test class "
178: + testClassName);
179: e.printStackTrace();
180: return;
181: }
182: Method mainMethod;
183: Class mainSignature[] = { args.getClass() };
184: try {
185: mainMethod = testClass.getMethod("main", mainSignature);
186: } catch (Exception e) {
187: System.err.println("Finding method main of test class "
188: + testClassName);
189: e.printStackTrace();
190: return;
191: }
192: // make new arg array of all the remaining args
193: String outArgs[] = new String[args.length - argCount];
194: for (int i = argCount; i < args.length; i++) {
195: outArgs[i - argCount] = args[i];
196: }
197: // DEBUG
198: // System.out.println("CLDCmainDEBUG");
199: // System.out.println("Impl is at "); printStrings(midImplPath);
200: // System.out.println("Suite is at "); printStrings(suitePath);
201: // System.out.println("Test class name is "+ testClassName);
202: // System.out.println("Test class args are "); printStrings(outArgs);
203: // System.out.println("END CLDCmainDEBUG");
204:
205: try {
206: mainMethod.invoke(null, new Object[] { outArgs });
207: } catch (Exception e) {
208: System.err.println("Invoking method main of test class "
209: + testClassName);
210: e.printStackTrace();
211: return;
212: }
213:
214: }
215: }
|