/plugins/FTP/tags/release-0-9-0/ftp/DirectoryCache.java

# · Java · 228 lines · 153 code · 19 blank · 56 comment · 15 complexity · a7d5f61a075418006945610ae03f98e7 MD5 · raw file

  1. /*
  2. * DirectoryCache.java - Caches remote directories to improve performance
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DirectoryCache.class program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package ftp;
  23. //{{{ Imports
  24. import java.io.*;
  25. import java.util.Enumeration;
  26. import java.util.Hashtable;
  27. import org.gjt.sp.jedit.io.*;
  28. import org.gjt.sp.jedit.MiscUtilities;
  29. import org.gjt.sp.jedit.jEdit;
  30. import org.gjt.sp.util.Log;
  31. //}}}
  32. public class DirectoryCache
  33. {
  34. //{{{ getCachedDirectory() method
  35. /**
  36. * Returns the specified cached directory listing, or null if
  37. * it is not in the cache.
  38. * @param url The URL
  39. * @since jEdit 2.6pre2
  40. */
  41. public static VFSFile[] getCachedDirectory(String url)
  42. {
  43. url = canon(url);
  44. synchronized(lock)
  45. {
  46. String path = (String)urlToCacheFileHash.get(url);
  47. if(path != null)
  48. {
  49. ObjectInputStream in = null;
  50. try
  51. {
  52. in = new ObjectInputStream(
  53. new BufferedInputStream(
  54. new FileInputStream(path)));
  55. return (VFSFile[])in.readObject();
  56. }
  57. catch(Exception e)
  58. {
  59. Log.log(Log.ERROR,DirectoryCache.class,e);
  60. return null;
  61. }
  62. finally
  63. {
  64. if(in != null)
  65. {
  66. try
  67. {
  68. in.close();
  69. }
  70. catch(Exception e)
  71. {
  72. }
  73. }
  74. }
  75. }
  76. else
  77. return null;
  78. }
  79. } //}}}
  80. //{{{ setCachedDirectory() method
  81. /**
  82. * Caches the specified directory listing.
  83. * @param url The URL
  84. * @param directory The directory listing
  85. * @since jEdit 2.6pre2
  86. */
  87. public static void setCachedDirectory(String url, VFSFile[] directory)
  88. {
  89. if(cacheDirectory == null)
  90. return;
  91. url = canon(url);
  92. synchronized(lock)
  93. {
  94. // filename generation algorithm is really simple...
  95. tmpFileCount++;
  96. long time = System.currentTimeMillis();
  97. String path = MiscUtilities.constructPath(cacheDirectory,
  98. "cache-" + tmpFileCount + "-" + time + ".tmp");
  99. ObjectOutputStream out = null;
  100. try
  101. {
  102. out = new ObjectOutputStream(
  103. new BufferedOutputStream(
  104. new FileOutputStream(path)));
  105. out.writeObject(directory);
  106. Log.log(Log.DEBUG,DirectoryCache.class,"Cached "
  107. + url + " to " + path);
  108. urlToCacheFileHash.put(url,path);
  109. }
  110. catch(Exception e)
  111. {
  112. Log.log(Log.ERROR,DirectoryCache.class,e);
  113. }
  114. finally
  115. {
  116. if(out != null)
  117. {
  118. try
  119. {
  120. out.close();
  121. }
  122. catch(Exception e)
  123. {
  124. }
  125. }
  126. }
  127. }
  128. } //}}}
  129. //{{{ clearCachedDirectory() method
  130. /**
  131. * Removes the cached listing of the specified directory.
  132. * @param url The URL
  133. * @since jEdit 2.6pre5
  134. */
  135. public static void clearCachedDirectory(String url)
  136. {
  137. url = canon(url);
  138. synchronized(lock)
  139. {
  140. Enumeration e = urlToCacheFileHash.keys();
  141. while(e.hasMoreElements())
  142. {
  143. String path = (String)e.nextElement();
  144. if(path.startsWith(url))
  145. {
  146. String cacheFile = (String)urlToCacheFileHash.remove(path);
  147. Log.log(Log.DEBUG,DirectoryCache.class,"Deleting " + cacheFile);
  148. new File(cacheFile).delete();
  149. }
  150. }
  151. }
  152. } //}}}
  153. //{{{ clearAllCachedDirectories() method
  154. /**
  155. * Removes all cached directory listings.
  156. * @since jEdit 2.6pre5
  157. */
  158. public static void clearAllCachedDirectories()
  159. {
  160. synchronized(lock)
  161. {
  162. Enumeration files = urlToCacheFileHash.elements();
  163. while(files.hasMoreElements())
  164. {
  165. String path = (String)files.nextElement();
  166. Log.log(Log.DEBUG,DirectoryCache.class,"Deleting " + path);
  167. new File(path).delete();
  168. }
  169. urlToCacheFileHash.clear();
  170. }
  171. } //}}}
  172. //{{{ Private members
  173. private static Object lock = new Object();
  174. private static int tmpFileCount;
  175. private static Hashtable urlToCacheFileHash;
  176. private static String cacheDirectory;
  177. private DirectoryCache() {}
  178. //{{{ canon() method
  179. /* This method exists so that foo/ and foo will both be cached
  180. * as the same URL. When the VFSPath class arrives, will get rid
  181. * of this kludge */
  182. private static String canon(String url)
  183. {
  184. if(url.length() != 0 && (url.endsWith("/")
  185. || url.endsWith(File.separator)))
  186. return url.substring(0,url.length() - 1);
  187. else
  188. return url;
  189. } //}}}
  190. //{{{ Class initializer
  191. static
  192. {
  193. urlToCacheFileHash = new Hashtable();
  194. String settingsDirectory = jEdit.getSettingsDirectory();
  195. if(settingsDirectory == null)
  196. {
  197. Log.log(Log.WARNING,DirectoryCache.class,"-nosettings "
  198. + "command line switch specified; remote directories");
  199. Log.log(Log.WARNING,DirectoryCache.class,"will not be cached.");
  200. }
  201. else
  202. {
  203. cacheDirectory = MiscUtilities.constructPath(settingsDirectory,
  204. "cache");
  205. new File(cacheDirectory).mkdirs();
  206. }
  207. } //}}}
  208. //}}}
  209. }