Class: OSGi::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/osgi/bundle.rb

Overview

A bundle is an OSGi artifact represented by a jar file or a folder. It contains a manifest file with specific OSGi headers.

Constant Summary collapse

B_NAME =

Keys used in the MANIFEST.MF file

"Bundle-SymbolicName"
B_REQUIRE =
"Require-Bundle"
B_IMPORT_PKG =
"Import-Package"
B_EXPORT_PKG =
"Export-Package"
B_FRAGMENT_HOST =
"Fragment-Host"
B_VERSION =
"Bundle-Version"
B_DEP_VERSION =
"bundle-version"
B_RESOLUTION =
"resolution"
B_LAZY_START =
"Bundle-ActivationPolicy"
B_OLD_LAZY_START =
"Eclipse-LazyStart"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, args = {:file => nil, :bundles=>[], :imports => [], :optional => false}) ⇒ Bundle

:nodoc:



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osgi/bundle.rb', line 96

def initialize(name, version, args = {:file => nil, :bundles=>[], :imports => [], :optional => false}) #:nodoc:
  @name = name
  @version = VersionRange.parse(version) || (version.nil? ? nil : Version.new(version))
  @bundles = args[:bundles] || []
  @imports = args[:imports] || []
  @exported_packages = args[:exported_packages] || []
  @file = args[:file]
  @optional = args[:optional]
  @start_level = 4
  @type = "jar" #it's always a jar, even if it is a directory: we will jar it for Maven.
  @group = GroupMatcher.instance.group(name)
end

Instance Attribute Details

#bundlesObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def bundles
  @bundles
end

#exported_packagesObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def exported_packages
  @exported_packages
end

#fileObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def file
  @file
end

#fragmentObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def fragment
  @fragment
end

#groupObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def group
  @group
end

#importsObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def imports
  @imports
end

#lazy_startObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def lazy_start
  @lazy_start
end

#nameObject Also known as: id

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def name
  @name
end

#optionalObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def optional
  @optional
end

#start_levelObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def start_level
  @start_level
end

#versionObject

Attributes of a bundle, derived from its manifest The name is always the symbolic name The version is either the exact version of the bundle or the range in which the bundle would be accepted. The file is the location of the bundle on the disk The optional tag is present on bundles resolved as dependencies, marked as optional. The start level is deduced from the bundles.info file. Default is 1. The lazy start is found in the bundles.info file group is the artifact group used for Maven. By default it is set to OSGI_GROUP_ID. fragment is a Bundle object that represents the fragment host of this bundle (which means this bundle is a fragment if this field is not null). exported_packages is an array of strings representing the packages exported by the bundle. imports is an array of BundlePackage objects representing the packages imported by the bundle.



92
93
94
# File 'lib/osgi/bundle.rb', line 92

def version
  @version
end

Class Method Details

.fromManifest(manifest, jarFile) ⇒ Object

Creates itself by loading from the manifest file passed to it as a hash Finds the name and version, and populates a list of dependencies.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/osgi/bundle.rb', line 37

def self.fromManifest(manifest, jarFile) 
  if manifest.first[B_NAME].nil?
    warn "Could not find the name of the bundle represented by #{jarFile}"
    return nil
  end
  
  #see http://aspsp.blogspot.com/2008/01/wheressystembundlejarfilecont.html for the system.bundle trick.
  #key.strip: sometimes there is a space between the comma and the name of the bundle.
  #Add the required bundles:
  bundles = []
  manifest.first[B_REQUIRE].each_pair {|key, value| bundles << Bundle.new(key.strip, value[B_DEP_VERSION], {:optional => value[B_RESOLUTION] == "optional"}) unless "system.bundle" == key} unless manifest.first[B_REQUIRE].nil?
  exports = []
  manifest.first[B_EXPORT_PKG].each_pair {|key, value| exports << BundlePackage.new(key.strip, value["version"])} unless manifest.first[B_EXPORT_PKG].nil?
  
  #Parse the version
  version = manifest.first[B_VERSION].nil? ? nil : manifest.first[B_VERSION].keys.first
  
  #Read the imports
  imports = []
  manifest.first[B_IMPORT_PKG].each_pair {|key, value| imports << BundlePackage.new(key.strip, value["version"], :is_export => false, :optional => value[B_RESOLUTION] == "optional")} unless manifest.first[B_IMPORT_PKG].nil?
  
  #Read the imported packages
  
  bundle = Bundle.new(manifest.first[B_NAME].keys.first, version, {:file => jarFile, :bundles => bundles, :imports => imports, :exported_packages => exports})
  if !manifest.first[B_LAZY_START].nil? 
    # We look for the value of BundleActivationPolicy: lazy or nothing usually. 
    # lazy may be spelled Lazy too apparently, so we downcase the string in case.
    bundle.lazy_start = "lazy" == manifest.first[B_LAZY_START].keys.first.strip.downcase
  else
    bundle.lazy_start = "true" == manifest.first[B_OLD_LAZY_START].keys.first.strip unless manifest.first[B_OLD_LAZY_START].nil?
  end
  if (bundle.lazy_start)
    bundle.start_level = 4
  else
    bundle.start_level = 1
  end
  
  bundle.fragment = Bundle.new(manifest.first[B_FRAGMENT_HOST].keys.first.strip, 
    manifest.first[B_FRAGMENT_HOST].values.first[B_DEP_VERSION]) unless (manifest.first[B_FRAGMENT_HOST].nil?)
  return bundle
end

Instance Method Details

#<=>(other) ⇒ Object

:nodoc:



134
135
136
137
138
139
140
# File 'lib/osgi/bundle.rb', line 134

def <=>(other) #:nodoc:
  if other.is_a?(Bundle)
   return to_s <=> other.to_s
  else
    return to_s <=> other
  end
end

#==(other) ⇒ Object Also known as: eql?

Compares with another bundle and returns true if the name and version match.



186
187
188
189
# File 'lib/osgi/bundle.rb', line 186

def ==(other) #:nodoc:
  return false unless other.is_a?(Bundle)
  name == other.name && version == other.version    
end

#fragment?Boolean

Returns true if the bundle is an OSGi fragment.

Returns:

  • (Boolean)


122
123
124
# File 'lib/osgi/bundle.rb', line 122

def fragment?
  !fragment.nil?
end

#fragmentsObject

Finds the fragments associated with this bundle.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/osgi/bundle.rb', line 171

def fragments
OSGi.registry.resolved_containers.collect {|i| 
  i.find_fragments(:host => name).select{|f|
    if f.fragment.version.is_a? VersionRange
      f.fragment.version.in_range(version)
    elsif f.fragment.version.nil?
      true
    else
      f.fragment.version == version 
    end
    }
  }.flatten.compact.collect{|b| b.dup }
end

#hashObject

Important for maps. We absolutely want to avoid having to resolve several times the same bundle



196
197
198
# File 'lib/osgi/bundle.rb', line 196

def hash
  return to_s.hash
end

#resolve(bundles = resolve_matching_artifacts) ⇒ Object

Resolve a bundle from itself, by finding the appropriate bundle in the OSGi containers. Returns self or the project it represents if a project is found to be self.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/osgi/bundle.rb', line 145

def resolve(bundles = resolve_matching_artifacts)
  bundle = case bundles.size
  when 0 then nil
  when 1 then bundles.first
  else
    BundleResolvingStrategies.send(OSGi.options.bundle_resolving_strategy, bundles)
  end
  if bundle.nil?
    warn "Could not resolve bundle for #{self.to_s}" 
    return nil
  end
  
  osgi = self.dup
  osgi.name = bundle.name
  osgi.version = bundle.version
  osgi.bundles = bundle.bundles
  osgi.file = bundle.file
  osgi.optional = bundle.optional
  osgi.start_level = bundle.start_level
  osgi.group = bundle.group

  osgi
end

#resolve_matching_artifactsObject

Resolves the matching artifacts.



113
114
115
116
117
118
# File 'lib/osgi/bundle.rb', line 113

def resolve_matching_artifacts
  
  return OSGi.registry.resolved_containers.collect {|i| 
    i.find(:name => name, :version => version)
  }.flatten.compact.collect{|b| b.dup }
end

#to_sObject

:nodoc:



126
127
128
# File 'lib/osgi/bundle.rb', line 126

def to_s #:nodoc:
   "#{name}:#{id}"
end

#to_yaml(opts = {}) ⇒ Object

:nodoc:



130
131
132
# File 'lib/osgi/bundle.rb', line 130

def to_yaml(opts = {}) #:nodoc:
  to_s.to_yaml(opts)
end