|
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 */ import java.util.*; import org.openide.filesystems.FileSystem; import java.io.File; import org.netbeans.lib.cvsclient.Client; import org.netbeans.lib.cvsclient.connection.*; import org.netbeans.lib.cvsclient.admin.*; import org.netbeans.lib.cvsclient.file.*; import org.netbeans.modules.vcscore.cache.*; import org.netbeans.modules.javacvs.commands.JavaCvsStatusManager; import org.netbeans.modules.javacvs.*; import org.netbeans.modules.javacvs.commands.FileSystemCommand; import org.netbeans.modules.javacvs.commands.CvsStatus; import org.openide.filesystems.FileObject; public class JavaCvsCache extends FileSystemCache { public static final int STRAT_COMMANDS = 6; public static final String JAVA_CACHE_NAME = "javacvs"; // NOI18N /** A hashtable of netbeans.cache files, identified by full path. * Key: full path (string) for Files File.getAbsolutePath() * for CvsCachDirs CvsCacheDir.getAbsolutePath() * Value: instance of CvsCacheDir * */ private HashMap cacheDirs; /** a hashmap of filesystems identified by the root directory. */ private HashMap fileSystems; private Client cacheClient; /** Creates new JavaCvsCache */ public JavaCvsCache() { super(JAVA_CACHE_NAME); cacheDirs = new HashMap(50); fileSystems = new HashMap(); } /* public void setHomeDirectory(File homeDir) { CvsCacheClient client = getCacheClient(); client.setHomeDirectory(homeDir); } */ public CacheFile getCacheFile(File toFind, int strategy, Object locker) { String parentDir; if (toFind.isDirectory() && toFind.getParentFile() == null) { parentDir = toFind.getPath(); } else { if (toFind.isDirectory() && strategy <= CacheHandler.STRAT_LOCAL) { parentDir = toFind.getPath(); } else { parentDir = toFind.getParentFile().getPath(); } } // String parentDir = toFind.getParentFile().getAbsolutePath(); CacheDir cacheDir = getDir(parentDir); if (cacheDir == null) { cacheDir = initCacheDir(new File(parentDir), strategy); } loadDir(cacheDir, strategy, locker); if (cacheDir == null) return null; if (toFind.isDirectory()) { if (toFind.getParentFile() == null || strategy <= CacheHandler.STRAT_LOCAL) { return cacheDir; } return cacheDir.getSubDir(toFind.getName()); } return cacheDir.getFile(toFind.getName()); // } } private CacheDir initCacheDir(File toInit, int strategy) { File parDir = toInit.getParentFile(); CacheDir parentDir = null; if (parDir != null) { parentDir = getDir(parDir.getPath()); } CacheDir initDir = null; if (parentDir == null || parentDir.getAppliedLevel() == CacheHandler.STRAT_NONE) { // CacheDir parentofParent = initCacheDir(toInit.getParentFile(), strategy); //TEMP // the current dir will be prolly a root directory of sorts.. initDir = new CvsCacheDir(getId(), toInit); //initDir.setParent(null); is null by default if (strategy != CacheHandler.STRAT_NONE) { registerDir(initDir); } } else { // now let's see in the disc cache first.. parent has to have the curr. dir in the children list initDir = parentDir.getSubDir(toInit.getName()); if (initDir == null) { initDir = new CvsCacheDir(getId(), toInit); parentDir.addChildDir(initDir, false); if (strategy == CacheHandler.STRAT_NONE) { unregisterDir(initDir); } // debugging.. // System.err.println(toInit.getAbsolutePath()); } } return initDir; /* CacheDir initDir = null; if (toInit.getParentFile() == null) { System.out.println("Root of Fs.. " + toInit.getAbsolutePath()); initDir = getDir(toInit.getAbsolutePath()); if (initDir == null) { // the current dir will be prolly a root directory of sorts.. System.out.println("created!"); initDir = new CvsCacheDir(getId(), toInit); initDir.setParent(null); registerDir(initDir); } return initDir; } CacheDir parentDir = getDir(toInit.getParentFile().getAbsolutePath()); if (parentDir == null) { E.err("initCacheDir=" + toInit.getName() + " is without Parent= " + toInit.getParentFile().getAbsolutePath()); // CacheDir parentofParent = initCacheDir(toInit.getParentFile(), strategy); //TEMP // the current dir will be prolly a root directory of sorts.. parentDir = initCacheDir(toInit.getParentFile()); System.out.println("found parent is: " + parentDir.getName()); initDir = new CvsCacheDir(getId(), toInit); parentDir.addChildDir(initDir, false); } else { // now let's see in the disc cache first.. parent has to have the curr. dir in the children list System.out.println("initCacheDir=" + initDir.getName() + " has parent=" + parentDir.getName()); initDir = parentDir.getSubDir(toInit.getName()); } return initDir; */ } public CacheDir getDir(String fullName) { CacheDir cacheDir = (CacheDir)cacheDirs.get(fullName); return cacheDir; } /* return a new cvs server client * before running any command, make sure to set up the Connection.. */ /* public CvsCacheClient getCacheClient() { return CvsCacheClient.getInstance(); } */ /* public JavaCvsFileSystem getInvokingFileSystem() { Iterator it = this.fileSystems.keySet().iterator(); while (it.hasNext()) { JavaCvsFileSystem fs = (JavaCvsFileSystem)it.next(); if (fs.isRunningRefreshSemaphore()) { return fs; } } return null; } */ public void addRefreshDisplayers(CvsStatus comm) { // System.out.println("doing nothing here.."); } public synchronized void writeAllToDisk() { Collection dirs = cacheDirs.values(); Iterator it = dirs.iterator(); while (it.hasNext()) { CacheDir oneDir = (CacheDir)it.next(); oneDir.writeToDisk(); } } public synchronized void registerDir(CacheDir dir) { cacheDirs.put(dir.getFilePath(), dir); } public synchronized void unregisterDir(CacheDir dir) { if (dir != null) { cacheDirs.remove(dir.getFilePath()); } } /** * Register all current filesystems with the cache. * It's needed for good working of the lockFileObjects method.. */ public void registerFileSystem(FileSystem fs) { fileSystems.put(fs, fs); } public void unregisterFileSystem(FileSystem fs) { fileSystems.remove(fs); if (fileSystems.size() == 0) { CacheHandler.getInstance().unregistedType(JavaCvsCache.JAVA_CACHE_NAME); } } /* public void removeTemporary(CacheDir topNode) { removeTemporary(topNode.getAbsolutePath()); } public void removeTemporary(String topNode) { // it reduces the number of locks on the directory by one.. // if the numbr reaches 1, then the fileobjects are released LinkedList list = (LinkedList)lockedFileObjects.get(topNode); if (list != null) { Integer numList = (Integer)locks.get(topNode); if (numList.intValue() == 1) { list.clear(); lockedFileObjects.remove(topNode); locks.remove(topNode); } else { Integer newNumList = new Integer(numList.intValue() - 1); locks.put(topNode, newNumList); } } } */ /** * Creates a list of FileObjects that are contents of the topNode. * Adds that list to the map of nodes being locked. * If such node is being already locked, another lock is multiplied. * Should also ensure the cache structure is created (from fs root) */ /* boolean lockFileObjects(CacheDir topNode, boolean recursively) { String path = topNode.getAbsolutePath(); return lockFileObjects(path, recursively); } public boolean lockFileObjects(String path, boolean recursively) { Set keys = fileSystems.keySet(); Iterator it = keys.iterator(); String fullPath = path; String found = null; FileSystem fs = null; File fsRootFile = null; // now lets find the right filesystem to grab the fileobjects from. while (it.hasNext()) { fs = (FileSystem)it.next(); FileObject fsRoot = fs.getRoot(); fsRootFile = FileSystemCommand.toFile(fsRoot); if (fsRootFile != null) { String fsRootString = fsRootFile.getAbsolutePath(); if (path.startsWith(fsRootString)) { break; } } fs = null; } if (fs == null) { // not found - exit. return false; } found = fsRootFile.getAbsolutePath(); // make path relative to fs.. path = path.substring(found.length()); path = path.replace('\\','/'); if (path.startsWith("/")) path = path.substring(1); FileObject topFO = fs.findResource(path); return doLockFileObjects(topFO, fullPath, recursively); } */ /** does create a dir structure in case it doesn't already exist * (in order to make sure nothing gets lost) * for Recursive refresh use, where it's not guaranteed that the cache will be present * @returns the */ /** public CacheDir prepareCache(File rootDir, boolean isRecursive, int strategyToUse) { // first check if the rootDir is in the File[] subfiles = rootDir.listFiles(); CacheDir cvsFile = null; for (int index = 0; index < subfiles.length; index++) { if (!subfiles[index].isDirectory()) { // sets all files to local before applying the refresh results. CvsCacheFile cvsFl = (CvsCacheFile)CacheHandler.getInstance().getCacheFile(subfiles[index], strategyToUse, JavaCvsCache.JAVA_CACHE_NAME); if (cvsFl != null) { cvsFl.setStatus(JavaCvsStatusManager.LOCAL); // System.out.println("prepared file = " + cvsFl.getName()); } continue; } cvsFile = (CacheDir)CacheHandler.getInstance().getCacheFile(subfiles[index], strategyToUse, JavaCvsCache.JAVA_CACHE_NAME); if (cvsFile != null && isRecursive) { // just sanity check cvsFile.setAppliedLevel(strategyToUse); prepareCache(new File(cvsFile.getAbsolutePath()), isRecursive, strategyToUse); D.deb("added prepared directory=" + cvsFile.getName()); } } D.deb("prepared dir=" + rootDir.getAbsolutePath()); return cvsFile; } */ /** Includes the generic algorithm of applying strategies to directories. * In case this principle doens't suit you, you need to rewrite it :) * Basically it first tries to load the local files in the directory, * then loads the disk cache and finally checks the server * Everything is based on the doesStrategyApply() result. */ protected void loadDir(CacheDir dir, int strategy, Object locker) { if (dir == null) return; boolean reload = false; if (dir.getAppliedLevel() > CacheHandler.STRAT_NONE) { reload = true; } // for local directories, load the directory everytime again. if (dir.isLocal() && strategy == CacheHandler.STRAT_LOCAL) { dir.setAppliedLevel(CacheHandler.STRAT_NONE); } super.loadDir(dir, strategy, locker); /* if (doesStrategyApply(dir, strategy, STRAT_COMMANDS)) { // TODO?? } */ if (strategy == CacheHandler.STRAT_LOCAL && reload) { // System.out.println("reloading.."); CvsCacheDir cvsDir = (CvsCacheDir)dir; cvsDir.repopulate(); // now the repopulate thing is done inside the populate method.. cvsDir.readVirtualsFromDisk(); // cvsDir.readFromDisk(); } /* if (dir.isLocal()) { if (doesStrategyApply(dir, strategy, CacheHandler.STRAT_REFRESH)) { dir.checkServer(); dir.setComplete(true); } if (doesStrategyApply(dir, strategy, CacheHandler.STRAT_REFRESHING)) { dir.setComplete(true); dir.setAppliedLevel(CacheHandler.STRAT_REFRESHING); } if (doesStrategyApply(dir, strategy, CacheHandler.STRAT_REFRESH_RECURS)) { dir.checkServerRecursive(); dir.setComplete(true); } } */ } } |
... 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.