jnlp 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ module Jnlp #:nodoc:
2
+ #
3
+ # Property objects encapsulate <property> elements present in a
4
+ # Java Web Start Jnlp <resources> element.
5
+ #
6
+ class Property
7
+ #
8
+ # Contains the Hpricot element parsed from the orginal Jnlp
9
+ # that was used to create the Property
10
+ #
11
+ attr_reader :property
12
+ #
13
+ # Contains the name of the Property
14
+ #
15
+ #
16
+ # Example:
17
+ #
18
+ # "maven.jnlp.version"
19
+ #
20
+ attr_reader :name
21
+ #
22
+ # Contains the value of the Property
23
+ #
24
+ #
25
+ # Example:
26
+ #
27
+ # "all-otrunk-snapshot-0.1.0-20080310.211515"
28
+ #
29
+ attr_reader :value
30
+ #
31
+ # Contains the value of the os attribute in the
32
+ # parent <resources> element that contains this property
33
+ # if the attribute was set in the parent.
34
+ # Example:
35
+ #
36
+ # "Mac OS X"
37
+ #
38
+ attr_reader :os
39
+ #
40
+ # Creates a new Jnlp::Property object.
41
+ # * _prop_: the Hpricot parsing of the specific jnlp/resources/property element
42
+ # * _os_: optional: include it if the resources parent element that contains the property has this attribute set
43
+ #
44
+ def initialize(prop, os=nil)
45
+ @property = prop
46
+ @name = prop['name']
47
+ @value = prop['value']
48
+ @os = os
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,422 @@
1
+ module Jnlp
2
+ #
3
+ # Jnlp::Resource objects encapsulate both jar and nativelib elements present in a
4
+ # Java Web Start Jnlp <resources> element.
5
+ #
6
+ class Resource
7
+ #
8
+ # Contains the Hpricot element parsed from the orginal Jnlp
9
+ # that was used to create the resource
10
+ #
11
+ attr_reader :resource
12
+ #
13
+ # Contains the path to the resource taken from the href attribute
14
+ #
15
+ # Example:
16
+ #
17
+ # "org/concord/httpclient/" || ""
18
+ #
19
+ attr_reader :href_path
20
+ #
21
+ # Contains the kind of the resource
22
+ #
23
+ # Example:
24
+ #
25
+ # "jar" || "nativelib"
26
+ #
27
+ attr_reader :kind
28
+ #
29
+ # Contains the base name of the resource
30
+ #
31
+ # Example:
32
+ #
33
+ # "httpclient"
34
+ #
35
+ attr_reader :name
36
+ #
37
+ # Contains the Java Web Start specification of the OS for
38
+ # the <resources> parent of this resource if the attribute
39
+ # value exists
40
+ #
41
+ # Example:
42
+ #
43
+ # ""
44
+ #
45
+ attr_reader :os
46
+ #
47
+ # Contains the href attribute of the resource
48
+ #
49
+ # Example:
50
+ #
51
+ # "net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy.jar"
52
+ #
53
+ attr_reader :main
54
+ #
55
+ # Contains a boolean that repesents whether the main_class for this
56
+ # jnlp is contained within this jar.
57
+ # This attribute is optional in a jnlp and if present should
58
+ # only be present and set to true on one jar resource in a jnlp.
59
+ #
60
+ attr_reader :href
61
+ #
62
+ # Contains the url reference to the resource
63
+ #
64
+ # Example:
65
+ #
66
+ # "http://jnlp.concord.org/dev/org/concord/httpclient/httpclient.jar?version-id=0.1.0-20071212.220020-17"
67
+ #
68
+ attr_reader :url
69
+ #
70
+ # Contains the url reference to the gzipped pack200 version of the resource
71
+ #
72
+ # Example:
73
+ #
74
+ # "http://jnlp.concord.org/dev/org/concord/httpclient/httpclient__V0.1.0-20071212.220020-17.jar.pack.gz"
75
+ #
76
+ attr_reader :url_pack_gz
77
+ #
78
+ # Contains the filename of the resource
79
+ #
80
+ # Example:
81
+ #
82
+ # "httpclient__V0.1.0-20071212.220020-17.jar"
83
+ #
84
+ attr_reader :suffix
85
+ #
86
+ # Contains the suffix of the resource
87
+ #
88
+ # Example:
89
+ #
90
+ # "__V0.1.0.jar"
91
+ #
92
+ attr_reader :filename
93
+ #
94
+ # Contains the filename of the gzipped pack200 version of the resource
95
+ #
96
+ # Example:
97
+ #
98
+ # "httpclient__V0.1.0-20071212.220020-17.jar.pack.gz"
99
+ #
100
+ attr_reader :filename_pack
101
+ #
102
+ # Contains the filename of the pack200 version of the resource
103
+ #
104
+ # Example:
105
+ #
106
+ # "httpclient__V0.1.0-20071212.220020-17.jar.pack"
107
+ #
108
+ attr_reader :filename_pack_gz
109
+ #
110
+ # Contains the size of the resource
111
+ #
112
+ # Example:
113
+ #
114
+ # 294248
115
+ #
116
+ attr_reader :size
117
+ #
118
+ # Contains the size of the gzipped pack200 version of the resource
119
+ #
120
+ # Example:
121
+ #
122
+ # 112213
123
+ #
124
+ attr_reader :size_pack_gz
125
+ #
126
+ # Contains boolean value indicating whether the signature of the
127
+ # cached local copy of the resource verified successfully
128
+ #
129
+ # The value is nil if no local cache has been created.
130
+ #
131
+ # Example:
132
+ #
133
+ # true || false || nil
134
+ #
135
+ attr_reader :signature_verified
136
+ #
137
+ # Contains the absolute local path of cache directory
138
+ #
139
+ # Example:
140
+ #
141
+ # "/Users/stephen/dev/jetty-jnlp-proxy/cache"
142
+ #
143
+ attr_reader :local_cache_dir
144
+ #
145
+ # Contains the relative local path of the resource
146
+ #
147
+ # Example:
148
+ #
149
+ # "net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar"
150
+ #
151
+ attr_reader :relative_local_path
152
+ #
153
+ # Contains the absolute local path of the resource
154
+ #
155
+ # Example:
156
+ #
157
+ # "/Users/stephen/dev/jetty-jnlp-proxy/cache/net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar"
158
+ #
159
+ attr_reader :local_path
160
+ #
161
+ # Contains the relative local path of the packed, gzipped resource
162
+ #
163
+ # Example:
164
+ #
165
+ # "net/sf/sail/webstart-proxy/jetty-proxy/jetty-proxy__V0.1.0-20080318.093629-72.jar.pack.gz"
166
+ #
167
+ attr_reader :relative_local_path_pack_gz
168
+ #
169
+ # Contains the absolute local path of the resource
170
+ #
171
+ # Example:
172
+ #
173
+ # "/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"
174
+ #
175
+ attr_reader :local_path_pack_gz
176
+ #
177
+ # Contains the version string of the resource
178
+ #
179
+ # Example:
180
+ #
181
+ # "0.1.0-20080318.093629-72"
182
+ #
183
+ attr_reader :version_str
184
+ #
185
+ # Contains the version of the resource
186
+ #
187
+ # Example:
188
+ #
189
+ # "0.1.0"
190
+ #
191
+ attr_reader :version
192
+ #
193
+ # Contains the date string of the resource
194
+ #
195
+ # Example:
196
+ #
197
+ # "20080318.093629"
198
+ #
199
+ attr_reader :date_str
200
+ #
201
+ # Contains a Ruby DateTime object representation of the resource's date string
202
+ #
203
+ # Example:
204
+ #
205
+ # #<DateTime: 85338394589/86400,0,2299161>
206
+ #
207
+ attr_reader :date_time
208
+ #
209
+ # Contains the revision of the resource
210
+ #
211
+ # Example:
212
+ #
213
+ # 72
214
+ #
215
+ attr_reader :revision
216
+ #
217
+ # Contains the certificate version of the resource
218
+ # if one exists, otherwize it is nil
219
+ #
220
+ # Example:
221
+ #
222
+ # "s1"
223
+ #
224
+ attr_reader :certificate_version
225
+ #
226
+ def initialize(res, codebase, os)
227
+ @resource = res
228
+ @kind = res.name
229
+ @main = res['main'] && res['main'] == 'true'
230
+ @href = res['href']
231
+ @href_path = File.dirname(@href)
232
+ if @href_path == '.'
233
+ @href_path = ''
234
+ else
235
+ @href_path = @href_path + '/'
236
+ end
237
+ @name = File.basename(@href).chomp('.jar')
238
+ @version_str = res['version']
239
+ if @version_str
240
+ @suffix = "__V#{@version_str}.jar"
241
+ else
242
+ @suffix = ".jar"
243
+ end
244
+ @filename = "#{@name}#{@suffix}"
245
+ @filename_pack = @filename + ".pack"
246
+ @filename_pack_gz = @filename_pack + ".gz"
247
+
248
+ @url = "#{codebase}/#{@href_path}#{@name}.jar"
249
+ @url << "?version-id=#{@version_str}" if @version_str
250
+ # example: data-util__V0.1.0-20070926.155656-107.jar.pack
251
+ # @url_pack = "#{codebase}/#{@href_path}#{@filename}.pack"
252
+ # example: data-util__V0.1.0-20070926.155656-107.jar.pack.gz
253
+ @url_pack_gz = "#{codebase}/#{@href_path}#{@filename}.pack.gz"
254
+ @version, @revision, @date_str, @date_time, @certificate_version = parse_version_str(@version_str)
255
+ @os = os
256
+ end
257
+ #
258
+ # parse_version_str
259
+ #
260
+ # input examples:
261
+ #
262
+ # "0.1.0-20070926.155656-107"
263
+ #
264
+ # or a newer example:
265
+ #
266
+ # "0.1.0-20090618.143130-890-s1"
267
+ #
268
+ # but ... some version strings just look like this:
269
+ #
270
+ # "0.1.0"
271
+ #
272
+ # or this:
273
+ #
274
+ # "2.1.7-r2"
275
+ #
276
+ # results:
277
+ #
278
+ # version # => '0.1.0'
279
+ # revision # => 20
280
+ # date_str # => '20070926.155656'
281
+ # date_time # => #<DateTime: 10673317777/10800,0,2299161>
282
+ # certificate_version # => 's1'
283
+ #
284
+ def parse_version_str(version_str)
285
+ version = date_str = certificate_version = ''
286
+ revision = date_time = nil
287
+ if version_str && version_str.length > 0
288
+ if md = version_str.match(/(\d|\.)+/)
289
+ version = md[0]
290
+ # date_str
291
+ if md2 = md.post_match.match(/-([\d]{8}.[\d]{6})(-|$)/)
292
+ date_str = md2[1]
293
+ d, t = date_str.scan(/\d+/) # => ["20070926", "155656"]
294
+ d1 = "#{d[0..3]}-#{d[4..5]}-#{d[6..7]}" # => "2007-09-26"
295
+ t1 = "#{t[0..1]}:#{t[2..3]}:#{t[4..5]}" # => "15:56:56"
296
+ dt = "#{d1}T#{t1}Z" # => "2007-09-26T15:56:56Z"
297
+ date_time = DateTime.parse(dt) # => #<DateTime: 10673317777/10800,0,2299161>
298
+ # revision
299
+ if md3 = md2.post_match.match(/\d+/)
300
+ revision = md3[0].to_i
301
+ end
302
+ else
303
+ if match = md.post_match[/\d+/]
304
+ revision = match.to_i
305
+ end
306
+ end
307
+ # certificate_version
308
+ if match = md.post_match[/-(s\d+)/, 1]
309
+ certificate_version = match
310
+ end
311
+ end
312
+ end
313
+ [version, revision, date_str, date_time, certificate_version]
314
+ end
315
+ #
316
+ # Set's up the local cache directory references
317
+ #
318
+ def local_cache_dir=(dir_path)
319
+ @local_cache_dir = File.expand_path(dir_path)
320
+ @relative_local_path = "#{@href_path}#{@filename}"
321
+ @local_path = "#{@local_cache_dir}/#{@relative_local_path}"
322
+ end
323
+ #
324
+ # Copies the file referenced in _source_ to _destination_
325
+ # _source_ can be a url or local file path
326
+ # _destination_ must be a local path
327
+ #
328
+ # Will copy file if the file does not exists
329
+ # OR if the the file exists but the signature has
330
+ # not been successfully verified.
331
+ #
332
+ # Returns file size if cached succesfully, false otherwise.
333
+ #
334
+ def update_cache(source=@url, destination=@local_path, options={})
335
+ unless destination
336
+ raise ArgumentError, "Must specify destination directory when updatng resource", caller
337
+ end
338
+ file_exists = File.exists?(destination)
339
+ if file_exists && @signature_verified == nil
340
+ verify_signature
341
+ end
342
+ unless file_exists && @signature_verified
343
+ FileUtils.mkdir_p(File.dirname(destination))
344
+ puts "reading: #{source}" if options[:verbose]
345
+ tried_to_read = 0
346
+ begin
347
+ jarfile = open(source)
348
+ rescue OpenURI::HTTPError => e
349
+ puts e
350
+ if tried_to_read < 1
351
+ tried_to_read += 1
352
+ retry
353
+ end
354
+ end
355
+ if jarfile.class == Tempfile
356
+ FileUtils.cp(jarfile.path, destination)
357
+ puts "copying to: #{destination}" if options[:verbose]
358
+ else
359
+ File.open(destination, 'w') {|f| f.write jarfile.read }
360
+ puts "writing to: #{destination}" if options[:verbose]
361
+ end
362
+ puts "#{jarfile.size} bytes written" if options[:verbose]
363
+ verify_signature ? jarfile.size : false
364
+ else
365
+ File.size(destination)
366
+ end
367
+ end
368
+ #
369
+ # Verifies signature of locallly cached resource
370
+ #
371
+ # Returns boolean value indicating whether the signature of the
372
+ # cached local copy of the resource verified successfully
373
+ #
374
+ # The value return is nil if no local cache has been created.
375
+ #
376
+ # Example:
377
+ #
378
+ # true || false || nil
379
+ #
380
+ def verify_signature
381
+ if @local_path
382
+ if RUBY_PLATFORM =~ /java/
383
+ begin
384
+ jarfile = java.util.jar.JarInputStream.new(FileInputStream.new(@local_path), true)
385
+ @signature_verified = true
386
+ rescue NativeException
387
+ @signature_verified = false
388
+ end
389
+ else
390
+ # Use IO.popen instead of system() to absorb
391
+ # jarsigners messages to $stdout
392
+ response = IO.popen("jarsigner -verify #{@local_path}"){|io| io.gets}
393
+ @signature_verified = ($?.exitstatus == 0)
394
+ end
395
+ else
396
+ nil
397
+ end
398
+ end
399
+ #
400
+ # Copies the resource referenced in Resource#url
401
+ # to the local cache.
402
+ #
403
+ # If the resource is successully cached locally and the
404
+ # signature is verified the size of the resource is retured.
405
+ #
406
+ # If the signature is not verified then false is returned.
407
+ #
408
+ def cache_resource(dest_dir=@local_cache_dir, options={})
409
+ unless dest_dir
410
+ raise ArgumentError, "Must specify destination directory when creating resource", caller
411
+ end
412
+ self.local_cache_dir=dest_dir
413
+ @size = update_cache(@url, @local_path, options)
414
+ if options[:include_pack_gz]
415
+ @relative_local_path_pack_gz = "#{@relative_local_path}.pack.gz"
416
+ @local_path_pack_gz = "#{dest_dir}/#{@relative_local_path_pack_gz}"
417
+ @size_pack_gz = update_cache(@url_pack_gz, @local_path_pack_gz, options)
418
+ end
419
+ @signature_verified ? @size : @signature_verified
420
+ end
421
+ end
422
+ end