KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > workareas > FileSystemWorkArea


1 package com.knowgate.workareas;
2
3 import java.lang.Exception JavaDoc;
4 import java.util.Properties JavaDoc;
5 import java.io.IOException JavaDoc;
6
7 import com.enterprisedt.net.ftp.FTPException;
8
9 import com.knowgate.debug.DebugFile;
10 import com.knowgate.dfs.FileSystem;
11
12 public class FileSystemWorkArea extends FileSystem {
13   private Properties JavaDoc oPropsCNF;
14
15   private FileSystemWorkArea() { oPropsCNF= null; }
16
17   private FileSystemWorkArea(String JavaDoc sUser, String JavaDoc sPwd) { oPropsCNF= null; }
18
19   public FileSystemWorkArea(Properties JavaDoc oEnvProps) {
20     super(oEnvProps);
21     oPropsCNF = oEnvProps;
22   }
23
24   // ---------------------------------------------------------------------------
25

26   /**
27    * <p>Create a complete directory branch under workarea root directory at /web branch</p>
28    * The given path is appended to "file://" + <i>workareasput</i> + <i>sWorkArea<</i>
29    * and the full resulting path is created if it does not exist.
30    * @param oProps Properties collection containing <i>workareasput</i> property
31    * (typically readed from file hipergate.cnf)
32    * @param sWorkArea WorkArea GUID
33    * @param sPath Relative path to be created under workarea root directory
34    * @throws IOException
35    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
36    */

37
38   private boolean mkworkpath (Properties JavaDoc oProps, String JavaDoc sWorkArea, String JavaDoc sPath)
39     throws IOException JavaDoc,IllegalArgumentException JavaDoc {
40     boolean bRetVal = false;
41
42     if (DebugFile.trace) {
43       DebugFile.writeln("Begin FileSystemWorkArea.mkworkpath(" + sPath + ")");
44       DebugFile.incIdent();
45     }
46
47     if (sWorkArea==null)
48       throw new IllegalArgumentException JavaDoc ("WorkArea GUID may not be null");
49
50     String JavaDoc sWorkAreaPath = oProps.getProperty("workareasput");
51     if (!sWorkAreaPath.endsWith(SLASH)) sWorkAreaPath += SLASH;
52
53     try {
54       if (null==sPath)
55         bRetVal = mkdirs("file://" + sWorkAreaPath + sWorkArea);
56       else
57         bRetVal = mkdirs("file://" + sWorkAreaPath + sWorkArea + SLASH + sPath);
58     }
59     catch (IOException JavaDoc ioe) { throw new IOException JavaDoc (ioe.getMessage()); }
60     catch (Exception JavaDoc e) { /* never thrown */ }
61
62     if (DebugFile.trace) {
63       DebugFile.decIdent();
64       DebugFile.writeln("End FileSystemWorkArea.mkworkpath() : " + String.valueOf(bRetVal));
65     }
66
67     return bRetVal;
68   } // mkworkpath
69

70   // ---------------------------------------------------------------------------
71

72   /**
73    * <p>Create workarea root directory at /web branch</p>
74    * @param oProps Properties collection containing <i>workareasput</i> property
75    * @param sWorkArea WorkArea GUID
76    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
77    */

78   private boolean mkworkpath (Properties JavaDoc oProps, String JavaDoc sWorkArea)
79     throws IOException JavaDoc,IllegalArgumentException JavaDoc {
80
81     return mkworkpath(oProps, sWorkArea, null);
82   }
83
84   // ---------------------------------------------------------------------------
85

86   /**
87    * <p>Create a complete directory branch under workarea root directory.</p>
88    * The given path is appended to "file://" + workareasput + <i>sWorkArea</i>
89    * and the full resulting path is created if it does not exist.
90    * @param sWorkArea WorkArea GUID
91    * @param sPath Relative path to be created under workareas root directory
92    * @throws IOException
93    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
94    */

95   public boolean mkworkpath (String JavaDoc sWorkArea, String JavaDoc sPath) throws Exception JavaDoc,IOException JavaDoc {
96     return mkworkpath(oPropsCNF, sWorkArea, sPath);
97   }
98
99   // ---------------------------------------------------------------------------
100

101   /**
102    * <p>Create workarea root directory at /web branch</p>
103    * @param oProps Properties collection containing <i>workareasput</i> property
104    * @param sWorkArea WorkArea GUID
105    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
106    */

107   public boolean mkworkpath (String JavaDoc sWorkArea)
108     throws IOException JavaDoc,IllegalArgumentException JavaDoc {
109
110     return mkworkpath(oPropsCNF, sWorkArea, null);
111   }
112
113   // ---------------------------------------------------------------------------
114

115   /**
116    * <p>Delete a directory and all its subdirectories and files
117    * under workarea root directory at /web branch.</p>
118    * The given path is appended to "file://" + <i>workareasput</i> + <i>sWorkArea</i>,
119    * the resulting directory and all its childs are deleted.
120    * @param oProps Properties collection containing workareasput property
121    * @param sWorkArea WorkArea GUID
122    * @param sPath Relative path to be created under workareas root directory
123    * @throws IOException
124    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
125    * @throws NullPointerException if "workareasput" property is not found at oProps Properties
126    */

127
128   private boolean rmworkpath (Properties JavaDoc oProps, String JavaDoc sWorkArea, String JavaDoc sPath)
129       throws IOException JavaDoc,IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
130     boolean bRetVal = false;
131
132     if (DebugFile.trace) {
133       DebugFile.writeln("Begin FileSystemWorkArea.rmworkpath(" + sPath + ")");
134       DebugFile.incIdent();
135     }
136
137     String JavaDoc sWorkAreaPath = oProps.getProperty("workareasput");
138
139     if (sWorkAreaPath==null) {
140       throw new NullPointerException JavaDoc("Cannot find property workareasput at configuration file");
141     }
142     else {
143       if (sPath == null) {
144         bRetVal = rmdir ("file://" + sWorkAreaPath);
145       }
146       else {
147         if (!sWorkAreaPath.endsWith(SLASH) && !sPath.startsWith(SLASH)) sWorkAreaPath += SLASH;
148         bRetVal = rmdir ("file://" + sWorkAreaPath + sPath);
149       }
150     }
151
152     if (DebugFile.trace) {
153       DebugFile.decIdent();
154       DebugFile.writeln("End FileSystemWorkArea.rmworkpath() : " + String.valueOf(bRetVal));
155     }
156
157     return bRetVal;
158   } // rmworkpath
159

160   // ---------------------------------------------------------------------------
161

162   /**
163    * <p>Delete a directory and all its subdirectories and files
164    * under workarea root directory at /web branch.</p>
165    * The given path is appended to "file://" + <i>workareasput</i> + <i>sWorkArea</i>,
166    * the resulting directory and all its childs are deleted.
167    * @param sWorkArea WorkArea GUID
168    * @param sPath Relative path to be created under workareas root directory
169    * @throws IOException
170    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
171    */

172
173   public boolean rmworkpath (String JavaDoc sWorkArea, String JavaDoc sPath)
174     throws IOException JavaDoc,IllegalArgumentException JavaDoc {
175     return rmworkpath(oPropsCNF, sWorkArea, sPath);
176   }
177
178   // ---------------------------------------------------------------------------
179

180   /**
181    * <p>Delete workarea root directory at /web branch.</p>
182    * The given path is appended to "file://" + <i>workareasput</i> + ,
183    * the resulting directory and all its childs are deleted.
184    * @param sWorkArea WorkArea GUID
185    * @throws IOException
186    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
187    */

188
189   public boolean rmworkpath (String JavaDoc sWorkArea)
190     throws IOException JavaDoc,IllegalArgumentException JavaDoc {
191     return rmworkpath(oPropsCNF, sWorkArea, null);
192   }
193
194   // ---------------------------------------------------------------------------
195

196   /**
197    * <p>Create a complete directory branch under storage root directory</p>
198    * The given path is appended to storage/domains/<i>iDomain</i>/workareas/<i>sWorkArea</i>/
199    * and the full resulting path is created if it does not exist.
200    * @param oProps Properties collection containing storage property
201    * (typically readed from file hipergate.cnf)
202    * @param iDomain Domain Numeric Identifier
203    * @param sWorkArea WorkArea GUID
204    * @param sPath Relative path to be created under workarea directory. For example:
205    * "ROOT/DOMAINS/TEST1/TEST1_USERS/TEST1_administrator/TEST1_administrator_temp"
206    * If sPath is <b>null</b> hen the workarea root directory itself will be created.
207    * @throws IOException
208    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
209    */

210
211   private boolean mkstorpath (Properties JavaDoc oProps, int iDomain, String JavaDoc sWorkArea, String JavaDoc sPath)
212     throws Exception JavaDoc,IOException JavaDoc,IllegalArgumentException JavaDoc {
213     boolean bRetVal = false;
214
215     if (DebugFile.trace) {
216       DebugFile.writeln("Begin FileSystemWorkArea.mkstorpath([Properties]," + String.valueOf(iDomain) + "," + sWorkArea + "," + sPath + ")");
217       DebugFile.incIdent();
218     }
219
220     if (sWorkArea==null)
221       throw new IllegalArgumentException JavaDoc("WorkArea GUID may not be null");
222
223     String JavaDoc sProtocol = oProps.getProperty("protocol", "file://");
224     String JavaDoc sWorkAreaPath = oProps.getProperty("storage");
225
226     if (sProtocol.equalsIgnoreCase("ftp://")) {
227       if (!sWorkAreaPath.endsWith("/")) sWorkAreaPath += "/";
228       sWorkAreaPath += "domains/" + String.valueOf(iDomain) + "/workareas/" + sWorkArea;
229
230       if (null==sPath)
231         bRetVal = mkdirs(sProtocol + oProps.getProperty("protocol", "localhost") + "/" + sWorkAreaPath);
232       else
233         bRetVal = mkdirs(sProtocol + oProps.getProperty("protocol", "localhost") + "/" + sWorkAreaPath + (sPath.startsWith("/") ? sPath : "/" + sPath));
234     }
235    else {
236       if (!sWorkAreaPath.endsWith(SLASH)) sWorkAreaPath += SLASH;
237
238       sWorkAreaPath += "domains" + SLASH + String.valueOf(iDomain) + SLASH + "workareas" + SLASH + sWorkArea;
239
240       if (null==sPath)
241         bRetVal = mkdirs(sProtocol + sWorkAreaPath);
242       else
243         bRetVal = mkdirs(sProtocol + sWorkAreaPath + (sPath.startsWith(SLASH) ? sPath : SLASH+sPath));
244    } // fi (sProtocol)
245

246     if (DebugFile.trace) {
247       DebugFile.decIdent();
248       DebugFile.writeln("End FileSystemWorkArea.mkstorpath() : " + String.valueOf(bRetVal));
249     }
250
251     return bRetVal;
252   } // mkworkpath
253

254   // ---------------------------------------------------------------------------
255

256   /**
257    * <p>Create a complete directory branch under storage root directory</p>
258    * The given path is appended to storage/domains/<i>iDomain</i>/workareas/<i>sWorkArea</i>/
259    * and the full resulting path is created if it does not exist.
260    * @param iDomain Domain Numeric Identifier
261    * @param sWorkArea WorkArea GUID
262    * @param sPath Relative path to be created under workarea directory. For example:
263    * "ROOT/DOMAINS/TEST1/TEST1_USERS/TEST1_administrator/TEST1_administrator_temp"
264    * If sPath is <b>null</b> hen the workarea root directory itself will be created.
265    * @throws IOException
266    * @throws IllegalArgumentException If sWorkArea is <b>null</b>
267    */

268   public boolean mkstorpath (int iDomain, String JavaDoc sWorkArea, String JavaDoc sPath) throws Exception JavaDoc,IOException JavaDoc {
269     return mkstorpath(oPropsCNF, iDomain, sWorkArea, sPath);
270   }
271
272   /**
273    * <p>Remove workarea files under /storage branch</p>
274    * @param oProps Properties collection containing storage property
275    * @param iDomain WorkArea Domain Numeric Identifier
276    * @param sWorkArea WorkArea GUID
277    * @param sPath Relative path from workarea subdirectory to delete.
278    * @throws Exception
279    * @throws IOException
280    */

281   private boolean rmstorpath (Properties JavaDoc oProps, int iDomain, String JavaDoc sWorkArea, String JavaDoc sPath) throws Exception JavaDoc,IOException JavaDoc {
282     boolean bRetVal = false;
283
284     if (DebugFile.trace) {
285       DebugFile.writeln("Begin FileSystemWorkArea.rmstorpath (" + String.valueOf(iDomain) + "," + sWorkArea + "," + (sPath==null ? "null" : sPath) + ")");
286       DebugFile.incIdent();
287     }
288
289     String JavaDoc sProtocol = oProps.getProperty("protocol", "file://");
290     String JavaDoc sWorkAreaPath = oProps.getProperty("storage");
291
292     if (sProtocol.equalsIgnoreCase("ftp://")) {
293       if (!sWorkAreaPath.endsWith("/")) sWorkAreaPath += "/";
294       sWorkAreaPath += "domains/" + String.valueOf(iDomain) + "/workareas/" + sWorkArea;
295
296       if (null==sPath)
297         bRetVal = rmdir(sProtocol + oProps.getProperty("protocol", "localhost") + "/" + sWorkAreaPath);
298       else
299         bRetVal = rmdir(sProtocol + oProps.getProperty("protocol", "localhost") + "/" + sWorkAreaPath + (sPath.startsWith("/") ? sPath : "/" + sPath));
300     }
301    else {
302       if (!sWorkAreaPath.endsWith(SLASH)) sWorkAreaPath += SLASH;
303
304       sWorkAreaPath += "domains" + SLASH + String.valueOf(iDomain) + SLASH + "workareas" + SLASH + sWorkArea;
305
306       if (null==sPath)
307         bRetVal = rmdir(sProtocol + sWorkAreaPath);
308       else
309         bRetVal = rmdir(sProtocol + sWorkAreaPath + (sPath.startsWith(SLASH) ? sPath : SLASH+sPath));
310    }
311
312     if (DebugFile.trace) {
313       DebugFile.decIdent();
314       DebugFile.writeln("End FileSystemWorkArea.rmworkpath() : " + String.valueOf(bRetVal));
315     }
316
317     return bRetVal;
318   } // rmstorpath
319

320   // ---------------------------------------------------------------------------
321

322   /**
323    * <p>Remove workarea files under /storage branch</p>
324    * @param iDomain WorkArea Domain Numeric Identifier
325    * @param sWorkArea WorkArea GUID
326    * @param sPath Relative path from workarea subdirectory to delete.
327    * @throws Exception
328    * @throws IOException
329    */

330
331   public boolean rmstorpath (int iDomain, String JavaDoc sWorkArea, String JavaDoc sPath) throws Exception JavaDoc,IOException JavaDoc {
332     return rmstorpath(oPropsCNF, iDomain, sWorkArea, sPath);
333   }
334
335   /**
336    * <p>Remove workarea files under /storage branch</p>
337    * @param iDomain WorkArea Domain Numeric Identifier
338    * @param sWorkArea WorkArea GUID
339    * @throws Exception
340    * @throws IOException
341    */

342
343   public boolean rmstorpath (int iDomain, String JavaDoc sWorkArea) throws Exception JavaDoc,IOException JavaDoc {
344     return rmstorpath(oPropsCNF, iDomain, sWorkArea, null);
345   }
346
347 }
Popular Tags