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: * @author Pavel Dolgov
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.applet;
021:
022: import java.applet.Applet;
023: import java.applet.AppletContext;
024: import java.applet.AudioClip;
025: import java.awt.Image;
026: import java.awt.Toolkit;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.net.URL;
030: import java.util.ArrayList;
031: import java.util.Collections;
032: import java.util.Enumeration;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: /**
039: * Collection of applets running in one document and loaded from the same code base,
040: * implementation of <b>AppletContext</b> interface
041: */
042: final class DocumentSlice implements AppletContext {
043:
044: final CodeBase codeBase;
045: final Document document;
046:
047: private final List<Proxy> proxies = new ArrayList<Proxy>();
048: private final Map<String, InputStream> streams = new HashMap<String, InputStream>();
049:
050: DocumentSlice(Document doc, CodeBase codeBase) {
051: this .document = doc;
052: this .codeBase = codeBase;
053: }
054:
055: void add(Proxy p) {
056: synchronized (proxies) {
057: proxies.add(p);
058: }
059: codeBase.factory.add(p);
060: }
061:
062: void remove(Proxy p) {
063: codeBase.factory.remove(p);
064:
065: boolean empty;
066: synchronized (proxies) {
067: proxies.remove(p);
068: empty = proxies.isEmpty();
069: }
070: if (empty) {
071: codeBase.remove(this );
072: document.remove(this );
073: }
074: }
075:
076: public Applet getApplet(String name) {
077:
078: synchronized (proxies) {
079: for (Proxy p : proxies) {
080: if (p.params.name.equals(name)) {
081: return p.getApplet();
082: }
083: }
084: return null;
085: }
086: }
087:
088: public Enumeration<Applet> getApplets() {
089:
090: synchronized (proxies) {
091: ArrayList<Applet> applets = new ArrayList<Applet>();
092: for (Proxy p : proxies) {
093: Applet a = p.getApplet();
094: if (a != null) {
095: applets.add(a);
096: }
097: }
098: return Collections.enumeration(applets);
099: }
100: }
101:
102: public AudioClip getAudioClip(URL url) {
103: return new AudioClipImpl(url);
104: }
105:
106: public Image getImage(URL url) {
107: return Toolkit.getDefaultToolkit().getImage(url);
108: }
109:
110: public InputStream getStream(String key) {
111:
112: synchronized (streams) {
113: return streams.get(key);
114: }
115: }
116:
117: public Iterator<String> getStreamKeys() {
118:
119: synchronized (streams) {
120: ArrayList<String> keys = new ArrayList<String>();
121: for (String string : streams.keySet()) {
122: keys.add(string);
123: }
124: return keys.iterator();
125: }
126: }
127:
128: public void setStream(String key, InputStream stream)
129: throws IOException {
130:
131: synchronized (streams) {
132: streams.put(key, stream);
133: }
134: }
135:
136: public void showDocument(URL url, String target) {
137: codeBase.factory.showDocument(this , url, target);
138: }
139:
140: public void showDocument(URL url) {
141: this .showDocument(url, null);
142: }
143:
144: public void showStatus(String status) {
145: codeBase.factory.showStatus(this, status);
146: }
147: }
|