KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.alfresco.filesys.server.filesys;
19
20 import java.util.Enumeration JavaDoc;
21
22 import org.alfresco.config.ConfigElement;
23 import org.alfresco.filesys.server.SrvSession;
24 import org.alfresco.filesys.server.auth.ClientInfo;
25 import org.alfresco.filesys.server.auth.InvalidUserException;
26 import org.alfresco.filesys.server.config.InvalidConfigurationException;
27 import org.alfresco.filesys.server.config.ServerConfiguration;
28 import org.alfresco.filesys.server.core.ShareMapper;
29 import org.alfresco.filesys.server.core.ShareType;
30 import org.alfresco.filesys.server.core.SharedDevice;
31 import org.alfresco.filesys.server.core.SharedDeviceList;
32 import org.alfresco.filesys.smb.server.repo.ContentContext;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Home Share Mapper Class
38  *
39  * <p>Maps disk share lookup requests to the list of shares defined in the server
40  * configuration and provides a dynamic home share mapped to the users home node.
41  *
42  * @author GKSpencer
43  */

44 public class HomeShareMapper implements ShareMapper
45 {
46     // Logging
47

48     private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol");
49     
50     // Home folder share name
51

52     public static final String JavaDoc HOME_FOLDER_SHARE = "HOME";
53     
54     // Server configuration
55

56     private ServerConfiguration m_config;
57
58     // Home folder share name
59

60     private String JavaDoc m_homeShareName = HOME_FOLDER_SHARE;
61     
62     // Debug enable flag
63

64     private boolean m_debug;
65
66     /**
67      * Default constructor
68      */

69     public HomeShareMapper()
70     {
71     }
72     
73     /**
74      * Initialize the share mapper
75      *
76      * @param config ServerConfiguration
77      * @param params ConfigElement
78      * @exception InvalidConfigurationException
79      */

80     public void initializeMapper(ServerConfiguration config, ConfigElement params) throws InvalidConfigurationException
81     {
82         // Save the server configuration
83

84         m_config = config;
85         
86         // Check if the home share name has been specified
87

88         String JavaDoc homeName = params.getAttribute("name");
89         if ( homeName != null && homeName.length() > 0)
90             m_homeShareName = homeName;
91
92         // Check if debug is enabled
93

94         if (params != null && params.getChild("debug") != null)
95             m_debug = true;
96     }
97
98     /**
99      * Check if debug output is enabled
100      *
101      * @return boolean
102      */

103     public final boolean hasDebug()
104     {
105         return m_debug;
106     }
107
108     /**
109      * Return the home folder share name
110      *
111      * @return String
112      */

113     public final String JavaDoc getHomeFolderName()
114     {
115         return m_homeShareName;
116     }
117     
118     /**
119      * Return the list of available shares.
120      *
121      * @param host String
122      * @param sess SrvSession
123      * @param allShares boolean
124      * @return SharedDeviceList
125      */

126     public SharedDeviceList getShareList(String JavaDoc host, SrvSession sess, boolean allShares)
127     {
128         // Check if the user has a home folder, and the session does not currently have any
129
// dynamic shares defined
130

131         if ( sess != null && sess.hasClientInformation() && sess.hasDynamicShares() == false)
132         {
133             ClientInfo client = sess.getClientInformation();
134             if ( client.hasHomeFolder())
135             {
136                 // Create the home folder share
137

138                 DiskSharedDevice homeShare = createHomeDiskShare(client);
139                 sess.addDynamicShare(homeShare);
140                 
141                 // Debug
142

143                 if ( logger.isDebugEnabled())
144                     logger.debug("Added " + getHomeFolderName() + " share to list of shares for " + client.getUserName());
145             }
146         }
147         
148         // Make a copy of the global share list and add the per session dynamic shares
149

150         SharedDeviceList shrList = new SharedDeviceList(m_config.getShares());
151         
152         if ( sess != null && sess.hasDynamicShares()) {
153             
154             // Add the per session dynamic shares
155

156             shrList.addShares(sess.getDynamicShareList());
157         }
158           
159         // Remove unavailable shares from the list and return the list
160

161         if ( allShares == false)
162             shrList.removeUnavailableShares();
163         return shrList;
164     }
165
166     /**
167      * Find a share using the name and type for the specified client.
168      *
169      * @param host String
170      * @param name String
171      * @param typ int
172      * @param sess SrvSession
173      * @param create boolean
174      * @return SharedDevice
175      * @exception InvalidUserException
176      */

177     public SharedDevice findShare(String JavaDoc tohost, String JavaDoc name, int typ, SrvSession sess, boolean create)
178             throws Exception JavaDoc
179     {
180         
181         // Check for the special HOME disk share
182

183         SharedDevice share = null;
184         
185         if (( typ == ShareType.DISK || typ == ShareType.UNKNOWN) && name.equalsIgnoreCase(getHomeFolderName()) &&
186                     sess.getClientInformation() != null) {
187
188             // Get the client details
189

190             ClientInfo client = sess.getClientInformation();
191                         
192             // DEBUG
193

194             if ( logger.isDebugEnabled())
195                 logger.debug("Map share " + name + ", type=" + ShareType.TypeAsString(typ) + ", client=" + client);
196                 
197             // Check if the user has a home folder node
198

199             if ( client != null && client.hasHomeFolder()) {
200                 
201                 // Check if the share has already been created for the session
202

203                 if ( sess.hasDynamicShares()) {
204                     
205                     // Check if the required share exists in the sessions dynamic share list
206

207                     share = sess.getDynamicShareList().findShare(name, typ, false);
208                     
209                     // DEBUG
210

211                     if ( logger.isDebugEnabled())
212                         logger.debug(" Reusing existing dynamic share for " + name);
213                 }
214
215                 // Check if we found a share, if not then create a new dynamic share for the home directory
216

217                 if ( share == null && create == true) {
218                     
219                     // Create the home share mapped to the users home folder
220

221                     DiskSharedDevice diskShare = createHomeDiskShare(client);
222                     
223                     // Add the new share to the sessions dynamic share list
224

225                     sess.addDynamicShare(diskShare);
226                     share = diskShare;
227                     
228                     // DEBUG
229

230                     if (logger.isDebugEnabled())
231                         logger.debug(" Mapped share " + name + " to " + client.getHomeFolder());
232                 }
233             }
234             else
235                 throw new InvalidUserException("No home directory");
236         }
237         else {
238         
239             // Find the required share by name/type. Use a case sensitive search first, if that fails use a case
240
// insensitive search.
241

242             share = m_config.getShares().findShare(name, typ, false);
243             
244             if ( share == null) {
245                 
246                 // Try a case insensitive search for the required share
247

248                 share = m_config.getShares().findShare(name, typ, true);
249             }
250         }
251         
252         // Check if the share is available
253

254         if ( share != null && share.getContext() != null && share.getContext().isAvailable() == false)
255             share = null;
256         
257         // Return the shared device, or null if no matching device was found
258

259         return share;
260     }
261
262     /**
263      * Delete temporary shares for the specified session
264      *
265      * @param sess SrvSession
266      */

267     public void deleteShares(SrvSession sess)
268     {
269
270         // Check if the session has any dynamic shares
271

272         if ( sess.hasDynamicShares() == false)
273             return;
274             
275         // Delete the dynamic shares
276

277         SharedDeviceList shares = sess.getDynamicShareList();
278         Enumeration JavaDoc<SharedDevice> enm = shares.enumerateShares();
279         
280         while ( enm.hasMoreElements()) {
281
282             // Get the current share from the list
283

284             SharedDevice shr = (SharedDevice) enm.nextElement();
285             
286             // Close the shared device
287

288             shr.getContext().CloseContext();
289             
290             // DEBUG
291

292             if (logger.isDebugEnabled())
293                 logger.debug("Deleted dynamic share " + shr);
294         }
295         
296         // Clear the dynamic share list
297

298         shares.removeAllShares();
299     }
300
301     /**
302      * Close the share mapper, release any resources.
303      */

304     public void closeMapper()
305     {
306         // TODO Auto-generated method stub
307

308     }
309
310     /**
311      * Create a disk share for the home folder
312      *
313      * @param client ClientInfo
314      * @return DiskSharedDevice
315      */

316     private final DiskSharedDevice createHomeDiskShare(ClientInfo client)
317     {
318         // Create the disk driver and context
319

320         DiskInterface diskDrv = m_config.getDiskInterface();
321         DiskDeviceContext diskCtx = new ContentContext("", "", client.getHomeFolder());
322
323         // Default the filesystem to look like an 80Gb sized disk with 90% free space
324

325         diskCtx.setDiskInformation(new SrvDiskInfo(2560, 64, 512, 2304));
326         
327         // Create a temporary shared device for the users home directory
328

329         return new DiskSharedDevice(getHomeFolderName(), diskDrv, diskCtx, SharedDevice.Temporary);
330     }
331 }
332
Popular Tags