osgi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
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
+ module OSGi #:nodoc:
17
+
18
+ # Functions declared on this module are used to select bundles exporting a particular package.
19
+ # Functions must have the signature (package, bundles)
20
+ # where
21
+ # package: the name of the package
22
+ # bundles: an array of bundles
23
+ #
24
+ module PackageResolvingStrategies
25
+
26
+ # Default module function that prompts the user to select the bundle(s)
27
+ # he'd like to select as dependencies.
28
+ #
29
+ def prompt(package, bundles)
30
+ bundle = nil
31
+ while (bundle.nil?)
32
+ puts "This package #{package} is exported by all the bundles present.\n" +
33
+ "Choose a bundle amongst those presented or press A to select them all:\n" + bundles.sort! {|a, b| a.version <=> b.version }.
34
+ collect {|b| "\t#{bundles.index(b) +1}. #{b.name} #{b.version}"}.join("\n")
35
+ number = $stdin.gets.chomp
36
+ begin
37
+ return bundles if (number == 'A')
38
+ number = number.to_i
39
+ number -= 1
40
+ bundle = bundles[number] if number >= 0 # no negative indexing here.
41
+ puts "Invalid index" if number < 0
42
+ rescue Exception => e
43
+ puts "Invalid index"
44
+ #do nothing
45
+ end
46
+ end
47
+ [bundle]
48
+ end
49
+
50
+ # Default module function that selects all the matching bundles to the dependencies.
51
+ # This is the default function.
52
+ #
53
+ def all(package, bundles)
54
+ warn "*** SPLIT PACKAGE: #{package} is exported by <#{bundles.join(", ")}>"
55
+ return bundles
56
+ end
57
+
58
+ module_function :prompt, :all
59
+ end
60
+
61
+ # Functions declared on this module are used to select bundles amongst a list of them,
62
+ # when requiring bundles through the Require-Bundle header.
63
+ # Functions must have the signature (bundles)
64
+ # where
65
+ # bundles: an array of bundles
66
+ #
67
+ module BundleResolvingStrategies
68
+ #
69
+ # Default strategy:
70
+ # the bundle with the highest version number is returned.
71
+ #
72
+ def latest(bundles)
73
+ bundles.sort {|a, b| a.version <=> b.version}.last
74
+ end
75
+
76
+ #
77
+ # The bundle with the lowest version number is returned.
78
+ #
79
+ def oldest(bundles)
80
+ bundles.sort {|a, b| a.version <=> b.version}.first
81
+ end
82
+
83
+ # Default module function that prompts the user to select the bundle
84
+ # he'd like to select as dependencies.
85
+ #
86
+ def prompt(bundles)
87
+ bundle = nil
88
+ while (bundle.nil?)
89
+ puts "Choose a bundle amongst those presented:\n" + bundles.sort! {|a, b| a.version <=> b.version }.
90
+ collect {|b| "\t#{bundles.index(b) +1}. #{b.name} #{b.version}"}.join("\n")
91
+ number = $stdin.gets.chomp
92
+ begin
93
+ number = number.to_i
94
+ number -= 1
95
+ bundle = bundles[number] if number >= 0 # no negative indexing here.
96
+ puts "Invalid index" if number < 0
97
+ rescue Exception => e
98
+ puts "Invalid index"
99
+ #do nothing
100
+ end
101
+ end
102
+ bundle
103
+ end
104
+
105
+ module_function :latest, :oldest, :prompt
106
+ end
107
+ end
@@ -0,0 +1,131 @@
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
+ module OSGi #:nodoc:
17
+
18
+ #
19
+ # A class to represent OSGi versions.
20
+ #
21
+ class Version
22
+
23
+ attr_accessor :major, :minor, :tiny, :qualifier
24
+
25
+ def initialize(string) #:nodoc:
26
+ digits = string.gsub(/\"/, '').split(".")
27
+ @major = digits[0]
28
+ @minor = digits[1]
29
+ @tiny = digits[2]
30
+ @qualifier = digits[3]
31
+ raise "Invalid version: " + self.to_s if @major == ""
32
+ raise "Invalid version: " + self.to_s if @minor == "" && (!@tiny != "" || !@qualifier != "")
33
+ raise "Invalid version: " + self.to_s if @tiny == "" && !@qualifier != ""
34
+ end
35
+
36
+
37
+ def to_s #:nodoc:
38
+ str = [major]
39
+ str << minor if minor
40
+ str << tiny if minor && tiny
41
+ str << qualifier if minor && tiny && qualifier
42
+ str.compact.join(".")
43
+ end
44
+
45
+ def <=>(other) #:nodoc:
46
+ if other.is_a? String
47
+ other = Version.new(other)
48
+ elsif other.nil?
49
+ return 1
50
+ end
51
+
52
+ [:major, :minor, :tiny, :qualifier].each do |digit|
53
+ return 0 if send(digit).nil? or other.send(digit).nil?
54
+
55
+ comparison = send(digit) <=> other.send(digit)
56
+ if comparison != 0
57
+ return comparison
58
+ end
59
+
60
+ end
61
+ return 0
62
+ end
63
+
64
+ def <(other) #:nodoc:
65
+ (self.<=>(other)) == -1
66
+ end
67
+
68
+ def >(other) #:nodoc:
69
+ (self.<=>(other)) == 1
70
+ end
71
+
72
+ def ==(other) #:nodoc:
73
+ (self.<=>(other)) == 0
74
+ end
75
+
76
+ def <=(other) #:nodoc:
77
+ (self.==(other)) || (self.<(other))
78
+ end
79
+
80
+ def >=(other) #:nodoc:
81
+ (self.==(other)) || (self.>(other))
82
+ end
83
+ end
84
+
85
+ class VersionRange #:nodoc:
86
+
87
+ attr_accessor :min, :max, :min_inclusive, :max_inclusive, :max_infinite
88
+
89
+ # Parses a string into a VersionRange.
90
+ # Returns false if the string could not be parsed.
91
+ #
92
+ def self.parse(string, max_infinite = false)
93
+ return string if string.is_a?(VersionRange) || string.is_a?(Version)
94
+ if !string.nil? && (match = string.match /\s*([\[|\(])([0-9|A-z|\.]*)\s*,\s*([0-9|A-z|\.]*)([\]|\)])/)
95
+ range = VersionRange.new
96
+ range.min = Version.new(match[2])
97
+ range.max = Version.new(match[3])
98
+ range.min_inclusive = match[1] == '['
99
+ range.max_inclusive = match[4] == ']'
100
+ range
101
+ elsif (!string.nil? && max_infinite && string.match(/[0-9|\.]*/))
102
+ range = VersionRange.new
103
+ range.min = Version.new(string)
104
+ range.max = nil
105
+ range.min_inclusive = true
106
+ range.max_infinite = true
107
+ range
108
+ else
109
+ false
110
+ end
111
+ end
112
+
113
+ def to_s #:nodoc:
114
+ "#{ min_inclusive ? '[' : '('}#{min},#{max_infinite ? "infinite" : max}#{max_inclusive ? ']' : ')'}"
115
+ end
116
+
117
+ # Returns true if the version is in the range of this VersionRange object.
118
+ # Uses OSGi versioning rules to determine if the version is in range.
119
+ #
120
+ def in_range(version)
121
+ return in_range(version.min) && (version.max_infinite ? true : in_range(version.max)) if version.is_a?(VersionRange)
122
+
123
+ result = min_inclusive ? min <= version : min < version
124
+ if (!max_infinite)
125
+ result &= max_inclusive ? max >= version : max > version
126
+ end
127
+ result
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,36 @@
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
+
17
+ Gem::Specification.new do |spec|
18
+ spec.name = 'osgi'
19
+ spec.version = '0.0.1'
20
+ spec.author = 'Antoine Toulme'
21
+ spec.email = "antoine@lunar-ocean.com"
22
+ spec.homepage = "http://github.com/atoulme/osgi"
23
+ spec.summary = "An implementation of the OSGi framework in Ruby"
24
+ spec.description = <<-TEXT
25
+ The OSGi framework offers an unique way for Java bundles to communicate and depend on each other.
26
+ This implementation of the OSGi framework is supposed to represent the framework as it should be in Ruby.
27
+ TEXT
28
+ spec.files = Dir['{doc,etc,lib,rakelib,spec}/**/*', '*.{gemspec,buildfile}'] +
29
+ ['LICENSE', 'NOTICE', 'README.rdoc', 'Rakefile']
30
+ spec.require_paths = ['lib']
31
+ spec.has_rdoc = true
32
+ spec.extra_rdoc_files = 'README.rdoc', 'LICENSE', 'NOTICE'
33
+ spec.rdoc_options = '--title', 'OSGi', '--main', 'README.rdoc',
34
+ '--webcvs', 'http://github.com/atoulme/osgi'
35
+ spec.add_dependency("manifest", "= 0.0.8")
36
+ end
@@ -0,0 +1,44 @@
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.join(File.dirname(__FILE__), '../spec_helpers')
17
+
18
+ describe OSGi::BundlePackage do
19
+
20
+ it 'should have a nice string representation' do
21
+ package = OSGi::BundlePackage.new("my.package", "1.0.0")
22
+ package2 = OSGi::BundlePackage.new("my.package", "[1.0.0,2.0.0]")
23
+ package.to_s.should == "Package my.package; version [1.0.0,infinite)"
24
+ package2.to_s.should == "Package my.package; version [1.0.0,2.0.0]"
25
+ end
26
+
27
+ it 'should be able to know if it equals another bundle package' do
28
+ package = OSGi::BundlePackage.new("my.package", "1.0.0")
29
+ package2 = OSGi::BundlePackage.new("my.package", "1.0.0")
30
+ package.should == package2
31
+ end
32
+
33
+ it 'should be able to know if it equals another bundle package with version range' do
34
+ package = OSGi::BundlePackage.new("javax.servlet", "[2.4.0,3.0.0)")
35
+ package2 = OSGi::BundlePackage.new("javax.servlet", "[2.4.0,3.0.0)")
36
+ package.should == package2
37
+ end
38
+
39
+ it 'should define the same hash when bundles are equal' do
40
+ package = OSGi::BundlePackage.new("my.package", "1.0.0")
41
+ package2 = OSGi::BundlePackage.new("my.package", "1.0.0")
42
+ package.hash.should == package2.hash
43
+ end
44
+ end
@@ -0,0 +1,94 @@
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.join(File.dirname(__FILE__), '..', 'spec_helpers')
17
+
18
+ describe OSGi::Bundle do
19
+ before :all do
20
+ manifest = <<-MANIFEST
21
+ Manifest-Version: 1.0
22
+ Bundle-ManifestVersion: 2
23
+ Bundle-SymbolicName: org.eclipse.core.resources; singleton:=true
24
+ Bundle-Version: 3.5.1.R_20090912
25
+ Bundle-ActivationPolicy: Lazy
26
+ MANIFEST
27
+ @bundle = OSGi::Bundle.fromManifest(Manifest.read(manifest), ".")
28
+ manifest2 = <<-MANIFEST
29
+ Manifest-Version: 1.0
30
+ Bundle-ManifestVersion: 2
31
+ Bundle-SymbolicName: org.eclipse.core.resources.x86; singleton:=true
32
+ Bundle-Version: 3.5.1.R_20090912
33
+ Bundle-ActivationPolicy: Lazy
34
+ Fragment-Host: org.eclipse.core.resources
35
+ MANIFEST
36
+ @fragment = OSGi::Bundle.fromManifest(Manifest.read(manifest2), ".")
37
+ end
38
+
39
+ it 'should read from a manifest' do
40
+ @bundle.name.should eql("org.eclipse.core.resources")
41
+ @bundle.version.to_s.should eql("3.5.1.R_20090912")
42
+ @bundle.lazy_start.should be_true
43
+ @bundle.start_level.should eql(4)
44
+ end
45
+
46
+ it 'should recognize itself as a fragment' do
47
+ @fragment.fragment?.should be_true
48
+ end
49
+
50
+ it 'should return nil if no name is given in the manifest' do
51
+ manifest = <<-MANIFEST
52
+ Manifest-Version: 1.0
53
+ Bundle-ManifestVersion: 2
54
+ Bundle-Version: 3.5.1.R_20090912
55
+ Bundle-ActivationPolicy: Lazy
56
+ MANIFEST
57
+ OSGi::Bundle.fromManifest(Manifest.read(manifest), ".").should be_nil
58
+ end
59
+
60
+ end
61
+
62
+ describe "fragments" do
63
+ before :all do
64
+ manifest = <<-MANIFEST
65
+ Manifest-Version: 1.0
66
+ Bundle-ManifestVersion: 2
67
+ Bundle-SymbolicName: org.eclipse.core.resources; singleton:=true
68
+ Bundle-Version: 3.5.1.R_20090912
69
+ Bundle-ActivationPolicy: Lazy
70
+ MANIFEST
71
+ manifest2 = <<-MANIFEST
72
+ Manifest-Version: 1.0
73
+ Bundle-ManifestVersion: 2
74
+ Bundle-SymbolicName: org.eclipse.core.resources.x86; singleton:=true
75
+ Bundle-Version: 3.5.1.R_20090912
76
+ Bundle-ActivationPolicy: Lazy
77
+ Fragment-Host: org.eclipse.core.resources
78
+ MANIFEST
79
+ @eclipse_instances = [createRepository("eclipse1")]
80
+ write File.join(@eclipse_instances.first, "plugins", "org.eclipse.core.resources-3.5.1.R_20090512", "META-INF", "MANIFEST.MF"), manifest
81
+ write File.join(@eclipse_instances.first, "plugins", "org.eclipse.core.resources.x86-3.5.1.R_20090512", "META-INF", "MANIFEST.MF"), manifest2
82
+ @bundle = OSGi::Bundle.fromManifest(Manifest.read(manifest), ".")
83
+ @fragment = OSGi::Bundle.fromManifest(Manifest.read(manifest2), ".")
84
+ end
85
+
86
+
87
+ it "should find the bundle fragments" do
88
+ OSGi.registry.containers = @eclipse_instances.dup
89
+
90
+ @bundle.fragments.should == [@fragment]
91
+ end
92
+
93
+ end
94
+
@@ -0,0 +1,89 @@
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.join(File.dirname(__FILE__), '../spec_helpers')
17
+
18
+ describe OSGi::Container do
19
+
20
+ before :all do
21
+ e1= createRepository("eclipse1")
22
+ e2= createRepository("eclipse2")
23
+ @eclipse_instances = [e1, e2]
24
+
25
+ write e1 + "/plugins/com.ibm.icu-3.9.9.R_20081204/META-INF/MANIFEST.MF", <<-MANIFEST
26
+ Manifest-Version: 1.0
27
+ Bundle-ManifestVersion: 2
28
+ Bundle-SymbolicName: com.ibm.icu; singleton:=true
29
+ Bundle-Version: 3.9.9.R_20081204
30
+ MANIFEST
31
+ write e1 + "/plugins/org.eclipse.core.resources-3.5.0.R_20090512/META-INF/MANIFEST.MF", <<-MANIFEST
32
+ Manifest-Version: 1.0
33
+ Bundle-ManifestVersion: 2
34
+ Bundle-SymbolicName: org.eclipse.core.resources; singleton:=true
35
+ Bundle-Version: 3.5.0.R_20090512
36
+ MANIFEST
37
+ write e1 + "/plugins/org.fragments-3.5.0.R_20090512/META-INF/MANIFEST.MF", <<-MANIFEST
38
+ Manifest-Version: 1.0
39
+ Bundle-ManifestVersion: 2
40
+ Bundle-SymbolicName: org.fragment; singleton:=true
41
+ Fragment-Host: org.eclipse.core.resources
42
+ Bundle-Version: 3.5.0.R_20090512
43
+ MANIFEST
44
+ write e2 + "/plugins/org.eclipse.core.resources-3.5.1.R_20090912/META-INF/MANIFEST.MF", <<-MANIFEST
45
+ Manifest-Version: 1.0
46
+ Bundle-ManifestVersion: 2
47
+ Bundle-SymbolicName: org.eclipse.core.resources; singleton:=true
48
+ Bundle-Version: 3.5.1.R_20090912
49
+ MANIFEST
50
+ write e2 + "/plugins/org.eclipse.ui-3.4.2.R_20090226/META-INF/MANIFEST.MF", <<-MANIFEST
51
+ Manifest-Version: 1.0
52
+ Bundle-ManifestVersion: 2
53
+ Bundle-SymbolicName: org.eclipse.ui; singleton:=true
54
+ Bundle-Version: 3.4.2.R_20090226
55
+ MANIFEST
56
+
57
+ write e2 + "/plugins/org.eclipse.ui-3.5.0.M_20090107/META-INF/MANIFEST.MF", <<-MANIFEST
58
+ Manifest-Version: 1.0
59
+ Bundle-ManifestVersion: 2
60
+ Bundle-SymbolicName: org.eclipse.ui; singleton:=true
61
+ Bundle-Version: 3.5.0.M_20090107
62
+ MANIFEST
63
+ end
64
+
65
+ it 'should be able to create an OSGi container instance on a folder' do
66
+ lambda {OSGi::Container.new(@eclipse_instances.first)}.should_not raise_error
67
+ end
68
+
69
+ it 'should be able to list the bundles present in the container' do
70
+ e1 = OSGi::Container.new(@eclipse_instances.first)
71
+ bundles = e1.bundles.select {|bundle| bundle.name == "com.ibm.icu"}
72
+ bundles.size.should eql(1)
73
+ end
74
+
75
+ it 'should be able to list the fragments present in the container' do
76
+ e1 = OSGi::Container.new(@eclipse_instances.first)
77
+ e1.fragments.size.should eql(1)
78
+ end
79
+
80
+ it 'should find a specific bundle in the container' do
81
+ e2 = OSGi::Container.new(@eclipse_instances.last)
82
+ e2.find(:name=> "org.eclipse.ui", :version => "3.5.0.M_20090107").should_not be_empty
83
+ end
84
+
85
+ it 'should find a specific fragment in the container' do
86
+ e1 = OSGi::Container.new(@eclipse_instances.first)
87
+ e1.find_fragments(:name=> "org.fragment").should_not be_empty
88
+ end
89
+ end