001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.tools.appletviewer;
019:
020: import java.applet.Applet;
021: import java.applet.AppletContext;
022: import java.applet.AudioClip;
023: import java.awt.Image;
024: import java.awt.Toolkit;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.net.URL;
028: import java.net.URLClassLoader;
029: import java.util.Enumeration;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: class ViewerAppletContext implements AppletContext {
035: private static final HashMap<String, Applet> namedApplets = new HashMap<String, Applet>();
036: private static final Vector<Applet> applets = new Vector<Applet>(1,
037: 1);
038: private static final HashMap<URL, ViewerClassLoader> loaders = new HashMap<URL, ViewerClassLoader>();
039:
040: private final AppletInfo appletInfo;
041:
042: static Applet loadApplet(AppletInfo appletInfo) throws Exception {
043:
044: String width = appletInfo.getParameter("WIDTH");
045: if (width != null) {
046: appletInfo.setWidth(width);
047: } else {
048: System.err.println("Warning: <" + appletInfo.getTag()
049: + "> tag requires width attribute.");
050: System.exit(-1);
051: }
052:
053: String heigth = appletInfo.getParameter("HEIGHT");
054: if (heigth != null) {
055: appletInfo.setHeight(heigth);
056: } else {
057: System.err.println("Warning: <" + appletInfo.getTag()
058: + "> tag requires height attribute.");
059: System.exit(-1);
060: }
061:
062: String code = appletInfo.getParameter("CODE");
063: if (code == null || code.equals("")) {
064: System.err.println("Warning: <" + appletInfo.getTag()
065: + "> tag requires code attribute.");
066: System.exit(0);
067: }
068:
069: code = (code.endsWith(".class")) ? code.substring(0, code
070: .length() - 6) : code;
071:
072: appletInfo.setCodeBase(appletInfo.getParameter("CODEBASE"));
073:
074: URL codeBase = appletInfo.getCodeBase();
075:
076: ViewerClassLoader loader = loaders.get(codeBase);
077: if (loader == null) {
078: loader = new ViewerClassLoader();
079: loaders.put(codeBase, loader);
080: }
081:
082: loader.addURLs(appletInfo.getClassLoaderURLs());
083: Class clz = loader.loadClass(code);
084: Applet applet = (Applet) clz.newInstance();
085:
086: applets.add(applet);
087:
088: String name = appletInfo.getParameter("NAME");
089: if (name != null && name != "")
090: namedApplets.put(name, applet);
091:
092: applet.setStub(new ViewerAppletStub(applet, appletInfo));
093:
094: return applet;
095: }
096:
097: public ViewerAppletContext(AppletInfo appletInfo) {
098: this .appletInfo = appletInfo;
099: }
100:
101: public Applet getApplet(String name) {
102: return namedApplets.get(name);
103: }
104:
105: public Enumeration<Applet> getApplets() {
106: return applets.elements();
107: }
108:
109: public AudioClip getAudioClip(URL url) {
110: return new ViewerAudioClip(url);
111: }
112:
113: public Image getImage(URL url) {
114: return Toolkit.getDefaultToolkit().createImage(url);
115: }
116:
117: public InputStream getStream(String key) {
118: // TODO Auto-generated method stub
119: return null;
120: }
121:
122: public Iterator<String> getStreamKeys() {
123: // TODO Auto-generated method stub
124: return null;
125: }
126:
127: public void setStream(String key, InputStream stream)
128: throws IOException {
129: // TODO Auto-generated method stub
130:
131: }
132:
133: public void showDocument(URL url) {
134: // TODO Auto-generated method stub
135:
136: }
137:
138: public void showDocument(URL url, String target) {
139: // TODO Auto-generated method stub
140:
141: }
142:
143: public void showStatus(String status) {
144: appletInfo.setStatus(status);
145: }
146:
147: void remove(Applet applet) {
148: String name = applet.getParameter("NAME");
149: synchronized (namedApplets) {
150: namedApplets.remove(name);
151: }
152:
153: synchronized (applets) {
154: applets.remove(applet);
155: }
156: }
157:
158: private static class ViewerClassLoader extends URLClassLoader {
159:
160: public ViewerClassLoader() {
161: super (new URL[0]);
162: }
163:
164: public void addURL(URL url) {
165: URL[] urls = getURLs();
166: boolean exists = false;
167: for (int i = 0; i < urls.length; i++) {
168: if (urls[i].equals(url)) {
169: exists = true;
170: break;
171: }
172: }
173: if (!exists)
174: super .addURL(url);
175: }
176:
177: public void addURLs(URL[] urls) {
178: if (urls == null)
179: return;
180: for (int i = 0; i < urls.length; i++) {
181: addURL(urls[i]);
182: }
183: }
184:
185: }
186: }
|