KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > registry > mergedctx > Cache


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.registry.mergedctx;
20
21 import org.netbeans.api.registry.ObjectRef;
22 import org.netbeans.spi.registry.BasicContext;
23 import org.netbeans.spi.registry.SpiUtils;
24
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.SoftReference JavaDoc;
27 import java.util.*;
28
29 /**
30  * Provides caching mechanism for BasicContextImpl and RootContextImpl, which should be constructed
31  * exclusively by calling these factory methods: createRootContext, createContext, getOrCreateParentCtx
32  * and getOrCreateSubCtx.
33  *
34  * @author Radek Matous
35  */

36 final class Cache {
37     /** <String, BasicContextImpl> all live MergedContext provided by this impl.*/
38     private final Map contextCache = Collections.synchronizedMap(new WeakHashMap());
39     private final Map objectCache = Collections.synchronizedMap(new WeakHashMap());
40
41     Cache() {
42     }
43
44     Object JavaDoc getContextSync() {
45         return contextCache;
46     }
47
48     BasicContextImpl cacheContext(final BasicContextImpl ctxImpl) {
49         final boolean isRoot = ctxImpl.getAbsolutePath().isRoot();
50
51         Reference JavaDoc retVal = (isRoot) ? new HardReference(ctxImpl) : new SoftReference JavaDoc(ctxImpl);
52         retVal = (Reference JavaDoc) contextCache.put(ctxImpl.getAbsolutePath(), retVal);
53         return (retVal == null) ? null : (BasicContextImpl) retVal.get();
54     }
55
56     BasicContextImpl getContext(final Resource resource) {
57         final Reference JavaDoc ref = (Reference JavaDoc) contextCache.get(resource);
58         return (ref == null) ? null : (BasicContextImpl) ref.get();
59     }
60
61     void removeContext(final Resource resource) {
62         contextCache.remove(resource);
63     }
64
65     void cacheObjectRef(final BasicContext root, final BasicContext ctx, final String JavaDoc bindingName, final Object JavaDoc object) {
66         if (object != null) {
67             objectCache.put(object, SpiUtils.createObjectRef(ctx, null, bindingName));
68         }
69     }
70
71     void removeObjectRef(final Object JavaDoc object) {
72         if (object != null)
73             objectCache.remove(object);
74     }
75
76     ObjectRef getObjectRef(final Object JavaDoc object) {
77         return (ObjectRef) objectCache.get(object);
78     }
79
80     Collection existingSubcontexts(final BasicContextImpl ctxImpl) {
81         final List eSubctxs = new ArrayList();
82         synchronized (contextCache) {
83             for (Iterator iterator = contextCache.values().iterator(); iterator.hasNext();) {
84                 final Reference JavaDoc ref = (Reference JavaDoc) iterator.next();
85                 final BasicContextImpl iCtx = (BasicContextImpl) ((ref == null) ? null : ref.get());
86                 if (iCtx != null && ctxImpl.getAbsolutePath().isSuperior(iCtx.getAbsolutePath())) {
87                     eSubctxs.add(iCtx);
88                 }
89             }
90         }
91         Collections.sort(eSubctxs, new Comparator() {
92             public int compare(final Object JavaDoc o1, final Object JavaDoc o2) {
93                 final BasicContextImpl ctx1 = (BasicContextImpl) o1;
94                 final BasicContextImpl ctx2 = (BasicContextImpl) o2;
95                 final int len1 = ctx1.getAbsolutePath().getPath().length();
96                 final int len2 = ctx2.getAbsolutePath().getPath().length();
97                 return (len2 - len1);
98             }
99         });
100
101         return Collections.unmodifiableCollection(eSubctxs);
102     }
103
104
105     private static final class HardReference extends SoftReference JavaDoc {
106         Object JavaDoc referent;
107
108         public HardReference(final Object JavaDoc referent) {
109             super(referent);
110             this.referent = referent;
111         }
112
113         public Object JavaDoc get() {
114             return referent;
115         }
116     }
117
118 }
119
Popular Tags