KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > netbios > Lmhosts


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.netbios;
20
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30 import jcifs.Config;
31 import jcifs.smb.SmbFileInputStream;
32 import jcifs.util.LogStream;
33
34 public class Lmhosts {
35
36     private static final String JavaDoc FILENAME = Config.getProperty( "jcifs.netbios.lmhosts" );
37     private static final Hashtable JavaDoc TAB = new Hashtable JavaDoc();
38     private static long lastModified = 1L;
39     private static int alt;
40     private static LogStream log = LogStream.getInstance();
41
42     /**
43      * This is really just for {@link jcifs.UniAddress}. It does
44      * not throw an {@link java.net.UnknownHostException} because this
45      * is queried frequently and exceptions would be rather costly to
46      * throw on a regular basis here.
47      */

48
49     public synchronized static NbtAddress getByName( String JavaDoc host ) {
50         return getByName( new Name( host, 0x20, null ));
51     }
52
53     synchronized static NbtAddress getByName( Name name ) {
54         NbtAddress result = null;
55
56         try {
57             if( FILENAME != null ) {
58                 File JavaDoc f = new File JavaDoc( FILENAME );
59                 long lm;
60
61                 if(( lm = f.lastModified() ) > lastModified ) {
62                     lastModified = lm;
63                     TAB.clear();
64                     alt = 0;
65                     populate( new FileReader JavaDoc( f ));
66                 }
67                 result = (NbtAddress)TAB.get( name );
68             }
69         } catch( FileNotFoundException JavaDoc fnfe ) {
70             if( log.level > 1 ) {
71                 log.println( "lmhosts file: " + FILENAME );
72                 fnfe.printStackTrace( log );
73             }
74         } catch( IOException JavaDoc ioe ) {
75             if( log.level > 0 )
76                 ioe.printStackTrace( log );
77         }
78         return result;
79     }
80
81     static void populate( Reader JavaDoc r ) throws IOException JavaDoc {
82         String JavaDoc line;
83         BufferedReader JavaDoc br = new BufferedReader JavaDoc( r );
84
85         while(( line = br.readLine() ) != null ) {
86             line = line.toUpperCase().trim();
87             if( line.length() == 0 ) {
88                 continue;
89             } else if( line.charAt( 0 ) == '#' ) {
90                 if( line.startsWith( "#INCLUDE " )) {
91                     line = line.substring( line.indexOf( '\\' ));
92                     String JavaDoc url = "smb:" + line.replace( '\\', '/' );
93
94                     if( alt > 0 ) {
95                         try {
96                             populate( new InputStreamReader JavaDoc( new SmbFileInputStream( url )));
97                         } catch( IOException JavaDoc ioe ) {
98                             log.println( "lmhosts URL: " + url );
99                             ioe.printStackTrace( log );
100                             continue;
101                         }
102
103                         /* An include was loaded successfully. We can skip
104                          * all other includes up to the #END_ALTERNATE tag.
105                          */

106
107                         alt--;
108                         while(( line = br.readLine() ) != null ) {
109                             line = line.toUpperCase().trim();
110                             if( line.startsWith( "#END_ALTERNATE" )) {
111                                 break;
112                             }
113                         }
114                     } else {
115                         populate( new InputStreamReader JavaDoc( new SmbFileInputStream( url )));
116                     }
117                 } else if( line.startsWith( "#BEGIN_ALTERNATE" )) {
118                     alt++;
119                 } else if( line.startsWith( "#END_ALTERNATE" ) && alt > 0 ) {
120                     alt--;
121                     throw new IOException JavaDoc( "no lmhosts alternate includes loaded" );
122                 }
123             } else if( Character.isDigit( line.charAt( 0 ))) {
124                 char[] data = line.toCharArray();
125                 int ip, i, j;
126                 Name name;
127                 NbtAddress addr;
128                 char c;
129
130                 c = '.';
131                 ip = i = 0;
132                 for( ; i < data.length && c == '.'; i++ ) {
133                     int b = 0x00;
134
135                     for( ; i < data.length && ( c = data[i] ) >= 48 && c <= 57; i++ ) {
136                         b = b * 10 + c - '0';
137                     }
138                     ip = ( ip << 8 ) + b;
139                 }
140                 while( i < data.length && Character.isWhitespace( data[i] )) {
141                     i++;
142                 }
143                 j = i;
144                 while( j < data.length && Character.isWhitespace( data[j] ) == false ) {
145                     j++;
146                 }
147
148                 name = new Name( line.substring( i, j ), 0x20, null );
149                 addr = new NbtAddress( name, ip, false, NbtAddress.B_NODE,
150                                     false, false, true, true,
151                                     NbtAddress.UNKNOWN_MAC_ADDRESS );
152                 TAB.put( name, addr );
153             }
154         }
155     }
156 }
157
Popular Tags