001 /*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016 package org.opengion.fukurou.util;
017
018 import java.io.File;
019 import java.io.IOException;
020
021 /**
022 * AbstractConnect.java は、å?通的に使用されã‚?ファイル伝é?関連の基本機è?を実è£?�—たã?Abstractクラスですã?
023 *
024 * -host=サーバã? -user=ユーザー -passwd=パスワーãƒ?-remoteFile=接続å?のファイルå�?をå¿??設定しますã?
025 * -localFile=ローカルのファイル名ã?、å¿??ではありませんがã?-command=DEL の場合にのみ不要でありã€?
026 * それ以外ã? command の場合ã?、å¿?¦�ですã?
027 *
028 * -command=[GET/PUT/DEL/GETDIR/PUTDIR/DELDIR] は、サーバã?に対しての処ç�??方法をæŒ?®šしますã?
029 * GET:サーバã?からローカルにファイル転送しまã�?初期値)
030 * PUT:ローカルファイルをサーバã?に PUT(STORE、SAVE、UPLOAD、などと同意èª?しますã?
031 * DEL:サーバã?のæŒ?®šã?ファイルを削除しますã?こã?場合ã?みã€?localFile 属æ?のæŒ?®šã?不要ですã?
032 * GETDIR,PUTDIR,DELDIR:æŒ?®šã?フォルãƒ?»¥下ã?ファイルをå?ç�?�—ますã?
033 *
034 * -mkdirs=[true/false] は、受けå?のファイル(GETæ™?LOCAL、PUTæ™?サーバã?)に取り込ã‚?ƒ•ァイルのãƒ?‚£レクトリã�?
035 * 存在しなã�??合に、作æ?するかどã�?�‹を指定しまã�?初期値:true)
036 * 通常、サーバã?に、フォルãƒ?šŽ層を作æ?してPUTする場合ã?動的にフォルãƒ?šŽ層を作æ?したã�?‚±ースで便利ですã?
037 * é€?�«、フォルãƒ??確定しており、指定フォルãƒ?»¥外に PUT するのはバグってã�?‚‹と事がåˆ?�‹ってã�?‚‹場合には
038 * false に設定して、存在しなã�?ƒ•ォルãƒ?�«PUT しよã�?�¨すると、エラーになるよã�?�«しますã?
039 *
040 * 引数æ–?—å?中に空白を含ã‚??合ã?、ダブルコーãƒ??ション("") で括って下さã�??
041 * 引数æ–?—å?の ã€?』ã?前後には、空白は挟めません。å¿?�šã€?key=value の様に
042 * 繋げてくださいã€?
043 *
044 * @og.formSample
045 * XXXConnect -host=サーバã? -user=ユーザー -passwd=パスワーãƒ?-remoteFile=接続å?のファイルå�?[-localFile=ローカルのファイル名]
046 * [-command=[GET/PUT/DEL/GETDIR/PUTDIR/DELDIR] ] [-display=[true/false] ] ・・・・
047 *
048 * -host=サーバã? ?š接続å?のサーバã?のアドレスまたã?、サーバã?å�?
049 * -user=ユーザー ?š接続するユーザーå�?
050 * -passwd=パスワーãƒ? ?š接続するユーザーのパスワーãƒ?
051 * -remoteFile=接続å?のファイルå�? ?š接続å?のサーバã?側のファイル名ã?PUT,GET 関係なくFTP側としてæŒ?®šしますã?
052 * [-localFile=ローカルのファイル名] ?šローカルのファイル名ã?PUT,GET 関係なくローカルファイルを指定しますã?
053 * [-port=ポã?ãƒ?] ?š接続するサーバã?のポã?トをæŒ?®šしますã?
054 * [-command=[GET/PUT/DEL] ] ?šサーバã?側での処ç�??方法をæŒ?®šしますã?
055 * [GETDIR/PUTDIR/DELDIR]] GET:FTP⇒LOCAL、PUT:LOCAL⇒FTP への転送でã�?初期値:GET)
056 * DEL:FTPファイルを削除しますã?
057 * GETDIR,PUTDIR,DELDIR æŒ?®šã?フォルãƒ?»¥下ã?ファイルをå?ç�?�—ますã?
058 * [-mkdirs=[true/false] ] ?š受けå?ファイル(GETæ™?LOCAL、PUTæ™?サーバã?)にãƒ?‚£レクトリを作æ?するかどã�?�‹(初期値:true)
059 * (false:ãƒ?‚£レクトリが無ければ、エラーにしますã?)
060 * [-encode=エンコード名 ] ?š日本語ファイル名などのエンコード名を指定しまã�?初期値:Windows-31J)
061 * [-timeout=タイãƒ?‚¢ウãƒ?秒] ] ?šDataタイãƒ?‚¢ウãƒ?初期値:600 [秒])
062 * [-display=[false/true] ] ?štrueは、検索状況を表示しまã�?初期値:false)
063 * [-debug=[false|true] ] ?šデバッグæƒ??を標準å?力に表示する(true)かしなã�?false)ã�?初期値:false[表示しない])
064 *
065 * @og.rev 5.1.6.0 (2010/05/01) 新規追åŠ?
066 *
067 * @version 5.0
068 * @author Kazuhiko Hasegawa
069 * @since JDK5.0,
070 */
071 public abstract class AbstractConnect implements ConnectIF {
072 public static final String CR = System.getProperty("line.separator");
073 private final StringBuilder errMsg = new StringBuilder( 200 );
074
075 /** 正常フラグ {@value} */
076 public static final boolean FLAG_OK = true;
077 /** 異常フラグ {@value} */
078 public static final boolean FLAG_NG = false;
079 /** Dataタイãƒ?‚¢ウãƒ?初期値:{@value} [秒]) */
080 public static final int TIMEOUT = 600 ;
081
082 /** サーバã? */
083 protected String host = null; // サーバã?
084 /** ユーザー */
085 protected String user = null; // ユーザー
086 /** パスワーãƒ?*/
087 protected String passwd = null; // パスワーãƒ?
088 /** ポã?ãƒ?*/
089 protected String port = null; // ポã?ãƒ?
090
091 /** ãƒ?‚£レクトリを作æ?するかどã�?�‹ */
092 protected boolean isMkdirs = true; // 受け側ファイルにãƒ?‚£レクトリを作æ?するかどã�?�‹。true:作æ?する / false:無ければエラー
093 /** Dataタイãƒ?‚¢ウãƒ? */
094 protected int timeout = TIMEOUT; // Dataタイãƒ?‚¢ウãƒ?初期値:600 [秒])
095 /** 検索状況を表示するかどã�?�‹ */
096 protected boolean isDisplay = false; // trueは、検索状況を表示しまã�?初期値:false)
097 /** ãƒ?ƒ�ãƒ?‚°æƒ??を表示するかどã�?�‹ */
098 protected boolean isDebug = false; // ãƒ?ƒ�ãƒ?‚°æƒ??を標準å?力に表示する(true)かしなã�?false)ã�?
099
100 /**
101 * サーバã?への接続ã?ログインを行いますã?
102 */
103 @Override
104 public abstract void connect() ;
105
106 /**
107 * command , localFile , remoteFile をå?に、FTP処ç�?‚’行いますã?
108 *
109 * こã?メソãƒ?ƒ‰は、connect( String , String , String )メソãƒ?ƒ‰、およã?ã€?
110 * paramInit() 実行後に、呼び出すå¿?¦�がありますã?
111 *
112 * ※ å†?ƒ¨で、command にæŒ?®šできなã�??をセãƒ?ƒˆしたかã?何らかã?エラーが発生したå?合ã?
113 *
114 * @param command GET:HOST⇒LOCAL 、PUT:LOCAL⇒HOST 、DEL:HOSTファイルを削除
115 * @param localFile ローカルのファイルå�?
116 * @param remoteFile HOST接続å?のファイルå�?
117 */
118 @Override
119 public void action( final String command, final String localFile, final String remoteFile ) {
120 String rmtFile = remoteFile.replace( '\\','/' );
121 if( isDisplay ) {
122 System.out.println( "ACTION: command=" + command + ",localFile=" + localFile + ",remoteFile=" + rmtFile );
123 }
124
125 try {
126 // 実際の処ç�?‚’行いますã?(GET/PUT/DEL)
127 if( "GET".equals( command ) ) {
128 actionGET( localFile, rmtFile );
129 }
130 else if( "PUT".equals( command ) ) {
131 actionPUT( localFile, rmtFile );
132 }
133 else if( "DEL".equals( command ) ) {
134 actionDEL( rmtFile );
135 }
136 else if( "GETDIR".equals( command ) ) {
137 actionGETdir( localFile, rmtFile );
138 }
139 else if( "PUTDIR".equals( command ) ) {
140 actionPUTdir( localFile, rmtFile );
141 }
142 else if( "DELDIR".equals( command ) ) {
143 actionDELdir( rmtFile );
144 }
145 else {
146 errAppend( "commandは、GET/PUT/DEL/GETDIR/PUTDIR/DELDIR からæŒ?®šくã�?�•ã�??" );
147 errAppend( " command = [" , command , "]" );
148 throw new RuntimeException( getErrMsg() );
149 }
150 }
151 catch( Exception ex ) {
152 errAppend( "Server action Error." );
153 errAppend( " command = [" , command , "]" );
154 errAppend( " localFile = [" , localFile , "]" );
155 errAppend( " remoteFile = [" , remoteFile , "]" );
156 errAppend( ex.getMessage() );
157 if( isDebug ) { ex.printStackTrace(); }
158 throw new RuntimeException( getErrMsg(),ex );
159 }
160 }
161
162 /**
163 * サーバã?との接続をクローズしますã?
164 *
165 * ログインされてã�?‚‹場合ã?、ログアウトも行いますã?
166 * コネクトされてã�?‚‹場合ã?、ディスコネクトしますã?
167 */
168 @Override
169 public abstract void disconnect();
170
171 /**
172 * command="GET" が指定されたときã?処ç�?‚’行いますã?
173 *
174 * 接続å?のサーバã?側のファイル名をローカルにãƒ?‚¦ンロードしますã?
175 *
176 * @param localFile ローカルのファイルå�?
177 * @param remoteFile 接続å?のファイルå�?
178 * @throws Exception 何らかã?エラーが発生したå?合ã?
179 */
180 protected abstract void actionGET( final String localFile, final String remoteFile ) throws Exception ;
181
182 /**
183 * command="GETDIR" が指定されたときã?処ç�?‚’行いますã?
184 *
185 * 接続å?のサーバã?側のãƒ?‚£レクトリ以下をローカルãƒ?‚£レクトリに階層構é?のままãƒ?‚¦ンロードしますã?
186 *
187 * @param localDir ローカルのãƒ?‚£レクトリå�?
188 * @param remoteDir 接続å?のãƒ?‚£レクトリå�?
189 * @throws Exception 何らかã?エラーが発生したå?合ã?
190 */
191 protected abstract void actionGETdir( final String localDir, final String remoteDir ) throws Exception ;
192
193 /**
194 * command="PUT" が指定されたときã?処ç�?‚’行いますã?
195 *
196 * ローカルファイルをã?接続å?のサーバã?側にアãƒ??ロードしますã?
197 *
198 * @param localFile ローカルのファイルå�?
199 * @param remoteFile 接続å?のファイルå�?
200 * @throws Exception 何らかã?エラーが発生したå?合ã?
201 */
202 protected abstract void actionPUT( final String localFile, final String remoteFile ) throws Exception ;
203
204 /**
205 * command="PUTDIR" が指定されたときã?処ç�?‚’行いますã?
206 *
207 * ローカルファイルのãƒ?‚£レクトリ以下を、接続å?のサーバã?側のãƒ?‚£レクトリに階層構é?のままアãƒ??ロードしますã?
208 *
209 * @og.rev 5.3.7.0 (2011/07/01) フォルãƒ?�«アクセスできなã�??合ã?、エラーを返しますã?
210 *
211 * @param localDir ローカルのãƒ?‚£レクトリå�?
212 * @param remoteDir 接続å?のãƒ?‚£レクトリå�?
213 * @throws Exception 何らかã?エラーが発生したå?合ã?
214 */
215 protected void actionPUTdir( final String localDir, final String remoteDir ) throws Exception {
216 File[] lclFiles = new File( localDir ).listFiles();
217
218 // 5.3.7.0 (2011/07/01) フォルãƒ?�«アクセスできなã�??合ã?、エラーを返しますã?
219 if( lclFiles == null ) {
220 errAppend( "æŒ?®šã?ãƒ?‚£レクトリは、アクセスできませんã€? );
221 errAppend( " localDir = [" , localDir , "]" );
222 throw new RuntimeException( getErrMsg() );
223 }
224
225 for( int i=0; i<lclFiles.length; i++ ) {
226 String lcl = lclFiles[i].getName();
227 if( lclFiles[i].isDirectory() ) {
228 actionPUTdir( addFile( localDir,lcl ),addFile( remoteDir,lcl ) );
229 }
230 else {
231 actionPUT( addFile( localDir,lcl ),addFile( remoteDir,lcl ) );
232 }
233 }
234 }
235
236 /**
237 * command="DEL" が指定されたときã?処ç�?‚’行いますã?
238 *
239 * 接続å?のサーバã?側のファイル名を削除しますã?
240 *
241 * @param remoteFile 接続å?のファイルå�?
242 * @throws Exception 何らかã?エラーが発生したå?合ã?
243 */
244 protected abstract void actionDEL( final String remoteFile ) throws Exception ;
245
246 /**
247 * command="DELDIR" が指定されたときã?処ç�?‚’行いますã?
248 *
249 * 接続å?のサーバã?側のãƒ?‚£レクトリ名を削除しますã?
250 *
251 * @param remoteDir 接続å?のãƒ?‚£レクトリå�?
252 * @throws Exception 何らかã?エラーが発生したå?合ã?
253 */
254 protected abstract void actionDELdir( final String remoteDir ) throws Exception ;
255
256 /**
257 * ローカルファイルのãƒ?‚£レクトリを作æ?しますã?
258 *
259 * 引数のファイル名ã?、ファイル名ですã?作æ?するãƒ?‚£レクトリは、そのファイルオブジェクトã?
260 * getParentFile() で取得されるãƒ?‚£レクトリまでを作æ?しますã?
261 *
262 * ※ ローカルファイルのãƒ?‚£レクトリの作æ?に失敗したå?合ã?、RuntimeException ã�?throw されますã?
263 *
264 * @param localFile ローカルのファイルå�?
265 * @throws IOException File#getCanonicalFile() で発生するå?出力エラー
266 */
267 protected void makeLocalDir( final String localFile ) throws IOException {
268 File fileDir = new File( localFile ).getCanonicalFile().getParentFile();
269 if( !fileDir.exists() ) {
270 if( ! fileDir.mkdirs() ) {
271 errAppend( "ローカルファイルのãƒ?‚£レクトリの作æ?に失敗しましたã€? );
272 errAppend( " localFile = [" , localFile , "]" );
273 throw new RuntimeException( getErrMsg() );
274 }
275 }
276 }
277
278 /**
279 * ãƒ?‚£レクトリとファイル名を合æ?しますã?
280 *
281 * 単純に、ディレクトリのæœ?¾Œと、ファイルのæœ??のã€?/" をチェãƒ?‚¯しã?
282 * 存在すれば、そのまま、結合しã?存在しなければã€?/" を追åŠ?�—ますã?
283 * 両方に存在する場合ã?、片方をã?ずしますã?
284 *
285 * @param dir ãƒ?‚£レクトリå�?
286 * @param file ファイルå�?
287 *
288 * @return 合æ?されたファイルå�?
289 */
290 protected String addFile( final String dir,final String file ) {
291 final String filepath ;
292
293 char ch1 = dir.charAt( dir.length()-1 ) ; // ãƒ?‚£レクトリのæœ?¾Œã?æ–??
294 char ch2 = file.charAt( 0 ) ; // ファイルのæœ??のæ–??
295
296 if( ch1 == '/' || ch1 == '\\' ) {
297 if( ch2 == '/' || ch2 == '\\' ) { // 両方とも存在するã€?
298 filepath = dir + file.substring(1);
299 }
300 else {
301 filepath = dir + file;
302 }
303 }
304 else {
305 if( ch2 == '/' || ch2 == '\\' ) { // ç‰?–¹のみ存在するã€?
306 filepath = dir + file;
307 }
308 else {
309 filepath = dir + "/" + file;
310 }
311 }
312
313 return filepath ;
314 }
315
316 /**
317 * サーバã?の、ã?ストã?ユーザー、パスワードを設定しますã?
318 *
319 * @param host サーバã?
320 * @param user ユーザー
321 * @param passwd パスワーãƒ?
322 */
323 @Override
324 public void setHostUserPass( final String host , final String user , final String passwd ) {
325 this.host = host;
326 this.user = user;
327 this.passwd = passwd;
328 }
329
330 /**
331 * サーバã?名を取得しますã?
332 *
333 * @return サーバã?
334 */
335 // protected String getHost() { return host; }
336
337 /**
338 * ユーザー名を取得しますã?
339 *
340 * @return ユーザー
341 */
342 // protected String getUser() { return user; }
343
344 /**
345 * パスワードを取得しますã?
346 *
347 * @return パスワーãƒ?
348 */
349 // protected String getPassword() { return passwd; }
350
351 /**
352 * 接続に利用するポã?ト番号を設定しますã?
353 *
354 * @param port 接続に利用するポã?ト番号
355 */
356 @Override
357 public void setPort( final String port ) {
358 if( port != null ) {
359 this.port = port ;
360 }
361 }
362
363 /**
364 * ポã?トを取得しますã?
365 * 設定されてã�?‚‹生ã?port属æ?(nullもありうã‚?を返しますã?
366 *
367 * @return ポã?ãƒ?
368 */
369 protected String getPort() { return port; }
370
371 /**
372 * ポã?トを取得しますã?
373 * 設定されてã�?‚‹port属æ?がã?nullの場合ã?、defPortを返しますã?
374 *
375 * @param defPort port ã�?null の場合ã?初期値
376 *
377 * @return ポã?ãƒ?
378 */
379 protected int getPort( final int defPort) {
380 return ( port == null ) ? defPort : Integer.parseInt( port );
381 }
382
383 /**
384 * それぞれの受け側ファイルにãƒ?‚£レクトリを作æ?するかどã�?�‹(初期値:true:作æ?する)ã€?
385 *
386 * -mkdirs=[true/false] は、受けå?のファイル(GETæ™?LOCAL、PUTæ™?サーバã?)に取り込ã‚?ƒ•ァイルのãƒ?‚£レクトリã�?
387 * 存在しなã�??合に、作æ?するかどã�?�‹を指定しまã�?初期値:true)
388 * 通常、サーバã?に、フォルãƒ?šŽ層を作æ?してPUTする場合ã?動的にフォルãƒ?šŽ層を作æ?したã�?‚±ースで便利ですã?
389 * é€?�«、フォルãƒ??確定しており、指定フォルãƒ?»¥外に PUT するのはバグってã�?‚‹と事がåˆ?�‹ってã�?‚‹場合には
390 * false に設定して、存在しなã�?ƒ•ォルãƒ?�«PUT しよã�?�¨すると、エラーになるよã�?�«しますã?
391 *
392 * @param isMkdirs 受け側ファイルにãƒ?‚£レクトリを作æ?するかどã�?�‹。true:作æ?する
393 */
394 @Override
395 public void setMkdirs( final boolean isMkdirs ) {
396 this.isMkdirs = isMkdirs ;
397 }
398
399 /**
400 * 受け側ファイルにãƒ?‚£レクトリを作æ?するかどã�?�‹を取得しますã?
401 *
402 * @return 受け側ファイルにãƒ?‚£レクトリを作æ?するかどã�?�‹。true:作æ?する
403 */
404 // protected boolean isMkdirs() { return isMkdirs; }
405
406 /**
407 * タイãƒ?‚¢ウトを秒でæŒ?®šしまã�?初期値:600 [秒])ã€?
408 *
409 * オリジナルの FTPClient#setDataTimeout( int ) は、ミリ秒でセãƒ?ƒˆ
410 * しますが、ここã?メソãƒ?ƒ‰では、秒でセãƒ?ƒˆしますã?
411 *
412 * @param timeout タイãƒ?‚¢ウãƒ?秒]
413 * @throws RuntimeException タイãƒ?‚¢ウトã?æŒ?®šが大きすぎた場å�?
414 */
415 @Override
416 public void setTimeout( final int timeout ) {
417 if( Integer.MAX_VALUE / 1000 < timeout ) {
418 errAppend( "タイãƒ?‚¢ウトã?æŒ?®šが大きすぎますã?" );
419 errAppend( " timeout = [" , timeout , "]" );
420 throw new RuntimeException( getErrMsg() );
421 }
422
423 this.timeout = timeout ;
424 }
425
426 /**
427 *æŒ?®šされたタイãƒ?‚¢ウトを秒で取得しまã�?初期値:600 [秒])ã€?
428 *
429 * @return タイãƒ?‚¢ウãƒ?秒]
430 */
431 // protected int getTimeout() { return timeout; }
432
433 /**
434 * 実行状況ã?表示可否 を設定しまã�?初期値:false:表示しなã�?ã€?
435 *
436 * @param isDisplay 実行状況ã?表示可否
437 */
438 @Override
439 public void setDisplay( final boolean isDisplay ) {
440 this.isDisplay = isDisplay ;
441 }
442
443 /**
444 * 実行状況ã?表示可否を取得しますã?
445 *
446 * @return 実行状況ã?表示可否
447 */
448 // protected boolean isDisplay() { return isDisplay; }
449
450 /**
451 * ãƒ?ƒ�ãƒ?‚°æƒ??の表示可否 を設定しまã�?初期値:false:表示しなã�?ã€?
452 *
453 * @param isDebug ãƒ?ƒ�ãƒ?‚°æƒ??の表示可否
454 */
455 @Override
456 public void setDebug( final boolean isDebug ) {
457 this.isDebug = isDebug ;
458 }
459
460 /**
461 * ãƒ?ƒ�ãƒ?‚°æƒ??の表示可否 を取得しますã?
462 *
463 * @return ãƒ?ƒ�ãƒ?‚°æƒ??の表示可否
464 */
465 // protected boolean isDebug() { return isDebug; }
466
467 /**
468 * 処ç�?¸に発生したエラーメãƒ?‚»ージをセãƒ?ƒˆしますã?
469 *
470 * @param msg メãƒ?‚»ージ化したいオブジェクãƒ?
471 */
472 protected void errAppend( final Object msg ) {
473 errMsg.append( String.valueOf(msg) ).append( CR );
474 }
475
476 /**
477 * 処ç�?¸に発生したエラーメãƒ?‚»ージをセãƒ?ƒˆしますã?
478 *
479 * @param msgs Object...
480 */
481 protected void errAppend( final Object... msgs ) {
482 for( int i=0; i<msgs.length; i++ ) {
483 errMsg.append( String.valueOf(msgs[i]) );
484 }
485
486 errMsg.append( CR );
487 }
488
489 /**
490 * 処ç�?¸に発生したエラーメãƒ?‚»ージを取りå?しますã?
491 *
492 * @return エラーメãƒ?‚»ージ
493 */
494 @Override
495 public String getErrMsg() {
496 return errMsg.toString();
497 }
498 }