naether 0.8.6-java → 0.9.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ require "#{File.dirname(__FILE__)}/../configuration"
2
+
3
+ # :title:Naether::Java::JRuby
4
+ #
5
+ # Singletn that handles Java interactions for JRuby. Providers helpers for
6
+ # nomalizing accesss.
7
+
8
+ # = Authors
9
+ # Michael Guymon
10
+ #
11
+ class Naether
12
+ class Java
13
+
14
+ class JRuby
15
+ include Singleton
16
+
17
+ attr_reader :loaded_paths, :class_loader
18
+
19
+ #
20
+ # Creates new instance by loading the Naether jar to the global ClassLoader
21
+ # and creating the internal Naether ClassLoader
22
+ #
23
+ def initialize
24
+ require 'java'
25
+
26
+ naether_jar = Naether::Configuration.naether_jar
27
+
28
+ @loaded_paths = []
29
+
30
+ load_paths(naether_jar)
31
+ @class_loader = com.tobedevoured.naether.PathClassLoader.new()
32
+ internal_load_paths(naether_jar)
33
+ end
34
+
35
+ #
36
+ # Create a Java Object
37
+ #
38
+ def create( target_class, *args )
39
+ @class_loader.newInstance(target_class, *args )
40
+ end
41
+
42
+ #
43
+ # Execute a Staic method on a Java class
44
+ #
45
+ def exec_static_method( target_class, target_method, params, types = nil )
46
+ unless params.is_a? Array
47
+ params = [params]
48
+ end
49
+
50
+ if types
51
+ unless types.is_a? Array
52
+ types = [types]
53
+ end
54
+ end
55
+
56
+ @class_loader.execStaticMethod( target_class, target_method, params, types )
57
+ end
58
+
59
+ #
60
+ # Load a path into the internal Naether ClassLoader
61
+ #
62
+ def internal_load_paths(paths)
63
+ load_paths = []
64
+ unless paths.is_a? Array
65
+ paths = [paths]
66
+ end
67
+
68
+ paths.each do |path|
69
+ expanded_path = File.expand_path(path)
70
+ if File.exists?( expanded_path )
71
+ @class_loader.addPath( expanded_path )
72
+
73
+ load_paths << expanded_path
74
+ end
75
+ end
76
+
77
+ load_paths
78
+ end
79
+
80
+ #
81
+ # Load a path onto the Global ClassLoader
82
+ #
83
+ def load_paths(paths)
84
+ load_paths = []
85
+ unless paths.is_a? Array
86
+ paths = [paths]
87
+ end
88
+
89
+ paths.each do |path|
90
+ expanded_path = File.expand_path(path)
91
+ if !@loaded_paths.include?( expanded_path ) && File.exists?( expanded_path )
92
+ $CLASSPATH << expanded_path
93
+ load_paths << expanded_path
94
+ @loaded_paths << expanded_path
95
+ end
96
+ end
97
+
98
+ load_paths
99
+ end
100
+
101
+ #
102
+ # Convert a Ruby Array to a Java ArrayList
103
+ #
104
+ def convert_to_java_list( ruby_array )
105
+ list = java.util.ArrayList.new
106
+ ruby_array.each do |item|
107
+ list.add( item )
108
+ end
109
+
110
+ list
111
+ end
112
+
113
+ #
114
+ # Convert a Java List to a Ruby Array
115
+ #
116
+ def convert_to_ruby_array( java_array, to_string = false )
117
+ java_array.to_a
118
+ end
119
+
120
+ #
121
+ # Convert a Java Map to a Ruby Hash
122
+ #
123
+ def convert_to_ruby_hash( java_hash, to_string = false )
124
+ java_hash.to_hash
125
+ end
126
+
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,153 @@
1
+ require "#{File.dirname(__FILE__)}/../configuration"
2
+
3
+ # :title:Naether::Java::Ruby
4
+ #
5
+ # Sngleton that handles Java interactions for Ruby using RJB. Providers helpers for
6
+ # nomalizing accesss.
7
+ #
8
+ # = Authors
9
+ # Michael Guymon
10
+ #
11
+ class Naether
12
+ class Java
13
+
14
+ class Ruby
15
+ include Singleton
16
+
17
+ attr_reader :loaded_paths, :class_loader
18
+
19
+ def initialize
20
+ require 'rjb'
21
+
22
+ naether_jar = Naether::Configuration.naether_jar
23
+
24
+ # Call Rjb::load with an empty classpath, incase Rjb::load has already been called
25
+ java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split
26
+ Rjb::load("", java_opts)
27
+
28
+ @loaded_paths = []
29
+ load_paths( naether_jar )
30
+
31
+ class_loader_class = Rjb::import( "com.tobedevoured.naether.PathClassLoader" )
32
+ @class_loader = class_loader_class.new()
33
+
34
+ internal_load_paths( naether_jar )
35
+
36
+ end
37
+
38
+ def create( target_class, *args )
39
+ #@class_loader.newInstance(target_class, *args )
40
+ if args.size > 0
41
+ @class_loader._invoke('newInstance', 'Ljava.lang.String;[Ljava.lang.Object;', target_class, args )
42
+ else
43
+ @class_loader._invoke('newInstance', 'Ljava.lang.String;', target_class )
44
+ end
45
+ end
46
+
47
+ def exec_static_method( target_class, target_method, params, types = nil )
48
+ unless params.is_a? Array
49
+ params = [params]
50
+ end
51
+
52
+ if types
53
+ unless types.is_a? Array
54
+ types = [types]
55
+ end
56
+ end
57
+ result = nil
58
+ if params.nil?
59
+ result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;', target_class, target_method )
60
+ elsif types.nil?
61
+ result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;Ljava.util.List;', target_class, target_method, convert_to_java_list(params) )
62
+ else
63
+ result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;Ljava.util.List;Ljava.util.List;', target_class, target_method, convert_to_java_list(params), convert_to_java_list(types) )
64
+ end
65
+
66
+ unless result.nil?
67
+ # Force toString on java.lang.String otherwise the result will be a Rjb::Proxy
68
+ if result.getClass().getName() == 'java.lang.String'
69
+ result.toString()
70
+ else
71
+ result
72
+ end
73
+ end
74
+ end
75
+
76
+ def internal_load_paths(paths)
77
+ loadable_paths = []
78
+ unless paths.is_a? Array
79
+ paths = [paths]
80
+ end
81
+
82
+ paths.each do |path|
83
+ expanded_path = File.expand_path(path)
84
+ if File.exists?( expanded_path )
85
+ @class_loader.addPath( path )
86
+ loadable_paths << expanded_path
87
+ end
88
+ end
89
+
90
+ loadable_paths
91
+ end
92
+
93
+ def load_paths(paths)
94
+ loadable_paths = []
95
+ unless paths.is_a? Array
96
+ paths = [paths]
97
+ end
98
+
99
+ paths.each do |path|
100
+ expanded_path = File.expand_path(path)
101
+ if !@loaded_paths.include?(expanded_path) && File.exists?( expanded_path )
102
+ @loaded_paths << expanded_path
103
+ Rjb::add_jar( expanded_path )
104
+ loadable_paths << expanded_path
105
+ end
106
+ end
107
+
108
+ loadable_paths
109
+ end
110
+
111
+ def convert_to_java_list( ruby_array )
112
+ list = Rjb::import("java.util.ArrayList").new
113
+ ruby_array.each do |item|
114
+ list.add( item )
115
+ end
116
+
117
+ list
118
+ end
119
+
120
+ def convert_to_ruby_array( java_array, to_string = false )
121
+ ruby_array = java_array.toArray()
122
+
123
+ if to_string
124
+ ruby_array = ruby_array.map { |x| x.toString()}
125
+ end
126
+
127
+ ruby_array
128
+ end
129
+
130
+ def convert_to_ruby_hash( java_hash, to_string = false )
131
+
132
+ hash = {}
133
+ keys = java_hash.keySet()
134
+ iterator = keys.iterator()
135
+ if to_string
136
+ while iterator.hasNext()
137
+ key = iterator.next().toString()
138
+ hash[key] = java_hash.get( key ).toString()
139
+ end
140
+ else
141
+ while iterator.hasNext()
142
+ key = iterator.next()
143
+ hash[key] = java_hash.get( key )
144
+ end
145
+ end
146
+
147
+ hash
148
+
149
+ end
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,142 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require "#{File.dirname(__FILE__)}/java"
17
+
18
+ class Naether
19
+
20
+ # Helper for interacting with a Maven POM
21
+ #
22
+ # @author Michael Guymon
23
+ #
24
+ class Maven
25
+
26
+ class << self
27
+
28
+ # Create an instance from a POM
29
+ #
30
+ # @params [String] pom_path
31
+ # @return [Naether::Maven]
32
+ def create_from_pom( pom_path )
33
+ maven = Maven.new( pom_path )
34
+ end
35
+
36
+ # Create an instance based on notation
37
+ #
38
+ # @params [String] notation
39
+ # @return [Naether::Maven]
40
+ # @see https://github.com/mguymon/naether/wiki/Notations
41
+ def create_from_notation( notation )
42
+ maven = Maven.new
43
+ maven.notation = notation
44
+ maven
45
+ end
46
+ end
47
+
48
+ #
49
+ # Create new instance
50
+ #
51
+ def initialize(pom_path = nil)
52
+ if pom_path
53
+ @project = Naether::Java.create("com.tobedevoured.naether.maven.Project", pom_path )
54
+ else
55
+ @project = Naether::Java.create("com.tobedevoured.naether.maven.Project" )
56
+ end
57
+
58
+ end
59
+
60
+ # Set the Notation for this Project
61
+ #
62
+ # @param [String] notation
63
+ # @see https://github.com/mguymon/naether/wiki/Notations
64
+ def notation=(notation)
65
+ @project.setProjectNotation( notation )
66
+ end
67
+
68
+ # Get dependences for Project as notations
69
+ #
70
+ # @param [Array<String>] scopes valid options are compile,test,runtime
71
+ def dependencies( scopes = nil)
72
+ if scopes
73
+ unless scopes.is_a? Array
74
+ scopes = [scopes]
75
+ end
76
+ end
77
+
78
+ if Naether.platform == 'java'
79
+ if scopes.nil?
80
+ deps = @project.getDependenciesNotation()
81
+ else
82
+ deps = @project.getDependenciesNotation( scopes )
83
+ end
84
+
85
+ else
86
+ list = nil
87
+ if scopes
88
+ list = Naether::Java.convert_to_java_list( scopes )
89
+
90
+ deps = @project._invoke('getDependenciesNotation', 'Ljava.util.List;', list)
91
+ else
92
+ deps = @project.getDependenciesNotation()
93
+ end
94
+
95
+ end
96
+
97
+ Naether::Java.convert_to_ruby_array( deps, true )
98
+ end
99
+
100
+ # Set dependencies
101
+ #
102
+ # @param [Array] dependencies
103
+ def dependencies=(dependencies)
104
+ @project.setDependencies( dependencies )
105
+ end
106
+
107
+ # Set repositories
108
+ #
109
+ # @param [Array] repositories of urls
110
+ def repositories=( repositories )
111
+ @project.setRepositories( repositories )
112
+ end
113
+
114
+ # Get the version
115
+ #
116
+ # return [String] version
117
+ def version()
118
+ return @project.getVersion()
119
+ end
120
+
121
+ # Load dependencies and remote repo from a {Naether} instance
122
+ def load_naether( naether )
123
+ self.dependencies= naether.resolver.getDependencies()
124
+ self.repositories= naether.resolver.getRemoteRepositoryUrls()
125
+ end
126
+
127
+ # Create the XML for a Maven Pom
128
+ #
129
+ # @return [String] pom xml
130
+ def build_pom()
131
+ @project.toXml()
132
+ end
133
+
134
+
135
+ # Write Maven Pom
136
+ #
137
+ # @param [String] file_path
138
+ def write_pom( file_path )
139
+ @project.writePom( file_path )
140
+ end
141
+ end
142
+ end
data/naether-0.9.0.jar ADDED
Binary file
data/naether.gemspec ADDED
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "naether"
8
+ s.version = "0.9.0"
9
+ s.platform = "java"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Michael Guymon"]
13
+ s.date = "2012-09-12"
14
+ s.description = "Java dependency resolver using Maven's Aether"
15
+ s.email = "michael@tobedevoured.com"
16
+ s.extensions = ["Rakefile"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "jar_dependencies.yml",
27
+ "lib/naether.rb",
28
+ "lib/naether/bootstrap.rb",
29
+ "lib/naether/configuration.rb",
30
+ "lib/naether/java.rb",
31
+ "lib/naether/java/jruby.rb",
32
+ "lib/naether/java/ruby.rb",
33
+ "lib/naether/maven.rb",
34
+ "naether-0.9.0.jar",
35
+ "naether.gemspec",
36
+ "pom.xml"
37
+ ]
38
+ s.homepage = "http://github.com/mguymon/naether"
39
+ s.licenses = ["Apache"]
40
+ s.require_paths = ["lib"]
41
+ s.rubyforge_project = "naether"
42
+ s.rubygems_version = "1.8.24"
43
+ s.summary = "Java dependency resolver using Maven's Aether"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rake>, ["~> 0.9.2"])
50
+ s.add_development_dependency(%q<rspec>, ["> 2.9.0"])
51
+ s.add_development_dependency(%q<bundler>, ["> 1.0.0"])
52
+ s.add_development_dependency(%q<jeweler>, ["> 1.5.2"])
53
+ s.add_development_dependency(%q<yard>, [">= 0"])
54
+ s.add_development_dependency(%q<kramdown>, [">= 0"])
55
+ s.add_development_dependency(%q<jruby-openssl>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<rake>, ["~> 0.9.2"])
58
+ s.add_dependency(%q<rspec>, ["> 2.9.0"])
59
+ s.add_dependency(%q<bundler>, ["> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["> 1.5.2"])
61
+ s.add_dependency(%q<yard>, [">= 0"])
62
+ s.add_dependency(%q<kramdown>, [">= 0"])
63
+ s.add_dependency(%q<jruby-openssl>, [">= 0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<rake>, ["~> 0.9.2"])
67
+ s.add_dependency(%q<rspec>, ["> 2.9.0"])
68
+ s.add_dependency(%q<bundler>, ["> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["> 1.5.2"])
70
+ s.add_dependency(%q<yard>, [">= 0"])
71
+ s.add_dependency(%q<kramdown>, [">= 0"])
72
+ s.add_dependency(%q<jruby-openssl>, [">= 0"])
73
+ end
74
+ end
75
+