Class: Processing::LibraryLoader
- Inherits:
-
Object
- Object
- Processing::LibraryLoader
- Defined in:
- lib/jruby_art/library_loader.rb
Overview
Encapsulate library loader functionality as a class
Instance Attribute Summary collapse
-
#sketchbook_library_path ⇒ Object
readonly
Returns the value of attribute sketchbook_library_path.
Instance Method Summary collapse
- #get_library_paths(library_name, extension = nil) ⇒ Object
- #get_platform_specific_library_paths(basename) ⇒ Object
-
#initialize ⇒ LibraryLoader
constructor
A new instance of LibraryLoader.
-
#library_loaded?(library_name) ⇒ Boolean
Detect if a library has been loaded (for conditional loading).
-
#load_java_library(library_name) ⇒ Object
HACK: For pure java libraries, such as the ones that are available on this page: processing.org/reference/libraries/index.html that include native code, we mess with the ‘Java ClassLoader’, so that you don’t have to futz with your PATH.
-
#load_libraries(*args) ⇒ Object
(also: #load_library)
Load a list of Ruby or Java libraries (in that order) Usage: load_libraries :opengl, :boids.
-
#load_ruby_library(library_name) ⇒ Object
For pure ruby libraries.
- #platform ⇒ Object
Constructor Details
#initialize ⇒ LibraryLoader
Returns a new instance of LibraryLoader.
12 13 14 15 |
# File 'lib/jruby_art/library_loader.rb', line 12 def initialize @sketchbook_library_path = File.join(Processing::RP_CONFIG['sketchbook_path'], 'libraries') @loaded_libraries = Hash.new(false) end |
Instance Attribute Details
#sketchbook_library_path ⇒ Object (readonly)
Returns the value of attribute sketchbook_library_path.
10 11 12 |
# File 'lib/jruby_art/library_loader.rb', line 10 def sketchbook_library_path @sketchbook_library_path end |
Instance Method Details
#get_library_paths(library_name, extension = nil) ⇒ Object
93 94 95 96 |
# File 'lib/jruby_art/library_loader.rb', line 93 def get_library_paths(library_name, extension = nil) dir = get_library_directory_path(library_name, extension) Dir.glob("#{dir}/*.{rb,jar}") end |
#get_platform_specific_library_paths(basename) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/jruby_art/library_loader.rb', line 84 def get_platform_specific_library_paths(basename) bits = '64' if java.lang.System.getProperty('sun.arch.data.model') == '32' || java.lang.System.getProperty('java.vm.name').index('32') bits = '32' unless platform =~ /macosx/ end [platform, platform + bits].map { |p| File.join(basename, p) } end |
#library_loaded?(library_name) ⇒ Boolean
Detect if a library has been loaded (for conditional loading)
18 19 20 |
# File 'lib/jruby_art/library_loader.rb', line 18 def library_loaded?(library_name) @loaded_libraries[library_name.to_sym] end |
#load_java_library(library_name) ⇒ Object
HACK: For pure java libraries, such as the ones that are available on this page: processing.org/reference/libraries/index.html that include native code, we mess with the ‘Java ClassLoader’, so that you don’t have to futz with your PATH. But it’s probably bad juju.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jruby_art/library_loader.rb', line 51 def load_java_library(library_name) library_name = library_name.to_sym return true if @loaded_libraries.include?(library_name) jpath = get_library_directory_path(library_name, 'jar') jars = get_library_paths(library_name, 'jar') return false if jars.empty? jars.each { |jar| require jar } platform_specific_library_paths = get_platform_specific_library_paths(jpath) platform_specific_library_paths = platform_specific_library_paths.select do |ppath| FileTest.directory?(ppath) && !Dir.glob(File.join(ppath, '*.{so,dll,jnilib}')).empty? end unless platform_specific_library_paths.empty? platform_specific_library_paths << java.lang.System.getProperty('java.library.path') new_library_path = platform_specific_library_paths.join(java.io.File.pathSeparator) java.lang.System.setProperty('java.library.path', new_library_path) field = java.lang.Class.for_name('java.lang.ClassLoader').get_declared_field('sys_paths') if field field.accessible = true field.set(java.lang.Class.for_name('java.lang.System').get_class_loader, nil) end end @loaded_libraries[library_name] = true end |
#load_libraries(*args) ⇒ Object Also known as: load_library
Load a list of Ruby or Java libraries (in that order) Usage: load_libraries :opengl, :boids
If a library is put into a ‘library’ folder next to the sketch it will be used instead of the library that ships with JRubyArt.
27 28 29 30 31 32 33 |
# File 'lib/jruby_art/library_loader.rb', line 27 def load_libraries(*args) = 'no such file to load -- %s' args.each do |lib| loaded = load_ruby_library(lib) || load_java_library(lib) raise(LoadError.new, format(, lib)) unless loaded end end |
#load_ruby_library(library_name) ⇒ Object
For pure ruby libraries. The library should have an initialization ruby file of the same name as the library folder.
39 40 41 42 43 44 45 |
# File 'lib/jruby_art/library_loader.rb', line 39 def load_ruby_library(library_name) library_name = library_name.to_sym return true if @loaded_libraries.include?(library_name) path = get_library_paths(library_name, 'rb').first return false unless path @loaded_libraries[library_name] = (require path) end |
#platform ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/jruby_art/library_loader.rb', line 75 def platform match = %w(mac linux windows).find do |os| java.lang.System.getProperty('os.name').downcase.index(os) end return 'other' unless match return match unless match =~ /mac/ 'macosx' end |