Skip to content

Commit 188e4bc

Browse files
committed
experimental commit
1 parent 11e06c8 commit 188e4bc

7 files changed

Lines changed: 208 additions & 2 deletions

File tree

lib/jruby_art.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Also, send samples and libraries.
55
unless defined? K9_ROOT
66
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
7-
K9_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
7+
K9_ROOT = File.absolute_path(File.dirname(__dir__))
88
end
99

1010
SKETCH_ROOT ||= Dir.pwd

lib/jruby_art/app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_relative '../rpextras'
55
require_relative '../jruby_art/helper_methods'
66
require_relative '../jruby_art/helpers/aabb'
7-
require_relative '../jruby_art/library_loader'
7+
require_relative '../jruby_art/new_loader'
88
require_relative '../jruby_art/config'
99

1010
# A wrapper module for the processing App

lib/jruby_art/java_library.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require_relative 'config'
2+
require_relative 'native_folder'
3+
require_relative 'native_loader'
4+
5+
# The JavaLibrary class
6+
class JavaLibrary
7+
attr_reader :dir, :path, :ppath
8+
9+
def initialize(name)
10+
@dir = '.'
11+
@path = File.join(dir, "#{name}.jar")
12+
end
13+
14+
def exist?
15+
File.exist? path
16+
end
17+
18+
def load_jars
19+
Dir["#{dir}/*.jar"].each do |jar|
20+
require jar
21+
end
22+
return unless native_binaries?
23+
add_binaries_to_classpath
24+
end
25+
26+
def native_binaries?
27+
native_folder = NativeFolder.new
28+
native = native_folder.name
29+
@ppath = File.join(dir, native)
30+
File.directory?(ppath) &&
31+
!Dir.glob(File.join(ppath, native_folder.extension)).empty?
32+
end
33+
34+
def add_binaries_to_classpath
35+
native_loader = NativeLoader.new
36+
native_loader.add_native_path(ppath)
37+
end
38+
end
39+
40+
# The LocalJavaLibrary class
41+
class LocalJavaLibrary < JavaLibrary
42+
def initialize(name)
43+
super
44+
@dir = File.join(Processing::SKETCH_ROOT, 'library', name)
45+
@path = File.join(dir, "#{name}.jar")
46+
end
47+
end
48+
49+
# The InstalledJavaLibrary class
50+
class InstalledJavaLibrary < JavaLibrary
51+
def initialize(name)
52+
super
53+
@dir = File.join(Processing::RP_CONFIG['sketchbook_path'], 'libraries')
54+
@path = File.join(dir, name, 'library', "#{name}.jar")
55+
end
56+
end

lib/jruby_art/native_folder.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'rbconfig'
2+
3+
# Utility to load native binaries on Java CLASSPATH
4+
class NativeFolder
5+
attr_reader :os, :bit
6+
7+
WIN_FORMAT = 'windows%d'
8+
LINUX_FORMAT = 'linux%d'
9+
# WIN_PATTERNS = [
10+
# /bccwin/i,
11+
# /cygwin/i,
12+
# /djgpp/i,
13+
# /ming/i,
14+
# /mswin/i,
15+
# /wince/i
16+
# ].freeze
17+
18+
def initialize
19+
@os = RbConfig::CONFIG['host_os'].downcase
20+
@bit = java.lang.System.get_property('os.arch') =~ /64/ ? 64 : 32
21+
end
22+
23+
def name
24+
return 'macosx' if os =~ /darwin/ || os =~ /mac/
25+
# return format(WIN_FORMAT, bit) if WIN_PATTERNS.include? os
26+
return format(LINUX_FORMAT, bit) if os =~ /linux/
27+
end
28+
29+
def extension
30+
return '*.so' if os =~ /linux/
31+
'*.dylib' # MacOS
32+
end
33+
end

lib/jruby_art/native_loader.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This class knows how to dynamically set the 'java' native library path
2+
# It might not work with java 9?
3+
class NativeLoader
4+
attr_reader :separator, :current_path
5+
6+
module JC
7+
java_import 'java.lang.Class'
8+
java_import 'java.lang.System'
9+
java_import 'java.io.File'
10+
end
11+
12+
def initialize
13+
@separator = JC::File.pathSeparatorChar
14+
@current_path = JC::System.getProperty('java.library.path')
15+
end
16+
17+
def add_native_path(pth)
18+
current_path << separator << pth
19+
JC::System.setProperty('java.library.path', current_path)
20+
field = JC::Class.for_name('java.lang.ClassLoader')
21+
.get_declared_field('sys_paths')
22+
return unless field
23+
field.accessible = true # some jruby magic
24+
field.set(JC::Class.for_name('java.lang.System').get_class_loader, nil)
25+
end
26+
end

lib/jruby_art/new_loader.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: false
2+
# The processing wrapper module
3+
module Processing
4+
require_relative 'ruby_library'
5+
require_relative 'java_library'
6+
7+
# Encapsulate library loader functionality as a class
8+
class LibraryLoader
9+
attr_reader :library
10+
11+
def initialize
12+
@loaded_libraries = Hash.new(false)
13+
end
14+
15+
# Detect if a library has been loaded (for conditional loading)
16+
def library_loaded?(library_name)
17+
@loaded_libraries[library_name.to_sym]
18+
end
19+
20+
# Load a list of Ruby or Java libraries (in that order)
21+
# Usage: load_libraries :video, :video_event
22+
#
23+
# If a library is put into a 'library' folder next to the sketch it will
24+
# be used instead of the library that ships with Propane.
25+
def load_libraries(*args)
26+
message = 'no such file to load -- %s'
27+
args.each do |lib|
28+
loaded = loader(lib)
29+
raise(LoadError.new, format(message, lib)) unless loaded
30+
end
31+
end
32+
alias load_library load_libraries
33+
34+
def loader(name)
35+
return true if @loaded_libraries.include?(name)
36+
fname = name.to_s
37+
if (@library = LocalRubyLibrary.new(fname)).exist?
38+
return require_library(library, name)
39+
end
40+
if (@library = InstalledRubyLibrary.new(fname)).exist?
41+
return require_library(library, name)
42+
end
43+
if (@library = LocalJavaLibrary.new(fname)).exist?
44+
return load_jars(library, name)
45+
end
46+
if (@library = InstalledJavaLibrary.new(fname)).exist?
47+
return load_jars(library, name)
48+
end
49+
false
50+
end
51+
52+
def load_jars(lib, name)
53+
lib.load_jars
54+
@loaded_libraries[name] = true
55+
end
56+
57+
def require_library(lib, name)
58+
@loaded_libraries[name] = require lib.path
59+
end
60+
end
61+
end

lib/jruby_art/ruby_library.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# The abstract RubyLibrary class
2+
class RubyLibrary
3+
attr_reader :path
4+
5+
def initialize(name)
6+
@path = nil
7+
end
8+
9+
def exist?
10+
File.exist? path
11+
end
12+
end
13+
14+
# The LocalRubyLibrary class
15+
class LocalRubyLibrary < RubyLibrary
16+
def initialize(name)
17+
super
18+
@path = File.absolute_path(
19+
File.join(Processing::SKETCH_ROOT, 'library', name, "#{name}.rb")
20+
)
21+
end
22+
end
23+
24+
# The InstalledLibraryLoader class
25+
class InstalledRubyLibrary < RubyLibrary
26+
def initialize(name)
27+
super
28+
@path = File.join(K9_ROOT, 'library', name, "#{name}.rb")
29+
end
30+
end

0 commit comments

Comments
 (0)