Class: Trinidad::Extensions::ValveWebAppExtension

Inherits:
WebAppExtension
  • Object
show all
Defined in:
lib/trinidad_valve_extension.rb

Instance Method Summary collapse

Instance Method Details

#configure(tomcat, app_context) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/trinidad_valve_extension.rb', line 8

def configure(tomcat, app_context)
  logger = app_context.logger

  @options[:valves] ||= Array.new

  if not @options[:valves].empty?
    @options[:valves].each do |valve_properties|
      valve_properties = valve_properties.clone
      class_name = valve_properties.delete 'className'

      if not class_name
        logger.warn("Tomcat valve defined without a 'className' attribute.  Skipping valve definition: '#{valve_properties.inspect}'")
        next
      end

      begin
        valve = get_valve(class_name)
      rescue NameError => e
        logger.warn("Tomcat valve '#{class_name}' not found.  Ensure valve exists in your classpath")
        next
      end

      set_valve_properties(valve, valve_properties)

      # Add the valve to the context using the suggested getPipeline()
      app_context.pipeline.add_valve(valve)
    end
  end
end

#get_valve(valve_name) ⇒ Object



38
39
40
41
# File 'lib/trinidad_valve_extension.rb', line 38

def get_valve(valve_name)
  valve_class = Java::JavaClass.for_name valve_name
  valve_instance = valve_class.constructor.new_instance.to_java
end

#replace_properties(text) ⇒ Object



49
50
51
52
# File 'lib/trinidad_valve_extension.rb', line 49

def replace_properties(text)
  java_import 'org.apache.tomcat.util.IntrospectionUtils'
  IntrospectionUtils.replace_properties(text, java.lang.System.getProperties(), nil)
end

#set_valve_properties(valve_instance, properties) ⇒ Object



43
44
45
46
47
# File 'lib/trinidad_valve_extension.rb', line 43

def set_valve_properties(valve_instance, properties)
  properties.each do |option,value|
    valve_instance.send("#{option}=", replace_properties(value.to_s))
  end
end