001: /*
002: * @(#)PXletRunner.java 1.13 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: /*
029: * A sample class to introduce xlet to the system
030: *
031: * Usage:
032: * cvm sun.mtask.xlet.PXletRunner -name <XletName> {-path <XletPath> |
033: * -codebase <URL_path>} [-args <arg1> [<arg2>] [<arg3>] ...]
034: *
035: * cvm sun.mtask.xlet.PXletRunner -filename <filename>
036: *
037: * The xlet should not be found in the classpath and
038: * <XletPath> is relative to the current directory.
039: */
040:
041: package sun.mtask.xlet;
042:
043: // To read the command line from a file.
044: import java.io.BufferedReader;
045: import java.io.FileReader;
046: import java.io.File;
047: import java.io.FileNotFoundException;
048: import java.io.IOException;
049: import java.util.StringTokenizer;
050:
051: // Data structure to save the command line options.
052: import java.util.Vector;
053:
054: import com.sun.xlet.XletLifecycleHandler;
055:
056: public class PXletRunner {
057:
058: private static boolean verbose = (System
059: .getProperty("cdcams.verbose") != null)
060: && (System.getProperty("cdcams.verbose").toLowerCase()
061: .equals("true"));
062:
063: static String[] flags = { "-name", "-path", "-codebase", "-args",
064: "-filename", "-laf", "-lafTheme", "-loadOnly" };
065:
066: static boolean isKey(String s) {
067: for (int i = 0; i < flags.length; i++)
068: if (s.equals(flags[i]))
069: return true;
070: return false;
071: }
072:
073: public static void main(String[] args) {
074: if (args.length < 2)
075: printErrorAndExit();
076:
077: // Parse the command line options.
078: if (args[0].equals("-filename")) {
079: String filename = args[1];
080: try {
081: BufferedReader reader = new BufferedReader(
082: new FileReader(filename));
083: Vector v = new Vector();
084: String s;
085: while ((s = reader.readLine()) != null) {
086: StringTokenizer tok = new StringTokenizer(s, " ");
087: while (tok.hasMoreTokens()) {
088: v.addElement(tok.nextToken());
089: }
090: }
091: args = new String[v.size()];
092: for (int i = 0; i < v.size(); i++) {
093: args[i] = (String) v.elementAt(i);
094: }
095: } catch (FileNotFoundException fnf) {
096: if (verbose) {
097: System.out.println("Could not find file "
098: + filename);
099: }
100: System.exit(1);
101: } catch (IOException ioe) {
102: if (verbose) {
103: System.out
104: .println("IOException caught while reading file "
105: + filename);
106: }
107: System.exit(1);
108: }
109: }
110:
111: String name = null;
112: String[] paths = null;
113: String[] xletArgs = new String[] {};
114: String laf = null; // L&F, if applicable
115: String lafTheme = null; // Theme for L&F, if applicable
116: boolean isLoadOnly = false; // If true, don't init the xlet
117: for (int i = 0; i < args.length;) {
118: try {
119: if (args[i].equals("-laf")) {
120: laf = args[++i];
121: } else if (args[i].equals("-lafTheme")) {
122: lafTheme = args[++i];
123: } else if (args[i].equals("-name")) {
124: name = args[++i];
125: if (!(args[++i].equals("-path") || args[i]
126: .equals("-codebase"))) {
127: printErrorAndExit();
128: }
129: Vector v = new Vector();
130: if (args[i].equals("-path")) {
131: StringTokenizer tok = new StringTokenizer(
132: args[++i], File.pathSeparator);
133: while (tok.hasMoreTokens()) {
134: v.addElement(tok.nextToken());
135: }
136: } else {
137: while ((i + 1) < args.length
138: && !args[i + 1].equals("-args")) {
139: v.addElement(args[++i]);
140: }
141: }
142: paths = (String[]) v.toArray(new String[v.size()]);
143: if ((i + 1) < args.length
144: && args[++i].equals("-args")) {
145: v = new Vector();
146: while ((i + 1) < args.length
147: && !isKey(args[++i])) {
148: v.addElement(args[i]);
149: }
150: xletArgs = (String[]) v.toArray(new String[v
151: .size()]);
152: }
153: } else if (args[i].equals("-loadOnly")) {
154: isLoadOnly = true;
155: i++;
156: } else {
157: i++;
158: }
159: } catch (Exception e) {
160: e.printStackTrace();
161: printErrorAndExit();
162: }
163: }
164: // Parsing is finished. Now start the xlet by calling methods on
165: // the Xlet Manager.
166:
167: try {
168: if (verbose) {
169: System.out.println("@@PXletRunner starting Xlet "
170: + name);
171: }
172:
173: // Get an instance of XletLifecycle from the Xlet Manager,
174: // and post a request on the handler.
175: PXletManager handler = PXletManager.createXlet(name, laf,
176: lafTheme, paths, xletArgs);
177:
178: // First, tell the system to recognize us as the
179: // singleton xlet.
180: //handler.register();
181:
182: if (!isLoadOnly) {
183: // Call a method so the xlet is initialized.
184: // Xlet.initXlet(XletContext) is invoked on the Xlet Manager.
185: handler.postInitXlet();
186: // Call a method so the xlet is started.
187: // Xlet.startXlet() is invoked on the Xlet Manager.
188: handler.postStartXlet();
189: }
190:
191: } catch (Throwable e) {
192: // If we can't get this xlet started, there is no need to linger.
193: // So go away.
194: if (verbose) {
195: System.out.println("Error while loading xlet: " + name);
196: }
197: e.printStackTrace();
198: System.exit(1);
199: }
200:
201: }
202:
203: // If there was a problem parsing the command line, call this method, which ultimately exits.
204: static void printErrorAndExit() {
205: if (verbose) {
206: System.out.println("\n\nPXletRunner Usage: ");
207: System.out.println("cvm sun.mtask.xlet.PXletRunner "
208: + "-name <xletname> -path <xletpath> ");
209: System.out.println("\nOptions");
210: System.out
211: .println("-filename <filename> Reads PXletRunner arguments from a file");
212: System.out
213: .println("-args <arguments separated by space> Xlet runtime arguments");
214: System.out
215: .println("-codebase <URLs separated by space> Specifies class location in URL format, replaces \"-path\" option");
216: System.out
217: .println("\nRepeat arguments to run more than one xlets");
218: }
219: System.exit(1);
220: }
221:
222: }
|