KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 /**
23  * <p>
24  * List of shared devices.
25  */

26 public class SharedDeviceList
27 {
28
29     // Shared device list
30

31     private Hashtable JavaDoc<String JavaDoc, SharedDevice> m_shares;
32
33     /**
34      * SharedDeviceList constructor.
35      */

36     public SharedDeviceList()
37     {
38
39         // Allocate the shared device list
40

41         m_shares = new Hashtable JavaDoc<String JavaDoc, SharedDevice>();
42     }
43
44     /**
45      * Copy constructor
46      *
47      * @param shrList SharedDeviceList
48      */

49     public SharedDeviceList(SharedDeviceList shrList)
50     {
51
52         // Allocate the shared device list
53

54         m_shares = new Hashtable JavaDoc<String JavaDoc, SharedDevice>();
55
56         // Copy the shares from the original list, shallow copy
57

58         addShares(shrList);
59     }
60
61     /**
62      * Add a shared device to the list.
63      *
64      * @param shr Shared device to be added to the list.
65      * @return True if the share was added successfully, else false.
66      */

67     public final boolean addShare(SharedDevice shr)
68     {
69
70         // Check if a share with the specified name already exists
71

72         if (m_shares.containsKey(shr.getName()))
73             return false;
74
75         // Add the shared device
76

77         m_shares.put(shr.getName(), shr);
78         return true;
79     }
80
81     /**
82      * Add shares from the specified list to this list, using a shallow copy
83      *
84      * @param shrList SharedDeviceList
85      */

86     public final void addShares(SharedDeviceList shrList)
87     {
88
89         // Copy the shares to this list
90

91         Enumeration JavaDoc<SharedDevice> enm = shrList.enumerateShares();
92
93         while (enm.hasMoreElements())
94             addShare(enm.nextElement());
95     }
96
97     /**
98      * Delete the specified shared device from the list.
99      *
100      * @param name String Name of the shared resource to remove from the list.
101      * @return SharedDevice that has been removed from the list, else null.
102      */

103     public final SharedDevice deleteShare(String JavaDoc name)
104     {
105
106         // Remove the shared device from the list
107

108         return (SharedDevice) m_shares.remove(name);
109     }
110
111     /**
112      * Return an enumeration to allow the shared devices to be listed.
113      *
114      * @return Enumeration<SharedDevice>
115      */

116     public final Enumeration JavaDoc<SharedDevice> enumerateShares()
117     {
118         return m_shares.elements();
119     }
120
121     /**
122      * Find the shared device with the specified name.
123      *
124      * @param name Name of the shared device to find.
125      * @return SharedDevice with the specified name, else null.
126      */

127     public final SharedDevice findShare(String JavaDoc name)
128     {
129         return m_shares.get(name);
130     }
131
132     /**
133      * Find the shared device with the specified name and type
134      *
135      * @param name Name of shared device to find
136      * @param typ Type of shared device (see ShareType)
137      * @param nocase Case sensitive search if false, else case insensitive search
138      * @return SharedDevice with the specified name and type, else null
139      */

140     public final SharedDevice findShare(String JavaDoc name, int typ, boolean nocase)
141     {
142
143         // Enumerate the share list
144

145         Enumeration JavaDoc<String JavaDoc> keys = m_shares.keys();
146
147         while (keys.hasMoreElements())
148         {
149
150             // Get the current share name
151

152             String JavaDoc curName = keys.nextElement();
153
154             if ((nocase == false && curName.equals(name)) || (nocase == true && curName.equalsIgnoreCase(name)))
155             {
156
157                 // Get the shared device and check if the share is of the required type
158

159                 SharedDevice share = (SharedDevice) m_shares.get(curName);
160                 if (share.getType() == typ || typ == ShareType.UNKNOWN)
161                     return share;
162             }
163         }
164
165         // Required share not found
166

167         return null;
168     }
169
170     /**
171      * Return the number of shared devices in the list.
172      *
173      * @return int
174      */

175     public final int numberOfShares()
176     {
177         return m_shares.size();
178     }
179
180     /**
181      * Remove shares that have an unavailable status from the list
182      *
183      * @return int
184      */

185     public final int removeUnavailableShares()
186     {
187
188         // Check if any shares are unavailable
189

190         Enumeration JavaDoc<SharedDevice> shrEnum = enumerateShares();
191         int remCnt = 0;
192
193         while (shrEnum.hasMoreElements())
194         {
195
196             // Check if the current share is unavailable
197

198             SharedDevice shr = shrEnum.nextElement();
199             if (shr.getContext() != null && shr.getContext().isAvailable() == false)
200             {
201                 deleteShare(shr.getName());
202                 remCnt++;
203             }
204         }
205
206         // Return the count of shares removed
207

208         return remCnt;
209     }
210
211     /**
212      * Remove all shared devices from the share list
213      */

214     public final void removeAllShares()
215     {
216         m_shares.clear();
217     }
218     
219     /**
220      * Return the share list as a string
221      *
222      * @return String
223      */

224     public String JavaDoc toString()
225     {
226
227         // Create a buffer to build the string
228

229         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
230         str.append("[");
231
232         // Enumerate the shares
233

234         Enumeration JavaDoc<String JavaDoc> enm = m_shares.keys();
235
236         while (enm.hasMoreElements())
237         {
238             String JavaDoc name = enm.nextElement();
239             str.append(name);
240             str.append(",");
241         }
242
243         // Remove the trailing comma
244

245         if (str.length() > 1)
246             str.setLength(str.length() - 1);
247         str.append("]");
248
249         // Return the string
250

251         return str.toString();
252     }
253 }
Popular Tags