KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > smb > SmbTree


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 com.knowgate.jcifs.smb;
20
21 import java.io.IOException JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23
24 import com.knowgate.jcifs.UniAddress;
25 import com.knowgate.jcifs.netbios.NbtAddress;
26 import com.knowgate.jcifs.Config;
27
28 import com.knowgate.debug.DebugFile;
29
30 class SmbTree {
31
32     private static final String JavaDoc DEFAULT_SERVICE = Config.getProperty( "jcifs.smb.client.serviceType", "?????" );
33
34     private int tid;
35     private String JavaDoc share;
36
37     String JavaDoc service = DEFAULT_SERVICE;
38     SmbSession session;
39     boolean treeConnected, inDfs;
40
41     SmbTree( SmbSession session, String JavaDoc share, String JavaDoc service ) {
42         this.session = session;
43         this.share = share.toUpperCase();
44         if( service != null && service.startsWith( "??" ) == false ) {
45             this.service = service;
46         }
47     }
48
49     boolean matches( String JavaDoc share, String JavaDoc service ) {
50         return this.share.equalsIgnoreCase( share ) &&
51                 ( service == null || service.startsWith( "??" ) ||
52                 this.service.equalsIgnoreCase( service ));
53     }
54     void sendTransaction( SmbComTransaction request,
55                             SmbComTransactionResponse response ) throws SmbException {
56         // transactions are not batchable
57
treeConnect( null, null );
58         if( service.equals( "A:" ) == false ) {
59             switch( ((SmbComTransaction)request).subCommand & 0xFF ) {
60                 case SmbComTransaction.NET_SHARE_ENUM:
61                 case SmbComTransaction.NET_SERVER_ENUM2:
62                 case SmbComTransaction.NET_SERVER_ENUM3:
63                 case SmbComTransaction.TRANS_PEEK_NAMED_PIPE:
64                 case SmbComTransaction.TRANS_WAIT_NAMED_PIPE:
65                 case SmbComTransaction.TRANS_CALL_NAMED_PIPE:
66                 case SmbComTransaction.TRANS_TRANSACT_NAMED_PIPE:
67                 case SmbComTransaction.TRANS2_GET_DFS_REFERRAL:
68                     break;
69                 default:
70                     throw new SmbException( "Invalid operation for " + service + " service" );
71             }
72         }
73         request.tid = tid;
74         if( inDfs && request.path != null && request.path.length() > 0 ) {
75             request.path = '\\' + session.transport().tconHostName + '\\' + share + request.path;
76         }
77         session.sendTransaction( request, response );
78     }
79     void send( ServerMessageBlock request,
80                             ServerMessageBlock response ) throws SmbException {
81         if( response != null ) {
82             response.received = false;
83         }
84         treeConnect( request, response );
85         if( request == null || (response != null && response.received )) {
86             return;
87         }
88         if( service.equals( "A:" ) == false ) {
89             switch( request.command ) {
90                 case ServerMessageBlock.SMB_COM_OPEN_ANDX:
91                 case ServerMessageBlock.SMB_COM_NT_CREATE_ANDX:
92                 case ServerMessageBlock.SMB_COM_READ_ANDX:
93                 case ServerMessageBlock.SMB_COM_WRITE_ANDX:
94                 case ServerMessageBlock.SMB_COM_CLOSE:
95                 case ServerMessageBlock.SMB_COM_TREE_DISCONNECT:
96                     break;
97                 default:
98                     throw new SmbException( "Invalid operation for " + service + " service" );
99             }
100         }
101         request.tid = tid;
102         if( inDfs && request.path != null && request.path.length() > 0 ) {
103             request.flags2 = ServerMessageBlock.FLAGS2_RESOLVE_PATHS_IN_DFS;
104             request.path = '\\' + session.transport().tconHostName + '\\' + share + request.path;
105         }
106         session.send( request, response );
107     }
108     void treeConnect( ServerMessageBlock andx,
109                             ServerMessageBlock andxResponse ) throws SmbException {
110         String JavaDoc unc;
111 synchronized( session.transport() ) {
112
113         if( treeConnected ) {
114             return;
115         }
116
117         /* The hostname to use in the path is only known for
118          * sure if the NetBIOS session has been successfully
119          * established.
120          */

121
122         session.transport.negotiate();
123
124         unc = "\\\\" + session.transport.tconHostName + '\\' + share;
125
126         /*
127          * Tree Connect And X Request / Response
128          */

129
130         if( DebugFile.trace )
131             DebugFile.writeln( "treeConnect: unc=" + unc + ",service=" + service );
132
133         SmbComTreeConnectAndXResponse response =
134                                     new SmbComTreeConnectAndXResponse( andxResponse );
135         SmbComTreeConnectAndX request =
136                                     new SmbComTreeConnectAndX( session, unc, service, andx );
137         session.send( request, response );
138
139         tid = response.tid;
140         service = response.service;
141         inDfs = response.shareIsInDfs;
142         treeConnected = true;
143 }
144     }
145     void treeDisconnect( boolean inError ) {
146 synchronized( session.transport ) {
147         if( treeConnected == false ) {
148             return;
149         }
150         if( !inError ) {
151             try {
152                 send( new SmbComTreeDisconnect(), null );
153             } catch( SmbException se ) {
154             }
155         }
156         treeConnected = false;
157 }
158     }
159
160     public String JavaDoc toString() {
161         return "SmbTree[share=" + share +
162             ",service=" + service +
163             ",tid=" + tid +
164             ",inDfs=" + inDfs +
165             ",treeConnected=" + treeConnected + "]";
166     }
167 }
168
Popular Tags