KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > core > SharedDevice


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.core;
18
19 import org.alfresco.filesys.server.auth.acl.AccessControl;
20 import org.alfresco.filesys.server.auth.acl.AccessControlList;
21
22 /**
23  * <p>
24  * The shared device class is the base class for all shared device implementations.
25  */

26 public class SharedDevice implements Comparable JavaDoc
27 {
28     // Share attribute types
29

30     public static final int Admin = 0x0001;
31     public static final int Hidden = 0x0002;
32     public static final int ReadOnly = 0x0004;
33     public static final int Temporary = 0x0008;
34
35     // Shared device name
36

37     private String JavaDoc m_name;
38
39     // Shared device type
40

41     private int m_type;
42
43     // Comment
44

45     private String JavaDoc m_comment;
46
47     // Device interface and context object
48

49     private DeviceInterface m_interface;
50     private DeviceContext m_drvCtx;
51
52     // Share attributes
53

54     private int m_attrib;
55
56     // Current and maximum connections to this shared device
57

58     private int m_maxUses = -1; // unlimited
59
private int m_curUses;
60
61     // Access control list
62

63     private AccessControlList m_acls;
64
65     /**
66      * SharedDevice constructor.
67      *
68      * @param name Shared device name.
69      * @param typ Share device type, as specified by class ShareType.
70      * @param ctx Context object that will be passed to the interface.
71      */

72     protected SharedDevice(String JavaDoc name, int typ, DeviceContext ctx)
73     {
74
75         // Set the shared name and device type
76

77         setName(name);
78         setType(typ);
79         setContext(ctx);
80     }
81
82     /**
83      * Return the shared device attribtues.
84      *
85      * @return int
86      */

87     public final int getAttributes()
88     {
89         return m_attrib;
90     }
91
92     /**
93      * Determine if the shared device has any access controls configured
94      *
95      * @return boolean
96      */

97     public final boolean hasAccessControls()
98     {
99         if (m_acls == null)
100             return false;
101         return true;
102     }
103
104     /**
105      * Return the access control list
106      *
107      * @return AccessControlList
108      */

109     public final AccessControlList getAccessControls()
110     {
111         return m_acls;
112     }
113
114     /**
115      * Check if the shared device has a comment
116      *
117      * @return boolean
118      */

119     public final boolean hasComment()
120     {
121         return m_comment != null ? true : false;
122     }
123
124     /**
125      * Return the shared device comment.
126      *
127      * @return java.lang.String
128      */

129     public final String JavaDoc getComment()
130     {
131         return m_comment;
132     }
133
134     /**
135      * Return the device interface specific context object.
136      *
137      * @return Device context.
138      */

139     public final DeviceContext getContext()
140     {
141         return m_drvCtx;
142     }
143
144     /**
145      * Return the device interface for this shared device.
146      *
147      * @return DeviceInterface
148      */

149     public DeviceInterface getInterface() throws InvalidDeviceInterfaceException
150     {
151         return m_interface;
152     }
153
154     /**
155      * Return the shared device name.
156      *
157      * @return java.lang.String
158      */

159     public final String JavaDoc getName()
160     {
161         return m_name;
162     }
163
164     /**
165      * Return the shared device type, as specified by the ShareType class.
166      *
167      * @return int
168      */

169     public int getType()
170     {
171         return m_type;
172     }
173
174     /**
175      * Return the current connection count for the share
176      *
177      * @return int
178      */

179     public final int getCurrentConnectionCount()
180     {
181         return m_curUses;
182     }
183
184     /**
185      * Return the maximum connection count for the share
186      *
187      * @return int
188      */

189     public final int getMaximumConnectionCount()
190     {
191         return m_maxUses;
192     }
193
194     /**
195      * Generates a hash code for the receiver. This method is supported primarily for hash tables,
196      * such as those provided in java.util.
197      *
198      * @return an integer hash code for the receiver
199      * @see java.util.Hashtable
200      */

201     public int hashCode()
202     {
203
204         // Use the share name to generate the hash code.
205

206         return getName().hashCode();
207     }
208
209     /**
210      * Determine if this is an admin share.
211      *
212      * @return boolean
213      */

214     public final boolean isAdmin()
215     {
216         return (m_attrib & Admin) == 0 ? false : true;
217     }
218
219     /**
220      * Determine if this is a hidden share.
221      *
222      * @return boolean
223      */

224     public final boolean isHidden()
225     {
226         return (m_attrib & Hidden) == 0 ? false : true;
227     }
228
229     /**
230      * Determine if the share is read-only.
231      *
232      * @return boolean
233      */

234     public final boolean isReadOnly()
235     {
236         return (m_attrib & ReadOnly) == 0 ? false : true;
237     }
238
239     /**
240      * Determine if the share is a temporary share
241      *
242      * @return boolean
243      */

244     public final boolean isTemporary()
245     {
246         return (m_attrib & Temporary) == 0 ? false : true;
247     }
248
249     /**
250      * Set the shared device comment string.
251      *
252      * @param comm java.lang.String
253      */

254     public final void setComment(String JavaDoc comm)
255     {
256         m_comment = comm;
257     }
258
259     /**
260      * Set the shared device attributes.
261      *
262      * @param attr int
263      */

264     public final void setAttributes(int attr)
265     {
266         m_attrib = attr;
267     }
268
269     /**
270      * Set the context that is passed to the device interface.
271      *
272      * @param ctx DeviceContext
273      */

274     protected void setContext(DeviceContext ctx)
275     {
276         m_drvCtx = ctx;
277     }
278
279     /**
280      * Set the device interface for this shared device.
281      *
282      * @param iface DeviceInterface
283      */

284     protected final void setInterface(DeviceInterface iface)
285     {
286         m_interface = iface;
287     }
288
289     /**
290      * Set the shared device name.
291      *
292      * @param name java.lang.String Shared device name.
293      */

294     protected final void setName(String JavaDoc name)
295     {
296         m_name = name;
297     }
298
299     /**
300      * Set the shared device type.
301      *
302      * @param typ int Shared device type, as specified by class ShareType.
303      */

304     protected final void setType(int typ)
305     {
306         m_type = typ;
307     }
308
309     /**
310      * Set the maximum connection coutn for this shared device
311      *
312      * @param maxConn int
313      */

314     public final void setMaximumConnectionCount(int maxConn)
315     {
316         m_maxUses = maxConn;
317     }
318
319     /**
320      * Set the access control list using the specified list
321      *
322      * @param acls AccessControlList
323      */

324     public final void setAccessControlList(AccessControlList acls)
325     {
326         m_acls = acls;
327     }
328
329     /**
330      * Add an access control to the shared device
331      *
332      * @param acl AccessControl
333      */

334     public final void addAccessControl(AccessControl acl)
335     {
336
337         // Check if the access control list has been allocated
338

339         if (m_acls == null)
340             m_acls = new AccessControlList();
341
342         // Add the access control
343

344         m_acls.addControl(acl);
345     }
346
347     /**
348      * Remove an access control
349      *
350      * @param idx int
351      * @return AccessControl
352      */

353     public final AccessControl removeAccessControl(int idx)
354     {
355
356         // validate the index
357

358         if (m_acls == null || idx < 0 || idx >= m_acls.numberOfControls())
359             return null;
360
361         // Remove the access control
362

363         return m_acls.removeControl(idx);
364     }
365
366     /**
367      * Remove all access controls from this shared device
368      */

369     public final void removeAllAccessControls()
370     {
371         if (m_acls != null)
372         {
373             m_acls.removeAllControls();
374             m_acls = null;
375         }
376     }
377
378     /**
379      * Parse and validate the parameters string and create a device context for the shared device.
380      *
381      * @param args String[]
382      * @return DeviceContext
383      */

384     public DeviceContext createContext(String JavaDoc[] args)
385     {
386         return new DeviceContext(args[0]);
387     }
388
389     /**
390      * Increment the connection count for the share
391      */

392     public synchronized void incrementConnectionCount()
393     {
394         m_curUses++;
395     }
396
397     /**
398      * Decrement the connection count for the share
399      */

400     public synchronized void decrementConnectionCount()
401     {
402         m_curUses--;
403     }
404
405     /**
406      * Compare this shared device to another shared device using the device name
407      *
408      * @param obj Object
409      */

410     public int compareTo(Object JavaDoc obj)
411     {
412         if (obj instanceof SharedDevice)
413         {
414             SharedDevice sd = (SharedDevice) obj;
415             return getName().compareTo(sd.getName());
416         }
417         return -1;
418     }
419
420     /**
421      * Compares two objects for equality. Returns a boolean that indicates whether this object is
422      * equivalent to the specified object. This method is used when an object is stored in a
423      * hashtable.
424      *
425      * @param obj the Object to compare with
426      * @return true if these Objects are equal; false otherwise.
427      * @see java.util.Hashtable
428      */

429     public boolean equals(Object JavaDoc obj)
430     {
431
432         // Check if the object is a SharedDevice
433

434         if (obj instanceof SharedDevice)
435         {
436
437             // Check if the share names are equal
438

439             SharedDevice shr = (SharedDevice) obj;
440             if (getName().compareTo(shr.getName()) == 0)
441                 return true;
442         }
443
444         // Object type, or share name is not equal
445

446         return false;
447     }
448
449     /**
450      * Returns a String that represents the value of this object.
451      *
452      * @return a string representation of the receiver
453      */

454     public String JavaDoc toString()
455     {
456
457         // Build a string that represents this shared device
458

459         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
460         str.append("[");
461         str.append(getName());
462         str.append(",");
463         str.append(ShareType.TypeAsString(getType()));
464         str.append(",");
465
466         if (hasAccessControls())
467         {
468             str.append("ACLs=");
469             str.append(m_acls.numberOfControls());
470         }
471
472         if (isAdmin())
473             str.append(",Admin");
474
475         if (isHidden())
476             str.append(",Hidden");
477
478         if (isReadOnly())
479             str.append(",ReadOnly");
480
481         if (isTemporary())
482             str.append(",Temp");
483
484         if (getContext() != null && getContext().isAvailable() == false)
485             str.append(",Offline");
486
487         if (m_drvCtx != null)
488         {
489             str.append(",");
490             str.append(m_drvCtx.toString());
491         }
492         str.append("]");
493
494         return str.toString();
495     }
496 }
Popular Tags