Class: Jnlp::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/jnlp/resource.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.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/jnlp/resource.rb', line 226

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"


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

def certificate_version
  @certificate_version
end

#date_strObject (readonly)

Contains the date string of the resource

Example:

"20080318.093629"


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

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>


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

def date_time
  @date_time
end

#filenameObject (readonly)

Contains the suffix of the resource

Example:

"__V0.1.0.jar"


92
93
94
# File 'lib/jnlp/resource.rb', line 92

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"


100
101
102
# File 'lib/jnlp/resource.rb', line 100

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"


108
109
110
# File 'lib/jnlp/resource.rb', line 108

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.



60
61
62
# File 'lib/jnlp/resource.rb', line 60

def href
  @href
end

#href_pathObject (readonly)

Contains the path to the resource taken from the href attribute

Example:

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


19
20
21
# File 'lib/jnlp/resource.rb', line 19

def href_path
  @href_path
end

#kindObject (readonly)

Contains the kind of the resource

Example:

"jar" || "nativelib"


27
28
29
# File 'lib/jnlp/resource.rb', line 27

def kind
  @kind
end

#local_cache_dirObject

Contains the absolute local path of cache directory

Example:

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


143
144
145
# File 'lib/jnlp/resource.rb', line 143

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"


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

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"


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

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"


53
54
55
# File 'lib/jnlp/resource.rb', line 53

def main
  @main
end

#nameObject (readonly)

Contains the base name of the resource

Example:

"httpclient"


35
36
37
# File 'lib/jnlp/resource.rb', line 35

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:

""


45
46
47
# File 'lib/jnlp/resource.rb', line 45

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"


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

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"


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

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



11
12
13
# File 'lib/jnlp/resource.rb', line 11

def resource
  @resource
end

#revisionObject (readonly)

Contains the revision of the resource

Example:

72


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

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


135
136
137
# File 'lib/jnlp/resource.rb', line 135

def signature_verified
  @signature_verified
end

#sizeObject (readonly)

Contains the size of the resource

Example:

294248


116
117
118
# File 'lib/jnlp/resource.rb', line 116

def size
  @size
end

#size_pack_gzObject (readonly)

Contains the size of the gzipped pack200 version of the resource

Example:

112213


124
125
126
# File 'lib/jnlp/resource.rb', line 124

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"


84
85
86
# File 'lib/jnlp/resource.rb', line 84

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"


68
69
70
# File 'lib/jnlp/resource.rb', line 68

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"


76
77
78
# File 'lib/jnlp/resource.rb', line 76

def url_pack_gz
  @url_pack_gz
end

#versionObject (readonly)

Contains the version of the resource

Example:

"0.1.0"


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

def version
  @version
end

#version_strObject (readonly)

Contains the version string of the resource

Example:

"0.1.0-20080318.093629-72"


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

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.



408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/jnlp/resource.rb', line 408

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’



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/jnlp/resource.rb', line 284

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.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/jnlp/resource.rb', line 334

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


380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/jnlp/resource.rb', line 380

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