KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > filesys > TreeConnection


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.filesys;
18
19 import org.alfresco.filesys.server.SrvSession;
20 import org.alfresco.filesys.server.core.DeviceContext;
21 import org.alfresco.filesys.server.core.DeviceInterface;
22 import org.alfresco.filesys.server.core.InvalidDeviceInterfaceException;
23 import org.alfresco.filesys.server.core.SharedDevice;
24
25 /**
26  * The tree connection class holds the details of a single SMB tree connection. A tree connection is
27  * a connection to a shared device.
28  */

29 public class TreeConnection
30 {
31
32     // Maximum number of open files allowed per connection.
33

34     public static final int MAXFILES = 8192;
35
36     // Number of initial file slots to allocate. Number of allocated slots will be doubled
37
// when required until MAXFILES is reached.
38

39     public static final int INITIALFILES = 32;
40
41     // Shared device that the connection is associated with
42

43     private SharedDevice m_shareDev;
44
45     // List of open files on this connection. Count of open file slots used.
46

47     private NetworkFile[] m_files;
48     private int m_fileCount;
49
50     // Access permission that the user has been granted
51

52     private int m_permission;
53
54     /**
55      * Construct a tree connection using the specified shared device.
56      *
57      * @param shrDev SharedDevice
58      */

59     public TreeConnection(SharedDevice shrDev)
60     {
61         m_shareDev = shrDev;
62         m_shareDev.incrementConnectionCount();
63     }
64
65     /**
66      * Add a network file to the list of open files for this connection.
67      *
68      * @param file NetworkFile
69      * @param sess SrvSession
70      * @return int
71      */

72     public final int addFile(NetworkFile file, SrvSession sess) throws TooManyFilesException
73     {
74
75         // Check if the file array has been allocated
76

77         if (m_files == null)
78             m_files = new NetworkFile[INITIALFILES];
79
80         // Find a free slot for the network file
81

82         int idx = 0;
83
84         while (idx < m_files.length && m_files[idx] != null)
85             idx++;
86
87         // Check if we found a free slot
88

89         if (idx == m_files.length)
90         {
91
92             // The file array needs to be extended, check if we reached the limit.
93

94             if (m_files.length >= MAXFILES)
95                 throw new TooManyFilesException();
96
97             // Extend the file array
98

99             NetworkFile[] newFiles = new NetworkFile[m_files.length * 2];
100             System.arraycopy(m_files, 0, newFiles, 0, m_files.length);
101             m_files = newFiles;
102         }
103
104         // Store the network file, update the open file count and return the index
105

106         m_files[idx] = file;
107         m_fileCount++;
108         return idx;
109     }
110
111     /**
112      * Close the tree connection, release resources.
113      *
114      * @param sess SrvSession
115      */

116     public final void closeConnection(SrvSession sess)
117     {
118
119         // Make sure all files are closed
120

121         if (openFileCount() > 0)
122         {
123
124             // Close all open files
125

126             for (int idx = 0; idx < m_files.length; idx++)
127             {
128
129                 // Check if the file is active
130

131                 if (m_files[idx] != null)
132                     removeFile(idx, sess);
133             }
134         }
135
136         // Decrement the active connection count for the shared device
137

138         m_shareDev.decrementConnectionCount();
139     }
140
141     /**
142      * Return the specified network file.
143      *
144      * @return NetworkFile
145      */

146     public final NetworkFile findFile(int fid)
147     {
148
149         // Check if the file id and file array are valid
150

151         if (m_files == null || fid >= m_files.length)
152             return null;
153
154         // Get the required file details
155

156         return m_files[fid];
157     }
158
159     /**
160      * Return the length of the file table
161      *
162      * @return int
163      */

164     public final int getFileTableLength()
165     {
166         if (m_files == null)
167             return 0;
168         return m_files.length;
169     }
170
171     /**
172      * Determine if the shared device has an associated context
173      *
174      * @return boolean
175      */

176     public final boolean hasContext()
177     {
178         if (m_shareDev != null)
179             return m_shareDev.getContext() != null ? true : false;
180         return false;
181     }
182
183     /**
184      * Return the interface specific context object.
185      *
186      * @return Device interface context object.
187      */

188     public final DeviceContext getContext()
189     {
190         if (m_shareDev == null)
191             return null;
192         return m_shareDev.getContext();
193     }
194
195     /**
196      * Return the share access permissions that the user has been granted.
197      *
198      * @return int
199      */

200     public final int getPermission()
201     {
202         return m_permission;
203     }
204
205     /**
206      * Deterimine if the access permission for the shared device allows read access
207      *
208      * @return boolean
209      */

210     public final boolean hasReadAccess()
211     {
212         if (m_permission == FileAccess.ReadOnly || m_permission == FileAccess.Writeable)
213             return true;
214         return false;
215     }
216
217     /**
218      * Determine if the access permission for the shared device allows write access
219      *
220      * @return boolean
221      */

222     public final boolean hasWriteAccess()
223     {
224         if (m_permission == FileAccess.Writeable)
225             return true;
226         return false;
227     }
228
229     /**
230      * Return the shared device that this tree connection is using.
231      *
232      * @return SharedDevice
233      */

234     public final SharedDevice getSharedDevice()
235     {
236         return m_shareDev;
237     }
238
239     /**
240      * Return the shared device interface
241      *
242      * @return DeviceInterface
243      */

244     public final DeviceInterface getInterface()
245     {
246         if (m_shareDev == null)
247             return null;
248         try
249         {
250             return m_shareDev.getInterface();
251         }
252         catch (InvalidDeviceInterfaceException ex)
253         {
254         }
255         return null;
256     }
257
258     /**
259      * Check if the user has been granted the required access permission for this share.
260      *
261      * @param perm int
262      * @return boolean
263      */

264     public final boolean hasPermission(int perm)
265     {
266         if (m_permission >= perm)
267             return true;
268         return false;
269     }
270
271     /**
272      * Return the count of open files on this tree connection.
273      *
274      * @return int
275      */

276     public final int openFileCount()
277     {
278         return m_fileCount;
279     }
280
281     /**
282      * Remove all files from the tree connection.
283      */

284     public final void removeAllFiles()
285     {
286
287         // Check if the file array has been allocated
288

289         if (m_files == null)
290             return;
291
292         // Clear the file list
293

294         for (int idx = 0; idx < m_files.length; m_files[idx++] = null)
295             ;
296         m_fileCount = 0;
297     }
298
299     /**
300      * Remove a network file from the list of open files for this connection.
301      *
302      * @param idx int
303      * @param sess SrvSession
304      */

305     public final void removeFile(int idx, SrvSession sess)
306     {
307
308         // Range check the file index
309

310         if (m_files == null || idx >= m_files.length)
311             return;
312
313         // Make sure the files is closed
314

315         if (m_files[idx] != null && m_files[idx].isClosed() == false)
316         {
317
318             // Close the file
319

320             try
321             {
322
323                 // Access the disk interface and close the file
324

325                 DiskInterface disk = (DiskInterface) m_shareDev.getInterface();
326                 disk.closeFile(sess, this, m_files[idx]);
327                 m_files[idx].setClosed(true);
328             }
329             catch (Exception JavaDoc ex)
330             {
331             }
332         }
333
334         // Remove the file and update the open file count.
335

336         m_files[idx] = null;
337         m_fileCount--;
338     }
339
340     /**
341      * Set the access permission for this share that the user has been granted.
342      *
343      * @param perm int
344      */

345     public final void setPermission(int perm)
346     {
347         m_permission = perm;
348     }
349
350     /**
351      * Return the tree connection as a string.
352      *
353      * @return java.lang.String
354      */

355     public String JavaDoc toString()
356     {
357         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
358         str.append("[");
359         str.append(m_shareDev.toString());
360         str.append(",");
361         str.append(m_fileCount);
362         str.append(":");
363         str.append(FileAccess.asString(m_permission));
364         str.append("]");
365         return str.toString();
366     }
367 }
Popular Tags