rjb 1.4.5 → 1.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +504 -504
- data/ChangeLog +6 -1
- data/data/rjb/jp/co/infoseek/hp/arton/rjb/RBridge.class +0 -0
- data/ext/RBridge.java +39 -39
- data/ext/depend +23 -23
- data/ext/extconf.h +1 -0
- data/ext/extconf.rb +77 -77
- data/ext/jp_co_infoseek_hp_arton_rjb_RBridge.h +21 -21
- data/ext/load.c +408 -408
- data/ext/riconv.c +218 -218
- data/ext/riconv.h +24 -24
- data/ext/rjb.c +3317 -3317
- data/ext/rjb.h +164 -164
- data/ext/rjbexception.c +167 -167
- data/lib/rjb.rb +122 -122
- data/readme.txt +35 -35
- data/samples/filechooser.rb +35 -35
- data/samples/unzip.rb +66 -66
- data/test/gctest.rb +24 -24
- data/test/listtest.rb +56 -56
- data/test/osx_jvmcheck.rb +9 -9
- data/test/rjbtest.jar +0 -0
- data/test/test.rb +873 -873
- data/test/test_osxjvm.rb +23 -23
- data/test/test_osxload.rb +64 -64
- data/test/test_unload.rb +0 -0
- metadata +15 -17
- data/lib/rjbcore.so +0 -0
data/ChangeLog
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
+
Sat Jan 26 2013 arton
|
2
|
+
*ext/rjb.c
|
3
|
+
RJB_VERSION -> 1.4.6 for new gem
|
4
|
+
*RBridge.class
|
5
|
+
Java Major Version -> 46
|
1
6
|
Sun Jan 13 2013 arton
|
2
7
|
*test/test.rb
|
3
8
|
add bignum argument test
|
4
9
|
*ext/rjb.c
|
5
10
|
accept bignum argument for 'J'
|
6
|
-
|
11
|
+
this bug(or at least mis spec.) was pointed by janroesner. Thanks janroesner !
|
7
12
|
Tue Dec 17 arton
|
8
13
|
*test/test.rb
|
9
14
|
add test_norarg_invoke
|
Binary file
|
data/ext/RBridge.java
CHANGED
@@ -1,39 +1,39 @@
|
|
1
|
-
/*
|
2
|
-
* Rjb - Ruby <-> Java Bridge
|
3
|
-
* Copyright(c) 2004 arton
|
4
|
-
*
|
5
|
-
* This library is free software; you can redistribute it and/or
|
6
|
-
* modify it under the terms of the GNU Lesser General Public
|
7
|
-
* License as published by the Free Software Foundation; either
|
8
|
-
* version 2.1 of the License, or (at your option) any later version.
|
9
|
-
*
|
10
|
-
* This library is distributed in the hope that it will be useful,
|
11
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
-
* Lesser General Public License for more details.
|
14
|
-
*
|
15
|
-
* $Id: RBridge.java 2 2006-04-11 19:04:40Z arton $
|
16
|
-
* $Log: RBridge.java,v $
|
17
|
-
* Revision 1.2 2004/06/19 09:05:00 arton
|
18
|
-
* delete debug lines
|
19
|
-
*
|
20
|
-
* Revision 1.1 2004/06/19 09:00:19 arton
|
21
|
-
* Initial revision
|
22
|
-
*
|
23
|
-
*/
|
24
|
-
package jp.co.infoseek.hp.arton.rjb;
|
25
|
-
import java.lang.reflect.InvocationHandler;
|
26
|
-
import java.lang.reflect.Method;
|
27
|
-
import java.lang.reflect.Proxy;
|
28
|
-
|
29
|
-
public class RBridge implements InvocationHandler {
|
30
|
-
public Object register(Class itf) {
|
31
|
-
return Proxy.newProxyInstance(itf.getClassLoader(),
|
32
|
-
new Class[] { itf }, this);
|
33
|
-
}
|
34
|
-
public Object invoke(Object proxy, Method method, Object[] args)
|
35
|
-
throws Throwable {
|
36
|
-
return call(method.getName(), proxy, args);
|
37
|
-
}
|
38
|
-
private native Object call(String methodName, Object target, Object[] args);
|
39
|
-
}
|
1
|
+
/*
|
2
|
+
* Rjb - Ruby <-> Java Bridge
|
3
|
+
* Copyright(c) 2004 arton
|
4
|
+
*
|
5
|
+
* This library is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU Lesser General Public
|
7
|
+
* License as published by the Free Software Foundation; either
|
8
|
+
* version 2.1 of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This library is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
* Lesser General Public License for more details.
|
14
|
+
*
|
15
|
+
* $Id: RBridge.java 2 2006-04-11 19:04:40Z arton $
|
16
|
+
* $Log: RBridge.java,v $
|
17
|
+
* Revision 1.2 2004/06/19 09:05:00 arton
|
18
|
+
* delete debug lines
|
19
|
+
*
|
20
|
+
* Revision 1.1 2004/06/19 09:00:19 arton
|
21
|
+
* Initial revision
|
22
|
+
*
|
23
|
+
*/
|
24
|
+
package jp.co.infoseek.hp.arton.rjb;
|
25
|
+
import java.lang.reflect.InvocationHandler;
|
26
|
+
import java.lang.reflect.Method;
|
27
|
+
import java.lang.reflect.Proxy;
|
28
|
+
|
29
|
+
public class RBridge implements InvocationHandler {
|
30
|
+
public Object register(Class itf) {
|
31
|
+
return Proxy.newProxyInstance(itf.getClassLoader(),
|
32
|
+
new Class[] { itf }, this);
|
33
|
+
}
|
34
|
+
public Object invoke(Object proxy, Method method, Object[] args)
|
35
|
+
throws Throwable {
|
36
|
+
return call(method.getName(), proxy, args);
|
37
|
+
}
|
38
|
+
private native Object call(String methodName, Object target, Object[] args);
|
39
|
+
}
|
data/ext/depend
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
riconv.o : riconv.c jp_co_infoseek_hp_arton_rjb_RBridge.h
|
2
|
-
rjb.o : rjb.c jp_co_infoseek_hp_arton_rjb_RBridge.h riconv.h rjb.h
|
3
|
-
rjbexception.o : rjbexception.c jp_co_infoseek_hp_arton_rjb_RBridge.h riconv.h rjb.h
|
4
|
-
load.o : load.c jp_co_infoseek_hp_arton_rjb_RBridge.h
|
5
|
-
jp_co_infoseek_hp_arton_rjb_RBridge.h : jniwrap.h ../data/rjb/jp/co/infoseek/hp/arton/rjb/RBridge.class
|
6
|
-
javah -classpath ../data/rjb jp.co.infoseek.hp.arton.rjb.RBridge
|
7
|
-
../data/rjb/jp/co/infoseek/hp/arton/rjb/RBridge.class : RBridge.java
|
8
|
-
mkdir -p ../data/rjb/jp/co/infoseek/hp/arton/rjb
|
9
|
-
javac -d ../data/rjb RBridge.java
|
10
|
-
test : rjbcore.so ../test/jp/co/infoseek/hp/arton/rjb/Test.class ../test/jp/co/infoseek/hp/arton/rjb/IBase.class ../test/jp/co/infoseek/hp/arton/rjb/Base.class ../test/jp/co/infoseek/hp/arton/rjb/ExtBase.class
|
11
|
-
ruby ../test/test.rb
|
12
|
-
../test/jp/co/infoseek/hp/arton/rjb/Test.class : ../test/Test.java
|
13
|
-
javac ../test/Test.java
|
14
|
-
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/Test.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
15
|
-
../test/jp/co/infoseek/hp/arton/rjb/IBase.class : ../test/IBase.java
|
16
|
-
javac ../test/IBase.java
|
17
|
-
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/IBase.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
18
|
-
../test/jp/co/infoseek/hp/arton/rjb/Base.class : ../test/Base.java ../test/jp/co/infoseek/hp/arton/rjb/IBase.class
|
19
|
-
javac -classpath ../test ../test/Base.java
|
20
|
-
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/Base.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
21
|
-
../test/jp/co/infoseek/hp/arton/rjb/ExtBase.class : ../test/ExtBase.java
|
22
|
-
javac -classpath ../test ../test/ExtBase.java
|
23
|
-
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/ExtBase.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
1
|
+
riconv.o : riconv.c jp_co_infoseek_hp_arton_rjb_RBridge.h
|
2
|
+
rjb.o : rjb.c jp_co_infoseek_hp_arton_rjb_RBridge.h riconv.h rjb.h
|
3
|
+
rjbexception.o : rjbexception.c jp_co_infoseek_hp_arton_rjb_RBridge.h riconv.h rjb.h
|
4
|
+
load.o : load.c jp_co_infoseek_hp_arton_rjb_RBridge.h
|
5
|
+
jp_co_infoseek_hp_arton_rjb_RBridge.h : jniwrap.h ../data/rjb/jp/co/infoseek/hp/arton/rjb/RBridge.class
|
6
|
+
javah -classpath ../data/rjb jp.co.infoseek.hp.arton.rjb.RBridge
|
7
|
+
../data/rjb/jp/co/infoseek/hp/arton/rjb/RBridge.class : RBridge.java
|
8
|
+
mkdir -p ../data/rjb/jp/co/infoseek/hp/arton/rjb
|
9
|
+
javac -d ../data/rjb RBridge.java
|
10
|
+
test : rjbcore.so ../test/jp/co/infoseek/hp/arton/rjb/Test.class ../test/jp/co/infoseek/hp/arton/rjb/IBase.class ../test/jp/co/infoseek/hp/arton/rjb/Base.class ../test/jp/co/infoseek/hp/arton/rjb/ExtBase.class
|
11
|
+
ruby ../test/test.rb
|
12
|
+
../test/jp/co/infoseek/hp/arton/rjb/Test.class : ../test/Test.java
|
13
|
+
javac ../test/Test.java
|
14
|
+
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/Test.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
15
|
+
../test/jp/co/infoseek/hp/arton/rjb/IBase.class : ../test/IBase.java
|
16
|
+
javac ../test/IBase.java
|
17
|
+
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/IBase.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
18
|
+
../test/jp/co/infoseek/hp/arton/rjb/Base.class : ../test/Base.java ../test/jp/co/infoseek/hp/arton/rjb/IBase.class
|
19
|
+
javac -classpath ../test ../test/Base.java
|
20
|
+
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/Base.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
21
|
+
../test/jp/co/infoseek/hp/arton/rjb/ExtBase.class : ../test/ExtBase.java
|
22
|
+
javac -classpath ../test ../test/ExtBase.java
|
23
|
+
$(RUBY) -r fileutils -e 'FileUtils.mkdir_p "../test/jp/co/infoseek/hp/arton/rjb";FileUtils.mv("../test/ExtBase.class", "../test/jp/co/infoseek/hp/arton/rjb")'
|
data/ext/extconf.h
CHANGED
data/ext/extconf.rb
CHANGED
@@ -1,77 +1,77 @@
|
|
1
|
-
#----------------------------------
|
2
|
-
# extconf.rb
|
3
|
-
# $Revision: $
|
4
|
-
# $Date: $
|
5
|
-
#----------------------------------
|
6
|
-
require 'mkmf'
|
7
|
-
|
8
|
-
class Path
|
9
|
-
|
10
|
-
def initialize()
|
11
|
-
if File::ALT_SEPARATOR.nil?
|
12
|
-
@file_separator = File::SEPARATOR
|
13
|
-
else
|
14
|
-
@file_separator = File::ALT_SEPARATOR
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def include(parent, child)
|
19
|
-
inc = joint(parent, child)
|
20
|
-
$INCFLAGS += " -I\"#{inc}\""
|
21
|
-
$CFLAGS += " -I\"#{inc}\""
|
22
|
-
inc
|
23
|
-
end
|
24
|
-
|
25
|
-
def joint(parent, child)
|
26
|
-
parent + @file_separator + child
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
javahome = ENV['JAVA_HOME']
|
32
|
-
if javahome.nil? && RUBY_PLATFORM =~ /darwin/
|
33
|
-
javahome = `/usr/libexec/java_home`.strip
|
34
|
-
end
|
35
|
-
unless javahome.nil?
|
36
|
-
if javahome[0] == ?" && javahome[-1] == ?"
|
37
|
-
javahome = javahome[1..-2]
|
38
|
-
end
|
39
|
-
raise "JAVA_HOME is not directory." unless File.directory?(javahome)
|
40
|
-
pt = Path.new
|
41
|
-
inc = pt.include(javahome, 'include')
|
42
|
-
if !File.exists?(inc) && RUBY_PLATFORM =~ /darwin/
|
43
|
-
inc = pt.include('/System/Library/Frameworks/JavaVM.framework', 'Headers')
|
44
|
-
end
|
45
|
-
Dir.open(inc).each do |d|
|
46
|
-
next if d[0] == ?.
|
47
|
-
if File.directory?(pt.joint(inc, d))
|
48
|
-
pt.include(inc, d)
|
49
|
-
break
|
50
|
-
end
|
51
|
-
end
|
52
|
-
else
|
53
|
-
raise "JAVA_HOME is not set."
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
def create_rjb_makefile
|
58
|
-
if have_header("jni.h")
|
59
|
-
have_func("locale_charset", "iconv.h")
|
60
|
-
have_func("nl_langinfo", "langinfo.h")
|
61
|
-
have_func("setlocale", "locale.h")
|
62
|
-
have_func("getenv")
|
63
|
-
$defs << "-DRJB_RUBY_VERSION_CODE="+RUBY_VERSION.gsub(/\./, '')
|
64
|
-
create_header
|
65
|
-
create_makefile("rjbcore")
|
66
|
-
else
|
67
|
-
raise "no jni.h in " + $INCFLAGS
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
case RUBY_PLATFORM
|
72
|
-
when /mswin32/
|
73
|
-
$CFLAGS += ' /W3'
|
74
|
-
when /cygwin/, /mingw/
|
75
|
-
$defs << '-DNONAMELESSUNION'
|
76
|
-
end
|
77
|
-
create_rjb_makefile
|
1
|
+
#----------------------------------
|
2
|
+
# extconf.rb
|
3
|
+
# $Revision: $
|
4
|
+
# $Date: $
|
5
|
+
#----------------------------------
|
6
|
+
require 'mkmf'
|
7
|
+
|
8
|
+
class Path
|
9
|
+
|
10
|
+
def initialize()
|
11
|
+
if File::ALT_SEPARATOR.nil?
|
12
|
+
@file_separator = File::SEPARATOR
|
13
|
+
else
|
14
|
+
@file_separator = File::ALT_SEPARATOR
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def include(parent, child)
|
19
|
+
inc = joint(parent, child)
|
20
|
+
$INCFLAGS += " -I\"#{inc}\""
|
21
|
+
$CFLAGS += " -I\"#{inc}\""
|
22
|
+
inc
|
23
|
+
end
|
24
|
+
|
25
|
+
def joint(parent, child)
|
26
|
+
parent + @file_separator + child
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
javahome = ENV['JAVA_HOME']
|
32
|
+
if javahome.nil? && RUBY_PLATFORM =~ /darwin/
|
33
|
+
javahome = `/usr/libexec/java_home`.strip
|
34
|
+
end
|
35
|
+
unless javahome.nil?
|
36
|
+
if javahome[0] == ?" && javahome[-1] == ?"
|
37
|
+
javahome = javahome[1..-2]
|
38
|
+
end
|
39
|
+
raise "JAVA_HOME is not directory." unless File.directory?(javahome)
|
40
|
+
pt = Path.new
|
41
|
+
inc = pt.include(javahome, 'include')
|
42
|
+
if !File.exists?(inc) && RUBY_PLATFORM =~ /darwin/
|
43
|
+
inc = pt.include('/System/Library/Frameworks/JavaVM.framework', 'Headers')
|
44
|
+
end
|
45
|
+
Dir.open(inc).each do |d|
|
46
|
+
next if d[0] == ?.
|
47
|
+
if File.directory?(pt.joint(inc, d))
|
48
|
+
pt.include(inc, d)
|
49
|
+
break
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
raise "JAVA_HOME is not set."
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def create_rjb_makefile
|
58
|
+
if have_header("jni.h")
|
59
|
+
have_func("locale_charset", "iconv.h")
|
60
|
+
have_func("nl_langinfo", "langinfo.h")
|
61
|
+
have_func("setlocale", "locale.h")
|
62
|
+
have_func("getenv")
|
63
|
+
$defs << "-DRJB_RUBY_VERSION_CODE="+RUBY_VERSION.gsub(/\./, '')
|
64
|
+
create_header
|
65
|
+
create_makefile("rjbcore")
|
66
|
+
else
|
67
|
+
raise "no jni.h in " + $INCFLAGS
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
case RUBY_PLATFORM
|
72
|
+
when /mswin32/
|
73
|
+
$CFLAGS += ' /W3'
|
74
|
+
when /cygwin/, /mingw/
|
75
|
+
$defs << '-DNONAMELESSUNION'
|
76
|
+
end
|
77
|
+
create_rjb_makefile
|
@@ -1,21 +1,21 @@
|
|
1
|
-
/* DO NOT EDIT THIS FILE - it is machine generated */
|
2
|
-
#include <jni.h>
|
3
|
-
/* Header for class jp_co_infoseek_hp_arton_rjb_RBridge */
|
4
|
-
|
5
|
-
#ifndef _Included_jp_co_infoseek_hp_arton_rjb_RBridge
|
6
|
-
#define _Included_jp_co_infoseek_hp_arton_rjb_RBridge
|
7
|
-
#ifdef __cplusplus
|
8
|
-
extern "C" {
|
9
|
-
#endif
|
10
|
-
/*
|
11
|
-
* Class: jp_co_infoseek_hp_arton_rjb_RBridge
|
12
|
-
* Method: call
|
13
|
-
* Signature: (Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
|
14
|
-
*/
|
15
|
-
JNIEXPORT jobject JNICALL Java_jp_co_infoseek_hp_arton_rjb_RBridge_call
|
16
|
-
(JNIEnv *, jobject, jstring, jobject, jobjectArray);
|
17
|
-
|
18
|
-
#ifdef __cplusplus
|
19
|
-
}
|
20
|
-
#endif
|
21
|
-
#endif
|
1
|
+
/* DO NOT EDIT THIS FILE - it is machine generated */
|
2
|
+
#include <jni.h>
|
3
|
+
/* Header for class jp_co_infoseek_hp_arton_rjb_RBridge */
|
4
|
+
|
5
|
+
#ifndef _Included_jp_co_infoseek_hp_arton_rjb_RBridge
|
6
|
+
#define _Included_jp_co_infoseek_hp_arton_rjb_RBridge
|
7
|
+
#ifdef __cplusplus
|
8
|
+
extern "C" {
|
9
|
+
#endif
|
10
|
+
/*
|
11
|
+
* Class: jp_co_infoseek_hp_arton_rjb_RBridge
|
12
|
+
* Method: call
|
13
|
+
* Signature: (Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
|
14
|
+
*/
|
15
|
+
JNIEXPORT jobject JNICALL Java_jp_co_infoseek_hp_arton_rjb_RBridge_call
|
16
|
+
(JNIEnv *, jobject, jstring, jobject, jobjectArray);
|
17
|
+
|
18
|
+
#ifdef __cplusplus
|
19
|
+
}
|
20
|
+
#endif
|
21
|
+
#endif
|
data/ext/load.c
CHANGED
@@ -1,408 +1,408 @@
|
|
1
|
-
/*
|
2
|
-
* Rjb - Ruby <-> Java Bridge
|
3
|
-
* Copyright(c) 2004,2005,2006,2009,2010,2011 arton
|
4
|
-
*
|
5
|
-
* This library is free software; you can redistribute it and/or
|
6
|
-
* modify it under the terms of the GNU Lesser General Public
|
7
|
-
* License as published by the Free Software Foundation; either
|
8
|
-
* version 2.1 of the License, or (at your option) any later version.
|
9
|
-
*
|
10
|
-
* This library is distributed in the hope that it will be useful,
|
11
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
-
* Lesser General Public License for more details.
|
14
|
-
*
|
15
|
-
* $Id: load.c 180 2011-12-05 16:34:29Z arton $
|
16
|
-
*/
|
17
|
-
|
18
|
-
#include <stdlib.h>
|
19
|
-
#include <stdio.h>
|
20
|
-
#include "ruby.h"
|
21
|
-
#include "extconf.h"
|
22
|
-
#if RJB_RUBY_VERSION_CODE < 190
|
23
|
-
#include "intern.h"
|
24
|
-
#include "st.h"
|
25
|
-
#include "util.h"
|
26
|
-
#else
|
27
|
-
#include "ruby/intern.h"
|
28
|
-
#include "ruby/st.h"
|
29
|
-
#include "ruby/util.h"
|
30
|
-
#endif
|
31
|
-
#include "jniwrap.h"
|
32
|
-
#include "jp_co_infoseek_hp_arton_rjb_RBridge.h"
|
33
|
-
#include "rjb.h"
|
34
|
-
|
35
|
-
#define JVM_TYPE "client"
|
36
|
-
#define ALT_JVM_TYPE "classic"
|
37
|
-
|
38
|
-
#if defined(_WIN32) || defined(__CYGWIN__)
|
39
|
-
#if defined(__CYGWIN__)
|
40
|
-
#define JVMDLL "%s/jre/bin/%s/jvm.dll"
|
41
|
-
#define DIRSEPARATOR '/'
|
42
|
-
#else
|
43
|
-
#define JVMDLL "%s\\jre\\bin\\%s\\jvm.dll"
|
44
|
-
#define DIRSEPARATOR '\\'
|
45
|
-
#if defined(_WIN64)
|
46
|
-
#undef JVM_TYPE
|
47
|
-
#define JVM_TYPE "server"
|
48
|
-
#endif
|
49
|
-
#endif
|
50
|
-
#define CLASSPATH_SEP ';'
|
51
|
-
#elif defined(__APPLE__) && defined(__MACH__)
|
52
|
-
static char* JVMDLL = "%s/JavaVM";
|
53
|
-
#define DIRSEPARATOR '/'
|
54
|
-
#define CLASSPATH_SEP ':'
|
55
|
-
#define HOME_NAME "/Home"
|
56
|
-
#define HOME_NAME_LEN strlen(HOME_NAME)
|
57
|
-
#define DEFAULT_HOME "/System/Library/Frameworks/JavaVM.framework"
|
58
|
-
#elif defined(_AIX)
|
59
|
-
#define ARCH "ppc"
|
60
|
-
#undef JVM_TYPE
|
61
|
-
#define JVM_TYPE "j9vm"
|
62
|
-
#elif defined(__hpux)
|
63
|
-
#define JVMDLL "%s/jre/lib/%s/%s/libjvm.sl"
|
64
|
-
#define ARCH "PA_RISC"
|
65
|
-
#undef JVM_TYPE
|
66
|
-
#define JVM_TYPE "server"
|
67
|
-
#define DIRSEPARATOR '/'
|
68
|
-
#define CLASSPATH_SEP ':'
|
69
|
-
#else /* defined(_WIN32) || defined(__CYGWIN__) */
|
70
|
-
#if defined(__sparc_v9__)
|
71
|
-
#define ARCH "sparcv9"
|
72
|
-
#elif defined(__sparc__)
|
73
|
-
#define ARCH "sparc"
|
74
|
-
#elif defined(__amd64__)
|
75
|
-
#define ARCH "amd64"
|
76
|
-
#undef JVM_TYPE
|
77
|
-
#define JVM_TYPE "server"
|
78
|
-
#elif defined(i586) || defined(__i386__)
|
79
|
-
#define ARCH "i386"
|
80
|
-
#endif
|
81
|
-
#ifndef ARCH
|
82
|
-
#include <sys/systeminfo.h>
|
83
|
-
#endif
|
84
|
-
#define JVMDLL "%s/jre/lib/%s/%s/libjvm.so"
|
85
|
-
#define DIRSEPARATOR '/'
|
86
|
-
#define CLASSPATH_SEP ':'
|
87
|
-
#endif
|
88
|
-
|
89
|
-
#if defined(__APPLE__) && defined(__MACH__)
|
90
|
-
static char* CREATEJVM = "JNI_CreateJavaVM";
|
91
|
-
static char* GETDEFAULTJVMINITARGS = "JNI_GetDefaultJavaVMInitArgs";
|
92
|
-
#else
|
93
|
-
#define CREATEJVM "JNI_CreateJavaVM"
|
94
|
-
#define GETDEFAULTJVMINITARGS "JNI_GetDefaultJavaVMInitArgs"
|
95
|
-
#endif
|
96
|
-
|
97
|
-
typedef int (JNICALL *GETDEFAULTJAVAVMINITARGS)(void*);
|
98
|
-
typedef int (JNICALL *CREATEJAVAVM)(JavaVM**, JNIEnv**, void*);
|
99
|
-
|
100
|
-
|
101
|
-
static VALUE jvmdll = Qnil;
|
102
|
-
static VALUE getdefaultjavavminitargsfunc;
|
103
|
-
static VALUE createjavavmfunc;
|
104
|
-
|
105
|
-
static int open_jvm(char* libpath)
|
106
|
-
{
|
107
|
-
int sstat;
|
108
|
-
VALUE* argv;
|
109
|
-
|
110
|
-
rb_require("dl");
|
111
|
-
#if !defined(RUBINIUS)
|
112
|
-
if (!rb_const_defined_at(rb_cObject, rb_intern("DL")))
|
113
|
-
{
|
114
|
-
rb_raise(rb_eRuntimeError, "Constants DL is not defined.");
|
115
|
-
return 0;
|
116
|
-
}
|
117
|
-
#endif
|
118
|
-
argv = ALLOCA_N(VALUE, 4);
|
119
|
-
*argv = rb_const_get(rb_cObject, rb_intern("DL"));
|
120
|
-
*(argv + 1) = rb_intern("dlopen");
|
121
|
-
*(argv + 2) = 1;
|
122
|
-
*(argv + 3) = rb_str_new2(libpath);
|
123
|
-
jvmdll = rb_protect(rjb_safe_funcall, (VALUE)argv, &sstat);
|
124
|
-
if (sstat)
|
125
|
-
{
|
126
|
-
return 0;
|
127
|
-
}
|
128
|
-
/* get function pointers of JNI */
|
129
|
-
#if RJB_RUBY_VERSION_CODE < 190
|
130
|
-
getdefaultjavavminitargsfunc = rb_funcall(rb_funcall(rb_funcall(jvmdll, rb_intern("[]"), 2, rb_str_new2(GETDEFAULTJVMINITARGS), rb_str_new2("IP")), rb_intern("to_ptr"), 0), rb_intern("to_i"), 0);
|
131
|
-
createjavavmfunc = rb_funcall(rb_funcall(rb_funcall(jvmdll, rb_intern("[]"), 2, rb_str_new2(CREATEJVM), rb_str_new2("IPPP")), rb_intern("to_ptr"), 0), rb_intern("to_i"), 0);
|
132
|
-
#else
|
133
|
-
getdefaultjavavminitargsfunc = rb_funcall(jvmdll, rb_intern("[]"), 1, rb_str_new2(GETDEFAULTJVMINITARGS));
|
134
|
-
createjavavmfunc = rb_funcall(jvmdll, rb_intern("[]"), 1, rb_str_new2(CREATEJVM));
|
135
|
-
#endif
|
136
|
-
return 1;
|
137
|
-
}
|
138
|
-
|
139
|
-
#if defined(__APPLE__) && defined(__MACH__)
|
140
|
-
static int file_exist(const char* dir, const char* file)
|
141
|
-
{
|
142
|
-
VALUE path = rb_funcall(rb_cFile, rb_intern("join"), 2,
|
143
|
-
rb_str_new2(dir), rb_str_new2(file));
|
144
|
-
VALUE ret = rb_funcall(rb_cFile, rb_intern("exist?"), 1, path);
|
145
|
-
return RTEST(ret);
|
146
|
-
}
|
147
|
-
#endif
|
148
|
-
|
149
|
-
/*
|
150
|
-
* not completed, only valid under some circumstances.
|
151
|
-
*/
|
152
|
-
static int load_jvm(const char* jvmtype)
|
153
|
-
{
|
154
|
-
char* libpath;
|
155
|
-
char* java_home;
|
156
|
-
char* jh;
|
157
|
-
|
158
|
-
jh = getenv("JAVA_HOME");
|
159
|
-
#if defined(__APPLE__) && defined(__MACH__)
|
160
|
-
if (!jh)
|
161
|
-
{
|
162
|
-
jh = DEFAULT_HOME;
|
163
|
-
}
|
164
|
-
else
|
165
|
-
{
|
166
|
-
if (strlen(jh) > HOME_NAME_LEN)
|
167
|
-
{
|
168
|
-
size_t len = strlen(jh);
|
169
|
-
char* p = ALLOCA_N(char, len + 8);
|
170
|
-
jh = strcpy(p, jh);
|
171
|
-
if (*(jh + len - 1) == '/')
|
172
|
-
{
|
173
|
-
--len;
|
174
|
-
*(jh + len) = '\0';
|
175
|
-
}
|
176
|
-
if (strcasecmp(jh + len - HOME_NAME_LEN, HOME_NAME) == 0)
|
177
|
-
{
|
178
|
-
strcpy(p + len, "/..");
|
179
|
-
}
|
180
|
-
}
|
181
|
-
if (!jvmtype && !file_exist(jh, "JavaVM"))
|
182
|
-
{
|
183
|
-
jh = DEFAULT_HOME;
|
184
|
-
}
|
185
|
-
}
|
186
|
-
#endif
|
187
|
-
if (!jh)
|
188
|
-
{
|
189
|
-
if (RTEST(ruby_verbose))
|
190
|
-
{
|
191
|
-
fprintf(stderr, "no JAVA_HOME environment\n");
|
192
|
-
}
|
193
|
-
return 0;
|
194
|
-
}
|
195
|
-
#if defined(_WIN32)
|
196
|
-
if (*jh == '"' && *(jh + strlen(jh) - 1) == '"')
|
197
|
-
{
|
198
|
-
char* p = ALLOCA_N(char, strlen(jh) + 1);
|
199
|
-
strcpy(p, jh + 1);
|
200
|
-
*(p + strlen(p) - 1) = '\0';
|
201
|
-
jh = p;
|
202
|
-
}
|
203
|
-
#endif
|
204
|
-
java_home = ALLOCA_N(char, strlen(jh) + 1);
|
205
|
-
strcpy(java_home, jh);
|
206
|
-
if (*(java_home + strlen(jh) - 1) == DIRSEPARATOR)
|
207
|
-
{
|
208
|
-
*(java_home + strlen(jh) - 1) = '\0';
|
209
|
-
}
|
210
|
-
#if defined(_WIN32) || defined(__CYGWIN__)
|
211
|
-
libpath = ALLOCA_N(char, sizeof(JVMDLL) + strlen(java_home)
|
212
|
-
+ strlen(jvmtype) + 1);
|
213
|
-
sprintf(libpath, JVMDLL, java_home, jvmtype);
|
214
|
-
#elif defined(__APPLE__) && defined(__MACH__)
|
215
|
-
libpath = ALLOCA_N(char, sizeof(JVMDLL) + strlen(java_home) + 1);
|
216
|
-
sprintf(libpath, JVMDLL, java_home);
|
217
|
-
#else /* not Windows / MAC OS-X */
|
218
|
-
libpath = ALLOCA_N(char, sizeof(JVMDLL) + strlen(java_home)
|
219
|
-
+ strlen(ARCH) + strlen(jvmtype) + 1);
|
220
|
-
sprintf(libpath, JVMDLL, java_home, ARCH, jvmtype);
|
221
|
-
#endif
|
222
|
-
return open_jvm(libpath);
|
223
|
-
}
|
224
|
-
|
225
|
-
static int load_bridge(JNIEnv* jenv)
|
226
|
-
{
|
227
|
-
JNINativeMethod nmethod[1];
|
228
|
-
jbyte buff[8192];
|
229
|
-
char* bridge;
|
230
|
-
size_t len;
|
231
|
-
FILE* f;
|
232
|
-
#if defined(RUBINIUS)
|
233
|
-
VALUE v = rb_const_get(rb_cObject, rb_intern("RjbConf"));
|
234
|
-
v = rb_const_get(v, rb_intern("BRIDGE_FILE"));
|
235
|
-
#else
|
236
|
-
VALUE v = rb_const_get_at(rb_const_get(rb_cObject, rb_intern("RjbConf")),
|
237
|
-
rb_intern("BRIDGE_FILE"));
|
238
|
-
#endif
|
239
|
-
bridge = StringValuePtr(v);
|
240
|
-
#if defined(DOSISH)
|
241
|
-
bridge = ALLOCA_N(char, strlen(bridge) + 8);
|
242
|
-
strcpy(bridge, StringValuePtr(v));
|
243
|
-
for (len = 0; bridge[len]; len++)
|
244
|
-
{
|
245
|
-
if (bridge[len] == '/')
|
246
|
-
{
|
247
|
-
bridge[len] = '\\';
|
248
|
-
}
|
249
|
-
}
|
250
|
-
#endif
|
251
|
-
f = fopen(bridge, "rb");
|
252
|
-
if (f == NULL)
|
253
|
-
{
|
254
|
-
return -1;
|
255
|
-
}
|
256
|
-
len = fread(buff, 1, sizeof(buff), f);
|
257
|
-
fclose(f);
|
258
|
-
rjb_rbridge = (*jenv)->DefineClass(jenv,
|
259
|
-
"jp/co/infoseek/hp/arton/rjb/RBridge", get_systemloader(jenv), buff, len);
|
260
|
-
if (rjb_rbridge == NULL)
|
261
|
-
{
|
262
|
-
rjb_check_exception(jenv, 1);
|
263
|
-
}
|
264
|
-
rjb_register_bridge = (*jenv)->GetMethodID(jenv, rjb_rbridge, "register",
|
265
|
-
"(Ljava/lang/Class;)Ljava/lang/Object;");
|
266
|
-
nmethod[0].name = "call";
|
267
|
-
nmethod[0].signature = "(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;";
|
268
|
-
nmethod[0].fnPtr = Java_jp_co_infoseek_hp_arton_rjb_RBridge_call;
|
269
|
-
(*jenv)->RegisterNatives(jenv, rjb_rbridge, nmethod, 1);
|
270
|
-
rjb_rbridge = (*jenv)->NewGlobalRef(jenv, rjb_rbridge);
|
271
|
-
return 0;
|
272
|
-
}
|
273
|
-
|
274
|
-
void rjb_unload_vm()
|
275
|
-
{
|
276
|
-
if (RTEST(jvmdll))
|
277
|
-
{
|
278
|
-
rb_funcall(jvmdll, rb_intern("close"), 0);
|
279
|
-
jvmdll = Qnil;
|
280
|
-
}
|
281
|
-
}
|
282
|
-
|
283
|
-
int rjb_create_jvm(JNIEnv** pjenv, JavaVMInitArgs* vm_args, char* userpath, VALUE argv)
|
284
|
-
{
|
285
|
-
static JavaVMOption soptions[] = {
|
286
|
-
#if defined(__sparc_v9__) || defined(__sparc__)
|
287
|
-
{ "-Xusealtsigs", NULL },
|
288
|
-
#elif defined(linux) || defined(__linux__)
|
289
|
-
{ "-Xrs", NULL },
|
290
|
-
#elif defined(__APPLE__) && defined(_ARCH_PPC)
|
291
|
-
{ "-Xrs", NULL },
|
292
|
-
#endif
|
293
|
-
#if defined(SHOW_JAVAGC)
|
294
|
-
{ "-verbose:gc", NULL },
|
295
|
-
#endif
|
296
|
-
{ "DUMMY", NULL }, /* for classpath count */
|
297
|
-
};
|
298
|
-
char* newpath;
|
299
|
-
size_t len;
|
300
|
-
int result;
|
301
|
-
GETDEFAULTJAVAVMINITARGS initargs;
|
302
|
-
CREATEJAVAVM createjavavm;
|
303
|
-
JavaVMOption* options;
|
304
|
-
size_t optlen;
|
305
|
-
int i;
|
306
|
-
VALUE optval;
|
307
|
-
|
308
|
-
if (!RTEST(jvmdll))
|
309
|
-
{
|
310
|
-
char* libjvm = getenv("JVM_LIB");
|
311
|
-
#if defined(_WIN32)
|
312
|
-
if (libjvm && *libjvm == '"' && *(libjvm + strlen(libjvm) - 1) == '"')
|
313
|
-
{
|
314
|
-
char* p = ALLOCA_N(char, strlen(libjvm) + 1);
|
315
|
-
strcpy(p, libjvm + 1);
|
316
|
-
*(p + strlen(p) - 1) = '\0';
|
317
|
-
libjvm = p;
|
318
|
-
}
|
319
|
-
#endif
|
320
|
-
if (libjvm == NULL || !open_jvm(libjvm))
|
321
|
-
{
|
322
|
-
#if defined(__APPLE__) && defined(__MACH__)
|
323
|
-
if (!(load_jvm(NULL)))
|
324
|
-
{
|
325
|
-
JVMDLL = "%s/Libraries/libjvm.dylib";
|
326
|
-
CREATEJVM = "JNI_CreateJavaVM_Impl";
|
327
|
-
GETDEFAULTJVMINITARGS = "JNI_GetDefaultJavaVMInitArgs_Impl";
|
328
|
-
#endif
|
329
|
-
if (!(load_jvm(JVM_TYPE) || load_jvm(ALT_JVM_TYPE)))
|
330
|
-
{
|
331
|
-
return -1;
|
332
|
-
}
|
333
|
-
#if defined(__APPLE__) && defined(__MACH__)
|
334
|
-
}
|
335
|
-
#endif
|
336
|
-
}
|
337
|
-
|
338
|
-
#if RJB_RUBY_VERSION_CODE < 190 && !defined(RUBINIUS)
|
339
|
-
ruby_errinfo = Qnil;
|
340
|
-
#else
|
341
|
-
rb_set_errinfo(Qnil);
|
342
|
-
#endif
|
343
|
-
}
|
344
|
-
|
345
|
-
if (NIL_P(getdefaultjavavminitargsfunc))
|
346
|
-
{
|
347
|
-
return -1;
|
348
|
-
}
|
349
|
-
initargs = (GETDEFAULTJAVAVMINITARGS)NUM2ULONG(getdefaultjavavminitargsfunc);
|
350
|
-
result = initargs(vm_args);
|
351
|
-
if (0 > result)
|
352
|
-
{
|
353
|
-
return result;
|
354
|
-
}
|
355
|
-
len = strlen(userpath);
|
356
|
-
if (getenv("CLASSPATH"))
|
357
|
-
{
|
358
|
-
len += strlen(getenv("CLASSPATH"));
|
359
|
-
}
|
360
|
-
newpath = ALLOCA_N(char, len + 32);
|
361
|
-
if (getenv("CLASSPATH"))
|
362
|
-
{
|
363
|
-
sprintf(newpath, "-Djava.class.path=%s%c%s",
|
364
|
-
userpath, CLASSPATH_SEP, getenv("CLASSPATH"));
|
365
|
-
}
|
366
|
-
else
|
367
|
-
{
|
368
|
-
sprintf(newpath, "-Djava.class.path=%s", userpath);
|
369
|
-
}
|
370
|
-
optlen = 0;
|
371
|
-
if (!NIL_P(argv))
|
372
|
-
{
|
373
|
-
optlen += RARRAY_LEN(argv);
|
374
|
-
}
|
375
|
-
optlen += COUNTOF(soptions);
|
376
|
-
options = ALLOCA_N(JavaVMOption, optlen);
|
377
|
-
options->optionString = newpath;
|
378
|
-
options->extraInfo = NULL;
|
379
|
-
for (i = 1; i < COUNTOF(soptions); i++)
|
380
|
-
{
|
381
|
-
*(options + i) = soptions[i - 1];
|
382
|
-
}
|
383
|
-
for (; i < optlen; i++)
|
384
|
-
{
|
385
|
-
optval = rb_ary_entry(argv, i - COUNTOF(soptions));
|
386
|
-
Check_Type(optval, T_STRING);
|
387
|
-
(options + i)->optionString = StringValueCStr(optval);
|
388
|
-
(options + i)->extraInfo = NULL;
|
389
|
-
}
|
390
|
-
vm_args->nOptions = (int)optlen;
|
391
|
-
vm_args->options = options;
|
392
|
-
vm_args->ignoreUnrecognized = JNI_TRUE;
|
393
|
-
if (NIL_P(createjavavmfunc))
|
394
|
-
{
|
395
|
-
return -1;
|
396
|
-
}
|
397
|
-
createjavavm = (CREATEJAVAVM)NUM2ULONG(createjavavmfunc);
|
398
|
-
result = createjavavm(&rjb_jvm, pjenv, vm_args);
|
399
|
-
if (!result)
|
400
|
-
{
|
401
|
-
result = load_bridge(*pjenv);
|
402
|
-
if (RTEST(ruby_verbose) && result < 0)
|
403
|
-
{
|
404
|
-
fprintf(stderr, "failed to load the bridge class\n");
|
405
|
-
}
|
406
|
-
}
|
407
|
-
return result;
|
408
|
-
}
|
1
|
+
/*
|
2
|
+
* Rjb - Ruby <-> Java Bridge
|
3
|
+
* Copyright(c) 2004,2005,2006,2009,2010,2011 arton
|
4
|
+
*
|
5
|
+
* This library is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU Lesser General Public
|
7
|
+
* License as published by the Free Software Foundation; either
|
8
|
+
* version 2.1 of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This library is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
* Lesser General Public License for more details.
|
14
|
+
*
|
15
|
+
* $Id: load.c 180 2011-12-05 16:34:29Z arton $
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <stdlib.h>
|
19
|
+
#include <stdio.h>
|
20
|
+
#include "ruby.h"
|
21
|
+
#include "extconf.h"
|
22
|
+
#if RJB_RUBY_VERSION_CODE < 190
|
23
|
+
#include "intern.h"
|
24
|
+
#include "st.h"
|
25
|
+
#include "util.h"
|
26
|
+
#else
|
27
|
+
#include "ruby/intern.h"
|
28
|
+
#include "ruby/st.h"
|
29
|
+
#include "ruby/util.h"
|
30
|
+
#endif
|
31
|
+
#include "jniwrap.h"
|
32
|
+
#include "jp_co_infoseek_hp_arton_rjb_RBridge.h"
|
33
|
+
#include "rjb.h"
|
34
|
+
|
35
|
+
#define JVM_TYPE "client"
|
36
|
+
#define ALT_JVM_TYPE "classic"
|
37
|
+
|
38
|
+
#if defined(_WIN32) || defined(__CYGWIN__)
|
39
|
+
#if defined(__CYGWIN__)
|
40
|
+
#define JVMDLL "%s/jre/bin/%s/jvm.dll"
|
41
|
+
#define DIRSEPARATOR '/'
|
42
|
+
#else
|
43
|
+
#define JVMDLL "%s\\jre\\bin\\%s\\jvm.dll"
|
44
|
+
#define DIRSEPARATOR '\\'
|
45
|
+
#if defined(_WIN64)
|
46
|
+
#undef JVM_TYPE
|
47
|
+
#define JVM_TYPE "server"
|
48
|
+
#endif
|
49
|
+
#endif
|
50
|
+
#define CLASSPATH_SEP ';'
|
51
|
+
#elif defined(__APPLE__) && defined(__MACH__)
|
52
|
+
static char* JVMDLL = "%s/JavaVM";
|
53
|
+
#define DIRSEPARATOR '/'
|
54
|
+
#define CLASSPATH_SEP ':'
|
55
|
+
#define HOME_NAME "/Home"
|
56
|
+
#define HOME_NAME_LEN strlen(HOME_NAME)
|
57
|
+
#define DEFAULT_HOME "/System/Library/Frameworks/JavaVM.framework"
|
58
|
+
#elif defined(_AIX)
|
59
|
+
#define ARCH "ppc"
|
60
|
+
#undef JVM_TYPE
|
61
|
+
#define JVM_TYPE "j9vm"
|
62
|
+
#elif defined(__hpux)
|
63
|
+
#define JVMDLL "%s/jre/lib/%s/%s/libjvm.sl"
|
64
|
+
#define ARCH "PA_RISC"
|
65
|
+
#undef JVM_TYPE
|
66
|
+
#define JVM_TYPE "server"
|
67
|
+
#define DIRSEPARATOR '/'
|
68
|
+
#define CLASSPATH_SEP ':'
|
69
|
+
#else /* defined(_WIN32) || defined(__CYGWIN__) */
|
70
|
+
#if defined(__sparc_v9__)
|
71
|
+
#define ARCH "sparcv9"
|
72
|
+
#elif defined(__sparc__)
|
73
|
+
#define ARCH "sparc"
|
74
|
+
#elif defined(__amd64__)
|
75
|
+
#define ARCH "amd64"
|
76
|
+
#undef JVM_TYPE
|
77
|
+
#define JVM_TYPE "server"
|
78
|
+
#elif defined(i586) || defined(__i386__)
|
79
|
+
#define ARCH "i386"
|
80
|
+
#endif
|
81
|
+
#ifndef ARCH
|
82
|
+
#include <sys/systeminfo.h>
|
83
|
+
#endif
|
84
|
+
#define JVMDLL "%s/jre/lib/%s/%s/libjvm.so"
|
85
|
+
#define DIRSEPARATOR '/'
|
86
|
+
#define CLASSPATH_SEP ':'
|
87
|
+
#endif
|
88
|
+
|
89
|
+
#if defined(__APPLE__) && defined(__MACH__)
|
90
|
+
static char* CREATEJVM = "JNI_CreateJavaVM";
|
91
|
+
static char* GETDEFAULTJVMINITARGS = "JNI_GetDefaultJavaVMInitArgs";
|
92
|
+
#else
|
93
|
+
#define CREATEJVM "JNI_CreateJavaVM"
|
94
|
+
#define GETDEFAULTJVMINITARGS "JNI_GetDefaultJavaVMInitArgs"
|
95
|
+
#endif
|
96
|
+
|
97
|
+
typedef int (JNICALL *GETDEFAULTJAVAVMINITARGS)(void*);
|
98
|
+
typedef int (JNICALL *CREATEJAVAVM)(JavaVM**, JNIEnv**, void*);
|
99
|
+
|
100
|
+
|
101
|
+
static VALUE jvmdll = Qnil;
|
102
|
+
static VALUE getdefaultjavavminitargsfunc;
|
103
|
+
static VALUE createjavavmfunc;
|
104
|
+
|
105
|
+
static int open_jvm(char* libpath)
|
106
|
+
{
|
107
|
+
int sstat;
|
108
|
+
VALUE* argv;
|
109
|
+
|
110
|
+
rb_require("dl");
|
111
|
+
#if !defined(RUBINIUS)
|
112
|
+
if (!rb_const_defined_at(rb_cObject, rb_intern("DL")))
|
113
|
+
{
|
114
|
+
rb_raise(rb_eRuntimeError, "Constants DL is not defined.");
|
115
|
+
return 0;
|
116
|
+
}
|
117
|
+
#endif
|
118
|
+
argv = ALLOCA_N(VALUE, 4);
|
119
|
+
*argv = rb_const_get(rb_cObject, rb_intern("DL"));
|
120
|
+
*(argv + 1) = rb_intern("dlopen");
|
121
|
+
*(argv + 2) = 1;
|
122
|
+
*(argv + 3) = rb_str_new2(libpath);
|
123
|
+
jvmdll = rb_protect(rjb_safe_funcall, (VALUE)argv, &sstat);
|
124
|
+
if (sstat)
|
125
|
+
{
|
126
|
+
return 0;
|
127
|
+
}
|
128
|
+
/* get function pointers of JNI */
|
129
|
+
#if RJB_RUBY_VERSION_CODE < 190
|
130
|
+
getdefaultjavavminitargsfunc = rb_funcall(rb_funcall(rb_funcall(jvmdll, rb_intern("[]"), 2, rb_str_new2(GETDEFAULTJVMINITARGS), rb_str_new2("IP")), rb_intern("to_ptr"), 0), rb_intern("to_i"), 0);
|
131
|
+
createjavavmfunc = rb_funcall(rb_funcall(rb_funcall(jvmdll, rb_intern("[]"), 2, rb_str_new2(CREATEJVM), rb_str_new2("IPPP")), rb_intern("to_ptr"), 0), rb_intern("to_i"), 0);
|
132
|
+
#else
|
133
|
+
getdefaultjavavminitargsfunc = rb_funcall(jvmdll, rb_intern("[]"), 1, rb_str_new2(GETDEFAULTJVMINITARGS));
|
134
|
+
createjavavmfunc = rb_funcall(jvmdll, rb_intern("[]"), 1, rb_str_new2(CREATEJVM));
|
135
|
+
#endif
|
136
|
+
return 1;
|
137
|
+
}
|
138
|
+
|
139
|
+
#if defined(__APPLE__) && defined(__MACH__)
|
140
|
+
static int file_exist(const char* dir, const char* file)
|
141
|
+
{
|
142
|
+
VALUE path = rb_funcall(rb_cFile, rb_intern("join"), 2,
|
143
|
+
rb_str_new2(dir), rb_str_new2(file));
|
144
|
+
VALUE ret = rb_funcall(rb_cFile, rb_intern("exist?"), 1, path);
|
145
|
+
return RTEST(ret);
|
146
|
+
}
|
147
|
+
#endif
|
148
|
+
|
149
|
+
/*
|
150
|
+
* not completed, only valid under some circumstances.
|
151
|
+
*/
|
152
|
+
static int load_jvm(const char* jvmtype)
|
153
|
+
{
|
154
|
+
char* libpath;
|
155
|
+
char* java_home;
|
156
|
+
char* jh;
|
157
|
+
|
158
|
+
jh = getenv("JAVA_HOME");
|
159
|
+
#if defined(__APPLE__) && defined(__MACH__)
|
160
|
+
if (!jh)
|
161
|
+
{
|
162
|
+
jh = DEFAULT_HOME;
|
163
|
+
}
|
164
|
+
else
|
165
|
+
{
|
166
|
+
if (strlen(jh) > HOME_NAME_LEN)
|
167
|
+
{
|
168
|
+
size_t len = strlen(jh);
|
169
|
+
char* p = ALLOCA_N(char, len + 8);
|
170
|
+
jh = strcpy(p, jh);
|
171
|
+
if (*(jh + len - 1) == '/')
|
172
|
+
{
|
173
|
+
--len;
|
174
|
+
*(jh + len) = '\0';
|
175
|
+
}
|
176
|
+
if (strcasecmp(jh + len - HOME_NAME_LEN, HOME_NAME) == 0)
|
177
|
+
{
|
178
|
+
strcpy(p + len, "/..");
|
179
|
+
}
|
180
|
+
}
|
181
|
+
if (!jvmtype && !file_exist(jh, "JavaVM"))
|
182
|
+
{
|
183
|
+
jh = DEFAULT_HOME;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
#endif
|
187
|
+
if (!jh)
|
188
|
+
{
|
189
|
+
if (RTEST(ruby_verbose))
|
190
|
+
{
|
191
|
+
fprintf(stderr, "no JAVA_HOME environment\n");
|
192
|
+
}
|
193
|
+
return 0;
|
194
|
+
}
|
195
|
+
#if defined(_WIN32)
|
196
|
+
if (*jh == '"' && *(jh + strlen(jh) - 1) == '"')
|
197
|
+
{
|
198
|
+
char* p = ALLOCA_N(char, strlen(jh) + 1);
|
199
|
+
strcpy(p, jh + 1);
|
200
|
+
*(p + strlen(p) - 1) = '\0';
|
201
|
+
jh = p;
|
202
|
+
}
|
203
|
+
#endif
|
204
|
+
java_home = ALLOCA_N(char, strlen(jh) + 1);
|
205
|
+
strcpy(java_home, jh);
|
206
|
+
if (*(java_home + strlen(jh) - 1) == DIRSEPARATOR)
|
207
|
+
{
|
208
|
+
*(java_home + strlen(jh) - 1) = '\0';
|
209
|
+
}
|
210
|
+
#if defined(_WIN32) || defined(__CYGWIN__)
|
211
|
+
libpath = ALLOCA_N(char, sizeof(JVMDLL) + strlen(java_home)
|
212
|
+
+ strlen(jvmtype) + 1);
|
213
|
+
sprintf(libpath, JVMDLL, java_home, jvmtype);
|
214
|
+
#elif defined(__APPLE__) && defined(__MACH__)
|
215
|
+
libpath = ALLOCA_N(char, sizeof(JVMDLL) + strlen(java_home) + 1);
|
216
|
+
sprintf(libpath, JVMDLL, java_home);
|
217
|
+
#else /* not Windows / MAC OS-X */
|
218
|
+
libpath = ALLOCA_N(char, sizeof(JVMDLL) + strlen(java_home)
|
219
|
+
+ strlen(ARCH) + strlen(jvmtype) + 1);
|
220
|
+
sprintf(libpath, JVMDLL, java_home, ARCH, jvmtype);
|
221
|
+
#endif
|
222
|
+
return open_jvm(libpath);
|
223
|
+
}
|
224
|
+
|
225
|
+
static int load_bridge(JNIEnv* jenv)
|
226
|
+
{
|
227
|
+
JNINativeMethod nmethod[1];
|
228
|
+
jbyte buff[8192];
|
229
|
+
char* bridge;
|
230
|
+
size_t len;
|
231
|
+
FILE* f;
|
232
|
+
#if defined(RUBINIUS)
|
233
|
+
VALUE v = rb_const_get(rb_cObject, rb_intern("RjbConf"));
|
234
|
+
v = rb_const_get(v, rb_intern("BRIDGE_FILE"));
|
235
|
+
#else
|
236
|
+
VALUE v = rb_const_get_at(rb_const_get(rb_cObject, rb_intern("RjbConf")),
|
237
|
+
rb_intern("BRIDGE_FILE"));
|
238
|
+
#endif
|
239
|
+
bridge = StringValuePtr(v);
|
240
|
+
#if defined(DOSISH)
|
241
|
+
bridge = ALLOCA_N(char, strlen(bridge) + 8);
|
242
|
+
strcpy(bridge, StringValuePtr(v));
|
243
|
+
for (len = 0; bridge[len]; len++)
|
244
|
+
{
|
245
|
+
if (bridge[len] == '/')
|
246
|
+
{
|
247
|
+
bridge[len] = '\\';
|
248
|
+
}
|
249
|
+
}
|
250
|
+
#endif
|
251
|
+
f = fopen(bridge, "rb");
|
252
|
+
if (f == NULL)
|
253
|
+
{
|
254
|
+
return -1;
|
255
|
+
}
|
256
|
+
len = fread(buff, 1, sizeof(buff), f);
|
257
|
+
fclose(f);
|
258
|
+
rjb_rbridge = (*jenv)->DefineClass(jenv,
|
259
|
+
"jp/co/infoseek/hp/arton/rjb/RBridge", get_systemloader(jenv), buff, len);
|
260
|
+
if (rjb_rbridge == NULL)
|
261
|
+
{
|
262
|
+
rjb_check_exception(jenv, 1);
|
263
|
+
}
|
264
|
+
rjb_register_bridge = (*jenv)->GetMethodID(jenv, rjb_rbridge, "register",
|
265
|
+
"(Ljava/lang/Class;)Ljava/lang/Object;");
|
266
|
+
nmethod[0].name = "call";
|
267
|
+
nmethod[0].signature = "(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;";
|
268
|
+
nmethod[0].fnPtr = Java_jp_co_infoseek_hp_arton_rjb_RBridge_call;
|
269
|
+
(*jenv)->RegisterNatives(jenv, rjb_rbridge, nmethod, 1);
|
270
|
+
rjb_rbridge = (*jenv)->NewGlobalRef(jenv, rjb_rbridge);
|
271
|
+
return 0;
|
272
|
+
}
|
273
|
+
|
274
|
+
void rjb_unload_vm()
|
275
|
+
{
|
276
|
+
if (RTEST(jvmdll))
|
277
|
+
{
|
278
|
+
rb_funcall(jvmdll, rb_intern("close"), 0);
|
279
|
+
jvmdll = Qnil;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
int rjb_create_jvm(JNIEnv** pjenv, JavaVMInitArgs* vm_args, char* userpath, VALUE argv)
|
284
|
+
{
|
285
|
+
static JavaVMOption soptions[] = {
|
286
|
+
#if defined(__sparc_v9__) || defined(__sparc__)
|
287
|
+
{ "-Xusealtsigs", NULL },
|
288
|
+
#elif defined(linux) || defined(__linux__)
|
289
|
+
{ "-Xrs", NULL },
|
290
|
+
#elif defined(__APPLE__) && defined(_ARCH_PPC)
|
291
|
+
{ "-Xrs", NULL },
|
292
|
+
#endif
|
293
|
+
#if defined(SHOW_JAVAGC)
|
294
|
+
{ "-verbose:gc", NULL },
|
295
|
+
#endif
|
296
|
+
{ "DUMMY", NULL }, /* for classpath count */
|
297
|
+
};
|
298
|
+
char* newpath;
|
299
|
+
size_t len;
|
300
|
+
int result;
|
301
|
+
GETDEFAULTJAVAVMINITARGS initargs;
|
302
|
+
CREATEJAVAVM createjavavm;
|
303
|
+
JavaVMOption* options;
|
304
|
+
size_t optlen;
|
305
|
+
int i;
|
306
|
+
VALUE optval;
|
307
|
+
|
308
|
+
if (!RTEST(jvmdll))
|
309
|
+
{
|
310
|
+
char* libjvm = getenv("JVM_LIB");
|
311
|
+
#if defined(_WIN32)
|
312
|
+
if (libjvm && *libjvm == '"' && *(libjvm + strlen(libjvm) - 1) == '"')
|
313
|
+
{
|
314
|
+
char* p = ALLOCA_N(char, strlen(libjvm) + 1);
|
315
|
+
strcpy(p, libjvm + 1);
|
316
|
+
*(p + strlen(p) - 1) = '\0';
|
317
|
+
libjvm = p;
|
318
|
+
}
|
319
|
+
#endif
|
320
|
+
if (libjvm == NULL || !open_jvm(libjvm))
|
321
|
+
{
|
322
|
+
#if defined(__APPLE__) && defined(__MACH__)
|
323
|
+
if (!(load_jvm(NULL)))
|
324
|
+
{
|
325
|
+
JVMDLL = "%s/Libraries/libjvm.dylib";
|
326
|
+
CREATEJVM = "JNI_CreateJavaVM_Impl";
|
327
|
+
GETDEFAULTJVMINITARGS = "JNI_GetDefaultJavaVMInitArgs_Impl";
|
328
|
+
#endif
|
329
|
+
if (!(load_jvm(JVM_TYPE) || load_jvm(ALT_JVM_TYPE)))
|
330
|
+
{
|
331
|
+
return -1;
|
332
|
+
}
|
333
|
+
#if defined(__APPLE__) && defined(__MACH__)
|
334
|
+
}
|
335
|
+
#endif
|
336
|
+
}
|
337
|
+
|
338
|
+
#if RJB_RUBY_VERSION_CODE < 190 && !defined(RUBINIUS)
|
339
|
+
ruby_errinfo = Qnil;
|
340
|
+
#else
|
341
|
+
rb_set_errinfo(Qnil);
|
342
|
+
#endif
|
343
|
+
}
|
344
|
+
|
345
|
+
if (NIL_P(getdefaultjavavminitargsfunc))
|
346
|
+
{
|
347
|
+
return -1;
|
348
|
+
}
|
349
|
+
initargs = (GETDEFAULTJAVAVMINITARGS)NUM2ULONG(getdefaultjavavminitargsfunc);
|
350
|
+
result = initargs(vm_args);
|
351
|
+
if (0 > result)
|
352
|
+
{
|
353
|
+
return result;
|
354
|
+
}
|
355
|
+
len = strlen(userpath);
|
356
|
+
if (getenv("CLASSPATH"))
|
357
|
+
{
|
358
|
+
len += strlen(getenv("CLASSPATH"));
|
359
|
+
}
|
360
|
+
newpath = ALLOCA_N(char, len + 32);
|
361
|
+
if (getenv("CLASSPATH"))
|
362
|
+
{
|
363
|
+
sprintf(newpath, "-Djava.class.path=%s%c%s",
|
364
|
+
userpath, CLASSPATH_SEP, getenv("CLASSPATH"));
|
365
|
+
}
|
366
|
+
else
|
367
|
+
{
|
368
|
+
sprintf(newpath, "-Djava.class.path=%s", userpath);
|
369
|
+
}
|
370
|
+
optlen = 0;
|
371
|
+
if (!NIL_P(argv))
|
372
|
+
{
|
373
|
+
optlen += RARRAY_LEN(argv);
|
374
|
+
}
|
375
|
+
optlen += COUNTOF(soptions);
|
376
|
+
options = ALLOCA_N(JavaVMOption, optlen);
|
377
|
+
options->optionString = newpath;
|
378
|
+
options->extraInfo = NULL;
|
379
|
+
for (i = 1; i < COUNTOF(soptions); i++)
|
380
|
+
{
|
381
|
+
*(options + i) = soptions[i - 1];
|
382
|
+
}
|
383
|
+
for (; i < optlen; i++)
|
384
|
+
{
|
385
|
+
optval = rb_ary_entry(argv, i - COUNTOF(soptions));
|
386
|
+
Check_Type(optval, T_STRING);
|
387
|
+
(options + i)->optionString = StringValueCStr(optval);
|
388
|
+
(options + i)->extraInfo = NULL;
|
389
|
+
}
|
390
|
+
vm_args->nOptions = (int)optlen;
|
391
|
+
vm_args->options = options;
|
392
|
+
vm_args->ignoreUnrecognized = JNI_TRUE;
|
393
|
+
if (NIL_P(createjavavmfunc))
|
394
|
+
{
|
395
|
+
return -1;
|
396
|
+
}
|
397
|
+
createjavavm = (CREATEJAVAVM)NUM2ULONG(createjavavmfunc);
|
398
|
+
result = createjavavm(&rjb_jvm, pjenv, vm_args);
|
399
|
+
if (!result)
|
400
|
+
{
|
401
|
+
result = load_bridge(*pjenv);
|
402
|
+
if (RTEST(ruby_verbose) && result < 0)
|
403
|
+
{
|
404
|
+
fprintf(stderr, "failed to load the bridge class\n");
|
405
|
+
}
|
406
|
+
}
|
407
|
+
return result;
|
408
|
+
}
|