001: /*
002: * @(#)XletRunner.java 1.26 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 com.sun.xlet.XletRunner -name <XletName> {-path <XletPath> |
033: * -codebase <URL_path>} [-args <arg1> [<arg2>] [<arg3>] ...]
034: *
035: * cvm com.sun.xlet.XletRunner -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 com.sun.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: public class XletRunner {
055: static String[] flags = { "-name", "-path", "-codebase", "-args",
056: "-filename" };
057:
058: static boolean isKey(String s) {
059: for (int i = 0; i < flags.length; i++)
060: if (s.equals(flags[i]))
061: return true;
062: return false;
063: }
064:
065: public static void main(String[] args) {
066: if (args.length < 2)
067: printErrorAndExit();
068:
069: // Parse the command line options.
070: if (args[0].equals("-filename")) {
071: String filename = args[1];
072: try {
073: BufferedReader reader = new BufferedReader(
074: new FileReader(filename));
075: Vector v = new Vector();
076: String s;
077: while ((s = reader.readLine()) != null) {
078: StringTokenizer tok = new StringTokenizer(s, " ");
079: while (tok.hasMoreTokens()) {
080: v.addElement(tok.nextToken());
081: }
082: }
083: args = new String[v.size()];
084: for (int i = 0; i < v.size(); i++) {
085: args[i] = (String) v.elementAt(i);
086: }
087: } catch (FileNotFoundException fnf) {
088: System.out.println("Could not find file " + filename);
089: System.exit(1);
090: } catch (IOException ioe) {
091: System.out
092: .println("IOException caught while reading file "
093: + filename);
094: System.exit(1);
095: }
096: }
097:
098: String name = null;
099: String[] paths;
100: String[] xletArgs = new String[] {};
101: for (int i = 0; i < args.length;) {
102: try {
103: if (args[i].equals("-name")) {
104: if (i + 1 == args.length) {
105: System.err.println("Name not specified");
106: printErrorAndExit();
107: }
108: name = args[++i];
109: if (++i == args.length
110: || !(args[i].equals("-path") || args[i]
111: .equals("-codebase"))) {
112: System.err.println("Missing path arguments");
113: printErrorAndExit();
114: }
115: Vector v = new Vector();
116: if (args[i].equals("-path")) {
117: if (++i == args.length) {
118: System.err.println("Path not specified");
119: printErrorAndExit();
120: }
121: StringTokenizer tok = new StringTokenizer(
122: args[i], File.pathSeparator);
123: while (tok.hasMoreTokens()) {
124: v.addElement(tok.nextToken());
125: }
126: } else {
127: while ((i + 1) < args.length
128: && !args[i + 1].equals("-args")) {
129: v.addElement(args[++i]);
130: }
131: }
132: paths = (String[]) v.toArray(new String[v.size()]);
133: if ((i + 1) < args.length
134: && args[++i].equals("-args")) {
135: v = new Vector();
136: while ((i + 1) < args.length
137: && !isKey(args[++i])) {
138: v.addElement(args[i]);
139: }
140: xletArgs = (String[]) v.toArray(new String[v
141: .size()]);
142: }
143:
144: // Parsing is finished. Now start the xlet by calling methods on
145: // the Xlet Manager.
146:
147: System.out.println("@@XletRunner starting Xlet "
148: + name);
149: try {
150: // Get an instance of XletLifecycle from the Xlet Manager,
151: // and post a request on the handler.
152: XletLifecycleHandler handler = XletManager
153: .createXlet(name, paths, xletArgs);
154: // Call a method so the xlet is initialized.
155: // Xlet.initXlet(XletContext) is invoked on the Xlet Manager.
156: handler.postInitXlet();
157: // Call a method so the xlet is started.
158: // Xlet.startXlet() is invoked on the Xlet Manager.
159: handler.postStartXlet();
160: } catch (Exception e) {
161: System.out.println("Error while loading xlet: "
162: + name);
163: e.printStackTrace();
164: System.exit(1);
165: }
166: xletArgs = new String[] {};
167: } else {
168: i++;
169: }
170: } catch (Exception e) {
171: e.printStackTrace();
172: printErrorAndExit();
173: }
174: }
175: if (name == null) {
176: System.err.println("Missing name arguments");
177: printErrorAndExit();
178: }
179: }
180:
181: // If there was a problem parsing the command line, call this method, which ultimately exits.
182: static void printErrorAndExit() {
183: System.err.println("XletRunner Usage: ");
184: System.err.println("cvm com.sun.xlet.XletRunner "
185: + "-name <xletname> -path <xletpath> ");
186: System.err.println("\nOptions");
187: System.err
188: .println("-filename <filename> Reads XletRunner arguments from a file");
189: System.err
190: .println("-args <arguments separated by space> Xlet runtime arguments");
191: System.err
192: .println("-codebase <URLs separated by space> Specifies class location in URL format, replaces \"-path\" option");
193: System.err
194: .println("\nRepeat arguments to run more than one xlets");
195: System.exit(1);
196: }
197: }
|