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.net.URL;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: /**
028: * Applet context factory
029: */
030: final class Factory {
031:
032: private final Callback callback;
033: private final Map<URL, CodeBase> codeBases = Collections
034: .synchronizedMap(new HashMap<URL, CodeBase>());
035: private final Map<Integer, Proxy> allProxies = Collections
036: .synchronizedMap(new HashMap<Integer, Proxy>());
037: private final Map<Integer, Document> documents = Collections
038: .synchronizedMap(new HashMap<Integer, Document>());
039:
040: Factory(Callback callback) {
041: this .callback = callback;
042: }
043:
044: CodeBase getCodeBase(URL url) {
045: synchronized (codeBases) {
046: CodeBase cb = codeBases.get(url);
047: if (cb == null) {
048: cb = new CodeBase(url, this );
049: codeBases.put(url, cb);
050: }
051: return cb;
052: }
053: }
054:
055: void remove(CodeBase codeBase) {
056: codeBases.remove(codeBase.codeBase);
057: }
058:
059: void remove(Document doc) {
060: documents.remove(new Integer(doc.id));
061: }
062:
063: void dispose(int id) {
064: Proxy p = allProxies.get(new Integer(id));
065: if (p == null) {
066: return;
067: }
068: p.docSlice.remove(p);
069: p.dispose();
070:
071: }
072:
073: Document getDocument(URL docBase, int docId) {
074: synchronized (documents) {
075: Document doc;
076: Integer objDocId = new Integer(docId);
077: doc = documents.get(objDocId);
078: if (doc == null) {
079: doc = new Document(this , docBase, docId);
080: documents.put(objDocId, doc);
081: }
082: return doc;
083: }
084: }
085:
086: void createAndRun(Parameters params) {
087:
088: CodeBase codeBase = getCodeBase(params.codeBase);
089: Document doc = getDocument(params.documentBase,
090: params.documentId);
091: DocumentSlice ds = codeBase.getDocumentSlice(doc);
092: doc.add(ds);
093:
094: Proxy p = new Proxy(ds, params);
095: p.create();
096: }
097:
098: void createAndRun(int id, long parentWindowId, URL documentBase,
099: int documentId, URL codeBase, String className,
100: String[] paramStrings, String name, Object container) {
101: Parameters params = new Parameters(id, parentWindowId,
102: documentBase, documentId, codeBase, className,
103: paramStrings, name, container);
104: createAndRun(params);
105: }
106:
107: void start(int id) {
108: Proxy p = allProxies.get(new Integer(id));
109: if (p != null) {
110: p.start();
111: }
112: }
113:
114: void stop(int id) {
115: Proxy p = allProxies.get(new Integer(id));
116: if (p != null) {
117: p.stop();
118: }
119: }
120:
121: void init(int id) {
122: Proxy p = allProxies.get(new Integer(id));
123: if (p != null) {
124: p.init();
125: }
126: }
127:
128: void destroy(int id) {
129: Proxy p = allProxies.get(new Integer(id));
130: if (p != null) {
131: p.destroy();
132: }
133: }
134:
135: void appletResize(Proxy p, int width, int height) {
136: callback.appletResize(p.params.id, width, height);
137: }
138:
139: void showStatus(DocumentSlice ds, String status) {
140: callback.showStatus(ds.document.id, status);
141: }
142:
143: void showDocument(DocumentSlice ds, URL url, String target) {
144: callback.showDocument(ds.document.id, url, target);
145: }
146:
147: void add(Proxy p) {
148: allProxies.put(new Integer(p.params.id), p);
149: }
150:
151: void remove(Proxy p) {
152: allProxies.remove(new Integer(p.params.id));
153: }
154:
155: void dump() {
156: for (Proxy p : allProxies.values()) {
157: System.err.println("app " + p.params.id + " " + " cb "
158: + p.docSlice.codeBase.hashCode() + " " + " doc "
159: + p.params.documentId + " " + p.params.codeBase
160: + p.params.className + " "
161: + (p.isActive() ? "active" : "stopped"));
162: }
163: for (CodeBase cb : codeBases.values()) {
164: System.err.println("cb " + cb.hashCode() + " "
165: + cb.threadGroup);
166: }
167: }
168: }
|