Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/io/xfile/FlexXFile.java


1   /*
2    * FlexXFile.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:38 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.io.xfile;
12  
13  import java.io.BufferedInputStream;
14  import java.io.BufferedOutputStream;
15  import java.io.BufferedReader;
16  import java.io.BufferedWriter;
17  import java.io.IOException;
18  import java.lang.reflect.Constructor;
19  
20  import com.flexstor.common.io.xfile.webnfs.WebNfsFile;
21  import com.flexstor.common.settings.Settings;
22  import com.flexstor.common.util.FlexDbServerHost;
23  import com.flexstor.common.util.StringUtil;
24  import com.flexstor.common.util.UrlAnalyzer;
25  /**
26  * This subclass of XFile overwrites some methods which otherwise will fail if:
27  * 
28  *     - The protocol used is a NFS URL and the path specified is mounted from another server
29  *     OR
30  *     - The path contains some escape codes and special characters that must be preserved.
31  * 
32  * Inside these overwriten methods we first determine if the path is a NFS URL; if it is, use
33  * the super class method; if that fails and the file is local to the JVM server we create a 
34  * new XFile instance (not FlexXFile) with a local path as argument where all special characters
35  * have been replaced.
36  *
37  * David C. 03/01/00
38  */
39  public class FlexXFile
40  
41  {
42     private FlexXFileI iFile;
43     private String sProtocol = "";
44  
45  
46     public FlexXFile(FlexXFile file, String str)
47     {
48        String sDir = file.getAbsolutePath();
49        String sFileSeparator = file.getFileSeparator();//sDir
50        
51        if ( !sDir.endsWith(sFileSeparator) )
52           sDir += sFileSeparator;
53  
54        sProtocol = UrlAnalyzer.getRemoteFileProtocol(sDir);
55        String sProtocolPackage;   
56        // If this class is instantiated within the FLEXdbServer, use the appserver's 
57        // remoteFileProtocol.package value; otherwise use the client's value. If the 
58        // protocol is UNC then use the unc protocol package
59        if ( sProtocol == null )
60        {
61           sProtocol = "UNC";
62           sProtocolPackage = "com.flexstor.common.io.xfile.unc";
63        }
64        else if ( FlexDbServerHost.isLocalHost() )
65           sProtocolPackage = Settings.getString(Settings.APP_SERVER_REMOTE_FILE_PROTOCOL_PACKAGE);
66        else
67           sProtocolPackage = Settings.getString(Settings.CLIENT_REMOTE_FILE_PROTOCOL_PACKAGE);
68                 
69        try
70        {
71           Class ifileClass = Class.forName(sProtocolPackage + "." + FlexXFile.toProper(sProtocol) + "File");
72           Class[] FlexArgs = new Class[] {java.lang.String.class};
73           Constructor ifileConstructor = ifileClass.getConstructor( FlexArgs );
74           Object[] theArgs = new Object[] { sDir + str };
75           iFile = (FlexXFileI) ifileConstructor.newInstance( theArgs );
76        }
77        catch (java.lang.ClassNotFoundException cnfe)
78        {
79           cnfe.printStackTrace();
80        }
81        catch (java.lang.NoSuchMethodException nsme)
82        {
83           nsme.printStackTrace();
84        }
85        catch (java.lang.IllegalAccessException iae)
86        {
87           iae.printStackTrace();
88        }
89        catch (java.lang.InstantiationException ie)
90        {
91           ie.printStackTrace();
92        }
93        catch (java.lang.reflect.InvocationTargetException ite)
94        {
95           ite.printStackTrace();
96        }
97     }
98  
99  
100    public FlexXFile(String str)
101    {
102       sProtocol = UrlAnalyzer.getRemoteFileProtocol(str);
103       String sProtocolPackage;   
104       // If this class is instantiated within the FLEXdbServer, use the appserver's 
105       // remoteFileProtocol.package value; otherwise use the client's value. If the 
106       // protocol is UNC then use the unc protocol package
107       if ( sProtocol == null )
108       {
109          sProtocol = "UNC";
110          sProtocolPackage = "com.flexstor.common.io.xfile.unc";
111       }
112       else if ( FlexDbServerHost.isLocalHost() )
113          sProtocolPackage = Settings.getString(Settings.APP_SERVER_REMOTE_FILE_PROTOCOL_PACKAGE);
114       else
115          sProtocolPackage = Settings.getString(Settings.CLIENT_REMOTE_FILE_PROTOCOL_PACKAGE);
116 
117       try
118       {
119          Class ifileClass = Class.forName(sProtocolPackage + "." + FlexXFile.toProper(sProtocol) + "File");
120          Class[] FlexArgs = new Class[] {java.lang.String.class};
121          Constructor ifileConstructor = ifileClass.getConstructor( FlexArgs );
122          Object[] theArgs = new Object[] { str };
123          iFile = (FlexXFileI) ifileConstructor.newInstance( theArgs );
124       }
125       catch (java.lang.ClassNotFoundException cnfe)
126       {
127          cnfe.printStackTrace();
128       }
129       catch (java.lang.NoSuchMethodException nsme)
130       {
131          nsme.printStackTrace();
132       }
133       catch (java.lang.IllegalAccessException iae)
134       {
135          iae.printStackTrace();
136       }
137       catch (java.lang.InstantiationException ie)
138       {
139          ie.printStackTrace();
140       }
141       catch (java.lang.reflect.InvocationTargetException ite)
142       {
143          ite.printStackTrace();
144       }
145    }
146    
147    public String[] extendedList()
148    {
149         return iFile.extendedList();
150    }
151    
152    public String[] extendedList(XFilenameFilter filter)
153    {
154         return iFile.extendedList(filter.getFilter(sProtocol));
155    }
156 
157    
158    public FlexXFileI getXFile()
159    {
160         return iFile;
161    }
162    
163    public BufferedInputStream getBufferedInputStream() throws IOException
164    {
165         return iFile.getBufferedInputStream();
166    }
167    
168    public BufferedInputStream getBufferedInputStream(int bufferSize) throws IOException
169    {
170         return iFile.getBufferedInputStream(bufferSize);
171    }
172    
173    public BufferedOutputStream getBufferedOutputStream(int bufferSize) throws IOException
174    {
175         return iFile.getBufferedOutputStream(bufferSize);
176    }
177    
178    public BufferedReader getBufferedReader(int bufferSize) throws IOException
179    {
180     
181         return iFile.getBufferedReader(bufferSize);
182    
183    }
184    
185    public BufferedWriter getBufferedWriter(int bufferSize) throws IOException
186    {
187         return iFile.getBufferedWriter(bufferSize);
188    }
189    
190    public BufferedOutputStream getBufferedOutputStream() throws IOException
191    {
192         return iFile.getBufferedOutputStream();
193    }
194    
195    public BufferedReader getBufferedReader() throws IOException
196    {
197         return iFile.getBufferedReader();
198    }
199    
200    public BufferedWriter getBufferedWriter() throws IOException
201    {
202         return iFile.getBufferedWriter();
203    }
204    
205    public WebNfsFile getWebNFSFile()
206    {
207             return (WebNfsFile) iFile;
208    }
209    
210    public String getPath()
211    {
212       return iFile.getPath(); 
213    }
214    
215    public String getFileSystemName()
216    {
217     return iFile.getFileSystemName();
218    }
219    
220    public String[] list(XFilenameFilter filter)
221    {
222       return iFile.list(filter.getFilter(sProtocol));
223    }
224    
225    public String[] list()
226    {
227      return iFile.list();
228    }
229 
230    public boolean mkdir()
231    {
232       return iFile.mkdir();
233 
234    }
235 
236    public boolean mkdirs()
237    {
238        return iFile.mkdirs();
239    }
240 
241    public String getName()
242    {
243        return iFile.getName();
244    }
245 
246    public String getParent()
247    {
248          return iFile.getParent();
249    }
250 
251    public boolean exists()
252    {
253          return iFile.exists();
254    }
255 
256    public boolean canRead()
257    {
258       return iFile.canRead();
259    }
260 
261    public boolean canWrite()
262    {
263          return iFile.canWrite();
264    }
265 
266    public boolean isDirectory()
267    {
268        return iFile.isDirectory();
269    }
270    
271    public boolean isFile()
272    {
273        return iFile.isFile();
274    }
275 
276    public boolean isAbsolute()
277    {
278        return iFile.isAbsolute();
279    }
280    
281    public long lastModified()
282    {
283          return iFile.lastModified();
284    }
285    
286    public long length()
287    {
288          return iFile.length();
289    }
290 
291    public boolean delete()
292    {
293        return iFile.delete();
294    }
295    
296    public boolean renameTo( FlexXFile xFile )
297    {
298         return iFile.renameTo(xFile.getXFile());
299    }
300    
301    public String getServer()
302    {
303       return iFile.getServer();
304    }
305    
306    /**
307    * Returns the local path; if this is a NFS URL, it will return the path, without the
308    * nfs://sServer portion.
309    */
310    public String getLocalPath()
311    {
312       return iFile.getLocalPath();
313    }
314    
315    /**
316    * Returns true if the files resides in this server; false otherwise
317    */
318    public boolean isLocalPath()
319    {
320       return iFile.isLocalPath();
321    }
322 
323    public String getLocalParent()
324    {
325       return iFile.getLocalParent();
326    }
327 
328    /**
329    * Returns the path of this file as a UNC (Universal Naming Convension)
330    * e.g. \\SERVER_NAME\location\filename
331    */
332    public String getUNCPath()
333    {
334       return iFile.getUNCPath();
335    }
336 
337    /**
338    * Returns the path of this file as a UNC (Universal Naming Convension), excluding the
339    * server name.
340    * e.g. \location\filename
341    */
342    public String getLocalUNCPath()
343    {
344       return iFile.getLocalUNCPath();
345    }
346 
347    public String getAbsolutePath()
348    {
349         return iFile.getAbsolutePath();
350    }
351 
352    public static String getFileSeparator(String sFile)
353    {
354       return WebNfsFile.getFileSeparator(sFile);
355    }
356 
357    /**
358     * Takes the protocol and capitalizes its first letter
359     * WebNfs is a special case which will be handled separate from the others
360     * since we need to capitalize both the 'W' and the 'N' and because the 
361     * protocol could be either webnfs or nfs.
362     * 
363     * @param s The String to capitalize
364     * @return The String with its first letter capitalized
365     */
366    public static String toProper( String sProtocol )
367    {
368       if ( sProtocol.equalsIgnoreCase("webnfs") || sProtocol.equalsIgnoreCase("nfs") )
369          return "WebNfs";
370       else
371          return StringUtil.toProper(sProtocol);
372    }
373 
374    public String getFileSeparator()
375    {
376       return iFile.getFileSeparator();   
377    }
378    
379    public String toString()
380    {
381       return iFile.toString();
382    }
383    
384 }