KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ThreadedSmbCrawler


1 /* examples for the jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */

18
19 import jcifs.smb.SmbFile;
20 import java.util.LinkedList JavaDoc;
21 import java.util.ListIterator JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.io.IOException JavaDoc;
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 JavaDoc {
40
41         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
42
43         public void run() {
44             while( true ) {
45                 try {
46                     DirEntry e;
47
48                     synchronized( dirList ) {
49                         while( dirList.isEmpty() ) {
50 //System.err.println( "workingThreads=" + workingThreads );
51
if( workingThreads == 0 ) {
52                                 return; // done
53
}
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 JavaDoc ioe ) {
82                             ioe.printStackTrace();
83                         }
84                     }
85                     synchronized( dirList ) {
86                         workingThreads--;
87                     }
88                 } catch( Exception JavaDoc x ) {
89                     synchronized( dirList ) {
90                         workingThreads--;
91                     }
92                     x.printStackTrace();
93                 }
94             }
95         }
96     }
97
98     LinkedList JavaDoc dirList;
99     int maxDepth, numThreads;
100     Thread JavaDoc[] threads;
101
102     ThreadedSmbCrawler( String JavaDoc dir, int maxDepth, int numThreads ) throws Exception JavaDoc {
103         this.maxDepth = maxDepth;
104         this.numThreads = numThreads;
105         threads = new Thread JavaDoc[numThreads];
106         dirList = new LinkedList JavaDoc();
107         dirList.add( new DirEntry( new SmbFile( dir ), maxDepth ));
108     }
109
110     long go() throws Exception JavaDoc {
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 JavaDoc[] argv) throws Exception JavaDoc {
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