001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.masterfs.filebasedfs.utils;
040:
041: import java.io.File;
042: import java.util.concurrent.ConcurrentHashMap;
043: import org.netbeans.modules.masterfs.filebasedfs.naming.NamingFactory;
044: import org.openide.util.Lookup;
045:
046: /**
047: *
048: * @author Radek Matous
049: */
050: public class FileChangedManager extends SecurityManager {
051: private static FileChangedManager INSTANCE;
052: private static final int CREATE_HINT = 2;
053: private static final int DELETE_HINT = 1;
054: private static final int AMBIGOUS_HINT = 3;
055:
056: private final ConcurrentHashMap<Integer, Integer> hints = new ConcurrentHashMap<Integer, Integer>();
057: private long shrinkTime = System.currentTimeMillis();
058:
059: public FileChangedManager() {
060: INSTANCE = this ;
061: }
062:
063: public static FileChangedManager getInstance() {
064: if (INSTANCE == null) {
065: Lookup.getDefault().lookup(SecurityManager.class);
066: assert INSTANCE != null;
067: }
068: return INSTANCE;
069: }
070:
071: @Override
072: public void checkDelete(String file) {
073: put(file, false);
074: }
075:
076: @Override
077: public void checkWrite(String file) {
078: put(file, true);
079: }
080:
081: public boolean impeachExistence(File f, boolean expectedExixts) {
082: Integer hint = remove(getKey(f));
083: boolean retval = (hint == null) ? false : true;
084: if (retval) {
085: if (hint == AMBIGOUS_HINT) {
086: return true;
087: } else {
088: retval = (expectedExixts != toState(hint));
089: }
090: }
091: return retval;
092: }
093:
094: public boolean exists(File file) {
095: boolean retval = file.exists();
096: remove(getKey(file));
097: put(file, retval);
098: return retval;
099: }
100:
101: private Integer put(int id, boolean state) {
102: shrinkTime = System.currentTimeMillis();
103: int val = toValue(state);
104: Integer retval = hints.putIfAbsent(id, val);
105: if (retval != null) {
106: if (retval != AMBIGOUS_HINT && retval != val) {
107: hints.put(id, AMBIGOUS_HINT);
108: }
109: }
110: return retval;
111: }
112:
113: private int toValue(boolean state) {
114: return state ? CREATE_HINT : DELETE_HINT;
115: }
116:
117: private boolean toState(int value) {
118: switch (value) {
119: case DELETE_HINT:
120: return false;
121: case CREATE_HINT:
122: return true;
123: }
124: return false;
125: }
126:
127: private Integer get(int id) {
128: long now = System.currentTimeMillis();
129: if ((now - shrinkTime) > 5000) {
130: int size = hints.size();
131: if (size > 1500) {
132: shrink();
133: }
134: shrinkTime = now;
135: }
136: return hints.get(id);
137: }
138:
139: private void shrink() {
140: hints.keySet().clear();
141: }
142:
143: private Integer remove(int id) {
144: return hints.remove(id);
145: }
146:
147: private static int getKey(File f) {
148: return NamingFactory.createID(f);
149: }
150:
151: private static int getKey(String f) {
152: return getKey(new File(f));
153: }
154:
155: private Integer put(String f, boolean value) {
156: return put(getKey(f), value);
157: }
158:
159: private Integer put(File f, boolean value) {
160: return put(getKey(f), value);
161: }
162:
163: private Integer get(String file) {
164: return get(getKey(file));
165: }
166:
167: private Integer get(File file) {
168: return get(getKey(file));
169: }
170: }
|