01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Pavel Dolgov
19: * @version $Revision: 1.2 $
20: */package org.apache.harmony.applet;
21:
22: import java.net.URL;
23: import java.net.URLClassLoader;
24: import java.util.IdentityHashMap;
25:
26: import org.apache.harmony.awt.ContextThreadGroup;
27:
28: /**
29: * Common context for all applets loaded from the same code base
30: */
31: final class CodeBase {
32:
33: final URL codeBase;
34: final URLClassLoader classLoader;
35: final ContextThreadGroup threadGroup;
36: final Factory factory;
37:
38: private final IdentityHashMap<Document, DocumentSlice> docSlices = new IdentityHashMap<Document, DocumentSlice>();
39:
40: CodeBase(URL url, Factory factory) {
41: this .codeBase = url;
42: this .factory = factory;
43: classLoader = new URLClassLoader(new URL[] { url });
44: threadGroup = new ContextThreadGroup("Context-"
45: + url.toString());
46: threadGroup.setMaxPriority(4);
47: }
48:
49: DocumentSlice getDocumentSlice(Document doc) {
50: synchronized (docSlices) {
51: DocumentSlice slice = docSlices.get(doc);
52: if (slice == null) {
53: slice = new DocumentSlice(doc, this );
54: docSlices.put(doc, slice);
55: }
56: return slice;
57: }
58: }
59:
60: void remove(DocumentSlice slice) {
61: boolean empty = false;
62:
63: synchronized (docSlices) {
64: docSlices.remove(slice.document);
65: empty = (docSlices.size() == 0);
66: }
67:
68: if (empty) {
69: threadGroup.dispose();
70: factory.remove(this);
71: }
72: }
73: }
|