Source code: com/flexstor/common/io/xfile/XFileAccessorFactory.java
1 /*
2 * XFileAccessorFactory.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 com.flexstor.common.io.FileAccessProtocol;
14 import com.flexstor.common.io.xfile.webnfs.WebNfsCopy;
15 import com.flexstor.common.settings.Settings;
16 import com.flexstor.common.util.FlexDbServerHost;
17 import com.flexstor.common.util.UrlAnalyzer;
18
19 /**
20 * XFileAccessor verifies that a file can be reached.
21 * It checks that the file protocol is valid and that credentials, if set, are also valid.
22 */
23 public class XFileAccessorFactory
24 {
25 protected static final int COPY = 1;
26 protected static final int DELETE = 2;
27 protected static final int MOVE = 3;
28 protected static final String UNC_PROTOCOL = "UNC";
29 protected static final String UNC_PROTOCOL_PACKAGE = "com.flexstor.common.io.xfile.unc.";
30 protected static final String BASE_PROTOCOL_PACKAGE;
31 static {
32 // If this class is instantiated within the FLEXdbServer, use the appserver's
33 // remoteFileProtocol.package value; otherwise use the client's value
34 String sBasePackage;
35 if ( FlexDbServerHost.isLocalHost() )
36 sBasePackage = Settings.getString(Settings.APP_SERVER_REMOTE_FILE_PROTOCOL_PACKAGE);
37 else
38 sBasePackage = Settings.getString(Settings.CLIENT_REMOTE_FILE_PROTOCOL_PACKAGE);
39
40 BASE_PROTOCOL_PACKAGE = sBasePackage.substring(0, sBasePackage.lastIndexOf('.') + 1);
41 }
42
43 private String sUser;
44 private String sPassword;
45 private String sValidatedUser = "";
46 private String sValidatedPassword = "";
47 private String sValidatedHost = "";
48 private String sProtocol = UNC_PROTOCOL;
49 private String sProtocolPackage = UNC_PROTOCOL_PACKAGE;
50 private boolean bCredentialsSet;
51 private boolean bCredentialsValidated;
52 private int nOperation;
53
54 protected XFileAccessorI fAccessor;
55
56 protected XFileAccessorFactory( int nOperation )
57 {
58 this.nOperation = nOperation;
59 }
60
61 /**
62 * Set the credentials for deleting a file. Before deleting the file, this class attempts
63 * to validate the credentials with the file system. The credentials will be checked once,
64 * unless they are changed by invoking this method again.
65 */
66 public void setCredentials( String sUser, String sPassword )
67 {
68 this.sUser = sUser;
69 this.sPassword = sPassword;
70 this.bCredentialsSet = true;
71 }
72
73 public void disconnect()
74 {
75 fAccessor.disconnect();
76 }
77
78 /**
79 * This method validates the operation if the file protocol is valid and if the credentials
80 * set are valid.
81 */
82 protected boolean validateOperation( String sPath )
83 {
84 if ( validateProtocol(sPath) && validateCredentials(sPath) )
85 {
86 System.out.println( "#### " + (sProtocol.equals(UNC_PROTOCOL) ? "local" : sProtocol) + " " + getOperation() + " validated." );
87 return true;
88 }
89 else
90 {
91 System.out.println( "#### " + (sProtocol.equals(UNC_PROTOCOL) ? "local" : sProtocol) + " " + getOperation() + " not validated." );
92 return false;
93 }
94 }
95
96 protected boolean validateOperation( String sPath1, String sPath2 )
97 {
98 String sProtocolOne = UrlAnalyzer.getRemoteFileProtocol( sPath1 );
99 // If protocol is null, it is a local path
100 if ( sProtocolOne == null )
101 sProtocolOne = FileAccessProtocol.FILE;
102
103 String sProtocolTwo = UrlAnalyzer.getRemoteFileProtocol( sPath2 );
104 if ( sProtocolTwo == null )
105 sProtocolTwo = FileAccessProtocol.FILE;
106
107 // TEMPORAY!!!!
108 // The FtpCopy class cannot handle the case when both files are ftp urls (what an irony!!!!)
109 // This case is handled by the WebNfsCopy class.
110 if ( sProtocolOne.equals( FileAccessProtocol.FTP ) &&
111 sProtocolTwo.equals( FileAccessProtocol.FTP ) &&
112 nOperation == COPY )
113 {
114 sProtocol = FileAccessProtocol.NFS;
115 fAccessor = new WebNfsCopy();
116 if ( validateCredentials( sPath1 ) && validateCredentials( sPath2 ) )
117 return true;
118 else
119 return false;
120 }
121 else
122 {
123 boolean bIsRemote = FileAccessProtocol.isRemoteProtocol( sProtocolOne );
124 // If the protocol is remote, do not bother checking the next path, the operation is remote.
125 if ( bIsRemote )
126 return validateOperation( sPath1 );
127 else
128 // Do not check if protocol is remote, at this point it doesn't matter; assuming protocol
129 // of sPath1 was local, sPath2 will determine the right accessor instance for this operation.
130 return validateOperation( sPath2 );
131 }
132
133
134 }
135
136 /**
137 * Validates the protocol
138 */
139 private boolean validateProtocol( String sPath )
140 {
141 setInstance(sPath);
142 if ( fAccessor == null )
143 return false;
144 else
145 return true;
146 }
147
148 /**
149 * Validates the credentials against the remote server
150 */
151 private boolean validateCredentials( String sPath )
152 {
153 // If credentials are not set, return true. We will assume that the file protocol in use
154 // does not requires authentication.
155 if ( bCredentialsSet )
156 {
157 String sHost = UrlAnalyzer.getServerDNS(sPath);
158
159 // First check if this credentials were already validated; if they were return true
160 if ( sHost.equals(sValidatedHost) &&
161 sUser.equals(sValidatedUser) &&
162 sPassword.equals(sValidatedPassword) &&
163 bCredentialsValidated )
164 return true;
165
166 sValidatedHost = sHost;
167 sValidatedUser = sUser;
168 sValidatedPassword = sPassword;
169
170 if ( !fAccessor.connect( sHost, sUser, sPassword ) )
171 {
172 bCredentialsValidated = false;
173 return false;
174 }
175 else
176 {
177 bCredentialsValidated = true;
178 return true;
179 }
180 }
181 else
182 return true;
183 }
184
185 /**
186 * Sets fAccessor to the proper instance of XFileDeleteI depending on the file protocol specified.
187 */
188 private void setInstance( String sPath )
189 {
190 String sNextProtocol = UrlAnalyzer.getRemoteFileProtocol(sPath);
191 // If protocol is null, it is a local path
192 if ( sNextProtocol == null )
193 sNextProtocol = UNC_PROTOCOL;
194
195 // If this protocol is not equal to the one already defined, set fAccessor to a new instance of
196 // XFileDeleteI, otherwise use the instance already defined.
197 if ( sNextProtocol != sProtocol || fAccessor == null )
198 {
199 sProtocol = sNextProtocol;
200 if ( sProtocol.equals(UNC_PROTOCOL) )
201 sProtocolPackage = UNC_PROTOCOL_PACKAGE;
202 else
203 sProtocolPackage = BASE_PROTOCOL_PACKAGE + sProtocol.toLowerCase() + '.';
204
205 try
206 {
207 switch ( nOperation )
208 {
209 case COPY:
210 Class iCopyClass = Class.forName(sProtocolPackage + FlexXFile.toProper(sProtocol) + "Copy");
211 fAccessor = (XFileAccessorI) iCopyClass.newInstance();
212 System.out.println( "#### Getting a " + sProtocol.toUpperCase() + "Copy." );
213 break;
214 case DELETE:
215 Class iDeleteClass = Class.forName(sProtocolPackage + FlexXFile.toProper(sProtocol) + "Delete");
216 fAccessor = (XFileAccessorI) iDeleteClass.newInstance();
217 System.out.println( "#### Getting a " + sProtocol.toUpperCase() + "Delete." );
218 break;
219 case MOVE:
220 Class iMoveClass = Class.forName(sProtocolPackage + FlexXFile.toProper(sProtocol) + "Move");
221 fAccessor = (XFileAccessorI) iMoveClass.newInstance();
222 System.out.println( "#### Getting a " + sProtocol.toUpperCase() + "Move." );
223 break;
224 }
225 }
226 catch (java.lang.ClassNotFoundException cnfe)
227 {
228 cnfe.printStackTrace();
229 }
230 catch (java.lang.IllegalAccessException iae)
231 {
232 iae.printStackTrace();
233 }
234 catch (java.lang.InstantiationException ie)
235 {
236 ie.printStackTrace();
237 }
238 }
239 }
240
241 private String getOperation()
242 {
243 switch ( nOperation )
244 {
245 case COPY:
246 return "Copy";
247 case DELETE:
248 return "Delete";
249 case MOVE:
250 return "Move";
251 default:
252 return "????";
253 }
254 }
255 }