KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > T2Crawler


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 jcifs.util.*;
21 import java.util.LinkedList JavaDoc;
22 import java.util.ListIterator JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 public class T2Crawler {
27
28     class Semaphore {
29         private int value = 0;
30
31         Semaphore() {value = 0;}
32         Semaphore(int initial) {value = initial;}
33
34         public synchronized void P() {
35             value--;
36             if (value < 0) {
37                 try {
38                     wait();
39                 } catch (InterruptedException JavaDoc e) {
40                     e.printStackTrace();
41                 }
42             }
43         }
44
45         public synchronized void V() {
46             value++;
47             notify();
48         }
49     }
50
51     class CrawlerThread extends Thread JavaDoc {
52         LinkedList JavaDoc list;
53         Semaphore sem;
54         SmbFile dir;
55         int depth;
56
57
58         CrawlerThread( SmbFile dir, Semaphore sem, int depth ) {
59             this.dir = dir;
60             list = new LinkedList JavaDoc();
61             list.add( dir );
62             this.sem = sem;
63             this.depth = depth;
64         }
65
66         public void run() {
67             SmbFile d;
68             SmbFile l[];
69
70             while( list.isEmpty() == false ) {
71                 int i;
72                 d = (SmbFile)list.remove( 0 );
73                 try {
74                     l = d.listFiles();
75
76 /* This is flawed. It decrements depth too agressively and causes the
77  * thread to finish prematurely. I do not know of a way to fix this
78  * because there is no concept of a stack here.
79  */

80                     depth--;
81                     for( i = 0; i < l.length; i++ ) {
82                         System.out.println( l[i] );
83                     // if( depth++ > 0 && l[i].isDirectory() && !l[i].isHidden() ) {
84
if( depth > 0 && l[i].isDirectory() ) {
85                             list.add( l[i] );
86                         }
87                     }
88                 } catch( Exception JavaDoc e ) {
89                     System.out.println( d );
90                     e.printStackTrace();
91                 }
92             }
93             sem.V();
94         }
95     }
96
97     T2Crawler( String JavaDoc dir, int numThreads, int depth ) throws Exception JavaDoc {
98         SmbFile top = new SmbFile( dir );
99         Semaphore sem = new Semaphore( numThreads );
100         SmbFile[] l = null;
101         int i = 0;
102
103         try {
104             l = top.listFiles();
105             depth--;
106             for( i = 0; i < l.length; i++ ) {
107                 try {
108                     System.out.println( l[i] );
109                     if( !l[i].isDirectory() || l[i].isHidden() ) {
110                         continue;
111                     }
112                     if( depth > 0 ) {
113                         sem.P();
114                         (new CrawlerThread( l[i], sem, depth )).start();
115                     }
116                 } catch( Exception JavaDoc e ) {
117                     e.printStackTrace();
118                 }
119             }
120             for( i = 0; i < l.length; i++ ) {
121                 try {
122                     l[i].canRead();
123                 } catch(Exception JavaDoc ex) {
124                     System.err.println( l[i] );
125                     ex.printStackTrace();
126                 }
127             }
128         } catch( Exception JavaDoc ex ) {
129             System.err.println( l[i] );
130             ex.printStackTrace();
131         }
132     }
133     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
134         if( argv.length < 3) {
135             System.out.println( "$ java -Djcifs.properties=miallen.prp T2Crawler <dir> <num threads> <depth>");
136             System.exit(1);
137         }
138         new T2Crawler( argv[0], Integer.parseInt( argv[1] ), Integer.parseInt( argv[2] ));
139     }
140 }
141
Popular Tags