update dev300-m58
[ooovba.git] / sj2 / stardiv / applet / AppletExecutionContext.java
blobddf81c1a15e9ebbc38a1bf876c99f239c5ab831a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AppletExecutionContext.java,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package stardiv.applet;
33 import java.applet.Applet;
34 import java.applet.AppletStub;
35 import java.applet.AppletContext;
36 import java.applet.AudioClip;
38 import java.awt.BorderLayout;
39 import java.awt.Container;
40 import java.awt.Dimension;
41 import java.awt.Panel;
42 import java.awt.Toolkit;
43 import java.awt.Window;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.ByteArrayOutputStream;
48 import java.io.File;
50 import java.net.URL;
51 import java.net.MalformedURLException;
53 import java.util.Hashtable;
54 import java.util.Vector;
56 import sun.misc.Queue;
58 import com.sun.star.lib.sandbox.ClassContextProxy;
59 import com.sun.star.lib.sandbox.ExecutionContext;
60 import com.sun.star.lib.sandbox.JarEntry;
61 import com.sun.star.lib.sandbox.ResourceProxy;
62 import com.sun.star.lib.sandbox.SandboxSecurity;
63 import com.sun.star.lib.sandbox.CodeSource;
65 import stardiv.controller.SjSettings;
67 public final class AppletExecutionContext extends ExecutionContext
68 implements AppletStub, LiveConnectable
70 private static final boolean DEBUG = false; // Enable / disable debug output
72 private Applet _applet;
73 private Container _container;
75 private DocumentProxy _documentProxy;
76 private Hashtable _parameters;
78 private String _className;
79 private Vector _jarResourceProxys = new Vector();
81 private URL _documentBase = null;
82 private URL _baseURL = null;
84 private Toolkit _toolkit;
86 //************** C++ WRAPPER ******************
87 private long pCppJSbxObject;
89 synchronized public void ClearNativeHandle() {
90 pCppJSbxObject = 0;
91 if(DEBUG)System.err.println("### AppletExecutionContext.ClearNativeHandle");
94 public AppletExecutionContext(long pCppJSbxObject) {
95 this.pCppJSbxObject = pCppJSbxObject;
97 //************** C++ WRAPPER ******************
99 public AppletExecutionContext( URL documentBase,
100 Hashtable parameters,
101 Container container,
102 long pCppJSbxObject)
104 this(pCppJSbxObject);
106 if(DEBUG) System.err.println("#### AppletExecutionContext.<init>:" + documentBase + " " + parameters + " " + container + " " + pCppJSbxObject);
107 _documentBase = documentBase;
108 _parameters = parameters;
109 _container = container;
111 _toolkit = container.getToolkit();
113 _documentProxy = DocumentProxy.getDocumentProxy(documentBase, _toolkit);
114 addObserver(_documentProxy);
117 public void init() {
118 _baseURL = null;
120 try {
121 String codeBase = getParameter("codebase");
123 if (!codeBase.endsWith("/")) {
124 codeBase += "/";
126 _baseURL = new URL(_documentBase, codeBase);
128 catch (MalformedURLException e) {
129 if(DEBUG) System.err.println("#### AppletExecutionContext: Could not create base Url");
132 if(_baseURL == null)
133 _baseURL = _documentBase;
135 if(DEBUG) System.err.println("##### " + getClass().getName() + ".init - baseUrl:" + _baseURL);
137 _className = getParameter("code");
138 String defaultExtension = ".class";
139 String oldExtension = ".java";
141 int extensionIndex = _className.lastIndexOf('.');
142 String extension = "";
144 if (extensionIndex != -1) {
145 extension = _className.substring(extensionIndex);
147 if(!extension.equals(defaultExtension) && !extension.equals(oldExtension)) {
148 extension = defaultExtension;
150 else
151 _className = _className.substring(0, extensionIndex);
154 String nm = "applet-" + _className;
156 _documentProxy.addExecutionContext(this, _className);
158 super.init(nm, ClassContextProxy.create(_baseURL, null, null, false));
160 // Set the property stardiv.security.noExit to true. That value will be used in
161 // SjSettings.changeProperties in the constructor of the SecurityManager SandboxSecurity
162 if (System.getSecurityManager() == null)
163 System.setProperty("stardiv.security.noExit", "true");
164 // SjSettings.changeProperties puts a lot of applet relating properties into the system properties
165 // and it sets the SecurityManager
166 SjSettings.changeProperties( System.getProperties());
168 if(DEBUG) System.err.println("#####" + getClass().getName() + ".init: _className=" + _className + " _baseURL=" + _baseURL);
171 void sDispose(long timeout) {
172 if(DEBUG) System.err.println("#### AppletExecutionContext.sDispose");
174 _container = null;
175 _jarResourceProxys = null;
177 super.dispose(timeout);
180 public void dispose(long timeout) {
181 sDispose(timeout); // call direct
183 /* Deadlock with TKT
184 class DisposeEvent extends java.awt.AWTEvent
185 implements java.awt.peer.ActiveEvent,
186 java.awt.ActiveEvent
188 private AppletExecutionContext executionContext;
189 private long timeout;
191 public DisposeEvent(AppletExecutionContext executionContext, long timeout) {
192 super(executionContext, 0);
194 this.executionContext = executionContext;
195 this.timeout = timeout;
198 public void dispatch() {
199 executionContext.sDispose(timeout);
203 toolkit.getSystemEventQueue().postEvent(new DisposeEvent(this, timeout));
207 protected int getIntParameter(String name) {
208 int value = 0;
209 String string = getParameter(name);
210 if(string != null)
211 value = Integer.valueOf(string).intValue();
213 return value;
216 protected void xload()
217 throws ClassNotFoundException,
218 InstantiationException,
219 IllegalAccessException
221 String archives = getParameter("archive");
223 try {
224 if(archives != null) {
225 int index = archives.indexOf(",");
226 while(index > -1) {
227 try { // try to load archive
228 loadArchive(archives.substring(0, index));
230 catch(MalformedURLException malformedURLException) {
231 System.err.println("#### can't load archive:" + archives.substring(0, index));
233 catch(IOException ioException) {
234 System.err.println("#### can't load archive:" + archives.substring(0, index) + " reason:" + ioException);
237 archives = archives.substring(index + 1).trim();
239 index = archives.indexOf(",");
241 if(archives.length() > 0) loadArchive(archives);
244 Class appletClass = classContext.loadClass(_className);
245 synchronized(_className) {
246 _applet = (Applet)appletClass.newInstance();
247 _applet.setStub(this);
249 appletResize(_container.getSize().width, _container.getSize().height);
251 _className.notifyAll();
254 catch(IOException eio) {
255 throw new ClassNotFoundException(eio.getMessage());
259 protected void xinit() {
260 java.awt.Dimension size = new Dimension(getIntParameter("width"), getIntParameter("height"));
262 _container.setLayout(null);
263 _container.setVisible(true);
264 _container.setSize(size);
265 _container.add(_applet);
267 _applet.setVisible(false);
268 _applet.setSize(size);
270 _container.validate();
272 _applet.init();
275 protected void xstart() {
276 _applet.setVisible(true);
277 _container.validate();
279 _applet.start();
282 protected void xstop() {
283 _applet.stop();
286 protected void xdestroy() {
287 if(DEBUG) System.err.println("##### " + getClass().getName() + ".xdestroy");
289 _applet.destroy();
290 _applet.setVisible(false);
291 _applet.setStub(null);
293 _documentProxy.removeExecutionContext(_applet.getClass().getName());
296 protected void xdispose() {
297 if(DEBUG) System.err.println("##### " + getClass().getName() + ".xdispose");
299 if(_container != null) {
300 _container.remove(_applet);
302 if(_container instanceof Window)
303 ((Window)_container).dispose();
306 _applet = null;
309 private void loadArchive(String archive) throws MalformedURLException, IOException {
310 ResourceProxy jarResourceProxy = ResourceProxy.load(new URL(_baseURL, archive), null /*_protectionDomain*/);
311 jarResourceProxy.loadJar(_baseURL);
312 _jarResourceProxys.addElement(jarResourceProxy);
315 public Applet getApplet() {
316 synchronized(_className) {
317 if(_applet == null) {
318 if(DEBUG)System.err.println("#### AppletExecutionContext.getApplet - waiting for applet");
319 try {
320 _className.wait();
322 catch(InterruptedException interruptedException) {
323 System.err.println("#### AppletExecutionContext.getApplet:" + interruptedException);
325 if(DEBUG)System.err.println("#### AppletExecutionContext.getApplet - got it");
328 return _applet;
332 * Methods for AppletStub interface
334 public void appletResize(int width, int height) {
335 if(DEBUG) System.err.println("##### " + getClass().getName() + ".appletResize: " + width + " " + height);
337 _container.setSize(width, height);
338 if(_applet != null)
339 _applet.setSize(width, height);
342 public AppletContext getAppletContext() {
343 return _documentProxy;
346 public URL getCodeBase() {
347 return classContext.getBase();
350 public URL getDocumentBase() {
351 return _documentProxy.getDocumentBase();
354 public String getParameter(String name) {
355 String string = (String)_parameters.get(name.toLowerCase());
356 if(string != null)
357 string = string.trim();
359 return string;
362 public boolean isActive() {
363 return getStatus() == STARTED && pCppJSbxObject != 0;
366 public void finalize() {
367 if(DEBUG) System.err.println("#### AppletExecutionContext finalized");
370 // sollte eigentlich im DocumentProxy sein, geht aber nicht
371 private native void xshowStatus(String status);
372 private native void xshowDocument(URL url, String aTarget);
374 void printStatus(String status) {
375 if(pCppJSbxObject != 0) xshowStatus(status);
378 void printDocument(URL url, String aTarget) {
379 if(pCppJSbxObject != 0) xshowDocument(url, aTarget);
382 native public Object getJavaScriptJSObjectWindow();