Class: Jnlp::Resource

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

Overview

Jnlp::Resource objects encapsulate both jar and nativelib elements present in a Java Web Start Jnlp <resources> element.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res, codebase, os) ⇒ Resource

Returns a new instance of Resource.



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/jnlp/jnlp.rb', line 365

def initialize(res, codebase, os)
  @resource = res
  @kind = res.name
  @main = res['main'] && res['main'] == 'true'
  @href = res['href']
  @href_path = File.dirname(@href)
  if @href_path == '.'
    @href_path = ''
  else
    @href_path = @href_path + '/'
  end
  @name = File.basename(@href).chomp('.jar')
  @version_str = res['version']
  if @version_str
    @suffix = "__V#{@version_str}.jar"
  else
    @suffix = ".jar"        
  end
  @filename = "#{@name}#{@suffix}"
  @filename_pack = @filename + ".pack"
  @filename_pack_gz = @filename_pack + ".gz"

  @url = "#{codebase}/#{@href_path}#{@name}.jar"
  @url << "?version-id=#{@version_str}" if @version_str
  # example: data-util__V0.1.0-20070926.155656-107.jar.pack
  # @url_pack = "#{codebase}/#{@href_path}#{@filename}.pack"
  # example: data-util__V0.1.0-20070926.155656-107.jar.pack.gz
  @url_pack_gz = "#{codebase}/#{@href_path}#{@filename}.pack.gz"
  @version, @revision, @date_str, @date_time, @certificate_version = parse_version_str(@version_str)
  @os = os
end

Instance Attribute Details

#certificate_versionObject (readonly)

Contains the certificate version of the resource if one exists, otherwize it is nil

Example:

"s1"


363
364
365
# File 'lib/jnlp/jnlp.rb', line 363

def certificate_version
  @certificate_version
end

#date_strObject (readonly)

Contains the date string of the resource

Example:

"20080318.093629"


338
339
340
# File 'lib/jnlp/jnlp.rb', line 338

def date_str
  @date_str
end

#date_timeObject (readonly)

Contains a Ruby DateTime object representation of the resource’s date string

Example:

#<DateTime: 85338394589/86400,0,2299161>


346
347
348
# File 'lib/jnlp/jnlp.rb', line 346

def date_time
  @date_time
end

#filenameObject (readonly)

Contains the suffix of the resource

Example:

"__V0.1.0.jar"


231
232
233
# File 'lib/jnlp/jnlp.rb', line 231

def filename
  @filename
end

#filename_packObject (readonly)

Contains the filename of the gzipped pack200 version of the resource

Example:

"httpclient__V0.1.0-20071212.220020-17.jar.pack.gz"


239
240
241
# File 'lib/jnlp/jnlp.rb', line 239

def filename_pack
  @filename_pack
end

#filename_pack_gzObject (readonly)

Contains the filename of the pack200 version of the resource

Example:

"httpclient__V0.1.0-20071212.220020-17.jar.pack"


247
248
249
# File 'lib/jnlp/jnlp.rb', line 247

def filename_pack_gz
  @filename_pack_gz
end

#hrefObject (readonly)

Contains a boolean that repesents whether the main_class for this jnlp is contained within this jar. This attribute is optional in a jnlp and if present should only be present and set to true on one jar resource in a jnlp.



199
200
201
# File 'lib/jnlp/jnlp.rb', line 199

def href
  @href
end

#href_pathObject (readonly)

Contains the path to the resource taken from the href attribute

Example:

"org/concord/httpclient/" || ""


158
159
160
# File 'lib/jnlp/jnlp.rb', line 158

def href_path
  @href_path
end

#kindObject (readonly)

Contains the kind of the resource

Example:

"jar" || "nativelib"


166
167
168
# File 'lib/jnlp/jnlp.rb', line 166

def kind
  @kind
end

#local_cache_dirObject

Contains the absolute local path of cache directory

Example:

"/Users/stephen/dev/jetty-jnlp-proxy/cache"


282
283
284
# File 'lib/jnlp/jnlp.rb', line 282

def local_cache_dir
  @local_cache_dir
end

#local_pathObject (readonly)

Contains the absolute local path of the resource

Example:

"/Users/stephen/dev/jetty-jnlp-proxy/cache/net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar"


298
299
300
# File 'lib/jnlp/jnlp.rb', line 298

def local_path
  @local_path
end

#local_path_pack_gzObject (readonly)

Contains the absolute local path of the resource

Example:

"/Users/stephen/dev/jetty-jnlp-proxy/cache/net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar.pack.gz"


314
315
316
# File 'lib/jnlp/jnlp.rb', line 314

def local_path_pack_gz
  @local_path_pack_gz
end

#mainObject (readonly)

Contains the href attribute of the resource

Example:

"net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy.jar"


192
193
194
# File 'lib/jnlp/jnlp.rb', line 192

def main
  @main
end

#nameObject (readonly)

Contains the base name of the resource

Example:

"httpclient"


174
175
176
# File 'lib/jnlp/jnlp.rb', line 174

def name
  @name
end

#osObject (readonly)

Contains the Java Web Start specification of the OS for the <resources> parent of this resource if the attribute value exists

Example:

""


184
185
186
# File 'lib/jnlp/jnlp.rb', line 184

def os
  @os
end

#relative_local_pathObject (readonly)

Contains the relative local path of the resource

Example:

"net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar"


290
291
292
# File 'lib/jnlp/jnlp.rb', line 290

def relative_local_path
  @relative_local_path
end

#relative_local_path_pack_gzObject (readonly)

Contains the relative local path of the resource

Example:

"net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar.pack.gz"


306
307
308
# File 'lib/jnlp/jnlp.rb', line 306

def relative_local_path_pack_gz
  @relative_local_path_pack_gz
end

#resourceObject (readonly)

Contains the Hpricot element parsed from the orginal Jnlp that was used to create the resource



150
151
152
# File 'lib/jnlp/jnlp.rb', line 150

def resource
  @resource
end

#revisionObject (readonly)

Contains the revision of the resource

Example:

72


354
355
356
# File 'lib/jnlp/jnlp.rb', line 354

def revision
  @revision
end

#signature_verifiedObject (readonly)

Contains boolean value indicating whether the signature of the cached local copy of the resource verified successfully

The value is nil if no local cache has been created.

Example:

true || false || nil


274
275
276
# File 'lib/jnlp/jnlp.rb', line 274

def signature_verified
  @signature_verified
end

#sizeObject (readonly)

Contains the size of the resource

Example:

294248


255
256
257
# File 'lib/jnlp/jnlp.rb', line 255

def size
  @size
end

#size_pack_gzObject (readonly)

Contains the size of the gzipped pack200 version of the resource

Example:

112213


263
264
265
# File 'lib/jnlp/jnlp.rb', line 263

def size_pack_gz
  @size_pack_gz
end

#suffixObject (readonly)

Contains the filename of the resource

Example:

"httpclient__V0.1.0-20071212.220020-17.jar"


223
224
225
# File 'lib/jnlp/jnlp.rb', line 223

def suffix
  @suffix
end

#urlObject (readonly)

Contains the url reference to the resource

Example:

"http://jnlp.concord.org/dev/org/concord/httpclient/httpclient.jar?version-id=0.1.0-20071212.220020-17"


207
208
209
# File 'lib/jnlp/jnlp.rb', line 207

def url
  @url
end

#url_pack_gzObject (readonly)

Contains the url reference to the gzipped pack200 version of the resource

Example:

"http://jnlp.concord.org/dev/org/concord/httpclient/httpclient__V0.1.0-20071212.220020-17.jar.pack.gz"


215
216
217
# File 'lib/jnlp/jnlp.rb', line 215

def url_pack_gz
  @url_pack_gz
end

#versionObject (readonly)

Contains the version of the resource

Example:

"0.1.0"


330
331
332
# File 'lib/jnlp/jnlp.rb', line 330

def version
  @version
end

#version_strObject (readonly)

Contains the version string of the resource

Example:

"0.1.0-20080318.093629-72"


322
323
324
# File 'lib/jnlp/jnlp.rb', line 322

def version_str
  @version_str
end

Instance Method Details

#cache_resource(dest_dir = @local_cache_dir, include_pack_gz = false) ⇒ Object

Copies the resource referenced in Resource#url to the local cache.

If the resource is successully cached locally and the signature is verified the size of the resource is retured.

If the signature is not verified then false is returned.



534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/jnlp/jnlp.rb', line 534

def cache_resource(dest_dir=@local_cache_dir, include_pack_gz = false)
  unless dest_dir
    raise ArgumentError, "Must specify destination directory when creating resource", caller
  end
  self.local_cache_dir=dest_dir
  @size = update_cache(@url, @local_path)
  if include_pack_gz
    @relative_local_path_gz = "#{@relative_local_path_gz}.pack.gz"
    @local_path_pack_gz = "#{dest_dir}/#{@relative_local_path_pack_gz}"
    @size_pack_gz = update_cache(@url_pack_gz, @local_path_pack_gz)
  end
  @signature_verified ? @size : @signature_verified
end

#parse_version_str(version_str) ⇒ Object

parse_version_str

input examples:

"0.1.0-20070926.155656-107"

or a newer example:

"0.1.0-20090618.143130-890-s1"

but … some version strings just look like this:

"0.1.0"

or this:

"2.1.7-r2"

results:

version # => ‘0.1.0’ revision # => 20 date_str # => ‘20070926.155656’ date_time # => #<DateTime: 10673317777/10800,0,2299161> certificate_version # => ‘s1’



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/jnlp/jnlp.rb', line 423

def parse_version_str(version_str)      
  version = date_str = certificate_version = ''
  revision = date_time = nil
  if version_str && version_str.length > 0
    if md = version_str.match(/(\d|\.)+/)
      version = md[0]
      # date_str
      if md2 = md.post_match.match(/-([\d]{8}.[\d]{6})(-|$)/)
        date_str = md2[1]
        d, t = date_str.scan(/\d+/)              # => ["20070926", "155656"]
        d1 = "#{d[0..3]}-#{d[4..5]}-#{d[6..7]}"   # => "2007-09-26"
        t1 = "#{t[0..1]}:#{t[2..3]}:#{t[4..5]}"   # => "15:56:56"
        dt = "#{d1}T#{t1}Z"                      # => "2007-09-26T15:56:56Z"
        date_time = DateTime.parse(dt)           # => #<DateTime: 10673317777/10800,0,2299161>
        # revision
        if md3 = md2.post_match.match(/\d+/)
          revision = md3[0].to_i
        end
      else
        if match = md.post_match[/\d+/]
          revision = match.to_i
        end
      end
      # certificate_version
      if match = md.post_match[/-(s\d+)/, 1]
        certificate_version = match
      end
    end
  end
  [version, revision, date_str, date_time, certificate_version]
end

#update_cache(source = @url, destination = @local_path) ⇒ Object

Copies the file referenced in source to destination source can be a url or local file path destination must be a local path

Will copy file if the file does not exists OR if the the file exists but the signature has not been successfully verified.

Returns file size if cached succesfully, false otherwise.



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/jnlp/jnlp.rb', line 473

def update_cache(source=@url, destination=@local_path)
  unless destination
    raise ArgumentError, "Must specify destination directory when updatng resource", caller
  end
  file_exists = File.exists?(destination)
  if file_exists && @signature_verified == nil
    verify_signature
  end
  unless file_exists && @signature_verified
    FileUtils.mkdir_p(File.dirname(destination))
    jarfile = open(source)
    if jarfile.class == Tempfile
      FileUtils.cp(jarfile.path, destination)
    else
      File.open(destination, 'w') {|f| f.write jarfile.read }
    end
    verify_signature ? jarfile.size : false
  else        
    File.size(destination)
  end
end

#verify_signatureObject

Verifys signature of locallly cached resource

Returns boolean value indicating whether the signature of the cached local copy of the resource verified successfully

The value return is nil if no local cache has been created.

Example:

true || false || nil


506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/jnlp/jnlp.rb', line 506

def verify_signature
  if @local_path
    if RUBY_PLATFORM =~ /java/
      begin
        jarfile = java.util.jar.JarInputStream.new(FileInputStream.new(@local_path), true)
        @signature_verified = true
      rescue NativeException
        @signature_verified = false
      end
    else
      # Use IO.popen instead of system() to absorb
      # jarsigners messages to $stdout
      response = IO.popen("jarsigner -verify #{@local_path}"){|io| io.gets}
      @signature_verified = ($?.exitstatus == 0)
    end
  else
    nil
  end
end