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, SftpException, IOException
195             */
196            @Override
197            protected void actionGET( final String localFile, final String remoteFile ) throws JSchException, SftpException, IOException {
198                    if( isDebug ) { System.out.println( "GET: " + remoteFile + " => " + localFile ); }
199    
200                    // GET(DOWNLOAD)å–得時ã¯ã€ãƒ­ãƒ¼ã‚«ãƒ«ãƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹å¿?¦ãŒã‚ã‚‹ã€?
201                    if( isMkdirs ) {
202                            makeLocalDir( localFile );
203                    }
204    
205                    channel.get( remoteFile,localFile );
206            }
207    
208            /**
209             * command="GETDIR" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
210             *
211             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä»¥ä¸‹ã‚’ローカルãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã«éšŽå±¤æ§‹é?ã®ã¾ã¾ãƒ?‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã—ã¾ã™ã?
212             *
213             * @param       localDir        ローカルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå?
214             * @param       remoteDir       SFTPå…ˆã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå?
215             * @throws IOException,JSchException,SftpException
216             */
217            @Override
218            protected void actionGETdir( final String localDir, final String remoteDir )  throws IOException,JSchException,SftpException {
219                    Vector<?> list = channel.ls( remoteDir );
220                    for (int i=0;i<list.size();i++) {
221                            LsEntry entry = (LsEntry)list.get(i);
222                            String rmt = entry.getFilename();
223                            if( ".".equals( rmt ) || "..".equals( rmt ) ) { continue; }             // "." ã§å§‹ã¾ã‚‹ãƒ•ァイルもã‚ã‚‹ã?ã§ã€equasl 判å®?
224                            SftpATTRS stat = entry.getAttrs();
225                            if( stat.isDir() ) {
226                                    actionGETdir( addFile( localDir,rmt ),addFile( remoteDir,rmt ) );
227                            }
228                            else {
229                                    actionGET( addFile( localDir,rmt ),addFile( remoteDir,rmt ) );
230                            }
231                    }
232            }
233    
234            /**
235             * command="PUT" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
236             *
237             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルåをローカルã«ãƒ?‚¦ãƒ³ãƒ­ãƒ¼ãƒ‰ã—ã¾ã™ã?
238             *
239             * @param       localFile       ローカルã®ãƒ•ァイルå?
240             * @param       remoteFile      SFTPå…ˆã?ファイルå?
241             * @throws JSchException, SftpException
242             */
243            @Override
244            protected void actionPUT( final String localFile, final String remoteFile ) throws JSchException,SftpException {
245                    if( isDebug ) { System.out.println( "PUT: " + localFile + " => " + remoteFile ); }
246    
247                    // PUT(UPLOAD)登録時ã?ã€ãƒªãƒ¢ãƒ¼ãƒˆãƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹å¿?¦ãŒã‚ã‚‹ã€?
248                    if( isMkdirs ) {
249                            // å‰å›žã®DIRã¨ã®æ¯”è¼?§ã€ã™ã§ã«å­˜åœ¨ã—ã¦ã?‚Œã°ã€makeDirectory 処ç?‚’パスã™ã‚‹ã€?
250                            int ad = remoteFile.lastIndexOf( '/' ) + 1;             // 区åˆ?‚Šæ–?­—ã‚’?‹ï¼‘ã™ã‚‹ã?
251                            String tmp = remoteFile.substring( 0,ad );
252    
253                            if( ad > 0 && !lastRemoteDir.startsWith( tmp ) ) {
254                                    lastRemoteDir = tmp;
255                                    if( remoteFile.startsWith( "/" ) ) {
256                                            String[] fls = remoteFile.split( "/" );
257                                            channel.cd( "/" );
258                                            for( int i=1; i<fls.length-1; i++ ) {
259                                                    try {
260                            //                              SftpATTRS stat = channel.lstat(fls[i]);         // 存在ã—ãªã?¨ã€SftpException
261                                                            channel.cd( fls[i] );                                           // 存在ã—ãªã?¨ã€SftpException
262                                                            continue;
263                                                    } catch (SftpException ex) {
264                                                            // ファイルãŒå­˜åœ¨ã—ãªã?¨ã?
265                                                            channel.mkdir( fls[i] );
266                                                            channel.cd( fls[i] );
267                                                    }
268                                            }
269                                    }
270                            }
271                    }
272    
273                    channel.put( localFile,remoteFile );
274            }
275    
276            /**
277             * command="DEL" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
278             *
279             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルåを削除ã—ã¾ã™ã?
280             *
281             * @param       remoteFile      SFTPå…ˆã?ファイルå?
282             * @throws SftpException
283             */
284            @Override
285            protected void actionDEL( final String remoteFile ) throws SftpException {
286                    if( isDebug ) { System.out.println( "DEL: " + remoteFile ); }
287    
288                    channel.rm( remoteFile );
289            }
290    
291            /**
292             * command="DELDIR" ãŒæŒ‡å®šã•れãŸã¨ãã?処ç?‚’行ã„ã¾ã™ã?
293             *
294             * 接続å?ã®SFTPサーãƒã?å´ã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªåを削除ã—ã¾ã™ã?
295             *
296             * @param       remoteDir       SFTPå…ˆã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå?
297             * @throws SftpException
298             */
299            @Override
300            protected void actionDELdir( final String remoteDir ) throws SftpException {
301    
302                    Vector<?> list = channel.ls( remoteDir );
303                    for (int i=0;i<list.size();i++) {
304                            LsEntry entry = (LsEntry)list.get(i);
305                            String rmt = entry.getFilename();
306                            if( ".".equals( rmt ) || "..".equals( rmt ) ) { continue; }             // "." ã§å§‹ã¾ã‚‹ãƒ•ァイルもã‚ã‚‹ã?ã§ã€equasl 判å®?
307                            SftpATTRS stat = entry.getAttrs();
308                            if( stat.isDir() ) {
309                                    actionDELdir( addFile( remoteDir,rmt ) );
310                            }
311                            else {
312                                    actionDEL( addFile( remoteDir,rmt ) );
313    //                              channel.rm( addFile( remoteDir,rmt ) );
314                            }
315                    }
316                    channel.rmdir( remoteDir );
317            }
318    
319            /**
320             * 公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?
321             *
322             * @param       keyFile 秘å¯?‚­ãƒ¼ãƒ•ァイルå?
323             */
324            public void setKeyFile( final String keyFile ) {
325                    if( keyFile != null ) {
326                            this.keyFile = keyFile ;
327                    }
328            }
329    
330    //      private static class MyUserInfo implements UserInfo {
331    //              public String getPassword() {
332    //                      return null;
333    //              }
334    //              // パスフレーズã¯ç©º
335    //              public String getPassphrase() {
336    //                      return "";
337    //              }
338    //              public boolean promptPassword(String arg0) {
339    //                      return true;
340    //              }
341    //              public boolean promptPassphrase(String arg0) {
342    //                      return true;
343    //              }
344    //              public boolean promptYesNo(String arg0) {
345    //                      return true;
346    //              }
347    //              public void showMessage(String arg0) {
348    //              }
349    //      }
350    
351    // ******************************************************************************************************* //
352    //       以下ã?å˜ç‹¬ã§ä½¿ç”¨ã™ã‚‹å ´åˆã? main処ç?
353    // ******************************************************************************************************* //
354    
355            private static final Map<String,String> mustProparty   ;          // ?»ãƒ—ロパティ?½å¿??ãƒã‚§ãƒ?‚¯ç”¨ Map
356            private static final Map<String,String> usableProparty ;          // ?»ãƒ—ロパティ?½æ•´åˆæ?ãƒã‚§ãƒ?‚¯ Map
357    
358            static {
359                    mustProparty = new LinkedHashMap<String,String>();
360                    mustProparty.put( "host",               "接続å?ã®SFTPサーãƒã?ã®ã‚¢ãƒ‰ãƒ¬ã‚¹ã¾ãŸã?ã€ã‚µãƒ¼ãƒã?å?å¿??)" );
361                    mustProparty.put( "user",               "接続ã™ã‚‹ãƒ¦ãƒ¼ã‚¶ãƒ¼å?å¿??)" );
362                    mustProparty.put( "remoteFile", "接続å?ã®SFTPサーãƒã?å´ã®ãƒ•ァイルå?å¿??)" );
363    
364                    usableProparty = new LinkedHashMap<String,String>();
365                    usableProparty.put( "passwd",           "接続ã™ã‚‹ãƒ¦ãƒ¼ã‚¶ãƒ¼ã®ãƒ‘スワーãƒ? );
366                    usableProparty.put( "localFile",        "ローカルã®ãƒ•ァイルå? );
367                    usableProparty.put( "port",                     "接続ã«åˆ©ç”¨ã™ã‚‹ãƒã?ト番å·ã‚’設定ã—ã¾ã™ã?" );
368                    usableProparty.put( "keyFile",          "公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?" );
369                    usableProparty.put( "command",          "SFTPサーãƒã?å´ã§ã®å‡¦ç??æ–¹æ³?GET/PUT/DEL)を指定ã—ã¾ã?åˆæœŸå€¤:GET)" );
370                    usableProparty.put( "mkdirs",           "å—ã‘å´ãƒ•ァイル(GETæ™?LOCALã€PUTæ™?SFTPサーãƒã?)ã«ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:true)" );
371                    usableProparty.put( "timeout",          "Dataタイãƒ?‚¢ã‚¦ãƒ?åˆæœŸå€¤:600 [ç§’])" );
372                    usableProparty.put( "display",          "[false/true]:trueã¯ã€æ¤œç´¢çжæ³ã‚’表示ã—ã¾ã?åˆæœŸå€¤:false)" );
373                    usableProparty.put( "debug",            "ãƒ?ƒãƒ?‚°æƒ??を標準å?力ã«è¡¨ç¤ºã™ã‚‹(true)ã‹ã—ãªã?false)ã? +
374                                                                                            CR + "(åˆæœŸå€¤:false:表示ã—ãªã?" );
375            }
376    
377            private static final String[] CMD_LST  = new String[] { "GET","PUT","DEL","GETDIR","PUTDIR","DELDIR" };
378    
379            /**
380             * ã“ã?クラスã®å‹•作確èªç”¨ã®ã€main メソãƒ?ƒ‰ã§ã™ã?
381             *
382             * @param       args    コマンド引数é…å?
383             */
384            public static void main( final String[] args ) {
385                    Argument arg = new Argument( "org.opengion.fukurou.util.SFTPConnect" );
386                    arg.setMustProparty( mustProparty );
387                    arg.setUsableProparty( usableProparty );
388                    arg.setArgument( args );
389    
390                    SFTPConnect sftp = new SFTPConnect();
391    
392                    String host   = arg.getProparty( "host");                       // SFTPサーãƒã?
393                    String user   = arg.getProparty( "user" );                      // ユーザー
394                    String passwd = arg.getProparty( "passwd" );            // パスワー�
395    
396                    sftp.setHostUserPass( host , user , passwd );
397    
398                    sftp.setPort(           arg.getProparty( "port"                                 ) );    // 接続ã«åˆ©ç”¨ã™ã‚‹ãƒã?ト番å·ã‚’設定ã—ã¾ã™ã?
399                    sftp.setKeyFile(        arg.getProparty( "keyFile"                              ) );    // 公開キー暗å·åŒ–æ–¹å¼ã‚’利用ã™ã‚‹å ´åˆã?キーファイルåã‚’æŒ?®šã—ã¾ã™ã?
400                    sftp.setMkdirs(         arg.getProparty( "mkdirs"       ,true           ) );    // å—ã‘å´ãƒ•ァイルã«ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ã‹ã©ã?‹
401                    sftp.setTimeout(        arg.getProparty( "timeout"      ,TIMEOUT        ) );    // Dataタイãƒ?‚¢ã‚¦ãƒ?åˆæœŸå€¤:600 [ç§’])
402                    sftp.setDisplay(        arg.getProparty( "display"      ,false          ) );    // trueã¯ã€æ¤œç´¢çжæ³ã‚’表示ã—ã¾ã?åˆæœŸå€¤:false)
403                    sftp.setDebug(          arg.getProparty( "debug"        ,false          ) );    // ãƒ?ƒãƒ?‚°æƒ??を標準å?力ã«è¡¨ç¤ºã™ã‚‹(true)ã‹ã—ãªã?false)ã?
404    
405                    try {
406                            // コãƒã‚¯ãƒˆã—ã¾ã™ã?
407                            sftp.connect();
408    
409                            String command          = arg.getProparty( "command" ,"GET" ,CMD_LST  );        // SFTP処ç??方法をæŒ?®šã—ã¾ã™ã?
410                            String localFile        = arg.getProparty( "localFile"  );                                      // ローカルã®ãƒ•ァイルå?
411                            String remoteFile       = arg.getProparty( "remoteFile" );                                      // SFTPå…ˆã?ファイルå?
412    
413                            // command , localFile , remoteFile ã‚’å?ã«ã€SFTP処ç?‚’行ã„ã¾ã™ã?
414                            sftp.action( command,localFile,remoteFile );
415                    }
416                    catch( RuntimeException ex ) {
417                            System.err.println( sftp.getErrMsg() );
418                    }
419                    finally {
420                            // ホストã¨ã®æŽ¥ç¶šã‚’終äº?—ã¾ã™ã?
421                            sftp.disconnect();
422                    }
423            }
424    }