Index: modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/AppletFrame.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/AppletFrame.java (revision 618557) +++ modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/AppletFrame.java (working copy) @@ -23,8 +23,6 @@ import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; -import java.net.URLClassLoader; -import java.net.URL; import java.util.HashSet; import javax.swing.AbstractAction; @@ -38,32 +36,18 @@ import javax.swing.SwingConstants; class AppletFrame extends JFrame { - private final AppletInfo appletInfo; private final Applet applet; private final JLabel statusLabel; private static ShutdownHandler shutdownHandler = new ShutdownHandler(); public AppletFrame(AppletInfo appletInfo) throws Exception { - this.appletInfo = appletInfo; - String code = this.appletInfo.getCode(); - if(code == null || code.equals("")){ - System.err.println("Warning: <" + appletInfo.getTag() +"> tag requires code attribute."); - System.exit(0); - } - - shutdownHandler.addFrame(this); - // Load applet class - if(appletInfo.getCodeBase() == null){ - appletInfo.setCodeBase(new URL(appletInfo.getDocumentBase(), "./")); - } + applet = ViewerAppletContext.loadApplet(appletInfo); - URLClassLoader cl = new URLClassLoader(appletInfo.getClassLoaderURLs()); - Class clz = cl.loadClass(code); - applet = (Applet)clz.newInstance(); - applet.setStub(new ViewerAppletStub(applet, appletInfo)); applet.setPreferredSize(new Dimension(appletInfo.getWidth(), appletInfo.getHeight())); + + shutdownHandler.addFrame(this); // Create menu bar setJMenuBar(createMenu()); @@ -109,6 +93,10 @@ return menuBar; } + + Applet getApplet(){ + return applet; + } private class StartAction extends AbstractAction { public StartAction() { @@ -163,7 +151,16 @@ } public void windowClosing(WindowEvent e) { - frameList.remove(e.getWindow()); + JFrame frame = (JFrame)e.getWindow(); + frameList.remove(frame); + + Applet applet = ((AppletFrame)frame).getApplet(); + if(applet != null){ + ViewerAppletContext ac = + (ViewerAppletContext)applet.getAppletContext(); + ac.remove(applet); + } + if (frameList.isEmpty()) System.exit(0); } @@ -184,5 +181,7 @@ frameList.add(frame); frame.addWindowListener(this); } + } + } Index: modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/AppletInfo.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/AppletInfo.java (revision 618557) +++ modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/AppletInfo.java (working copy) @@ -31,7 +31,6 @@ private URL codeBase; private URL archive; private String archiveStr; - private String code; private String tagName; private int width; private int height; @@ -76,10 +75,6 @@ this.archive = archive; } - public void setArchive(String archive) { - this.archiveStr = archive; - } - public String getParameter(String name) { return params.get(name.toUpperCase()); } @@ -88,14 +83,6 @@ params.put(name.toUpperCase(), value); } - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = (code.endsWith(".class"))?code.substring(0, code.length()-6):code; - } - public int getWidth() { return width; } @@ -130,6 +117,7 @@ } public URL []getClassLoaderURLs() { + archiveStr = getParameter("ARCHIVE"); URL []res = (archive == null && archiveStr == null)?new URL[1]:new URL[2]; switch (res.length) { case 2: res[1] = getArchive(); Index: modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/HTMLParser.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/HTMLParser.java (revision 618557) +++ modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/HTMLParser.java (working copy) @@ -94,22 +94,13 @@ appletInfo.setDocumentBase(documentBase); SimpleAttributeSet attributes = getAttributes(); - appletInfo.setWidth((String)attributes.getAttribute(HTML.Attribute.WIDTH)); - appletInfo.setHeight((String)attributes.getAttribute(HTML.Attribute.HEIGHT)); - appletInfo.setArchive((String)attributes.getAttribute(HTML.Attribute.ARCHIVE)); + appletInfo.setParameter("WIDTH", (String)attributes.getAttribute(HTML.Attribute.WIDTH)); + appletInfo.setParameter("HEIGHT", (String)attributes.getAttribute(HTML.Attribute.HEIGHT)); + appletInfo.setParameter("CODE", (String)attributes.getAttribute(HTML.Attribute.CODE)); + appletInfo.setParameter("CODEBASE", (String)attributes.getAttribute(HTML.Attribute.CODEBASE)); + appletInfo.setParameter("ARCHIVE", (String)attributes.getAttribute(HTML.Attribute.ARCHIVE)); - try { - appletInfo.setCode((String)attributes.getAttribute(HTML.Attribute.CODE)); - } catch (Exception e) {} - - if (htmlTag == HTML.Tag.APPLET) { - try { - appletInfo.setCodeBase((String)attributes.getAttribute(HTML.Attribute.CODEBASE)); - } catch (Exception e) { - appletInfo.setCodeBase((URL)null); - } - } } } @@ -125,13 +116,10 @@ HTML.Tag htmlTag = tag.getHTMLTag(); if (appletInfo != null && htmlTag == HTML.Tag.PARAM) { SimpleAttributeSet attributes = getAttributes(); - String name = (String)attributes.getAttribute(HTML.Attribute.NAME); - if(name.equalsIgnoreCase("code")){ - appletInfo.setCode((String)attributes.getAttribute(HTML.Attribute.VALUE)); - } else { - appletInfo.setParameter(name, (String)attributes.getAttribute(HTML.Attribute.VALUE)); - } + appletInfo.setParameter((String)attributes.getAttribute(HTML.Attribute.NAME), + (String)attributes.getAttribute(HTML.Attribute.VALUE)); } + } } } Index: modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java (revision 618557) +++ modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java (working copy) @@ -37,9 +37,7 @@ try { ais = AudioSystem.getAudioInputStream(url); } catch (Exception e) { - System.out.println(url.toString()); e.printStackTrace(); - ais = null; return; } af = ais.getFormat(); Index: modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAppletContext.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAppletContext.java (revision 618557) +++ modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAppletContext.java (working copy) @@ -25,24 +25,80 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLClassLoader; import java.util.Enumeration; +import java.util.HashMap; import java.util.Iterator; +import java.util.Vector; class ViewerAppletContext implements AppletContext { + private static final HashMap namedApplets = new HashMap(); + private static final Vector applets = new Vector(1, 1); + private static final HashMap loaders = + new HashMap(); + private final AppletInfo appletInfo; + static Applet loadApplet(AppletInfo appletInfo) throws Exception { + + String width = appletInfo.getParameter("WIDTH"); + if(width != null) { + appletInfo.setWidth(width); + } else { + System.err.println("Warning: <" + appletInfo.getTag() +"> tag requires width attribute."); + System.exit(-1); + } + + String heigth = appletInfo.getParameter("HEIGHT"); + if(heigth != null) { + appletInfo.setHeight(heigth); + } else { + System.err.println("Warning: <" + appletInfo.getTag() +"> tag requires height attribute."); + System.exit(-1); + } + + String code = appletInfo.getParameter("CODE"); + if(code == null || code.equals("")){ + System.err.println("Warning: <" + appletInfo.getTag() +"> tag requires code attribute."); + System.exit(0); + } + + code = (code.endsWith(".class"))?code.substring(0, code.length()-6):code; + + appletInfo.setCodeBase(appletInfo.getParameter("CODEBASE")); + + URL codeBase = appletInfo.getCodeBase(); + + ViewerClassLoader loader = loaders.get(codeBase); + if(loader == null){ + loader = new ViewerClassLoader(); + loaders.put(codeBase, loader); + } + + loader.addURLs(appletInfo.getClassLoaderURLs()); + Class clz = loader.loadClass(code); + Applet applet = (Applet)clz.newInstance(); + + applets.add(applet); + + String name = appletInfo.getParameter("NAME"); + if(name != null && name != "") namedApplets.put(name, applet); + + applet.setStub(new ViewerAppletStub(applet, appletInfo)); + + return applet; + } + public ViewerAppletContext(AppletInfo appletInfo) { this.appletInfo = appletInfo; } public Applet getApplet(String name) { - // TODO Auto-generated method stub - return null; + return namedApplets.get(name); } public Enumeration getApplets() { - // TODO Auto-generated method stub - return null; + return applets.elements(); } public AudioClip getAudioClip(URL url) { @@ -81,4 +137,42 @@ public void showStatus(String status) { appletInfo.setStatus(status); } + + void remove(Applet applet){ + String name = applet.getParameter("NAME"); + synchronized(namedApplets){ + namedApplets.remove(name); + } + + synchronized(applets){ + applets.remove(applet); + } + } + + private static class ViewerClassLoader extends URLClassLoader{ + + public ViewerClassLoader(){ + super(new URL[0]); + } + + public void addURL(URL url){ + URL[] urls = getURLs(); + boolean exists = false; + for(int i = 0; i < urls.length; i++){ + if(urls[i].equals(url)){ + exists = true; + break; + } + } + if(!exists) super.addURL(url); + } + + public void addURLs(URL[] urls){ + if(urls == null) return; + for(int i = 0; i < urls.length; i++){ + addURL(urls[i]); + } + } + + } }