Class: Trinidad::WebApp

Inherits:
Object
  • Object
show all
Defined in:
lib/trinidad/web_app.rb

Direct Known Subclasses

RackupWebApp, RailsWebApp, WarWebApp

Defined Under Namespace

Classes: Holder

Constant Summary collapse

DEFAULT_SERVLET_CLASS =

by default we resolve by it’s name

nil
DEFAULT_SERVLET_NAME =
'default'
JSP_SERVLET_CLASS =

by default we resolve by it’s name

nil
JSP_SERVLET_NAME =
'jsp'
RACK_SERVLET_CLASS =
'org.jruby.rack.RackServlet'
RACK_SERVLET_NAME =

in-case of a “custom” rack servlet class

'rack'
RACK_FILTER_CLASS =
'org.jruby.rack.RackFilter'
RACK_FILTER_NAME =
'rack'
@@defaults =
Trinidad::Configuration::DEFAULTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, default_config) ⇒ WebApp

Returns a new instance of WebApp.



16
17
18
19
20
# File 'lib/trinidad/web_app.rb', line 16

def initialize(config, default_config)
  @config, @default_config = config, default_config || {}
  complete_config!
  # NOTE: we should maybe @config.freeze here ?!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/trinidad/web_app.rb', line 8

def config
  @config
end

#default_configObject (readonly)

Returns the value of attribute default_config.



8
9
10
# File 'lib/trinidad/web_app.rb', line 8

def default_config
  @default_config
end

Class Method Details

.create(config, default_config = Trinidad.configuration) ⇒ Object



10
11
12
13
14
# File 'lib/trinidad/web_app.rb', line 10

def self.create(config, default_config = Trinidad.configuration)
  war?(config, default_config) ? WarWebApp.new(config, default_config) :
    rackup?(config, default_config) ? RackupWebApp.new(config, default_config) :
      RailsWebApp.new(config, default_config)
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
25
# File 'lib/trinidad/web_app.rb', line 22

def [](key)
  key = key.to_sym
  config.key?(key) ? config[key] : default_config[key]
end

#add_context_param(param_name, param_value) ⇒ Object



137
138
139
140
141
142
# File 'lib/trinidad/web_app.rb', line 137

def add_context_param(param_name, param_value)
  @context_params ||= {}
  if ! param_value.nil? && ! web_xml_context_param(param_name)
    @context_params[param_name] = param_value.to_s
  end
end

#aliasesObject

:public => { :aliases => … }



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/trinidad/web_app.rb', line 172

def aliases # :public => { :aliases => ... }
  return nil unless aliases = ( self[:aliases] || public_config[:aliases] )
  return aliases if aliases.is_a?(String)
  # "/aliasPath1=docBase1,/aliasPath2=docBase2"
  @aliases ||= aliases.map do |path, base|
    path = path.to_s
    if (root = '/') != path[0, 1]
      path = (root << path)
    end
    "#{path}=#{File.expand_path(base, root_dir)}"
  end.join(',')
end

#cache_max_sizeObject

The cache max size in kB



199
200
201
202
# File 'lib/trinidad/web_app.rb', line 199

def cache_max_size # :public => { :cache_max_size => ... }
  # ((BaseDirContext) resources).setCacheMaxSize
  self[:cache_max_size] || public_config[:cache_max_size]
end

#cache_object_max_sizeObject

The max size for a cached object in kB



205
206
207
208
# File 'lib/trinidad/web_app.rb', line 205

def cache_object_max_size # :public => { :cache_object_max_size => ... }
  # ((BaseDirContext) resources).setCacheObjectMaxSize
  self[:cache_object_max_size] || public_config[:cache_object_max_size]
end

#cache_ttlObject

Cache entry time-to-live in millis



211
212
213
214
# File 'lib/trinidad/web_app.rb', line 211

def cache_ttl # :public => { :cache_ttl => ... }
  # ((BaseDirContext) resources).setCacheTTL
  self[:cache_ttl] || public_config[:cache_ttl]
end

#caching_allowed?Boolean

:public => { :cached => … }

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/trinidad/web_app.rb', line 185

def caching_allowed? # :public => { :cached => ... }
  # ((BaseDirContext) resources).setCached(isCachingAllowed())
  return @caching_allowed unless @caching_allowed.nil?
  @caching_allowed = self[:caching_allowed]
  if @caching_allowed.nil?
    @caching_allowed = public_config[:cached]
    if @caching_allowed.nil?
      @caching_allowed = environment != 'development'
    end
  end
  @caching_allowed = !! @caching_allowed
end

#class_loaderObject



216
217
218
219
# File 'lib/trinidad/web_app.rb', line 216

def class_loader
  @class_loader ||= 
    org.jruby.util.JRubyClassLoader.new(JRuby.runtime.jruby_class_loader)
end

#class_loader!Object



221
222
223
# File 'lib/trinidad/web_app.rb', line 221

def class_loader!
  ( @class_loader = nil ) || class_loader
end

#context_nameObject



44
45
46
47
# File 'lib/trinidad/web_app.rb', line 44

def context_name
  name = self[:context_name] || self[:name]
  name ? name.to_s : name
end

#context_paramsObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/trinidad/web_app.rb', line 124

def context_params
  @context_params ||= {}
  add_context_param 'jruby.min.runtimes', jruby_min_runtimes
  add_context_param 'jruby.max.runtimes', jruby_max_runtimes
  add_context_param 'jruby.initial.runtimes', jruby_min_runtimes
  add_context_param 'jruby.runtime.acquire.timeout', jruby_runtime_acquire_timeout
  add_context_param 'jruby.compat.version', jruby_compat_version || RUBY_VERSION
  add_context_param 'public.root', File.join('/', public_root)
  @context_params
end

#context_pathObject



39
40
41
42
# File 'lib/trinidad/web_app.rb', line 39

def context_path
  path = self[:context_path] || self[:path]
  path ? path.to_s : path
end

#context_xmlObject



93
# File 'lib/trinidad/web_app.rb', line 93

def context_xml; self[:context_xml] || self[:default_context_xml]; end

#default_deployment_descriptorObject

Deprecated.


150
151
152
153
# File 'lib/trinidad/web_app.rb', line 150

def default_deployment_descriptor
  return nil if @default_deployment_descriptor == false
  @default_deployment_descriptor ||= expand_path(default_web_xml) || false
end

#default_servletObject

Returns a servlet config for the DefaultServlet. This servlet is setup for each and every Tomcat context and is named ‘default’ and mapped to ‘/’ we allow fine tunning of this servlet. Return values should be interpreted as follows :

true - do nothing leave the servlet as set-up (by default)
false - remove the set-up default (e.g. configured in web.xml)


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/trinidad/web_app.rb', line 248

def default_servlet
  return @default_servlet unless @default_servlet.nil?
  @default_servlet ||= begin
    if ! web_xml_servlet?(DEFAULT_SERVLET_CLASS, DEFAULT_SERVLET_NAME)
      default_servlet = self[:default_servlet]
      if default_servlet.is_a?(javax.servlet.Servlet)
        { :instance => default_servlet }
      elsif default_servlet == false
        false # forced by user to remove
      elsif default_servlet == true
        true # forced by user to leave as is
      else
        default_servlet = {} if default_servlet.nil?
        unless default_servlet.key?(:class)
          # we use a custom class by default to server /public assets :
          default_servlet[:class] = 'rb.trinidad.servlets.DefaultServlet'
        end
        default_servlet
      end
    else
      false # configured in web.xml thus remove the (default) "default"
    end
  end
end

#default_web_xmlObject



95
# File 'lib/trinidad/web_app.rb', line 95

def default_web_xml; self[:default_web_xml]; end

#define_lifecycleObject



227
228
229
# File 'lib/trinidad/web_app.rb', line 227

def define_lifecycle
  Trinidad::Lifecycle::WebApp::Default.new(self)
end

#deployment_descriptorObject



144
145
146
147
# File 'lib/trinidad/web_app.rb', line 144

def deployment_descriptor
  return nil if @deployment_descriptor == false
  @deployment_descriptor ||= expand_path(web_xml) || false
end

#doc_baseObject

NOTE: should be set to application root (base) directory thus JRuby-Rack correctly resolves relative paths for the context!



51
# File 'lib/trinidad/web_app.rb', line 51

def doc_base; self[:doc_base] || root_dir; end

#environmentObject

TODO check web.xml



73
# File 'lib/trinidad/web_app.rb', line 73

def environment; self[:environment] || @@defaults[:environment]; end

#extensionsObject



117
118
119
120
121
122
# File 'lib/trinidad/web_app.rb', line 117

def extensions
  @extensions ||= begin
    extensions = default_config[:extensions] || {}
    extensions.merge(config[:extensions] || {})
  end
end

#generate_class_loaderObject

Deprecated.

replaced with #class_loader!



225
# File 'lib/trinidad/web_app.rb', line 225

def generate_class_loader; class_loader!; end

#init_paramsObject

Deprecated.

replaced with #context_params



135
# File 'lib/trinidad/web_app.rb', line 135

def init_params; context_params; end

#java_classesObject



102
103
104
105
# File 'lib/trinidad/web_app.rb', line 102

def java_classes
  # accepts #deprecated :classes_dir syntax
  self[:java_classes] || self[:classes_dir] || File.join(java_lib, 'classes')
end

#java_classes_dirObject Also known as: classes_dir



112
113
114
# File 'lib/trinidad/web_app.rb', line 112

def java_classes_dir
  @java_classes_dir ||= self[:java_classes_dir] || expand_path(java_classes)
end

#java_libObject



97
98
99
100
# File 'lib/trinidad/web_app.rb', line 97

def java_lib
  # accepts #deprecated :libs_dir syntax
  self[:java_lib] || self[:libs_dir] || @@defaults[:java_lib]
end

#java_lib_dirObject Also known as: libs_dir



107
108
109
# File 'lib/trinidad/web_app.rb', line 107

def java_lib_dir
  @java_lib_dir ||= self[:java_lib_dir] || expand_path(java_lib)
end

#jruby_max_runtimesObject



61
62
63
64
65
66
67
# File 'lib/trinidad/web_app.rb', line 61

def jruby_max_runtimes
  if max = config[:jruby_max_runtimes]
    return max.to_i # max specified overrides :threadsafe
  else # but :threadsafe takes precendence over default :
    self[:threadsafe] ? 1 : default_config[:jruby_max_runtimes]
  end
end

#jruby_min_runtimesObject



53
54
55
56
57
58
59
# File 'lib/trinidad/web_app.rb', line 53

def jruby_min_runtimes
  if min = config[:jruby_min_runtimes]
    return min.to_i # min specified overrides :threadsafe
  else # but :threadsafe takes precendence over default :
    self[:threadsafe] ? 1 : default_config[:jruby_min_runtimes]
  end
end

#jruby_runtime_acquire_timeoutObject



69
70
71
# File 'lib/trinidad/web_app.rb', line 69

def jruby_runtime_acquire_timeout
  self[:jruby_runtime_acquire_timeout] || 5.0 # default 10s seems too high
end

#jsp_servletObject

Returns a servlet config for the JspServlet. This servlet is setup by default for every Tomcat context and is named ‘jsp’ with ‘*.jsp’ and ‘*.jspx’ mappings. Return values should be interpreted as follows :

true - do nothing leave the servlet as set-up (by default)
false - remove the set-up servlet (by default we do not need jsp support)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/trinidad/web_app.rb', line 282

def jsp_servlet
  return @jsp_servlet unless @jsp_servlet.nil?
  @jsp_servlet ||= begin
    if ! web_xml_servlet?(JSP_SERVLET_CLASS, JSP_SERVLET_NAME)
      jsp_servlet = self[:jsp_servlet]
      if jsp_servlet.is_a?(javax.servlet.Servlet)
        { :instance => jsp_servlet }
      else
        jsp_servlet || false # remove jsp support unless specified
      end
    else
      false # configured in web.xml thus remove the default "jsp"
    end
  end
end

#key?(key, use_default = true) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/trinidad/web_app.rb', line 27

def key?(key, use_default = true)
  key = key.to_sym
  return true if config.has_key?(key)
  use_default ? default_config.key?(key) : false
end

#log_dirObject

by a “Rails” convention defaults to ‘[RAILS_ROOT]/log’



85
86
87
# File 'lib/trinidad/web_app.rb', line 85

def log_dir
  @log_dir ||= self[:log_dir] || File.join(root_dir, 'log')
end

#monitorObject



89
90
91
# File 'lib/trinidad/web_app.rb', line 89

def monitor
  File.expand_path(self[:monitor] || 'restart.txt', work_dir)
end

#public_configObject

we do support nested :public configuration e.g. : public:

root: /assets
cache: true
cache_ttl: 60000


165
166
167
168
169
170
# File 'lib/trinidad/web_app.rb', line 165

def public_config
  @public_config ||= 
    self[:public].is_a?(String) ? 
      { :root => self[:public] } : 
        ( self[:public] || {} )
end

#public_dirObject



75
76
77
# File 'lib/trinidad/web_app.rb', line 75

def public_dir
  @public_dir ||= expand_path(public_root)
end

#public_rootObject Also known as: public



155
156
157
# File 'lib/trinidad/web_app.rb', line 155

def public_root
  @public_root ||= ( public_config[:root] || @@defaults[:public] )
end

#rack_listenerObject



340
341
342
# File 'lib/trinidad/web_app.rb', line 340

def rack_listener
  context_listener unless web_xml_listener?(context_listener)
end

#rack_servletObject

Returns a config for the RackServlet or nil if no need to set-up one. (to be used for dispatching to this Rack / Rails web application)



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/trinidad/web_app.rb', line 305

def rack_servlet
  return nil if @rack_servlet == false
  @rack_servlet ||= begin
    rack_servlet = self[:rack_servlet] || self[:servlet] || {}
    
    if rack_servlet.is_a?(javax.servlet.Servlet)
      { :instance => rack_servlet, :name => RACK_SERVLET_NAME, :mapping => '/*' }
    else
      servlet_class = rack_servlet[:class] || RACK_SERVLET_CLASS
      servlet_name = rack_servlet[:name] || RACK_SERVLET_NAME
      
      if ! web_xml_servlet?(servlet_class, servlet_name) &&
          ! web_xml_filter?(RACK_FILTER_CLASS, RACK_FILTER_NAME)
        {
          :instance => rack_servlet[:instance],
          :class => servlet_class, :name => servlet_name,
          :init_params => rack_servlet[:init_params],
          :async_supported => !! ( rack_servlet.has_key?(:async_supported) ?
              rack_servlet[:async_supported] : async_supported ),
          :load_on_startup => ( rack_servlet[:load_on_startup] || 2 ).to_i,
          :mapping => rack_servlet[:mapping] || '/*'
        }
      else
        if ! rack_servlet.empty?
          logger.info "ignoring :rack_servlet configuration for " +
                      "#{context_path} due #{deployment_descriptor}"
        end
        false # no need to setup a rack servlet
      end
    end
  end || nil
end

#reset!Object

Reset the hold web application state so it gets re-initialized. Please note that the configuration objects are not cleared.



233
234
235
236
237
# File 'lib/trinidad/web_app.rb', line 233

def reset!
  vars = instance_variables.map(&:to_sym) 
  vars = vars - [ :'@config', :'@default_config' ]
  vars.each { |var| instance_variable_set(var, nil) }
end

#servletObject

Deprecated.

use #rack_servlet instead



338
# File 'lib/trinidad/web_app.rb', line 338

def servlet; rack_servlet; end

#solo?Boolean

Returns:

  • (Boolean)


346
347
348
# File 'lib/trinidad/web_app.rb', line 346

def solo?
  ! is_a?(WarWebApp) && config[:solo]
end

#threadsafe?Boolean

Returns:

  • (Boolean)


350
351
352
# File 'lib/trinidad/web_app.rb', line 350

def threadsafe?
  jruby_min_runtimes == 1 && jruby_max_runtimes == 1
end

#war?Boolean

Returns:

  • (Boolean)


344
# File 'lib/trinidad/web_app.rb', line 344

def war?; self.class.war?(config); end

#web_app_dirObject

is getting deprecated soon



37
# File 'lib/trinidad/web_app.rb', line 37

alias_method :web_app_dir, :root_dir

#web_xmlObject



94
# File 'lib/trinidad/web_app.rb', line 94

def web_xml; self[:web_xml] || self[:default_web_xml]; end

#web_xml_context_param(name) ⇒ Object

Returns a param-value for a context-param with a given param-name.



408
409
410
411
412
413
# File 'lib/trinidad/web_app.rb', line 408

def web_xml_context_param(name)
  return nil unless web_xml_doc
  if param = web_xml_doc.root.elements["/web-app/context-param[param-name = '#{name}']"]
    param.elements['param-value'].text
  end
end

#web_xml_filter?(filter_class, filter_name = nil) ⇒ Boolean

Returns true if a filter definition with a given filter-class is found.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/trinidad/web_app.rb', line 386

def web_xml_filter?(filter_class, filter_name = nil)
  return nil unless web_xml_doc
  if filter_class
    filter_xpath = "/web-app/filter[filter-class = '#{filter_class}']"
    return true if web_xml_doc.root.elements[filter_xpath] # else try name
  end
  if filter_name
    filter_xpath = "/web-app/filter[filter-name = '#{filter_name}']"
    return !! web_xml_doc.root.elements[filter_xpath]
  end
  
  return false if filter_class || filter_name
  raise ArgumentError, "nor filter_class nor filter_name given"
end

#web_xml_listener?(listener_class) ⇒ Boolean

Returns true if a listener definition with a given listener-class is found.

Returns:

  • (Boolean)


402
403
404
405
# File 'lib/trinidad/web_app.rb', line 402

def web_xml_listener?(listener_class)
  return nil unless web_xml_doc
  !! web_xml_doc.root.elements["/web-app/listener[listener-class = '#{listener_class}']"]
end

#web_xml_servlet?(servlet_class, servlet_name = nil) ⇒ Boolean

Returns true if there’s a servlet with the given servlet-class name configured or if the optional name second argument is given it also checks for a servlet with the given name.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/trinidad/web_app.rb', line 370

def web_xml_servlet?(servlet_class, servlet_name = nil)
  return nil unless web_xml_doc
  if servlet_class
    servlet_xpath = "/web-app/servlet[servlet-class = '#{servlet_class}']"
    return true if web_xml_doc.root.elements[servlet_xpath] # else try name
  end
  if servlet_name
    servlet_xpath = "/web-app/servlet[servlet-name = '#{servlet_name}']"
    return !! web_xml_doc.root.elements[servlet_xpath]
  end
  
  return false if servlet_class || servlet_name
  raise ArgumentError, "nor servlet_class nor servlet_name given"
end

#work_dirObject

by (a “Rails”) convention use ‘[RAILS_ROOT]/tmp’



80
81
82
# File 'lib/trinidad/web_app.rb', line 80

def work_dir
  @work_dir ||= self[:work_dir] || File.join(root_dir, 'tmp')
end