KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > repo > pseudo > ContentPseudoFileImpl


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.smb.server.repo.pseudo;
19
20 import org.alfresco.filesys.server.SrvSession;
21 import org.alfresco.filesys.server.filesys.FileName;
22 import org.alfresco.filesys.server.filesys.TreeConnection;
23 import org.alfresco.filesys.smb.server.SMBSrvSession;
24 import org.alfresco.filesys.smb.server.repo.ContentContext;
25 import org.alfresco.filesys.smb.server.repo.FileState;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Content Filesystem Driver Pseudo File Implementation
31  *
32  * <p>Pseudo file implementation for the content disk driver.
33  *
34  * @author gkspencer
35  */

36 public class ContentPseudoFileImpl implements PseudoFileInterface
37 {
38     // Logging
39

40     private static final Log logger = LogFactory.getLog(ContentPseudoFileImpl.class);
41     
42     /**
43      * Check if the specified path refers to a pseudo file
44      *
45      * @param sess SrvSession
46      * @param tree TreeConnection
47      * @param path String
48      * @return boolean
49      */

50     public boolean isPseudoFile(SrvSession sess, TreeConnection tree, String JavaDoc path)
51     {
52         // Check if the path is for a pseudo file
53

54         ContentContext ctx = (ContentContext) tree.getContext();
55         boolean isPseudo = false;
56         
57         String JavaDoc[] paths = FileName.splitPath( path);
58         FileState fstate = getStateForPath( ctx, paths[0]);
59         
60         if ( fstate != null && fstate.hasPseudoFiles())
61         {
62             // Check if there is a matching pseudo file
63

64             PseudoFile pfile = fstate.getPseudoFileList().findFile( paths[1], false);
65             if ( pfile != null)
66                 isPseudo = true;
67         }
68         
69         // Return the pseudo file status
70

71         return isPseudo;
72     }
73
74     /**
75      * Return the pseudo file for the specified path, or null if the path is not a pseudo file
76      *
77      * @param sess SrvSession
78      * @param tree TreeConnection
79      * @param path String
80      * @return PseudoFile
81      */

82     public PseudoFile getPseudoFile(SrvSession sess, TreeConnection tree, String JavaDoc path)
83     {
84         // Check if the path is for a pseudo file
85

86         ContentContext ctx = (ContentContext) tree.getContext();
87         
88         String JavaDoc[] paths = FileName.splitPath( path);
89         FileState fstate = getStateForPath( ctx, paths[0]);
90         
91         if ( fstate != null && fstate.hasPseudoFiles())
92         {
93             // Check if there is a matching pseudo file
94

95             PseudoFile pfile = fstate.getPseudoFileList().findFile( paths[1], false);
96             if ( pfile != null)
97                 return pfile;
98         }
99         
100         // Not a pseudo file
101

102         return null;
103     }
104
105     /**
106      * Add pseudo files to a folder so that they appear in a folder search
107      *
108      * @param sess SrvSession
109      * @param tree TreeConnection
110      * @param path String
111      * @return int
112      */

113     public int addPseudoFilesToFolder(SrvSession sess, TreeConnection tree, String JavaDoc path)
114     {
115         // Access the device context
116

117         int pseudoCnt = 0;
118         ContentContext ctx = (ContentContext) tree.getContext();
119         FileState fstate = getStateForPath( ctx, path);
120
121         // Check if pseudo files have already been added for this folder
122

123         if ( fstate.hasPseudoFiles())
124             return 0;
125         
126         // Check if this is a CIFS session
127

128         boolean isCIFS = sess instanceof SMBSrvSession;
129         
130         // Add the drag and drop pseudo file, if enabled
131

132         if ( isCIFS && ctx.hasDragAndDropApp())
133         {
134             // If the file state is null create a file state for the path
135

136             if ( fstate == null)
137                 ctx.getStateTable().findFileState( path, true, true);
138             
139             // Check if the folder name starts with 'DRAG', if so then enable the drag and drop pseudo file
140
// for this folder
141

142             String JavaDoc[] allPaths = FileName.splitAllPaths( path);
143             String JavaDoc lastPath = allPaths[allPaths.length - 1].toUpperCase();
144             
145             if ( lastPath.startsWith("DRAG") && fstate.hasPseudoFiles() == false)
146             {
147                 // Enable the drag and drop pseudo file
148

149                 fstate.addPseudoFile( ctx.getDragAndDropApp());
150
151                 // Update the count of pseudo files added
152

153                 pseudoCnt++;
154                 
155                 // DEBUG
156

157                 if ( logger.isInfoEnabled())
158                     logger.info("Added drag/drop pseudo file for " + path);
159             }
160         }
161
162         // Add the URL link pseudo file, if enabled
163

164         if ( isCIFS && ctx.hasURLFile())
165         {
166             // Make sure the state has the associated node details
167

168             if ( fstate.getNodeRef() != null)
169             {
170                 // Build the URL file data
171

172                 StringBuilder JavaDoc urlStr = new StringBuilder JavaDoc();
173             
174                 urlStr.append("[InternetShortcut]\r\n");
175                 urlStr.append("URL=");
176                 urlStr.append(ctx.getURLPrefix());
177                 urlStr.append("navigate/browse/workspace/SpacesStore/");
178                 urlStr.append( fstate.getNodeRef().getId());
179                 urlStr.append("\r\n");
180     
181                 // Create the in memory pseudo file for the URL link
182

183                 byte[] urlData = urlStr.toString().getBytes();
184                 
185                 MemoryPseudoFile urlFile = new MemoryPseudoFile( ctx.getURLFileName(), urlData);
186                 fstate.addPseudoFile( urlFile);
187                 
188                 // Update the count of files added
189

190                 pseudoCnt++;
191                 
192                 // DEBUG
193

194                 if ( logger.isInfoEnabled())
195                     logger.info("Added URL link pseudo file for " + path);
196             }
197         }
198         
199         // Return the count of pseudo files added
200

201         return pseudoCnt;
202     }
203
204     /**
205      * Delete a pseudo file
206      *
207      * @param sess SrvSession
208      * @param tree TreeConnection
209      * @param path String
210      */

211     public void deletePseudoFile(SrvSession sess, TreeConnection tree, String JavaDoc path)
212     {
213         // Access the device context
214

215         ContentContext ctx = (ContentContext) tree.getContext();
216         
217         // Get the file state for the parent folder
218

219         String JavaDoc[] paths = FileName.splitPath( path);
220         FileState fstate = getStateForPath( ctx, paths[0]);
221
222         // Check if the folder has any pseudo files
223

224         if ( fstate == null || fstate.hasPseudoFiles() == false)
225             return;
226
227         // Remove the pseudo file from the list
228

229         fstate.getPseudoFileList().removeFile( paths[1], false);
230     }
231     
232     /**
233      * Return the file state for the specified path
234      *
235      * @param ctx ContentContext
236      * @param path String
237      * @return FileState
238      */

239     private final FileState getStateForPath(ContentContext ctx, String JavaDoc path)
240     {
241         // Check if there is a cached state for the path
242

243         FileState fstate = null;
244         
245         if ( ctx.hasStateTable())
246         {
247             // Get the file state for a file/folder
248

249             fstate = ctx.getStateTable().findFileState(path);
250         }
251         
252         // Return the file state
253

254         return fstate;
255     }
256 }
257
Popular Tags