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:



222
223
224
225
# File 'lib/rubylet/war_task.rb', line 222

def initialize
  yield(self) if block_given?
  define
end

Instance Method Details

#external_jrubyObject



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

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

#filter(&block) ⇒ Object



227
228
229
# File 'lib/rubylet/war_task.rb', line 227

def filter(&block)
  filters << block
end

#glassfish_descriptor(opts = {}) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rubylet/war_task.rb', line 286

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



299
300
301
302
303
304
# File 'lib/rubylet/war_task.rb', line 299

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/



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
373
# File 'lib/rubylet/war_task.rb', line 344

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)


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

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

#rubylet_jarObject



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

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



243
244
245
# File 'lib/rubylet/war_task.rb', line 243

def servlet(&block)
  servlets << block
end

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

Yields:

  • (f)


237
238
239
240
241
# File 'lib/rubylet/war_task.rb', line 237

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

#war(warfile) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rubylet/war_task.rb', line 315

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

    container = rubylet_jar
    z.dir.mkdir('WEB-INF/lib')
    z.file.open("WEB-INF/lib/#{::File.basename(container)}", 'w') do |f|
      ::File.open(container) do |input|
        buf = ''
        while input.read(4096, buf)
          f.write(buf)
        end
      end
    end
  end
end

#webapp_descriptor(opts = {}) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rubylet/war_task.rb', line 253

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