Class: Rubylet::WarTask

Inherits:
Rake::TaskLib
  • Object
show all
Extended by:
ParamAccessor
Includes:
CommonParams
Defined in:
lib/rubylet/war_task.rb

Instance Method Summary collapse

Methods included from ParamAccessor

param_accessor

Methods included from CommonParams

#common_params, #gem

Constructor Details

#initialize {|_self| ... } ⇒ WarTask

Returns a new instance of WarTask.

Yields:

  • (_self)

Yield Parameters:



231
232
233
234
# File 'lib/rubylet/war_task.rb', line 231

def initialize
  yield(self) if block_given?
  define
end

Instance Method Details

#copy_stream(input, output) ⇒ Object

Bug in JRuby prevents IO.copy_stream

See github.com/jruby/jruby/issues/437



377
378
379
380
381
382
# File 'lib/rubylet/war_task.rb', line 377

def copy_stream(input, output)
  buf = ''
  while input.read(4096, buf)
    output.write(buf)
  end
end

#external_jrubyObject



246
247
248
249
250
251
# File 'lib/rubylet/war_task.rb', line 246

def external_jruby
  listeners << proc do |w|
    w.listener! 'com.commongroundpublishing.slf4j.impl.ServletContextLoggerSCL'
    w.listener! 'com.commongroundpublishing.rubylet.ExternalJRubyLoader'
  end
end

#filter(&block) ⇒ Object



236
237
238
# File 'lib/rubylet/war_task.rb', line 236

def filter(&block)
  filters << block
end

#glassfish_descriptor(opts = {}) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rubylet/war_task.rb', line 302

def glassfish_descriptor(opts = {})
  builder = GlassfishDescriptorBuilder.new(opts)
  builder.glassfish_web_app { |w|
    w.alternate_doc_root!('/*', resource_base)
    resources.each do |res|
      w.resource_ref do |r|
        r.res_ref_name res[:name]
        r.jndi_name res[:jndi_name] || res[:name]
      end
    end
  }
end

#jetty_descriptor(opts = {}) ⇒ Object



315
316
317
318
319
320
# File 'lib/rubylet/war_task.rb', line 315

def jetty_descriptor(opts = {})
  builder = JettyDescriptorBuilder.new(opts)
  builder.configure! { |c|
    c.set!('resourceBase', resource_base)
  }
end

#mvn(*args) ⇒ Object

Runs maven if we are a JRuby process. Currently not used.

See watchitlater.com/blog/2011/08/jruby-rake-and-maven/



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/rubylet/war_task.rb', line 387

def mvn(*args)
  mvn = catch(:mvn) do
    ENV['PATH'].split(::File::PATH_SEPARATOR).each do |path|
      Dir.glob(::File.join(path, 'mvn')).each do |file|
        if ::File.executable?(file)
          throw :mvn, if ::File.symlink?(file)
                        ::File.readlink(file)
                      else
                        file
                      end
        end
      end
    end
  end

  raise 'No maven found on $PATH' unless mvn
  
  mvn_home = ::File.expand_path(::File.join(mvn, '..', '..'))
  m2_conf = ::File.expand_path(::File.join(mvn_home, 'bin', 'm2.conf'))

  java.lang.System.setProperty('maven.home', mvn_home)
  java.lang.System.setProperty('classworlds.conf', m2_conf)

  Dir[::File.join(mvn_home, 'boot', '*.jar')].each do |jar|
    require jar
  end
  launcher = Java::org.codehaus.plexus.classworlds.launcher.Launcher
  exit_code = launcher.mainWithExitCode(args.flatten.to_java(:string))
  raise "Maven exited #{exit_code}" unless exit_code == 0
end

#rubylet {|s| ... } ⇒ Object

Yields:

  • (s)


263
264
265
266
267
# File 'lib/rubylet/war_task.rb', line 263

def rubylet
  s = RubyletDescriptor.new(self)
  yield(s) if block_given?
  servlets << s
end

#rubylet_jarObject



322
323
324
325
326
327
328
329
# File 'lib/rubylet/war_task.rb', line 322

def rubylet_jar
  base = ::File.expand_path('../..', __FILE__)
  jar = Dir[::File.join(base, 'rubylet-ee-*.jar')].first
  unless jar
    raise 'cannot find rubylet-ee-VERSION.jar; was this gem built correctly?'
  end
  jar
end

#servlet(&block) ⇒ Object



259
260
261
# File 'lib/rubylet/war_task.rb', line 259

def servlet(&block)
  servlets << block
end

#servlet_context_loggerObject



240
241
242
243
244
# File 'lib/rubylet/war_task.rb', line 240

def servlet_context_logger
  listeners << proc do |w|
    w.listener! 'com.commongroundpublishing.slf4j.impl.ServletContextListenerSCL'
  end
end

#static_file_filter {|f| ... } ⇒ Object

Yields:

  • (f)


253
254
255
256
257
# File 'lib/rubylet/war_task.rb', line 253

def static_file_filter
  f = StaticFileFilter.new
  yield(f) if block_given?
  filters << f
end

#war(warfile) ⇒ Object



331
332
333
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
368
369
370
371
372
# File 'lib/rubylet/war_task.rb', line 331

def war(warfile)
  Zip::ZipFile.open(warfile, Zip::ZipFile::CREATE) do |z|
    z.dir.mkdir('WEB-INF')
    z.file.open('WEB-INF/web.xml', 'w') do |f|
      webapp_descriptor(:target => f)
    end
    z.file.open('WEB-INF/jetty-web.xml', 'w') do |f|
      jetty_descriptor(:target => f)
    end
    z.file.open('WEB-INF/glassfish-web.xml', 'w') do |f|
      glassfish_descriptor(:target => f)
    end

    begin
      # we are using ruby version number with "a" suffix as
      # equivalent to java/maven "-SNAPSHOT"
      ee_version = if VERSION.to_s =~ /(.*)a$/
                     "#{$1}-SNAPSHOT"
                   else
                     VERSION.to_s
                   end

      deps = MiniAether::Spec.new do
        group 'com.commongroundpublishing' do
          jar "rubylet-ee:#{ee_version}"
          jar 'slf4j-servletcontext:1.0.0'
        end
      end.resolve

      dir = 'WEB-INF/lib'
      z.dir.mkdir(dir)
      deps.each do |source|
        dest = "#{dir}/#{::File.basename(source)}"
        z.file.open(dest, 'w') do |output|
          ::File.open(source) { |input| copy_stream(input, output) }
        end
      end
    rescue => e
      raise "Error resolving dependencies: #{e}"
    end
  end
end

#webapp_descriptor(opts = {}) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rubylet/war_task.rb', line 269

def webapp_descriptor(opts = {})
  builder = WebappDescriptorBuilder.new(opts)
  builder.web_app!(java_servlets_version) { |w|
    w.display_name display_name()

    # If defined as context params, will apply to all Rubylet servlets
    common_params do |key, value|
      w.context_param! key, value
    end

    listeners.each do |block|
      block.call(w)
    end

    filters.each do |block|
      block.call(w)
    end

    servlets.each do |block|
      block.call(w)
    end

    resources.each do |res|
      w.resource_ref do |r|
        r.description(res[:description]) if res[:description]
        r.res_ref_name(res[:name])
        r.res_type(res[:type] || 'javax.sql.DataSource')
        r.res_auth(res[:auth] || 'Container')
      end
    end
  }
end