1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
package com.eteks.sweethome3d.applet;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.awt.*;
import javax.swing.*;
import javax.swing.JApplet;
import javax.swing.JLabel;
import com.eteks.sweethome3d.tools.ExtensionsClassLoader;
public class SweetHome3DApplet extends JApplet {
private static final long serialVersionUID = 1L;
private Object appletApplication;
@Override
public void start() {
// TODO Auto-generated method stub
super.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
@Override
public void init() {
System.out.println("simo init");
if (!isJava5OrSuperior()) {
showError("<html><p>This applet may be run under Windows, Mac OS X 10.4 / 10.5, Linux and Solaris." +
"<br>It requires Java version 5 or superior.</p>" +
"<p>Please, check Java version set in Java preferences under Mac OS X," +
"<br>or update your Java Runtime to the latest version available at java.com under the other systems.</p>");
} else {
createAppletApplication();
}
}
public void destroy() {
if (this.appletApplication != null) {
try {
Method destroyMethod = this.appletApplication.getClass().getMethod("destroy", new Class [0]);
destroyMethod.invoke(this.appletApplication, new Object [0]);
} catch (Exception ex) {
// Can't do better than print stack trace when applet is destroyed
ex.printStackTrace();
}
}
this.appletApplication = null;
// Collect deleted objects (seems to be required under Mac OS X when the applet is being reloaded)
System.gc();
}
/**
* Returns <code>true</code> if one of the homes edited by this applet is modified.
*/
public boolean isModified() {
if (this.appletApplication != null) {
try {
Method destroyMethod = this.appletApplication.getClass().getMethod("isModified", new Class [0]);
return ((Boolean)destroyMethod.invoke(this.appletApplication, new Object [0])).booleanValue();
} catch (Exception ex) {
// Can't do better than print stack trace
ex.printStackTrace();
}
}
return false;
}
/**
* Returns <code>true</code> if current JVM version is 5+.
*/
private boolean isJava5OrSuperior() {
String javaVersion = System.getProperty("java.version");
String [] javaVersionParts = javaVersion.split("\\.|_");
if (javaVersionParts.length >= 1) {
try {
// Return true for Java SE 5 and superior
if (Integer.parseInt(javaVersionParts [1]) >= 5) {
return true;
}
} catch (NumberFormatException ex) {
}
}
return false;
}
/**
* Shows the given text in a label.
*/
private void showError(String text) {
JLabel label = new JLabel(text, JLabel.CENTER);
setContentPane(label);
}
/**
* Creates a new <code>AppletApplication</code> instance that manages this applet content.
*/
private void createAppletApplication() {
System.out.println("simo createAppletApplication");
try {
Class sweetHome3DAppletClass = SweetHome3DApplet.class;
List java3DFiles = new ArrayList(Arrays.asList(new String [] {
"j3dcore.jar", // Main Java 3D jars
"vecmath.jar",
"j3dutils.jar",
"windows/j3dcore-d3d.dll", // Windows DLLs
"windows/j3dcore-ogl.dll",
"windows/j3dcore-ogl-cg.dll",
"windows/j3dcore-ogl-chk.dll",
"macosx/gluegen-rt.jar", // Mac OS X jars and DLLs
"macosx/jogl.jar",
"macosx/libgluegen-rt.jnilib",
"macosx/libjogl.jnilib",
"macosx/libjogl_awt.jnilib",
"macosx/libjogl_cg.jnilib"}));
if (System.getProperty("os.name").startsWith("Linux")
&& "64".equals(System.getProperty("sun.arch.data.model"))) {
java3DFiles.add("linux/x64/libj3dcore-ogl.so"); // Linux DLLs
} else {
java3DFiles.add("linux/i386/libj3dcore-ogl.so");
java3DFiles.add("linux/i386/libj3dcore-ogl-cg.so");
}
List applicationPackages = new ArrayList(Arrays.asList(new String [] {
"com.eteks.sweethome3d",
"javax.media",
"javax.vecmath",
"com.sun.j3d",
"com.sun.opengl",
"com.sun.gluegen.runtime",
"javax.media.opengl",
"com.sun.media",
"com.ibm.media",
"jmpapps.util",
"com.microcrowd.loader.java3d",
"org.sunflow"}));
applicationPackages.addAll(getPluginsPackages());
String applicationClassName = getApplicationClassName();
if (!applicationClassName.startsWith((String)applicationPackages.get(0))) {
String [] applicationClassParts = applicationClassName.split("\\.");
String applicationClassPackageBase = "";
// Contains the two first part of class package at most
for (int i = 0, n = Math.min(applicationClassParts.length - 1, 2); i < n; i++) {
if (i > 0) {
applicationClassPackageBase += ".";
}
applicationClassPackageBase += applicationClassParts [i];
}
applicationPackages.add(applicationClassPackageBase);
}
ClassLoader extensionsClassLoader = new ExtensionsClassLoader(
sweetHome3DAppletClass.getClassLoader(), sweetHome3DAppletClass.getProtectionDomain(),
(String [])java3DFiles.toArray(new String [java3DFiles.size()]),
(String [])applicationPackages.toArray(new String [applicationPackages.size()]));
// Call application constructor with reflection
Class applicationClass = extensionsClassLoader.loadClass(applicationClassName);
Constructor applicationConstructor =
applicationClass.getConstructor(new Class [] {JApplet.class});
this.appletApplication = applicationConstructor.newInstance(new Object [] {this});
} catch (Throwable ex) {
if (ex instanceof InvocationTargetException) {
ex = ((InvocationTargetException)ex).getCause();
}
if (ex instanceof AccessControlException) {
showError("<html>If you want to run this applet, you must relaunch your browser,"
+ "<br>display this page again and accept the displayed digital signature.");
} else {
showError("<html>Can't start applet:<br>Exception"
+ ex.getClass().getName() + " " + ex.getMessage());
ex.printStackTrace();
}
}
}
/**
* Returns the name of the {@linkplain AppletApplication application} class associated to this applet.
* This class must have a constructor taking in parameter a <code>JApplet</code>.
*/
protected String getApplicationClassName() {
return "com.eteks.sweethome3d.applet.AppletApplication";
}
/**
* Returns the collection of packages that are found in plugins.
*/
private Collection getPluginsPackages() {
System.out.println("simo get pluginsPackages");
String pluginURLs = getParameter("pluginURLs");
if (pluginURLs != null) {
Set pluginPackages = new HashSet();
// Add to pluginPackages all the packages contained in the plugin URLs
String [] urlStrings = pluginURLs.split("\\s|,");
for (int i = 0; i < urlStrings.length; i++) {
try {
URL pluginUrl = new URL(getCodeBase(), urlStrings [i]);
ZipInputStream zipIn = null;
try {
// Open a zip input from pluginUrl
zipIn = new ZipInputStream(pluginUrl.openStream());
// Try directories in current zip stream
for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
String zipEntryName = entry.getName();
int lastIndex = zipEntryName.lastIndexOf('/');
if (zipEntryName.endsWith(".class")) {
if (lastIndex == -1) {
pluginPackages.add(""); // Add empty package
} else {
pluginPackages.add(zipEntryName.substring(0, lastIndex).replace('/', '.'));
}
}
}
} catch (IOException ex) {
// Ignore furniture plugin
} finally {
if (zipIn != null) {
try {
zipIn.close();
} catch (IOException ex) {
}
}
}
} catch (MalformedURLException ex) {
// Ignore malformed URLs
}
}
return pluginPackages;
}
return Collections.EMPTY_SET;
}
} |
Partager