KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > 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
20 package org.netbeans.modules.masterfs;
21
22 import org.openide.filesystems.FileObject;
23
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.SoftReference JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import java.util.*;
28
29 /**
30  * Provides caching management for MasterFileObjects, that should be never
31  * constructed directly (instead should be used method getOrCreate).
32  *
33  * Neither children references nor parent reference are kept.
34  *
35  * @author Radek Matous
36  */

37 final class Cache {
38     /** maps <String resourceName, FileSystem fs>*/
39     private Map res2DfoMap;
40     private static Cache instance;
41
42     /***
43      * @return one shared instance of Cache
44      */

45     static Cache getDefault() {
46         synchronized (Cache.class) {
47             if (instance == null) {
48                 instance = new Cache();
49                 instance.res2DfoMap = Collections.synchronizedMap(new WeakHashMap());
50             }
51         }
52         return instance;
53     }
54
55     private Cache() {
56     }
57
58     /**
59      * Looks up in cache MasterFileObject identified with resPath
60      * @param resPath identifies MasterFileObject
61      * @return instance of MasterFileObject if exists in cache or null
62      */

63     MasterFileObject get(final ResourcePath resPath) {
64         //resPath = getNormalizedPath(resPath);
65
MasterFileObject retVal = getValidOrInvalid(resPath);
66
67         if (retVal != null)
68             retVal = (retVal.isValid()) ? retVal : null;
69
70         return retVal;
71     }
72
73     MasterFileObject getValidOrInvalid(final ResourcePath resPath) {
74         final Reference JavaDoc ref = (Reference JavaDoc) res2DfoMap.get(resPath.getNormalizedPath());
75         MasterFileObject retVal = null;
76         if (ref != null)
77             retVal = (MasterFileObject) ref.get();
78         return retVal;
79     }
80
81     /**
82      * Looks up in cache MasterFileObject identified with resPath. If there
83      * is no appropriate object in cache, then new one is created, but only if
84      * delegate can be found
85      * @param resPath
86      * @return MasterFileObject or null, if delegate can't be resolved
87      */

88     MasterFileObject getOrCreate(final ResourcePath resPath) {
89         final MasterFileObject retVal = get(resPath);
90         if (retVal != null) return retVal;
91
92         FileObject delegate = Delegate.resolve(resPath);
93         return (delegate == null) ? null : getOrCreate(resPath, delegate);
94     }
95
96     /**
97      * Looks up in cache MasterFileObject identified with resPath. If there
98      * is no appropriate object in cache, then new one is created, but only if
99      * delegate can be found
100      * @param resPath identification of MasterFileObject
101      * @param delegate
102      * @return MasterFileObject
103      */

104     MasterFileObject getOrCreate(final ResourcePath resPath, final FileObject delegate) {
105         MasterFileObject retVal = getValidOrInvalid(resPath);
106         boolean isRetValValid = (retVal != null && !retVal.isValid()) ? false : true;
107                         
108         if (retVal != null && isRetValValid) {
109             return retVal;
110         }
111
112         MasterFileObject nRetVal = new MasterFileObject(resPath, delegate);
113         if (nRetVal.isValid()) {
114             retVal = nRetVal;
115             put(retVal);
116         }
117         
118         return retVal;
119     }
120
121     /**
122      * @return all objects from cache as enumeration
123      */

124     Enumeration getAll() {
125         final ArrayList arrayList = new ArrayList();
126         synchronized (res2DfoMap) {
127             final Iterator it = res2DfoMap.entrySet().iterator();
128             while (it.hasNext()) {
129                 Map.Entry entry = (Map.Entry) it.next();
130                 Reference JavaDoc ref = (Reference JavaDoc) entry.getValue();
131                 if (ref != null) {
132                     MasterFileObject hfo = (MasterFileObject) ref.get();
133                     if (hfo != null && hfo.getDelegate().isValid())
134                         arrayList.add(hfo);
135                 }
136             }
137         }
138         final Object JavaDoc[] array = new Object JavaDoc[arrayList.size()];
139         arrayList.toArray(array);
140         return org.openide.util.Enumerations.array (array);
141     }
142
143     void clear() {
144         res2DfoMap.clear();
145     }
146
147     private MasterFileObject put(final MasterFileObject hfo) {
148         MasterFileObject retVal = hfo;
149         synchronized (res2DfoMap) {
150             final MasterFileObject test = get(hfo.getResource());
151             if (test == null || !test.isValid()) {
152                 res2DfoMap.remove(hfo.getResource().getNormalizedPath());
153                 res2DfoMap.put(hfo.getResource().getNormalizedPath(), createReference(hfo));
154             } else {
155                 retVal = test;
156             }
157         }
158         return retVal;
159     }
160
161     private static Reference JavaDoc createReference(final MasterFileObject hfo) {
162         return new SoftReference JavaDoc(hfo);
163         //return new WeakReference(hfo);
164
}
165
166     /**
167      * Replaces original MasterFileObject in cache with new one
168      * @param oldResPath identifies the old one MasterFileObject in cache
169      * @param newObject that replaces the original one
170      */

171     void replace(final String JavaDoc oldResPath, final MasterFileObject newObject) {
172         synchronized (res2DfoMap) {
173             final MasterFileObject test = getValidOrInvalid(new ResourcePath(oldResPath));
174             if (test != null) res2DfoMap.remove(oldResPath);
175             put(newObject);
176         }
177     }
178
179
180 }
181
Popular Tags