2011年2月9日水曜日

H2 DatabaseとOrion SSH2を使用してSFTPでフルパスを取得する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでフルパスを取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_get_fullpath as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
String orionssh2_get_fullpath(String host, String user,
String password, String path)
throws Exception
{
if( host == null ){
throw new Exception("server is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null )password = "";
if( path == null ){
throw new Exception("path is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
String result = null;
try
{
// サーバに接続
conn = new com.trilead.ssh2.Connection(host);
conn.connect();
if( !conn.authenticateWithPassword(user, password) ){
throw new Exception("authentication failed.");
}

// SFTP client作成
sftpc = new SFTPv3Client(conn);
sftpc.setCharset("UTF-8");

// フルパスを取得
result = sftpc.canonicalPath(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return result;
}
$$


実行例
select orionssh2_get_fullpath('192.168.1.25', 'user', 'password', 'sf.png')


※以下のjarをCLASSPATH環境変数に追加
orion-ssh2-214.jar

○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07)

○関連情報
H2 Database上でOrion SSH2を使用するユーザー定義関数のまとめ
・H2 Databaseに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿