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.IOException;
019    import java.util.Map;
020    import java.util.LinkedHashMap ;
021    import java.util.Vector;
022    import java.util.Hashtable;
023    
024    import com.jcraft.jsch.JSch;
025    import com.jcraft.jsch.Session;
026    import com.jcraft.jsch.ChannelSftp;
027    import com.jcraft.jsch.ChannelSftp.LsEntry;
028    import com.jcraft.jsch.SftpATTRS;
029    import com.jcraft.jsch.JSchException;
030    import com.jcraft.jsch.SftpException;
031    
032    
033    /**
034     * SFTPConnect.java ã¯ã€å?通的ã«ä½¿ç”¨ã•れã‚?SFTP関連ã®åŸºæœ¬æ©Ÿè?を実è£?—ãŸã?クラスã§ã™ã?
035     *
036     * ã“れã¯ã€org.apache.commons.net.ftp.FTPClient ã‚’ã?ースã«é–‹ç™ºã•れã¦ã?¾ã™ã?
037     * ã“ã?クラスã®å®Ÿè¡Œã«ã¯ã€commons-net-ftp-2.0.jar ãŒå¿?¦ã§ã™ã?
038     *
039     * -host=SFTPサーãƒã? -user=ユーザー -passwd=パスワーãƒ?-remoteFile=SFTPå…ˆã?ファイルå?ã‚’å¿??設定ã—ã¾ã™ã?
040     * -localFile=ローカルã®ãƒ•ァイルåã?ã€å¿??ã§ã¯ã‚りã¾ã›ã‚“ãŒã?-command=DEL ã®å ´åˆã«ã®ã¿ä¸è¦ã§ã‚りã€?
041     * ãれ以外ã? command ã®å ´åˆã?ã€å¿?¦ã§ã™ã?
042     *
043     * -command=[GET/PUT/DEL/GETDIR/PUTDIR/DELDIR] ã¯ã€SFTPサーãƒã?ã«å¯¾ã—ã¦ã®å‡¦ç??方法をæŒ?®šã—ã¾ã™ã?
044     *   GET:SFTPサーãƒã?ã‹ã‚‰ãƒ­ãƒ¼ã‚«ãƒ«ã«ãƒ•ァイル転é€ã—ã¾ã?åˆæœŸå€¤)ã€?
045     *   PUT:ローカルファイルをSFTPサーãƒã?ã« PUT(STOREã€SAVEã€UPLOADã€ãªã©ã¨åŒæ„èª?ã—ã¾ã™ã?
046     *   DEL:SFTPサーãƒã?ã®æŒ?®šã?ファイルを削除ã—ã¾ã™ã?ã“ã?å ´åˆã?ã¿ã€?localFile 属æ?ã®æŒ?®šã?ä¸è¦ã§ã™ã?
047     *   GETDIR,PUTDIR,DELDIR:æŒ?®šã?フォルãƒ?»¥ä¸‹ã?ファイルをå?ç?—ã¾ã™ã?
048     *
049     * -mkdirs=[true/false] ã¯ã€å—ã‘å?ã®ãƒ•ァイル(GETæ™?LOCALã€PUTæ™?SFTPサーãƒã?)ã«å–り込ã‚?ƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã?
050     * 存在ã—ãªã??åˆã«ã€ä½œæ?ã™ã‚‹ã‹ã©ã?‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:true)ã€?
051     * 通常ã€SFTPサーãƒã?ã«ã€ãƒ•ォルãƒ?šŽå±¤ã‚’作æ?ã—ã¦PUTã™ã‚‹å ´åˆã?å‹•çš„ã«ãƒ•ォルãƒ?šŽå±¤ã‚’作æ?ã—ãŸã?‚±ãƒ¼ã‚¹ã§ä¾¿åˆ©ã§ã™ã?
052     * é€?«ã€ãƒ•ォルãƒ??確定ã—ã¦ãŠã‚Šã€æŒ‡å®šãƒ•ォルãƒ?»¥å¤–ã« PUT ã™ã‚‹ã®ã¯ãƒã‚°ã£ã¦ã?‚‹ã¨äº‹ãŒåˆ?‹ã£ã¦ã?‚‹å ´åˆã«ã¯
053     * false ã«è¨­å®šã—ã¦ã€å­˜åœ¨ã—ãªã?ƒ•ォルãƒ?«PUT ã—よã?¨ã™ã‚‹ã¨ã€ã‚¨ãƒ©ãƒ¼ã«ãªã‚‹ã‚ˆã?«ã—ã¾ã™ã?
054     *
055     * 引数æ–?­—å?中ã«ç©ºç™½ã‚’å«ã‚??åˆã?ã€ãƒ€ãƒ–ルコーãƒ??ション("") ã§æ‹¬ã£ã¦ä¸‹ã•ã??
056     * 引数æ–?­—å?ã® ã€?ã€ã?å‰å¾Œã«ã¯ã€ç©ºç™½ã¯æŒŸã‚ã¾ã›ã‚“。å¿?šã€?key=value ã®æ§˜ã«
057     * 繋ã’ã¦ãã ã•ã„ã€?
058     *
059     * @og.formSample
060     *  SFTPConnect -host=SFTPサーãƒã? -user=ユーザー -passwd=パスワーãƒ?-remoteFile=SFTPå…ˆã?ファイルå?[-localFile=ローカルã®ãƒ•ァイルå]
061     *                   [-mode=[ASCII/BINARY]  ] [-command=[GET/PUT/DEL/GETDIR/PUTDIR/DELDIR] ] [-passive=[true/false] ]
062     *
063     *    -host=SFTPサーãƒã?                ?šæŽ¥ç¶šå?ã®SFTPサーãƒã?ã®ã‚¢ãƒ‰ãƒ¬ã‚¹ã¾ãŸã?ã€ã‚µãƒ¼ãƒã?å?
064     *    -user=ユーザー                    ?šæŽ¥ç¶šã™ã‚‹ãƒ¦ãƒ¼ã‚¶ãƒ¼å?
065     *    -remoteFile=SFTPå…ˆã?ファイルå?   ?šæŽ¥ç¶šå?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルåã?PUT,GET 関係ãªãSFTPå´ã¨ã—ã¦æŒ?®šã—ã¾ã™ã?
066     *   [-passwd=パスワード]               ?šæŽ¥ç¶šã™ã‚‹ãƒ¦ãƒ¼ã‚¶ãƒ¼ã®ãƒ‘スワーãƒ?
067     *   [-localFile=ローカルã®ãƒ•ァイルå]  ?šãƒ­ãƒ¼ã‚«ãƒ«ã®ãƒ•ァイルåã?PUT,GET 関係ãªãローカルファイルを指定ã—ã¾ã™ã?
068     *   [-port=ãƒã?ãƒ?]                    ?šæŽ¥ç¶šã™ã‚‹ã‚µãƒ¼ãƒã?ã®ãƒã?トをæŒ?®šã—ã¾ã™ã?
069     *   [-keyFile=秘å¯?‚­ãƒ¼ãƒ•ァイル ]       ?šå?開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?
070     *   [-command=[GET/PUT/DEL] ]          ?šSFTPサーãƒã?å´ã§ã®å‡¦ç??方法をæŒ?®šã—ã¾ã™ã?
071     *             [GETDIR/PUTDIR/DELDIR]]          GET:SFTP⇒LOCALã€PUT:LOCAL⇒SFTP ã¸ã®è»¢é€ã§ã?åˆæœŸå€¤:GET)
072     *                                              DEL:SFTPファイルを削除ã—ã¾ã™ã?
073     *                                              GETDIR,PUTDIR,DELDIR æŒ?®šã?フォルãƒ?»¥ä¸‹ã?ファイルをå?ç?—ã¾ã™ã?
074     *   [-mkdirs=[true/false]  ]           ?šå—ã‘å?ファイル(GETæ™?LOCALã€PUTæ™?SFTPサーãƒã?)ã«ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:true)
075     *                                              (false:ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªãŒç„¡ã‘れã°ã€ã‚¨ãƒ©ãƒ¼ã«ã—ã¾ã™ã?)
076     *   [-timeout=タイãƒ?‚¢ã‚¦ãƒ?ç§’] ]       ?šDataタイãƒ?‚¢ã‚¦ãƒ?åˆæœŸå€¤:600 [ç§’])
077     *   [-display=[false/true] ]           ?štrueã¯ã€æ¤œç´¢çжæ³ã‚’表示ã—ã¾ã?åˆæœŸå€¤:false)
078     *   [-debug=[false|true]   ]           ?šãƒ‡ãƒãƒƒã‚°æƒ??を標準å?力ã«è¡¨ç¤ºã™ã‚‹(true)ã‹ã—ãªã?false)ã?åˆæœŸå€¤:false[表示ã—ãªã„])
079     *
080     * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦è¿½åŠ?
081     *
082     * @version  5.0
083     * @author       Kazuhiko Hasegawa
084     * @since    JDK5.0,
085     */
086    public final class SFTPConnect extends AbstractConnect {
087            private final JSch jsch;
088    
089            private static final int DEF_PORT       = 22;                   // ãƒã?ãƒ?
090    
091            private boolean isConnect       = false;                // コãƒã‚¯ãƒˆæ¸ˆã¿ã‹ã©ã?‹ã€?
092    
093            private String  lastRemoteDir   = "/";          // SFTPå…ˆã?æœ?¾Œã«ç™»éŒ²ã—ãŸãƒ•ォルãƒ?(mkdir ã®é«˜é?化ã?ãŸã‚)
094            private String  keyFile                 = null;         // 公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?
095    
096            private Session         session = null;
097            private ChannelSftp channel     = null;
098    
099            /**
100             * ãƒ?ƒ•ォルトコンストラクター
101             */
102            public SFTPConnect() {
103                    jsch = new JSch();
104            }
105    
106            /**
107             * SFTPサーãƒã?ã¸ã®æŽ¥ç¶šã?ログインを行ã„ã¾ã™ã?
108             *
109             * ã“ã?メソãƒ?ƒ‰ã¯ã€å?期化メソãƒ?ƒ‰ã§ã™ã?
110             * SFTPサーãƒã?ã¸ã®æŽ¥ç¶šã?ログインを行ã„ã¾ã™ã?ã§ã€è¤?•°ãƒ•ァイルを転é€ã™ã‚?
111             * ケースã§ã¯ã€æœ€åˆã«?‘度ã?‘呼ã³å‡ºã™ã ã‘ã§ã™ã?
112             * 接続å?を変更ã™ã‚‹å ´åˆã?ã€ã‚‚ã?¸?º¦ã“ã?メソãƒ?ƒ‰ã‚’コールã™ã‚‹å¿?¦ãŒã‚りã¾ã™ã?
113             * (ãã?よã†ãªå ´åˆã?ã€??常ã€ã‚ªãƒ–ジェクトを構築ã—ãªãŠã™æ–¹ãŒã‚ˆã?¨æ€ã„ã¾ã™ã?)
114             *
115             */
116            @Override
117            public void connect() {
118                    if( isDisplay ) { System.out.println( "CONNECT: HOST=" + host + ",USER=" + user + ",PORT=" + port ); }
119    
120                    // ã‚‚ã—ã€ã™ã§ã«æŽ¥ç¶šã•れã¦ã?Ÿå ´åˆã?ã€ã‚¯ãƒ­ãƒ¼ã‚¹å‡¦ç?‚’行ã„ã¾ã™ã?
121                    if( isConnect ) { disconnect(); }
122    
123                    // HostKeyãƒã‚§ãƒ?‚¯ã‚’行ã‚ãªã?
124                    Hashtable<String,String> config = new Hashtable<String,String>();
125                    config.put( "StrictHostKeyChecking", "no" );
126                    JSch.setConfig( config );
127    
128                    // サーãƒã?ã«å¯¾ã—ã¦æŽ¥ç¶šã‚’è¡Œã„ã¾ã™ã?
129                    try {
130                            if( keyFile == null ) {
131                                    // パスワードèªè¨¼
132                                    session=jsch.getSession( user, host, getPort( DEF_PORT ) );
133                                    session.setPassword( passwd );
134                            }
135                            else {
136                                    // 公開キーã€ç§˜å¯?‚­ãƒ¼èªè¨¼
137                                    jsch.addIdentity( keyFile );
138                                    session=jsch.getSession( user, host, getPort( DEF_PORT ) );
139                    //              session.setUserInfo(new MyUserInfo());
140                            }
141    
142                            session.connect( timeout*1000 );                // タイãƒ?‚¢ã‚¦ãƒˆã?設å®?
143    
144                            channel=(ChannelSftp)session.openChannel("sftp");
145                            channel.connect();
146                    }
147                    catch ( JSchException ex ) {
148                            errAppend( "SFTP server refused connection. " );
149                            errAppend( "   host    = [" , host      , "]" );
150                            errAppend( "   user    = [" , user      , "]" );
151                            errAppend( "   port    = [" , port      , "]" );
152                            errAppend( ex );
153                            if( isDebug ) { ex.printStackTrace(); }
154                            disconnect();
155                            throw new RuntimeException( getErrMsg(),ex );
156                    }
157    
158                    isConnect = true;
159            }
160    
161            /**
162             * SFTPサーãƒã?ã¨ã®æŽ¥ç¶šã‚’クローズã—ã¾ã™ã?
163             *
164             * ログインã•れã¦ã?‚‹å ´åˆã?ã€ãƒ­ã‚°ã‚¢ã‚¦ãƒˆã‚‚行ã„ã¾ã™ã?
165             * コãƒã‚¯ãƒˆã•れã¦ã?‚‹å ´åˆã?ã€ãƒ‡ã‚£ã‚¹ã‚³ãƒã‚¯ãƒˆã—ã¾ã™ã?
166             *
167             */
168            @Override
169            public void disconnect() {
170                    if( isDisplay ) { System.out.println( "DISCONNECT:" ); }
171    
172                    if( isConnect ) {
173                            isConnect = false;
174                            try {
175                                    channel.disconnect();
176                                    session.disconnect();
177                            }
178                            catch( Throwable th ) {
179                                    errAppend( "disconnect Error." );
180                                    errAppend( th );
181                                    if( isDebug ) { th.printStackTrace(); }
182                                    throw new RuntimeException( getErrMsg(),th );
183                            }
184                    }
185            }
186    
187            /**
188             * command="GET" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
189             *
190             * ローカルファイルをã?接続å?ã®SFTPサーãƒã?å´ã«ã‚¢ãƒ??ロードã—ã¾ã™ã?
191             *
192             * @param       localFile       ローカルã®ãƒ•ァイルå?
193             * @param       remoteFile      SFTPå…ˆã?ファイルå?
194             * @throws  JSchException JSCHエラーãŒç™ºç”Ÿã—ãŸã¨ã?
195             * @throws  SftpException SFTPエラーãŒç™ºç”Ÿã—ãŸã¨ã?
196             * @throws  IOException  入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
197             */
198            @Override
199            protected void actionGET( final String localFile, final String remoteFile ) throws JSchException, SftpException, IOException {
200                    if( isDebug ) { System.out.println( "GET: " + remoteFile + " => " + localFile ); }
201    
202                    // GET(DOWNLOAD)å–得時ã¯ã€ãƒ­ãƒ¼ã‚«ãƒ«ãƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹å¿?¦ãŒã‚ã‚‹ã€?
203                    if( isMkdirs ) {
204                            makeLocalDir( localFile );
205                    }
206    
207                    channel.get( remoteFile,localFile );
208            }
209    
210            /**
211             * command="GETDIR" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
212             *
213             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä»¥ä¸‹ã‚’ローカルãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã«éšŽå±¤æ§‹é?ã®ã¾ã¾ãƒ?‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã—ã¾ã™ã?
214             *
215             * @param       localDir        ローカルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå?
216             * @param       remoteDir       SFTPå…ˆã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå?
217             * @throws  JSchException JSCHエラーãŒç™ºç”Ÿã—ãŸã¨ã?
218             * @throws  SftpException SFTPエラーãŒç™ºç”Ÿã—ãŸã¨ã?
219             * @throws  IOException  入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
220             */
221            @Override
222            protected void actionGETdir( final String localDir, final String remoteDir )  throws IOException,JSchException,SftpException {
223                    Vector<?> list = channel.ls( remoteDir );
224                    for (int i=0;i<list.size();i++) {
225                            LsEntry entry = (LsEntry)list.get(i);
226                            String rmt = entry.getFilename();
227                            if( ".".equals( rmt ) || "..".equals( rmt ) ) { continue; }             // "." ã§å§‹ã¾ã‚‹ãƒ•ァイルもã‚ã‚‹ã?ã§ã€equasl 判å®?
228                            SftpATTRS stat = entry.getAttrs();
229                            if( stat.isDir() ) {
230                                    actionGETdir( addFile( localDir,rmt ),addFile( remoteDir,rmt ) );
231                            }
232                            else {
233                                    actionGET( addFile( localDir,rmt ),addFile( remoteDir,rmt ) );
234                            }
235                    }
236            }
237    
238            /**
239             * command="PUT" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
240             *
241             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルåをローカルã«ãƒ?‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã—ã¾ã™ã?
242             *
243             * @param       localFile       ローカルã®ãƒ•ァイルå?
244             * @param       remoteFile      SFTPå…ˆã?ファイルå?
245             * @throws JSchException 処ç?¸­ã« JSch エラーãŒç™ºç”Ÿã—ãŸå?å?
246             * @throws SftpException 処ç?¸­ã« Sftp エラーãŒç™ºç”Ÿã—ãŸå?å?
247             */
248            @Override
249            protected void actionPUT( final String localFile, final String remoteFile ) throws JSchException,SftpException {
250                    if( isDebug ) { System.out.println( "PUT: " + localFile + " => " + remoteFile ); }
251    
252                    // PUT(UPLOAD)登録時ã?ã€ãƒªãƒ¢ãƒ¼ãƒˆãƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹å¿?¦ãŒã‚ã‚‹ã€?
253                    if( isMkdirs ) {
254                            // å‰å›žã®DIRã¨ã®æ¯”è¼?§ã€ã™ã§ã«å­˜åœ¨ã—ã¦ã?‚Œã°ã€makeDirectory 処ç?‚’パスã™ã‚‹ã€?
255                            int ad = remoteFile.lastIndexOf( '/' ) + 1;             // 区åˆ?‚Šæ–?­—ã‚’?‹ï¼‘ã™ã‚‹ã?
256                            String tmp = remoteFile.substring( 0,ad );
257    
258                            if( ad > 0 && !lastRemoteDir.startsWith( tmp ) ) {
259                                    lastRemoteDir = tmp;
260                                    if( remoteFile.startsWith( "/" ) ) {
261                                            String[] fls = remoteFile.split( "/" );
262                                            channel.cd( "/" );
263                                            for( int i=1; i<fls.length-1; i++ ) {
264                                                    try {
265                            //                              SftpATTRS stat = channel.lstat(fls[i]);         // 存在ã—ãªã?¨ã€SftpException
266                                                            channel.cd( fls[i] );                                           // 存在ã—ãªã?¨ã€SftpException
267                                                            continue;
268                                                    } catch (SftpException ex) {
269                                                            // ファイルãŒå­˜åœ¨ã—ãªã?¨ã?
270                                                            channel.mkdir( fls[i] );
271                                                            channel.cd( fls[i] );
272                                                    }
273                                            }
274                                    }
275                            }
276                    }
277    
278                    channel.put( localFile,remoteFile );
279            }
280    
281            /**
282             * command="DEL" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
283             *
284             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルåを削除ã—ã¾ã™ã?
285             *
286             * @param       remoteFile      SFTPå…ˆã?ファイルå?
287             * @throws SftpException SFTPサーãƒã?å´ã®ãƒ•ァイルåã?削除ã«å¤±æ•—ã—ãŸã¨ã?
288             */
289            @Override
290            protected void actionDEL( final String remoteFile ) throws SftpException {
291                    if( isDebug ) { System.out.println( "DEL: " + remoteFile ); }
292    
293                    channel.rm( remoteFile );
294            }
295    
296            /**
297             * command="DELDIR" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
298             *
299             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªåを削除ã—ã¾ã™ã?
300             *
301             * @param       remoteDir       SFTPå…ˆã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå?
302             * @throws SftpException SFTPサーãƒã?å´ã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªåã?削除ã«å¤±æ•—ã—ãŸã¨ã?
303             */
304            @Override
305            protected void actionDELdir( final String remoteDir ) throws SftpException {
306    
307                    Vector<?> list = channel.ls( remoteDir );
308                    for (int i=0;i<list.size();i++) {
309                            LsEntry entry = (LsEntry)list.get(i);
310                            String rmt = entry.getFilename();
311                            if( ".".equals( rmt ) || "..".equals( rmt ) ) { continue; }             // "." ã§å§‹ã¾ã‚‹ãƒ•ァイルもã‚ã‚‹ã?ã§ã€equasl 判å®?
312                            SftpATTRS stat = entry.getAttrs();
313                            if( stat.isDir() ) {
314                                    actionDELdir( addFile( remoteDir,rmt ) );
315                            }
316                            else {
317                                    actionDEL( addFile( remoteDir,rmt ) );
318    //                              channel.rm( addFile( remoteDir,rmt ) );
319                            }
320                    }
321                    channel.rmdir( remoteDir );
322            }
323    
324            /**
325             * 公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?
326             *
327             * @param       keyFile 秘å¯?‚­ãƒ¼ãƒ•ァイルå?
328             */
329            public void setKeyFile( final String keyFile ) {
330                    if( keyFile != null ) {
331                            this.keyFile = keyFile ;
332                    }
333            }
334    
335    //      private static class MyUserInfo implements UserInfo {
336    //              public String getPassword() {
337    //                      return null;
338    //              }
339    //              // パスフレーズã¯ç©º
340    //              public String getPassphrase() {
341    //                      return "";
342    //              }
343    //              public boolean promptPassword(String arg0) {
344    //                      return true;
345    //              }
346    //              public boolean promptPassphrase(String arg0) {
347    //                      return true;
348    //              }
349    //              public boolean promptYesNo(String arg0) {
350    //                      return true;
351    //              }
352    //              public void showMessage(String arg0) {
353    //              }
354    //      }
355    
356    // ******************************************************************************************************* //
357    //       以下ã?å˜ç‹¬ã§ä½¿ç”¨ã™ã‚‹å ´åˆã? main処ç?
358    // ******************************************************************************************************* //
359    
360            private static final Map<String,String> mustProparty   ;          // ?»ãƒ—ロパティ?½å¿??ãƒã‚§ãƒ?‚¯ç”¨ Map
361            private static final Map<String,String> usableProparty ;          // ?»ãƒ—ロパティ?½æ•´åˆæ?ãƒã‚§ãƒ?‚¯ Map
362    
363            static {
364                    mustProparty = new LinkedHashMap<String,String>();
365                    mustProparty.put( "host",               "接続å?ã®SFTPサーãƒã?ã®ã‚¢ãƒ‰ãƒ¬ã‚¹ã¾ãŸã?ã€ã‚µãƒ¼ãƒã?å?å¿??)" );
366                    mustProparty.put( "user",               "接続ã™ã‚‹ãƒ¦ãƒ¼ã‚¶ãƒ¼å?å¿??)" );
367                    mustProparty.put( "remoteFile", "接続å?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルå?å¿??)" );
368    
369                    usableProparty = new LinkedHashMap<String,String>();
370                    usableProparty.put( "passwd",           "接続ã™ã‚‹ãƒ¦ãƒ¼ã‚¶ãƒ¼ã®ãƒ‘スワーãƒ? );
371                    usableProparty.put( "localFile",        "ローカルã®ãƒ•ァイルå? );
372                    usableProparty.put( "port",                     "接続ã«åˆ©ç”¨ã™ã‚‹ãƒã?ト番å·ã‚’設定ã—ã¾ã™ã?" );
373                    usableProparty.put( "keyFile",          "公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?" );
374                    usableProparty.put( "command",          "SFTPサーãƒã?å´ã§ã®å‡¦ç??æ–¹æ³?GET/PUT/DEL)を指定ã—ã¾ã?åˆæœŸå€¤:GET)" );
375                    usableProparty.put( "mkdirs",           "å—ã‘å´ãƒ•ァイル(GETæ™?LOCALã€PUTæ™?SFTPサーãƒã?)ã«ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:true)" );
376                    usableProparty.put( "timeout",          "Dataタイãƒ?‚¢ã‚¦ãƒ?åˆæœŸå€¤:600 [ç§’])" );
377                    usableProparty.put( "display",          "[false/true]:trueã¯ã€æ¤œç´¢çжæ³ã‚’表示ã—ã¾ã?åˆæœŸå€¤:false)" );
378                    usableProparty.put( "debug",            "ãƒ?ƒãƒ?‚°æƒ??を標準å?力ã«è¡¨ç¤ºã™ã‚‹(true)ã‹ã—ãªã?false)ã? +
379                                                                                            CR + "(åˆæœŸå€¤:false:表示ã—ãªã?" );
380            }
381    
382            private static final String[] CMD_LST  = new String[] { "GET","PUT","DEL","GETDIR","PUTDIR","DELDIR" };
383    
384            /**
385             * ã“ã?クラスã®å‹•作確èªç”¨ã®ã€main メソãƒ?ƒ‰ã§ã™ã?
386             *
387             * @param       args    コマンド引数é…å?
388             */
389            public static void main( final String[] args ) {
390                    Argument arg = new Argument( "org.opengion.fukurou.util.SFTPConnect" );
391                    arg.setMustProparty( mustProparty );
392                    arg.setUsableProparty( usableProparty );
393                    arg.setArgument( args );
394    
395                    SFTPConnect sftp = new SFTPConnect();
396    
397                    String host   = arg.getProparty( "host");                       // SFTPサーãƒã?
398                    String user   = arg.getProparty( "user" );                      // ユーザー
399                    String passwd = arg.getProparty( "passwd" );            // パスワー�
400    
401                    sftp.setHostUserPass( host , user , passwd );
402    
403                    sftp.setPort(           arg.getProparty( "port"                                 ) );    // 接続ã«åˆ©ç”¨ã™ã‚‹ãƒã?ト番å·ã‚’設定ã—ã¾ã™ã?
404                    sftp.setKeyFile(        arg.getProparty( "keyFile"                              ) );    // 公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?
405                    sftp.setMkdirs(         arg.getProparty( "mkdirs"       ,true           ) );    // å—ã‘å´ãƒ•ァイルã«ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ã‹ã©ã?‹
406                    sftp.setTimeout(        arg.getProparty( "timeout"      ,TIMEOUT        ) );    // Dataタイãƒ?‚¢ã‚¦ãƒ?åˆæœŸå€¤:600 [ç§’])
407                    sftp.setDisplay(        arg.getProparty( "display"      ,false          ) );    // trueã¯ã€æ¤œç´¢çжæ³ã‚’表示ã—ã¾ã?åˆæœŸå€¤:false)
408                    sftp.setDebug(          arg.getProparty( "debug"        ,false          ) );    // ãƒ?ƒãƒ?‚°æƒ??を標準å?力ã«è¡¨ç¤ºã™ã‚‹(true)ã‹ã—ãªã?false)ã?
409    
410                    try {
411                            // コãƒã‚¯ãƒˆã—ã¾ã™ã?
412                            sftp.connect();
413    
414                            String command          = arg.getProparty( "command" ,"GET" ,CMD_LST  );        // SFTP処ç??方法をæŒ?®šã—ã¾ã™ã?
415                            String localFile        = arg.getProparty( "localFile"  );                                      // ローカルã®ãƒ•ァイルå?
416                            String remoteFile       = arg.getProparty( "remoteFile" );                                      // SFTPå…ˆã?ファイルå?
417    
418                            // command , localFile , remoteFile ã‚’å?ã«ã€SFTP処ç?‚’行ã„ã¾ã™ã?
419                            sftp.action( command,localFile,remoteFile );
420                    }
421                    catch( RuntimeException ex ) {
422                            System.err.println( sftp.getErrMsg() );
423                    }
424                    finally {
425                            // ホストã¨ã®æŽ¥ç¶šã‚’終äº?—ã¾ã™ã?
426                            sftp.disconnect();
427                    }
428            }
429    }