1 22 23 package org.gjt.sp.jedit.io; 24 25 import javax.swing.JOptionPane ; 27 import javax.swing.SwingUtilities ; 28 import java.awt.Component ; 29 import java.awt.Frame ; 30 import java.io.IOException ; 31 import java.util.*; 32 33 import org.gjt.sp.jedit.gui.ErrorListDialog; 34 import org.gjt.sp.jedit.msg.VFSUpdate; 35 import org.gjt.sp.jedit.*; 36 import org.gjt.sp.util.Log; 37 import org.gjt.sp.util.WorkThreadPool; 38 import org.gjt.sp.util.StandardUtilities; 39 41 55 public class VFSManager 56 { 57 61 public static final String SERVICE = "org.gjt.sp.jedit.io.VFS"; 62 63 67 public static void init() 68 { 69 int count = jEdit.getIntegerProperty("ioThreadCount",4); 70 ioThreadPool = new WorkThreadPool("jEdit I/O",count); 71 JARClassLoader classLoader = new JARClassLoader(); 72 for(int i = 0; i < ioThreadPool.getThreadCount(); i++) 73 { 74 ioThreadPool.getThread(i).setContextClassLoader( 75 classLoader); 76 } 77 } 79 83 public static void start() 84 { 85 ioThreadPool.start(); 86 } 88 90 95 public static VFS getFileVFS() 96 { 97 return fileVFS; 98 } 100 105 public static VFS getUrlVFS() 106 { 107 return urlVFS; 108 } 110 114 public static VFS getVFSByName(String name) 115 { 116 VFS vfs = (VFS)ServiceManager.getService(SERVICE,name); 118 if(vfs == null) 119 return vfsHash.get(name); 120 else 121 return vfs; 122 } 124 130 public static VFS getVFSForProtocol(String protocol) 131 { 132 if(protocol.equals("file")) 133 return fileVFS; 134 else 135 { 136 VFS vfs = (VFS)ServiceManager.getService(SERVICE,protocol); 137 if(vfs == null) 138 vfs = protocolHash.get(protocol); 139 140 if(vfs != null) 141 return vfs; 142 else 143 return urlVFS; 144 } 145 } 147 153 public static VFS getVFSForPath(String path) 154 { 155 if(MiscUtilities.isURL(path)) 156 return getVFSForProtocol(MiscUtilities.getProtocolOfURL(path)); 157 else 158 return fileVFS; 159 } 161 166 public static void registerVFS(String protocol, VFS vfs) 167 { 168 Log.log(Log.DEBUG,VFSManager.class,"Registered " 169 + vfs.getName() + " filesystem for " 170 + protocol + " protocol"); 171 vfsHash.put(vfs.getName(),vfs); 172 protocolHash.put(protocol,vfs); 173 } 175 179 public static Enumeration<VFS> getFilesystems() 180 { 181 return vfsHash.elements(); 182 } 184 189 public static String [] getVFSs() 190 { 191 List<String > returnValue = new LinkedList<String >(); 194 String [] newAPI = ServiceManager.getServiceNames(SERVICE); 195 if(newAPI != null) 196 { 197 for(int i = 0; i < newAPI.length; i++) 198 { 199 returnValue.add(newAPI[i]); 200 } 201 } 202 Enumeration<String > oldAPI = vfsHash.keys(); 203 while(oldAPI.hasMoreElements()) 204 returnValue.add(oldAPI.nextElement()); 205 return returnValue.toArray(new String [returnValue.size()]); 206 } 208 210 212 216 public static WorkThreadPool getIOThreadPool() 217 { 218 return ioThreadPool; 219 } 221 226 public static void waitForRequests() 227 { 228 ioThreadPool.waitForRequests(); 229 } 231 235 public static boolean errorOccurred() 236 { 237 return error; 238 } 240 244 public static int getRequestCount() 245 { 246 return ioThreadPool.getRequestCount(); 247 } 249 255 public static void runInAWTThread(Runnable run) 256 { 257 ioThreadPool.addWorkRequest(run,true); 258 } 260 265 public static void runInWorkThread(Runnable run) 266 { 267 ioThreadPool.addWorkRequest(run,false); 268 } 270 272 277 public static void error(IOException e, String path, Component comp) 278 { 279 Log.log(Log.ERROR,VFSManager.class,e); 280 VFSManager.error(comp,path,"ioerror",new String [] { e.toString() }); 281 } 283 287 public static void error(final Component comp, final String error, final Object [] args) 288 { 289 if(SwingUtilities.isEventDispatchThread()) 291 { 292 GUIUtilities.error(comp,error,args); 293 return; 294 } 295 296 VFSManager.error = true; 303 304 runInAWTThread(new Runnable () 305 { 306 public void run() 307 { 308 VFSManager.error = false; 309 310 if(comp == null || !comp.isShowing()) 311 GUIUtilities.error(null,error,args); 312 else 313 GUIUtilities.error(comp,error,args); 314 } 315 }); 316 } 318 328 public static void error(Component comp, 329 final String path, 330 String messageProp, 331 Object [] args) 332 { 333 final Frame frame = JOptionPane.getFrameForComponent(comp); 334 335 synchronized(errorLock) 336 { 337 error = true; 338 339 errors.add(new ErrorListDialog.ErrorEntry( 340 path,messageProp,args)); 341 342 if(errors.size() == 1) 343 { 344 345 346 VFSManager.runInAWTThread(new Runnable () 347 { 348 public void run() 349 { 350 String caption = jEdit.getProperty( 351 "ioerror.caption" + (errors.size() == 1 352 ? "-1" : ""),new Integer [] { 353 Integer.valueOf(errors.size())}); 354 new ErrorListDialog( 355 frame.isShowing() 356 ? frame 357 : jEdit.getFirstView(), 358 jEdit.getProperty("ioerror.title"), 359 caption,errors,false); 360 errors.clear(); 361 error = false; 362 } 363 }); 364 } 365 } 366 } 368 377 public static void sendVFSUpdate(VFS vfs, String path, boolean parent) 378 { 379 if(parent) 380 { 381 sendVFSUpdate(vfs,vfs.getParentOfPath(path),false); 382 sendVFSUpdate(vfs,path,false); 383 } 384 else 385 { 386 if(path.length() != 1 && (path.endsWith("/") 388 || path.endsWith(java.io.File.separator))) 389 path = path.substring(0,path.length() - 1); 390 391 synchronized(vfsUpdateLock) 392 { 393 for(int i = 0; i < vfsUpdates.size(); i++) 394 { 395 VFSUpdate msg = vfsUpdates.get(i); 396 if(msg.getPath().equals(path)) 397 { 398 return; 401 } 402 } 403 404 vfsUpdates.add(new VFSUpdate(path)); 405 406 if(vfsUpdates.size() == 1) 407 { 408 VFSManager.runInAWTThread(new SendVFSUpdatesSafely()); 412 } 413 } 414 } 415 } 417 static class SendVFSUpdatesSafely implements Runnable 419 { 420 public void run() 421 { 422 synchronized(vfsUpdateLock) 423 { 424 Collections.sort(vfsUpdates, 430 new StandardUtilities.StringCompare() 431 ); 432 for(int i = 0; i < vfsUpdates.size(); i++) 433 { 434 EditBus.send(vfsUpdates.get(i)); 435 } 436 437 vfsUpdates.clear(); 438 } 439 } 440 } 442 444 private static WorkThreadPool ioThreadPool; 446 private static VFS fileVFS; 447 private static VFS urlVFS; 448 private static final Hashtable<String , VFS> vfsHash; 449 private static final Map<String , VFS> protocolHash; 450 private static boolean error; 451 private static final Object errorLock = new Object (); 452 private static final Vector<ErrorListDialog.ErrorEntry> errors; 453 private static final Object vfsUpdateLock = new Object (); 454 private static final List<VFSUpdate> vfsUpdates; 455 457 static 459 { 460 errors = new Vector<ErrorListDialog.ErrorEntry>(); 461 fileVFS = new FileVFS(); 462 urlVFS = new UrlVFS(); 463 vfsHash = new Hashtable<String , VFS>(); 464 protocolHash = new Hashtable<String , VFS>(); 465 vfsUpdates = new ArrayList<VFSUpdate>(10); 466 } 468 private VFSManager() {} 469 } 471 | Popular Tags |