1 18 19 import jcifs.smb.SmbFile; 20 import java.util.LinkedList ; 21 import java.util.ListIterator ; 22 import java.net.MalformedURLException ; 23 import java.io.IOException ; 24 25 public class ThreadedSmbCrawler { 26 27 static int workingThreads = 0; 28 29 class DirEntry { 30 SmbFile dir; 31 int depth; 32 33 DirEntry( SmbFile dir, int depth ) { 34 this.dir = dir; 35 this.depth = depth; 36 } 37 } 38 39 class SmbCrawlerThread extends Thread { 40 41 StringBuffer sb = new StringBuffer (); 42 43 public void run() { 44 while( true ) { 45 try { 46 DirEntry e; 47 48 synchronized( dirList ) { 49 while( dirList.isEmpty() ) { 50 if( workingThreads == 0 ) { 52 return; } 54 dirList.wait( 2000 ); 55 } 56 e = (DirEntry)dirList.remove( 0 ); 57 if( e.depth == 0 ) { 58 continue; 59 } 60 workingThreads++; 61 } 62 63 SmbFile[] l = e.dir.listFiles(); 64 65 int n = maxDepth - e.depth; 66 67 for(int i = 0; l != null && i < l.length; i++ ) { 68 try { 69 sb.setLength( 0 ); 70 for( int k = 0; k < n; k++ ) { 71 sb.append( " " ); 72 } 73 SmbFile d = l[i]; 74 System.err.println( sb.append( d )); 75 if( d.isDirectory() ) { 76 synchronized( dirList ) { 77 dirList.add( new DirEntry( d, e.depth - 1 )); 78 dirList.notify(); 79 } 80 } 81 } catch( IOException ioe ) { 82 ioe.printStackTrace(); 83 } 84 } 85 synchronized( dirList ) { 86 workingThreads--; 87 } 88 } catch( Exception x ) { 89 synchronized( dirList ) { 90 workingThreads--; 91 } 92 x.printStackTrace(); 93 } 94 } 95 } 96 } 97 98 LinkedList dirList; 99 int maxDepth, numThreads; 100 Thread [] threads; 101 102 ThreadedSmbCrawler( String dir, int maxDepth, int numThreads ) throws Exception { 103 this.maxDepth = maxDepth; 104 this.numThreads = numThreads; 105 threads = new Thread [numThreads]; 106 dirList = new LinkedList (); 107 dirList.add( new DirEntry( new SmbFile( dir ), maxDepth )); 108 } 109 110 long go() throws Exception { 111 int i; 112 long start = System.currentTimeMillis(); 113 114 for( i = 0; i < numThreads; i++ ) { 115 threads[i] = new SmbCrawlerThread(); 116 threads[i].start(); 117 } 118 for( i = 0; i < numThreads; i++ ) { 119 threads[i].join(); 120 } 121 122 return System.currentTimeMillis() - start; 123 } 124 125 public static void main(String [] argv) throws Exception { 126 ThreadedSmbCrawler tsc; 127 128 if( argv.length < 3 ) { 129 System.err.println( "usage: ThreadedSmbCrawler dir depth numThreads" ); 130 System.exit( 1 ); 131 } 132 133 tsc = new ThreadedSmbCrawler( argv[0], Integer.parseInt( argv[1] ), Integer.parseInt( argv[2] )); 134 System.err.println( "Crawling Complete: " + (tsc.go() / 1000) + "sec" ); 135 } 136 } 137 | Popular Tags |