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.



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/jnlp/jnlp.rb', line 438

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"


436
437
438
# File 'lib/jnlp/jnlp.rb', line 436

def certificate_version
  @certificate_version
end

#date_strObject (readonly)

Contains the date string of the resource

Example:

"20080318.093629"


411
412
413
# File 'lib/jnlp/jnlp.rb', line 411

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>


419
420
421
# File 'lib/jnlp/jnlp.rb', line 419

def date_time
  @date_time
end

#filenameObject (readonly)

Contains the suffix of the resource

Example:

"__V0.1.0.jar"


304
305
306
# File 'lib/jnlp/jnlp.rb', line 304

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"


312
313
314
# File 'lib/jnlp/jnlp.rb', line 312

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"


320
321
322
# File 'lib/jnlp/jnlp.rb', line 320

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.



272
273
274
# File 'lib/jnlp/jnlp.rb', line 272

def href
  @href
end

#href_pathObject (readonly)

Contains the path to the resource taken from the href attribute

Example:

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


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

def href_path
  @href_path
end

#kindObject (readonly)

Contains the kind of the resource

Example:

"jar" || "nativelib"


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

def kind
  @kind
end

#local_cache_dirObject

Contains the absolute local path of cache directory

Example:

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


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

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"


371
372
373
# File 'lib/jnlp/jnlp.rb', line 371

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"


387
388
389
# File 'lib/jnlp/jnlp.rb', line 387

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"


265
266
267
# File 'lib/jnlp/jnlp.rb', line 265

def main
  @main
end

#nameObject (readonly)

Contains the base name of the resource

Example:

"httpclient"


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

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:

""


257
258
259
# File 'lib/jnlp/jnlp.rb', line 257

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"


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

def relative_local_path
  @relative_local_path
end

#relative_local_path_pack_gzObject (readonly)

Contains the relative local path of the packed, gzipped resource

Example:

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


379
380
381
# File 'lib/jnlp/jnlp.rb', line 379

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



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

def resource
  @resource
end

#revisionObject (readonly)

Contains the revision of the resource

Example:

72


427
428
429
# File 'lib/jnlp/jnlp.rb', line 427

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


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

def signature_verified
  @signature_verified
end

#sizeObject (readonly)

Contains the size of the resource

Example:

294248


328
329
330
# File 'lib/jnlp/jnlp.rb', line 328

def size
  @size
end

#size_pack_gzObject (readonly)

Contains the size of the gzipped pack200 version of the resource

Example:

112213


336
337
338
# File 'lib/jnlp/jnlp.rb', line 336

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"


296
297
298
# File 'lib/jnlp/jnlp.rb', line 296

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"


280
281
282
# File 'lib/jnlp/jnlp.rb', line 280

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"


288
289
290
# File 'lib/jnlp/jnlp.rb', line 288

def url_pack_gz
  @url_pack_gz
end

#versionObject (readonly)

Contains the version of the resource

Example:

"0.1.0"


403
404
405
# File 'lib/jnlp/jnlp.rb', line 403

def version
  @version
end

#version_strObject (readonly)

Contains the version string of the resource

Example:

"0.1.0-20080318.093629-72"


395
396
397
# File 'lib/jnlp/jnlp.rb', line 395

def version_str
  @version_str
end

Instance Method Details

#cache_resource(dest_dir = @local_cache_dir, options = {}) ⇒ 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.



620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/jnlp/jnlp.rb', line 620

def cache_resource(dest_dir=@local_cache_dir, options={})
  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, options)
  if options[:include_pack_gz]
    @relative_local_path_pack_gz = "#{@relative_local_path}.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, options)
  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’



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/jnlp/jnlp.rb', line 496

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, options = {}) ⇒ 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.



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/jnlp/jnlp.rb', line 546

def update_cache(source=@url, destination=@local_path, options={})
  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))
    puts "reading:     #{source}" if options[:verbose]
    tried_to_read = 0
    begin
      jarfile = open(source)
    rescue OpenURI::HTTPError => e
      puts e
      if tried_to_read < 1
        tried_to_read += 1
        retry
      end
    end
    if jarfile.class == Tempfile
      FileUtils.cp(jarfile.path, destination)
      puts "copying to:  #{destination}" if options[:verbose]
    else
      File.open(destination, 'w') {|f| f.write jarfile.read }
      puts "writing to:  #{destination}" if options[:verbose]
    end
    puts "#{jarfile.size} bytes written" if options[:verbose]
    verify_signature ? jarfile.size : false
  else
    File.size(destination)
  end
end

#verify_signatureObject

Verifies 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


592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/jnlp/jnlp.rb', line 592

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