ラベル H2Database_OrionSSH2 の投稿を表示しています。 すべての投稿を表示
ラベル H2Database_OrionSSH2 の投稿を表示しています。 すべての投稿を表示

2011年2月17日木曜日

H2 DatabaseとOrion SSH2を使用してサーバ上でコマンドを実行する関数を作成する

H2 DatabaseとOrion SSH2を使用してサーバ上でコマンドを実行する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_exec as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
String orionssh2_exec(String host, String user,
String password, String cmd)
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( cmd == null ){
throw new Exception("cmd is not specified.");
}

com.trilead.ssh2.Connection conn = null;
Session sess = null;
String result = null;

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

// コマンド実行
sess = conn.openSession();
sess.execCommand(cmd);
// 標準出力
BufferedReader stdout = new BufferedReader(
new InputStreamReader(
new StreamGobbler(sess.getStdout()), "UTF-8"));
StringBuffer sbuf = new StringBuffer();
String line = "";
while((line = stdout.readLine()) != null ){
sbuf.append(line);
sbuf.append("\n");
}
result = sbuf.toString();
}
finally
{
if( sess != null )sess.close();
if( conn != null )conn.close();
}

return result;
}
$$


実行例
select orionssh2_exec(
'192.168.1.25',
'user', 'password',
'ls -alF')


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

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

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

2011年2月15日火曜日

H2 DatabaseとOrion SSH2を使用してBLOBをSFTP上へアップロードする関数を作成する

H2 DatabaseとOrion SSH2を使用してBLOBをSFTP上へアップロードする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_put_binary as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_put_binary(String host, String user,
String password, String path, Blob blob)
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;

InputStream bis = null;
SFTPv3FileHandle fh = 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");

// ファイルをアップロード
fh = sftpc.createFile(path);
bis = blob.getBinaryStream();
byte buf[] = new byte[8192];
long fo = 0;
int sz = 0;
while((sz = bis.read(buf, 0, 8192)) != -1){
sftpc.write(fh, fo, buf, 0, sz);
fo += sz;
}

}
finally
{
if( bis != null )bis.close();
if( fh != null )sftpc.closeFile(fh);
if( sftpc != null )sftpc.close();
if( conn != null )conn.close();
}

return new Integer(0);
}
$$


実行例
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
orionssh2_get_binary(
'192.168.1.25',
'user', 'password',
'sf.png')
);
select orionssh2_put_binary(
'192.168.1.25',
'user', 'password',
'upload.png', c2)
from blobtest
where c1 = 1;


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

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

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

2011年2月13日日曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルをダウンロードしてBLOBとして返す関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでファイルをダウンロードしてBLOBとして返す関数を作成するには、以下の手順を実行します。

1.以下のソースファイルをantなどでコンパイルします。
Orionssh2GetBinaryFunction.java
package com.serverarekore.h2;
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;

public class Orionssh2GetBinaryFunction
{
static public InputStream orionssh2_get_binary(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;

// サーバに接続
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");

// ファイルをダウンロード
final File tf = File.createTempFile("lobtmp", null);
SFTPv3FileHandle fh = sftpc.openFileRO(path);

BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(tf));
byte buf[] = new byte[8192];
long fo = 0;
int sz = 0;
while((sz = sftpc.read(fh, fo, buf, 0, 8192)) != -1){
bos.write(buf, 0, sz);
fo += sz;
}
sftpc.closeFile(fh);
bos.flush();
bos.close();

InputStream ris = new InputStream(){
private final InputStream fis = new FileInputStream(tf);
private boolean deleted = false;
private final File fn = tf;
public int autoDelete(int sz) throws IOException
{
if(sz < 0 ){
close();
}
return sz;
}
public void close() throws IOException
{
if( !deleted ){
fis.close();
fn.delete();
deleted = true;
}
}
public int read(byte buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf, off, len));
}

public int read(byte buf[]) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf));
}

public int read() throws IOException
{
return deleted?-1:autoDelete(fis.read());
}
};

return ris;
}
}

build.xmlのサンプル
<project name="H2Functions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="c:/Program Files/H2/bin/h2-1.3.149.jar"/>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:/share/jars/Orionssh2GetBinary.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.システム環境変数CLASSPATHにコンパイルしてできたOrionssh2GetBinary.jarを追加し、
H2 Databaseのサービスを再起動。

3.H2 Consoleから以下のコマンドを実行して、ファンクション作成。
create alias orionssh2_get_binary for 
"com.serverarekore.h2.Orionssh2GetBinaryFunction.orionssh2_get_binary";

4.以下のようなSQLでSFTP上のファイルをBLOBに格納できます。
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
orionssh2_get_binary(
'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に関する他の記事はこちらを参照してください。

2011年2月11日金曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルを移動・リネームする関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでファイルを移動・リネームする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_mv as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_mv(String host, String user,
String password, String path1, String path2)
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( path1 == null ){
throw new Exception("path1 is not specified.");
}
if( path2 == null ){
throw new Exception("path2 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");

// ファイルを移動・リネーム
sftpc.mv(path1, path2);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_mv('192.168.1.25', 'user', 'password', 'sf.png', './test/sf2.png')


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

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

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

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に関する他の記事はこちらを参照してください。

2011年2月7日月曜日

H2 DatabaseとOrion SSH2を使用してSFTPでシンボリックリンクのターゲットを表示する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでシンボリックリンクのターゲットを表示する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_read_link as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
String orionssh2_read_link(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.readLink(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return result;
}
$$


実行例
select orionssh2_read_link('192.168.1.25', 'user', 'password', 'symlink_sf.png')


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

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

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

2011年2月5日土曜日

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを削除する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを削除する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_rmdir as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_rmdir(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;
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");

// ディレクトリ削除
sftpc.rmdir(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_rmdir('192.168.1.25', 'user', 'password', 'test_dir')


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

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

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

2011年2月3日木曜日

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを作成する関数を作成する

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

create alias if not exists orionssh2_mkdir as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_mkdir(String host, String user,
String password, String path, Integer permission)
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.");
}
if( permission == null ){
throw new Exception("permission is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = 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");

// ディレクトリ作成
sftpc.mkdir(path, permission);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_mkdir('192.168.1.25', 'user', 'password', 'test_dir', 0x777)


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

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

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

2011年2月1日火曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルのパーミッションを取得する関数を作成する

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

create alias if not exists orionssh2_get_permissions 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_permissions(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;
SFTPv3FileAttributes fa = 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");

// ファイルの情報を取得
fa = sftpc.lstat(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return fa.getOctalPermissions();
}
$$


実行例
select orionssh2_get_permissions('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に関する他の記事はこちらを参照してください。

2011年1月30日日曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルのGIDを取得する関数を作成する

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

create alias if not exists orionssh2_get_gid as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_get_gid(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;
SFTPv3FileAttributes fa = 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");

// ファイルの情報を取得
fa = sftpc.lstat(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(fa.gid);
}
$$


実行例
select orionssh2_get_gid('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に関する他の記事はこちらを参照してください。

2011年1月28日金曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルのUIDを取得する関数を作成する

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

create alias if not exists orionssh2_get_uid as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_get_uid(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;
SFTPv3FileAttributes fa = 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");

// ファイルの情報を取得
fa = sftpc.lstat(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(fa.uid);
}
$$


実行例
select orionssh2_get_uid('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に関する他の記事はこちらを参照してください。

2011年1月26日水曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルの最終更新日時を取得する関数を作成する

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

create alias if not exists orionssh2_get_mtime as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
java.sql.Timestamp orionssh2_get_mtime(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;
SFTPv3FileAttributes fa = 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");

// ファイルの情報を取得
fa = sftpc.lstat(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new java.sql.Timestamp(fa.mtime*1000);
}
$$


実行例
select orionssh2_get_mtime('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に関する他の記事はこちらを参照してください。

2011年1月24日月曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルのサイズを取得する関数を作成する

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

create alias if not exists orionssh2_get_size as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Long orionssh2_get_size(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;
SFTPv3FileAttributes fa = 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");

// ファイルの情報を取得
fa = sftpc.lstat(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return fa.size;
}
$$


実行例
select orionssh2_get_size('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に関する他の記事はこちらを参照してください。

2011年1月22日土曜日

H2 DatabaseとOrion SSH2を使用してSFTPでシンボリックリンクを作成する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでシンボリックリンクを作成する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_create_symlink as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_create_symlink(String host, String user,
String password, String path1, String path2)
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( path1 == null ){
throw new Exception("path1 is not specified.");
}
if( path2 == null ){
throw new Exception("path2 is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = 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");

// シンボリックリンク作成
sftpc.createSymlink(path1, path2);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_create_symlink('192.168.1.25', 
'user', 'password', './symlink_sf.png', './sf.png')


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

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

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

2011年1月20日木曜日

H2 DatabaseとOrion SSH2でサーバ上のファイルを列挙する関数を作成する

H2 DatabaseとOrion SSH2でサーバ上のファイルを列挙する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_list_files as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
ResultSet orionssh2_list_files(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 )path = ".";

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("PATH", Types.VARCHAR, 1024, 0);

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = 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");

// ファイル列挙
Vector files = sftpc.ls(path);
for(int fi=0;fi<files.size();fi++){
SFTPv3DirectoryEntry dirent =
(SFTPv3DirectoryEntry)files.get(fi);
rs.addRow(dirent.filename);
}
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return rs;
}
$$


実行例
select * from orionssh2_list_files('192.168.1.25', 'user', 'password', '.')


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

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

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