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.presentation.autotester;
026:
027: import com.sun.jump.command.JUMPIsolateLifecycleRequest;
028: import com.sun.jump.common.JUMPApplication;
029: import com.sun.jump.common.JUMPContent;
030: import com.sun.jump.common.JUMPAppModel;
031: import com.sun.jump.executive.JUMPApplicationProxy;
032: import com.sun.jump.executive.JUMPExecutive;
033: import com.sun.jump.executive.JUMPIsolateFactory;
034: import com.sun.jump.executive.JUMPIsolateProxy;
035: import com.sun.jump.message.JUMPMessage;
036: import com.sun.jump.message.JUMPMessageDispatcher;
037: import com.sun.jump.message.JUMPMessageDispatcherTypeException;
038: import com.sun.jump.message.JUMPMessageHandler;
039: import com.sun.jump.module.installer.JUMPInstallerModule;
040: import com.sun.jump.module.installer.JUMPInstallerModuleFactory;
041: import com.sun.jump.module.download.JUMPDownloadDescriptor;
042: import com.sun.jump.module.download.JUMPDownloader;
043: import com.sun.jump.module.download.JUMPDownloadModule;
044: import com.sun.jump.module.download.JUMPDownloadModuleFactory;
045: import com.sun.jump.module.presentation.JUMPPresentationModule;
046:
047: import com.sun.jumpimpl.module.download.DownloadDestinationImpl;
048:
049: import java.net.URL;
050: import java.util.Map;
051:
052: /**
053: * A simple JUMP launcher for auto testing.
054: */
055: public class AutoTester implements JUMPPresentationModule,
056: JUMPMessageHandler {
057:
058: String nextDescriptor = null;
059: static final String DESCRIPTOR_KEY = "jump.descriptor.url";
060:
061: int currentIsolateId;
062:
063: public void load(Map map) {
064: nextDescriptor = (String) System.getProperty(DESCRIPTOR_KEY);
065: if (nextDescriptor == null)
066: nextDescriptor = (String) map.get(DESCRIPTOR_KEY);
067: }
068:
069: public void stop() {
070: }
071:
072: public void unload() {
073: }
074:
075: public void start() {
076:
077: System.err.println("*** Starting AutoTester ***");
078:
079: if (nextDescriptor == null) {
080: System.err.println(DESCRIPTOR_KEY
081: + " value not found, exiting");
082: return;
083: }
084:
085: installAndPerformTests(nextDescriptor);
086:
087: // shut down the server
088: new com.sun.jumpimpl.os.JUMPOSInterfaceImpl().shutdownServer();
089: System.exit(0);
090: }
091:
092: /**
093: * Installs/Updates a test suite, runs the first MIDlets in the suite,
094: * uninstall the suite, and install the next suite, until there is no more midlet
095: * suite and HTTP 404 not found error is thrown during the download time.
096: */
097: public void installAndPerformTests(String descriptorUrl) {
098:
099: JUMPExecutive executive = JUMPExecutive.getInstance();
100:
101: JUMPDownloadModule downloadModule;
102: JUMPInstallerModule installerModule;
103:
104: JUMPIsolateFactory lcm = executive.getIsolateFactory();
105:
106: // Get the download and install modules based on the URL
107: if (descriptorUrl.trim().toLowerCase().endsWith(".jad")) {
108: downloadModule = JUMPDownloadModuleFactory
109: .getInstance()
110: .getModule(
111: JUMPDownloadModuleFactory.PROTOCOL_MIDP_OTA);
112: installerModule = JUMPInstallerModuleFactory.getInstance()
113: .getModule(JUMPAppModel.MIDLET);
114: } else {
115: downloadModule = JUMPDownloadModuleFactory.getInstance()
116: .getModule(
117: JUMPDownloadModuleFactory.PROTOCOL_OMA_OTA);
118: installerModule = JUMPInstallerModuleFactory.getInstance()
119: .getModule(JUMPAppModel.XLET);
120: }
121:
122: try {
123:
124: // Start the message dispatcher first, for the lifecycle events.
125: JUMPMessageDispatcher md = executive.getMessageDispatcher();
126: md.registerHandler(
127: JUMPIsolateLifecycleRequest.MESSAGE_TYPE, this );
128:
129: while (true) { // Exits when the exception happens.
130:
131: JUMPDownloadDescriptor descriptor = downloadModule
132: .createDescriptor(descriptorUrl);
133: JUMPDownloader downloader = downloadModule
134: .createDownloader(descriptor);
135:
136: // downloader.start is synchronous right now
137: URL contentUrl = downloader
138: .start(new DownloadDestinationImpl(descriptor));
139: JUMPContent[] contents = installerModule.install(
140: contentUrl, descriptor);
141:
142: if (contents == null || contents.length == 0) {
143: // Problem with the installation, break.
144: break;
145: }
146:
147: JUMPApplication app = (JUMPApplication) contents[0];
148:
149: synchronized (this ) {
150:
151: // Launch the isolate and record the isolate id
152: JUMPIsolateProxy ip = lcm.newIsolate(app
153: .getAppType());
154: setLaunchedIsolateId(ip.getIsolateId());
155:
156: // Launch the app inside the newly created isolate
157: System.err.println("*** Isolate "
158: + ip.getIsolateId() + " trying to launch: "
159: + app.getTitle() + "...");
160: JUMPApplicationProxy proxy = ip.startApp(app, null);
161:
162: if (proxy == null) {
163: // failure, uninstall the bundle and exit.
164: System.err.println("Launching failed " + app);
165: installerModule.uninstall(app);
166: return;
167: }
168:
169: trace("*** Launch of " + app.getTitle()
170: + " successed");
171:
172: // Launch succeeded.
173: // Wait for the handleMessage() to deliver the death notification.
174: wait();
175: }
176: // All contents from the bundle is executed, uninstall.
177: installerModule.uninstall(contents[0]);
178: }
179:
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: }
184:
185: /**
186: * A message handling routine.
187: * Waits for the isolate destroyed notification, and if the message
188: * is about the isolate we're waiting for, notify the blocking
189: * thread in start() method.
190: */
191: public void handleMessage(JUMPMessage message) {
192: if (JUMPIsolateLifecycleRequest.MESSAGE_TYPE.equals(message
193: .getType())) {
194: trace("==== MESSAGE RECEIVED: JUMPIsolateLifecycleRequest");
195: JUMPIsolateLifecycleRequest cmd = (JUMPIsolateLifecycleRequest) JUMPIsolateLifecycleRequest
196: .fromMessage(message);
197: if (JUMPIsolateLifecycleRequest.ID_ISOLATE_DESTROYED
198: .equals(cmd.getCommandId())) {
199:
200: synchronized (this ) {
201: if (getLaunchedIsolateId() == cmd.getIsolateId())
202: notify();
203: }
204: }
205: }
206: }
207:
208: public void setLaunchedIsolateId(int isolateId) {
209: currentIsolateId = isolateId;
210: }
211:
212: public int getLaunchedIsolateId() {
213: return currentIsolateId;
214: }
215:
216: void trace(String str) {
217: if (false) {
218: System.out.println(str);
219: }
220: }
221: }
|