|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.javacvs.caching; /** * * @author mkleint * @version */ import java.io.*; import java.util.*; import org.netbeans.modules.vcscore.cache.*; import org.netbeans.modules.javacvs.commands.JavaCvsStatusManager; import org.netbeans.modules.javacvs.*; import org.netbeans.modules.javacvs.commands.*; public class CvsCacheDir extends CacheDir { // protected String status; private static final String CACHE_FILENAME = File.separator + "CVS" + File.separator + "netbeans.cache"; // NOI18N private static final String CACHE_NEW_FILENAME = File.separator + "CVS" + File.separator + "javacvs.cache"; // NOI18N private static final String CACHE_VIRTUAL_FILENAME = File.separator + "CVS" + File.separator + "javacvs.virtuals"; // NOI18N private static FileFilter cvsFilter = new FileFilter() { public boolean accept(File f) { if (f.isDirectory() && f.getName().equalsIgnoreCase("CVS")) return true; // NOI18N return false; } }; //private boolean firstTimer; /** Creates new CvsCacheDir */ public CvsCacheDir(String cacheName, File dirFile) { this(cacheName, dirFile, new CvsCacheFile.CvsPersistentData(true)); } CvsCacheDir(String cacheName, File dirFile, CacheFile.PersistentData data) { super(cacheName, dirFile, data); File cvsSubDir = new File(dirFile,"CVS"); // NOI18N if (!cvsSubDir.exists()) { setLocal(true); setStatus(JavaCvsStatusManager.LOCAL); //firstTimer = false; } else { setLocal(false); setStatus(" "); // NOI18N //firstTimer = true; } } /* void setFirstTimer(boolean first) { firstTimer = first; } boolean isFirstTimer() { return firstTimer; } */ protected CacheFile createChildFromData(CacheFile.PersistentData data) { CacheFile file; if (data.isDirectory()) { file = new CvsCacheDir(getCacheName(), new File(dirFile, data.getName()), data); } else { file = new CvsCacheFile(getCacheName(), data); } return file; } public synchronized void populateWithLocal(Object locker) { if (getAppliedLevel() >= CacheHandler.STRAT_LOCAL) { repopulate(); } else { // System.out.println("populateWithLocal()=" + dirFile.getAbsolutePath()); // E.err("populateWithLocal()" + dirFile.getAbsolutePath()); File[] subFiles = dirFile.listFiles(); if (subFiles == null) { return; } CacheHandler handler = CacheHandler.getInstance(); CacheFile file; for (int index = 0; index < subFiles.length; index++) { File oneFile = subFiles[index]; if (oneFile == null) continue; if (oneFile.isDirectory()) { if (getSubDir(oneFile.getName()) == null) { file = new CvsCacheDir(getCacheObject().getId(), oneFile); addChildDir((CacheDir) file, false); handler.addCacheLocker(locker, file); } } else { if (getFile(oneFile.getName()) == null) { file = new CvsCacheFile(getCacheObject().getId(), oneFile.getName()); this.addFile(file, false); handler.addCacheLocker(locker, file); } } } setAppliedLevel(CacheHandler.STRAT_LOCAL); } readVirtualsFromDisk(); // in order to have the disk cache added during children() calls. } public synchronized void repopulate() { // System.out.println("repopulate()=" + dirFile.getAbsolutePath()); if (!dirFile.exists()) { removeAll(true); if (getParent() != null) { getParent().removeChildDir(getName(), true); } return; } File[] cvsDir = dirFile.listFiles(cvsFilter); setLocal(cvsDir.length == 0); File[] subFiles = dirFile.listFiles(); CacheDir[] cacheDirs = getSubDirs(); // remove wrong directories.. for (int index = 0; index < cacheDirs.length; index++) { CacheDir dir = cacheDirs[index]; if (dir == null) continue; File found = null; for (int ind2 = 0; ind2 < subFiles.length; ind2++) { if (subFiles[ind2] == null) continue; if (subFiles[ind2].getName().equals(dir.getName())) { found = subFiles[ind2]; subFiles[ind2] = null; break; } } if (found == null) { removeChildDir(dir.getName(), false); } else if (!found.isDirectory()) { removeChildDir(dir.getName(), false); } } // remove wrong files.. CacheFile[] cacheFiles = getFiles(); for (int index = 0; index < cacheFiles.length; index++) { CacheFile cfile = cacheFiles[index]; // System.out.println("cFile=" + cfile.getName()); if (cfile == null) continue; File found = null; for (int ind2 = 0; ind2 < subFiles.length; ind2++) { if (subFiles[ind2] == null) continue; if (subFiles[ind2].getName().equals(cfile.getName())) { found = subFiles[ind2]; subFiles[ind2] = null; break; } } if (found == null) { // the cache file is not present locally.. // System.out.println("cFile=" + cfile.getName() + " not found"); // System.out.println("islocal=" + isLocal()); // System.out.println("status =" + cfile.getStatus()); if (isLocal()) { removeFile(cfile.getName(), false); } else { String status = cfile.getStatus(); if (status == null) { // cfile.setStatus(JavaCvsStatusManager.UNKNOWN); } else if (status.equals(JavaCvsStatusManager.LOCALLY_REMOVED)) { // keep it. // removeFile(cfile.getName(), false); } else if (status.equals(JavaCvsStatusManager.LOCAL) || status.equals(JavaCvsStatusManager.UNKNOWN) || status.equals(JavaCvsStatusManager.LOCALLY_ADDED)) { removeFile(cfile.getName(), false); } else { cfile.setStatus(JavaCvsStatusManager.NEEDS_CHECKOUT); } } } else { if (isLocal() && (!cfile.getStatus().equals(JavaCvsStatusManager.LOCAL))) { removeFile(cfile.getName(), false); } } } //now add the ones that were not present.. for (int ind3 = 0; ind3 < subFiles.length; ind3++) { if (subFiles[ind3] == null) continue; File file = subFiles[ind3]; if (!file.exists()) continue; if (file.isDirectory()) { addChildDir(new CvsCacheDir(JavaCvsCache.JAVA_CACHE_NAME, file), false); } else { addFile(new CvsCacheFile(JavaCvsCache.JAVA_CACHE_NAME, file.getName()), false); } } } /** returns the name of the directory */ public String getName() { if (dirFile.getParentFile() == null) { return dirFile.getAbsolutePath(); } return dirFile.getName(); // return short name, just name of file.. // return dirName; } public void simpleWriteToDisk() { // System.out.println("writing " + getAbsolutePath()); BufferedWriter out = null; BufferedWriter virtOut = null; try{ virtOut = new BufferedWriter(new FileWriter(getCacheVirtualsFileName())); // NOI18N // files get written first CacheFile[] files = getFiles(); for(int i = 0; i < files.length; i++) { CvsCacheFile file = (CvsCacheFile) files[i]; if (file != null) { File virtCheck = new File(file.getAbsolutePath()); if (!virtCheck.exists()) { virtOut.write(file.getName()); virtOut.newLine(); } } } virtOut.flush(); out = new BufferedWriter(new FileWriter(getCacheNewFileName())); // NOI18N // files get written first for(int i = 0; i < files.length; i++) { CvsCacheFile file = (CvsCacheFile) files[i]; if (file != null && !file.getStatus().equals(JavaCvsStatusManager.LOCAL)) { out.write(((CvsCacheFile.CvsPersistentData) file.getPersistentData()).writeLineToDisk()); out.newLine(); } } out.flush(); setModifiedContent(false); } catch (IOException e){ // E.err(e,"writeDirToDiskCache() failed for "+getName()); // NOI18N } finally { try { if (out != null) out.close(); if (virtOut != null) virtOut.close(); File oldContent = new File(getCacheFileName()); if (oldContent.exists()) { oldContent.delete(); } } catch (IOException e) { // E.err(e,"writeDirToDiskCache() failed for "+getName()); // NOI18N } } } public synchronized void writeToDisk() { //if (!isComplete()) return; File[] cvsDir = dirFile.listFiles(cvsFilter); if (cvsDir == null || cvsDir.length == 0) return; // it's not a CVS dir.. nothing is written.. File netbeansFile = new File(getCacheNewFileName()); // even if this directory is not modified , the cache file is written down // .. BUT only when if already doesn't exist.. (we don't want to overwrite not-read files, do we? :) // useful to create cache files and populate the structure with netbeans.cache files //if ((!this.isModifiedContent()) && (netbeansFile.exists())) return; // nothing to save.. noting changed (status of files might change even if the content is not modified!) // Do not write down directories that hasn't been at least locally read (that would write empty dir. // Also do not write if the file netbeans.cache exists AND wasn't read. if ((!netbeansFile.exists() && (getAppliedLevel() < CacheHandler.STRAT_DISK)) || (getAppliedLevel() < CacheHandler.STRAT_DISK)) return; simpleWriteToDisk(); } private boolean readFromDisk_Old() { //synchronized removed since it's only called from readFromDisk() boolean isOk = false; File cacheFile=new File(getCacheFileName()); // the actual netbeans.cache file // System.out.println("readFromDisk()=" + this.getAbsolutePath()); if(cacheFile.exists() && cacheFile.canRead() ){ // System.out.println("reading old=" + getAbsolutePath()); BufferedReader in = null; try{ in = new BufferedReader(new FileReader(cacheFile)); String line=null; LinkedList processedList = new LinkedList(); //setFirstTimer(false); //boolean reload = false; while ((line=in.readLine())!=null ){ CacheFile file = CvsCacheFile.readFileCache(line, this); /* if (!reload) { reload = isFirstTimer(); } */ if (file != null) { if (file instanceof CvsCacheFile) { processedList.add(file); } } } in.close(); isOk= true; CacheFile[] files = getFiles(); LinkedList toBeRemoved = new LinkedList(); for(int i = 0; i < files.length; i++) { CvsCacheFile fil = (CvsCacheFile) files[i]; if (!processedList.contains(fil)) { File f = new File(fil.getAbsolutePath()); if (!f.exists()) { // System.out.println("in cvscachedir.. removing=" + f.getAbsolutePath()); toBeRemoved.add(fil); } } } if (toBeRemoved.size() > 0) { Iterator it2 = toBeRemoved.iterator(); while (it2.hasNext()) { removeFile(((CacheFile)it2.next()).getName(), false); } } setAppliedLevel(CacheHandler.STRAT_DISK); /* currently should not happen since we load the disk on children() call.. if (reload) { org.openide.util.RequestProcessor.postRequest(new Runnable() { public void run() { // System.out.println("started a refreshing thread for=" + CvsCacheDir.this.getAbsolutePath()); CvsCacheDir.this.getCacheObject().fireCacheHandlerEvent(FileSystemCache.EVENT_ADD, CvsCacheDir.this); } }); } */ return isOk; }catch (IOException e){ // E.err(e,"readDirFromDiskCache() failed"); // NOI18N } finally { if (in != null) { try {in.close(); } catch (IOException exc) {} in = null; } } } else { // if not exists, let's assume we applied the level setAppliedLevel(CacheHandler.STRAT_DISK); } return isOk; } public boolean readFromDisk() { return readFromDisk(null); } /** * new way of reading from disk. */ public synchronized boolean readFromDisk(Object locker) { boolean isOk = false; File cacheFile=new File(getCacheNewFileName()); // the actual netbeans.cache file // System.out.println("readFromDisk()=" + this.getAbsolutePath()); if(cacheFile.exists() && cacheFile.canRead() ){ // System.out.println("reading new=" + getAbsolutePath()); BufferedReader in = null; try{ in = new BufferedReader(new FileReader(cacheFile)); String line=null; LinkedList processedList = new LinkedList(); //setFirstTimer(false); //boolean reload = false; CacheHandler handler = CacheHandler.getInstance(); while ((line=in.readLine())!=null ){ CacheFile file = CvsCacheFile.readFileCache(line, this); /* if (!reload) { reload = isFirstTimer(); } */ if (file != null) { if (file instanceof CvsCacheFile) { processedList.add(file); handler.addCacheLocker(locker, file); } } } in.close(); isOk= true; CacheFile[] files = getFiles(); LinkedList toBeRemoved = new LinkedList(); for (int i = 0; i < files.length; i++) { CvsCacheFile fil = (CvsCacheFile) files[i]; if (!processedList.contains(fil)) { File f = new File(fil.getAbsolutePath()); if (!f.exists()) { // System.out.println("in cvscachedir.. removing=" + f.getAbsolutePath()); toBeRemoved.add(fil); } } } if (toBeRemoved.size() > 0) { Iterator it2 = toBeRemoved.iterator(); while (it2.hasNext()) { removeFile(((CacheFile)it2.next()).getName(), false); } } setAppliedLevel(CacheHandler.STRAT_DISK); return isOk; }catch (IOException e){ // E.err(e,"readDirFromDiskCache() failed"); // NOI18N } finally { if (in != null) { try {in.close(); } catch (IOException exc) {} in = null; } } } else { // for backward compatibility, do it the old way.. File oldStorage = new File(getCacheFileName()); if (oldStorage.exists() && oldStorage.canRead()) { readFromDisk_Old(); } else { setAppliedLevel(CacheHandler.STRAT_DISK); } } return isOk; } /** * Read just one file from disk cache. This method must not return |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.