| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/ruby1.8 -w |
| 2 # |
| 3 # Copyright:: Copyright 2009 Google Inc. |
| 4 # Original Author:: Ryan Brown (mailto:ribrdb@google.com) |
| 5 # |
| 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 # you may not use this file except in compliance with the License. |
| 8 # You may obtain a copy of the License at |
| 9 # |
| 10 # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 # |
| 12 # Unless required by applicable law or agreed to in writing, software |
| 13 # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 # See the License for the specific language governing permissions and |
| 16 # limitations under the License. |
| 17 |
| 18 require 'rubygems' |
| 19 require 'rack' |
| 20 require 'rexml/document' |
| 21 |
| 22 class WebXmlBuilder < Rack::Builder |
| 23 DUMMY_APP = Proc.new{|env|} |
| 24 |
| 25 def initialize(&block) |
| 26 @path = "/" |
| 27 @paths = Hash.new {|h, k| h[k] = []} |
| 28 instance_eval(&block) if block_given? |
| 29 end |
| 30 |
| 31 def use(middleware, *args, &block) |
| 32 if middleware.respond_to? :to_xml |
| 33 @paths[@path] << [middleware, args, block] |
| 34 else |
| 35 @paths[@path] << [middleware.new(DUMMY_APP, *args, &block)] |
| 36 end |
| 37 end |
| 38 |
| 39 def map(path, &block) |
| 40 if path !~ %r{^https?://} # we can only natively support path matching |
| 41 saved_path = @path |
| 42 @path = [@path, path].join('/').squeeze('/') |
| 43 begin |
| 44 instance_eval(&block) if block_given? |
| 45 ensure |
| 46 @path = saved_path |
| 47 end |
| 48 end |
| 49 end |
| 50 |
| 51 def run(app) |
| 52 @paths[@path] << [app, [], nil] |
| 53 end |
| 54 |
| 55 def to_xml |
| 56 xml = REXML::Document.new.add_element('web-app') |
| 57 xml.add_attribute("xmlns", "http://java.sun.com/xml/ns/javaee") |
| 58 xml.add_attribute("version", "2.5") |
| 59 each_path do |path, objects| |
| 60 pattern = path + "*" |
| 61 objects.each do |object, args, block| |
| 62 if object.respond_to? :to_xml |
| 63 object.to_xml(pattern, xml, *args, &block) |
| 64 end |
| 65 end |
| 66 end |
| 67 xml |
| 68 end |
| 69 |
| 70 private |
| 71 |
| 72 def each_path |
| 73 @paths.sort {|a, b| b[0].length - a[0].length}.each do |path, value| |
| 74 yield path, value |
| 75 end |
| 76 end |
| 77 end |
| 78 |
| 79 class JavaServlet |
| 80 def initialize(klass, name=nil) |
| 81 @klass = klass.to_s |
| 82 @name = (name || klass).to_s |
| 83 end |
| 84 |
| 85 def call(env) |
| 86 raise RuntimeError, "JavaServlet should be dispatched by web.xml" |
| 87 end |
| 88 |
| 89 def to_xml(pattern, xml) |
| 90 servlet = xml.add_element('servlet') |
| 91 servlet.add_element('servlet-name').add_text(@name) |
| 92 servlet.add_element('servlet-class').add_text(@klass) |
| 93 map = xml.add_elemtn('servlet-mapping') |
| 94 map.add_element('servlet-name').add_text(@name) |
| 95 map.add_element('url-pattern').add_text(pattern.to_s) |
| 96 end |
| 97 end |
| 98 |
| 99 class JavaServletFilter |
| 100 def self.to_xml(pattern, xml, klass, name=nil) |
| 101 name ||= klass |
| 102 filter = xml.add_element('filter') |
| 103 filter.add_element('filter-name').add_text(name.to_s) |
| 104 filter.add_element('filter-class').add_text(klass.to_s) |
| 105 map = xml.add_element('filter-mapping') |
| 106 map.add_element('filter-name').add_text(name.to_s) |
| 107 map.add_element('url-pattern').add_text(pattern.to_s) |
| 108 end |
| 109 |
| 110 def self.new(app, *args, &block) |
| 111 app |
| 112 end |
| 113 end |
| 114 |
| 115 class JavaContextListener |
| 116 def self.to_xml(pattern, xml, klass) |
| 117 listener = xml.add_element('listener') |
| 118 listener.add_element('listener-class').add_text(klass.to_s) |
| 119 end |
| 120 |
| 121 def self.new(app, *args, &block) |
| 122 app |
| 123 end |
| 124 end |
| 125 |
| 126 class JavaContextParams |
| 127 def self.to_xml(pattern, xml, params) |
| 128 params.each do |name, value| |
| 129 e = xml.add_element('context-param') |
| 130 e.add_element('param-name').add_text(name.to_s) |
| 131 e.add_element('param-value').add_text(value.to_s) |
| 132 end |
| 133 end |
| 134 |
| 135 def self.new(app, *args, &block) |
| 136 app |
| 137 end |
| 138 end |
| OLD | NEW |