Class: OSGi::Bundle

Inherits:
Object show all
Includes:
Buildr::ActsAsArtifact
Defined in:
lib/buildr4osgi/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:



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/buildr4osgi/osgi/bundle.rb', line 163

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



159
160
161
# File 'lib/buildr4osgi/osgi/bundle.rb', line 159

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.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/buildr4osgi/osgi/bundle.rb', line 104

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)} 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

.fromProject(project) ⇒ Object

Creates a bundle out of a project, using the manifest defined in its bundle package and the MANIFEST.MF file present in the project if any.



92
93
94
95
96
97
98
99
100
# File 'lib/buildr4osgi/osgi/bundle.rb', line 92

def self.fromProject(project)
  packaging = project.packages.select {|package| package.is_a?(BundlePackaging)}
  raise "More than one bundle packaging is defined over the project #{project.id}, see BOSGI-16." if packaging.size > 1
  return nil if packaging.empty?
  manifest = ::Buildr::Packaging::Java::Manifest.new(File.exists?("META-INF/MANIFEST.MF") ? File.read("META-INF/MANIFEST.MF") : nil) 
  manifest.main.merge!(project.manifest)
  manifest.main.merge!(packaging.first.manifest)
  fromManifest(Manifest.read(manifest.to_s), packaging.first.to_s)
end

Instance Method Details

#<=>(other) ⇒ Object

:nodoc:



212
213
214
215
216
217
218
# File 'lib/buildr4osgi/osgi/bundle.rb', line 212

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.



265
266
267
268
# File 'lib/buildr4osgi/osgi/bundle.rb', line 265

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)


200
201
202
# File 'lib/buildr4osgi/osgi/bundle.rb', line 200

def fragment?
  !fragment.nil?
end

#fragments(project) ⇒ Object

Finds the fragments associated with this bundle.



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/buildr4osgi/osgi/bundle.rb', line 250

def fragments(project)
project.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

#resolve(project, bundles = resolve_matching_artifacts(project)) ⇒ 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.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/buildr4osgi/osgi/bundle.rb', line 223

def resolve(project, bundles = resolve_matching_artifacts(project))
  bundle = case bundles.size
  when 0 then nil
  when 1 then bundles.first
  else
    BundleResolvingStrategies.send(project.osgi.options.bundle_resolving_strategy, bundles)
  end
  if bundle.nil?
    warn "Could not resolve bundle for #{self.to_s}" 
    return nil
  end
  return bundle if bundle.is_a?(Buildr::Project)
  
  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_artifacts(project) ⇒ Object

Resolves the matching artifacts associated with the project.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/buildr4osgi/osgi/bundle.rb', line 180

def resolve_matching_artifacts(project)
  # Collect the bundle projects, duplicate them so no changes can be applied to them
  # and extend them with the BundleProjectMatcher module
  
  b_projects = BundleProjects::bundle_projects.select {|p|
    unless p == project
      p.extend BundleProjectMatcher
      p.matches(:name => name, :version => version)
    end
  }
  #projects take precedence over the dependencies elsewhere, that's what happens in Eclipse
  # for example
  return b_projects unless b_projects.empty?
  return project.osgi.registry.resolved_containers.collect {|i| 
    i.find(:name => name, :version => version)
  }.flatten.compact.collect{|b| b.dup }
end

#to_sObject

:nodoc:



204
205
206
# File 'lib/buildr4osgi/osgi/bundle.rb', line 204

def to_s #:nodoc:
   to_spec()
end

#to_yaml(opts = {}) ⇒ Object

:nodoc:



208
209
210
# File 'lib/buildr4osgi/osgi/bundle.rb', line 208

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