naether 0.8.6 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require "#{File.dirname(__FILE__)}/java"
2
+ require "#{File.dirname(__FILE__)}/configuration"
2
3
  require 'yaml'
3
4
  require 'open-uri'
4
5
  require 'fileutils'
@@ -21,19 +22,13 @@ class Naether
21
22
  ENV['M2_REPO'] || File.expand_path('~/.m2/repository')
22
23
  end
23
24
 
24
- # Find naether jar relative to the gem install
25
- def naether_jar
26
- Dir.glob(File.expand_path("#{File.dirname(__FILE__)}/../../naether*.jar")).first
27
- end
28
-
29
25
  # write bootstrap dependencies to yaml file
30
- def write_dependencies( jar_path = nil, dest = 'jar_dependencies.yml' )
31
- Naether::Java.load_jars_dir( jar_path || Naether::JAR_LIB )
26
+ def write_dependencies( dest = 'jar_dependencies.yml' )
32
27
  deps = {};
33
- if Naether.platform == 'java'
34
- deps[:dependencies] = com.slackworks.naether.Bootstrap.dependencies.to_a
28
+ if Naether::Configuration.platform == 'java'
29
+ deps[:dependencies] = com.tobedevoured.naether.Bootstrap.dependencies.to_a
35
30
  else
36
- bootstrap = Rjb::import('com.slackworks.naether.Bootstrap')
31
+ bootstrap = Rjb::import('com.tobedevoured.naether.Bootstrap')
37
32
  deps[:dependencies] = bootstrap.dependencies.toArray().map{ |dep| dep.toString() }
38
33
  end
39
34
 
@@ -50,13 +45,36 @@ class Naether
50
45
  end
51
46
 
52
47
  if dep_file.nil?
53
- dep_file = "#{File.dirname( __FILE__ )}/../../jar_dependencies.yml"
48
+ dep_file = Naether::Configuration.dependencies_yml
54
49
  end
55
50
 
56
51
  dep = YAML.load_file( dep_file )
57
52
  @@dependencies = dep[:dependencies]
58
53
  end
59
54
 
55
+
56
+ def bootstrap_local_repo(local_repo = nil, opts = {} )
57
+ local_repo = local_repo || default_local_repo
58
+
59
+ opts[:local_repo] = local_repo
60
+
61
+ temp_naether_dir = File.join( local_repo, ".naether" )
62
+
63
+
64
+ deps = download_dependencies( temp_naether_dir, opts )
65
+
66
+ jars = (deps[:exists] + deps[:downloaded]).map {|jar| jar.values.first }
67
+
68
+ jars = Naether::Java.internal_load_paths( jars )
69
+
70
+ if ( deps[:downloaded].size > 0)
71
+ install_dependencies_to_local_repo( deps[:downloaded], jars, opts )
72
+ end
73
+
74
+ #raise Naether::Java.instance.java.class_loader.getLoadedPaths().map { |x| x.toString() }.sort.inspect
75
+
76
+ end
77
+
60
78
  def download_dependencies( dest, opts = {} )
61
79
 
62
80
  if !File.exists? dest
@@ -68,13 +86,12 @@ class Naether
68
86
  if opts[:deps]
69
87
  deps[:missing] = opts[:deps]
70
88
  else
71
- deps = check_local_repo_for_deps( opts[:local_repo] )
89
+ deps = check_local_repo_for_deps( opts[:local_repo], opts )
72
90
  end
73
91
 
74
92
  deps[:downloaded] = []
75
93
 
76
94
  if deps[:missing].size > 0
77
- puts "Downloading jars for Naether"
78
95
 
79
96
  deps[:missing].each do |dep|
80
97
  notation = dep.split(":")
@@ -96,7 +113,7 @@ class Naether
96
113
 
97
114
  deps[:downloaded] << { dep => maven_path }
98
115
  else
99
- deps[:exists] << { dep => maven_path }
116
+ deps[:downloaded] << { dep => maven_path }
100
117
  end
101
118
  end
102
119
  end
@@ -104,16 +121,16 @@ class Naether
104
121
  deps
105
122
  end
106
123
 
107
- def check_local_repo_for_deps(local_repo = nil)
124
+ def check_local_repo_for_deps(local_repo = nil, opts = {} )
108
125
 
109
126
  local_repo = local_repo || default_local_repo
110
127
  local_repo = File.expand_path(local_repo)
111
128
 
112
129
  #puts "Checking #{local_repo} for jars to bootstrap Naether"
113
130
 
114
- deps = {:exists => [], :missing => [] }
131
+ deps = {:exists => [], :missing => [], :downloaded => [] }
115
132
 
116
- dependencies.each do |dep|
133
+ dependencies( opts[:dep_yaml] ).each do |dep|
117
134
  notation = dep.split(":")
118
135
  group = notation[0].gsub("\.", File::SEPARATOR)
119
136
  artifact = notation[1].gsub("\.", File::SEPARATOR)
@@ -131,41 +148,22 @@ class Naether
131
148
  end
132
149
 
133
150
  end
134
-
135
151
  deps
136
152
  end
137
153
 
138
- def install_dependencies_to_local_repo( jars_or_dir, opts = {} )
154
+ def install_dependencies_to_local_repo( install_jars, naether_jars, opts = {} )
139
155
 
140
- @naether = nil
141
- jars = []
142
- unless jars_or_dir.is_a? Array
143
- @naether = Naether.create_from_paths( jars_or_dir, opts[:naether_jar_dir] )
144
- jars = Dir.glob( "#{jars_or_dir}#{File::SEPARATOR}*.jar" )
145
- else
146
- @naether = Naether.create_from_jars( jars_or_dir )
147
- jars = jars_or_dir
148
- end
156
+ require "#{File.dirname(__FILE__)}/../naether"
157
+
158
+ @naether = Naether.create_from_jars( naether_jars )
149
159
 
150
160
  if opts[:local_repo]
151
161
  @naether.local_repo_path = opts[:local_repo]
152
162
  end
153
163
 
154
- dependencies.each do |dep|
155
- notation = dep.split(":")
156
- group = notation[0].gsub("\.", File::SEPARATOR)
157
- artifact = notation[1].gsub("\.", File::SEPARATOR)
158
- type = notation[2]
159
- version = notation[3]
160
-
161
- name = "#{artifact}-#{version}.#{type}"
162
-
163
- jar = jars.select { |x| x =~ /#{name}/ }
164
- if jar.size > 0
165
- jar = jar[0]
166
- @naether.install( dep, nil, jar )
167
- end
168
-
164
+ install_jars.each do |dep|
165
+ notation, path = dep.to_a.first
166
+ @naether.install( notation, nil, path )
169
167
  end
170
168
 
171
169
  @naether
@@ -0,0 +1,61 @@
1
+
2
+ class Naether
3
+ class Configurator
4
+ def initialize(data={})
5
+ gem_dir = File.expand_path("#{File.dirname(__FILE__)}/../../")
6
+
7
+ version = nil
8
+
9
+ # Load VERSION file from gem to VERSION var
10
+ if File.exists?( File.expand_path("#{File.dirname(__FILE__)}/../../VERSION") )
11
+ version = IO.read(File.expand_path("#{File.dirname(__FILE__)}/../../VERSION")).strip
12
+
13
+ # VERSION file not found in gem dir, assume in current path, e.g.running from checkout
14
+ else
15
+ version = IO.read(File.expand_path("VERSION")).strip
16
+ end
17
+
18
+
19
+
20
+ @data = {
21
+ :gem_dir => gem_dir,
22
+ :naether_jar => File.join( gem_dir, "naether-#{version}.jar"),
23
+ :platform => ($platform || RUBY_PLATFORM[/java/] || 'ruby'),
24
+ :version => version,
25
+ :depenencies_yml => File.expand_path("#{File.dirname( __FILE__ )}/../../jar_dependencies.yml")
26
+ }
27
+
28
+ update!(data)
29
+ end
30
+
31
+ def update!(data)
32
+ data.each do |key, value|
33
+ self[key] = value
34
+ end
35
+ end
36
+
37
+ def [](key)
38
+ @data[key.to_sym]
39
+ end
40
+
41
+ def []=(key, value)
42
+ if value.class == Hash
43
+ @data[key.to_sym] = Config.new(value)
44
+ else
45
+ @data[key.to_sym] = value
46
+ end
47
+ end
48
+
49
+ def method_missing(sym, *args)
50
+ if sym.to_s =~ /(.+)=$/
51
+ self[$1] = args.first
52
+ else
53
+ self[sym]
54
+ end
55
+ end
56
+ end
57
+
58
+ unless defined?(Naether::Configuration)
59
+ Naether::Configuration = Naether::Configurator.new
60
+ end
61
+ end
data/lib/naether/java.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'singleton'
2
+ require "#{File.dirname(__FILE__)}/configuration"
2
3
 
3
4
  # :title:Naether::Java
4
5
  #
@@ -14,39 +15,22 @@ class Naether
14
15
 
15
16
  attr_reader :java
16
17
 
17
- def initialize
18
- if Naether.platform == 'java'
18
+ def initialize()
19
+ naether_jar = Naether::Configuration.naether_jar
20
+
21
+ unless File.exists? naether_jar
22
+ raise "Cannot create Naether::Java: #{naether_jar} does not exist"
23
+ end
24
+
25
+ if Naether::Configuration.platform == 'java'
26
+ require "#{File.dirname(__FILE__)}/java/jruby"
19
27
  @java = Naether::Java::JRuby.instance
20
28
  else
29
+ require "#{File.dirname(__FILE__)}/java/ruby"
21
30
  @java = Naether::Java::Ruby.instance
22
31
  end
23
32
  end
24
33
 
25
- # Jars loaded
26
- def self.loaded_jars
27
- instance.java.loaded_jars
28
- end
29
-
30
- # Loads all jars from the array of paths
31
- def self.load_jars_dir(paths)
32
- unless paths.is_a? Array
33
- paths = [paths]
34
- end
35
-
36
- jars = []
37
- paths.each do |path|
38
- jars = jars + Dir.glob( "#{File.expand_path(path)}/*.jar", File::FNM_CASEFOLD)
39
- end
40
-
41
- Naether::Java.load_jars(jars)
42
-
43
- end
44
-
45
- # Load jars for the runtime platform
46
- def self.load_jars(jars)
47
- instance.java.load_jars(jars)
48
- end
49
-
50
34
  # Paths loaded
51
35
  def self.loaded_paths
52
36
  instance.java.loaded_paths
@@ -57,12 +41,21 @@ class Naether
57
41
  instance.java.load_paths(paths)
58
42
  end
59
43
 
44
+ # Load paths for the runtime platform
45
+ def self.internal_load_paths(paths)
46
+ instance.java.internal_load_paths(paths)
47
+ end
48
+
60
49
  def self.create( target_class, *args )
61
50
  instance.java.create( target_class, *args )
62
51
  end
63
52
 
64
- def self.java_class( target_class )
65
- instance.java.java_class( target_class )
53
+ def self.exec_static_method( target_class, target_method, *args )
54
+ instance.java.exec_static_method( target_class, target_method, *args )
55
+ end
56
+
57
+ def self.convert_to_java_list( ruby_array )
58
+ instance.java.convert_to_java_list( ruby_array )
66
59
  end
67
60
 
68
61
  def self.convert_to_ruby_array( java_array, to_string = false )
@@ -73,174 +66,5 @@ class Naether
73
66
  instance.java.convert_to_ruby_hash( java_hash, to_string )
74
67
  end
75
68
 
76
- #
77
- # Handle loading jars for JRuby
78
- #
79
- class JRuby
80
- include Singleton
81
-
82
- attr_reader :loaded_jars, :loaded_paths
83
-
84
- def initialize
85
- require 'java'
86
-
87
- @loaded_jars = []
88
- @loaded_paths = []
89
- end
90
-
91
- def create( target_class, *args )
92
- java_class = java_class(target_class)
93
- java_class.new( *args )
94
- end
95
-
96
- def java_class( target_class )
97
- eval(target_class)
98
- end
99
-
100
- def load_paths(paths)
101
- load_paths = []
102
- unless paths.is_a? Array
103
- paths = [paths]
104
- end
105
-
106
- paths.each do |path|
107
- expanded_path = File.expand_path(path)
108
- if !@loaded_paths.include? expanded_path
109
- $CLASSPATH << expanded_path
110
- load_paths << expanded_path
111
- @loaded_paths << expanded_path
112
- end
113
- end
114
-
115
- load_paths
116
- end
117
-
118
- def load_jars(jars)
119
- load_jars = []
120
- unless jars.is_a? Array
121
- jars = [jars]
122
- end
123
-
124
- jars.each do |jar|
125
- expanded_jar = File.expand_path(jar)
126
- if !@loaded_jars.include? expanded_jar
127
- require expanded_jar
128
- load_jars << expanded_jar
129
- @loaded_jars << expanded_jar
130
- end
131
- end
132
-
133
- load_jars
134
- end
135
-
136
- def convert_to_ruby_array( java_array, to_string = false )
137
- java_array.to_a
138
- end
139
-
140
- def convert_to_ruby_hash( java_hash, to_string = false )
141
- java_hash.to_hash
142
- end
143
-
144
- end
145
-
146
- #
147
- # Handle loading jars for Ruby via Rjb
148
- #
149
- class Ruby
150
- include Singleton
151
-
152
- attr_reader :loaded_jars, :loaded_paths
153
-
154
- def initialize()
155
- require 'rjb'
156
-
157
- # Call Rjb::load with an empty classpath, incase Rjb::load has already been called
158
- java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split
159
- Rjb::load("", java_opts)
160
-
161
- @loaded_jars = []
162
- @loaded_paths = []
163
- end
164
-
165
- def create( target_class, *args )
166
- target_class = java_class(target_class)
167
- target_class.new( *args )
168
- end
169
-
170
- def java_class( target_class )
171
- Rjb::import( target_class )
172
- end
173
-
174
- def load_paths(paths)
175
- loadable_paths = []
176
- unless paths.is_a? Array
177
- paths = [paths]
178
- end
179
-
180
- paths.each do |path|
181
- expanded_path = File.expand_path(path)
182
- if !@loaded_paths.include? expanded_path
183
- @loaded_paths << expanded_path
184
- loadable_paths << expanded_path
185
- end
186
- end
187
-
188
- Rjb::load(loadable_paths.join(File::PATH_SEPARATOR))
189
-
190
- loadable_paths
191
- end
192
-
193
- def load_jars(jars)
194
- loadable_jars = []
195
- unless jars.is_a?( Array )
196
- jars = [jars]
197
- end
198
-
199
- jars.each do |jar|
200
- expanded_jar = File.expand_path(jar)
201
- if !@loaded_jars.include? expanded_jar
202
- loadable_jars << expanded_jar
203
- @loaded_jars << expanded_jar
204
- end
205
- end
206
-
207
- unless loadable_jars.empty?
208
- Rjb::add_jar( loadable_jars )
209
- end
210
-
211
- loadable_jars
212
- end
213
-
214
- def convert_to_ruby_array( java_array, to_string = false )
215
- ruby_array = java_array.toArray()
216
-
217
- if to_string
218
- ruby_array = ruby_array.map { |x| x.toString()}
219
- end
220
-
221
- ruby_array
222
- end
223
-
224
- def convert_to_ruby_hash( java_hash, to_string = false )
225
-
226
- hash = {}
227
- keys = java_hash.keySet()
228
- iterator = keys.iterator()
229
- if to_string
230
- while iterator.hasNext()
231
- key = iterator.next().toString()
232
- hash[key] = java_hash.get( key ).toString()
233
- end
234
- else
235
- while iterator.hasNext()
236
- key = iterator.next()
237
- hash[key] = java_hash.get( key )
238
- end
239
- end
240
-
241
- hash
242
-
243
- end
244
- end
245
69
  end
246
70
  end