trinidad_lifecycle_extension 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README +67 -0
- data/Rakefile +147 -0
- data/lib/trinidad_lifecycle_extension.rb +46 -0
- data/spec/fixtures/fake_lifecycle_listener.rb +24 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/trinidad_lifecycle_extension_spec.rb +32 -0
- data/trinidad_lifecycle_extension.gemspec +69 -0
- metadata +76 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== Trinidad
|
2
|
+
|
3
|
+
Copyright (c) 2010 David Calavera
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
Trinidad lifecycle extension
|
2
|
+
============================
|
3
|
+
|
4
|
+
This extension allows you to add lifecycle listeners written in ruby to the
|
5
|
+
Trinidad's server context as well as each application context that runs on top
|
6
|
+
of Trinidad.
|
7
|
+
|
8
|
+
|
9
|
+
[WARNING] This extension is completely experimental and has not been tested nor
|
10
|
+
published yet.
|
11
|
+
|
12
|
+
|
13
|
+
Configuration
|
14
|
+
=============
|
15
|
+
|
16
|
+
Add this extension into the Trinidad's configuration file and include the path
|
17
|
+
to the directory where the listeners are. For instance:
|
18
|
+
|
19
|
+
extensions:
|
20
|
+
lifecycle:
|
21
|
+
path: 'lib/lifecycle'
|
22
|
+
|
23
|
+
Trinidad will try to load each class into that directory and add it to the
|
24
|
+
approrpiated context regarding where the extension will be configured, into the
|
25
|
+
server section or into the web_app section.
|
26
|
+
|
27
|
+
|
28
|
+
How to write a lifecycle listener
|
29
|
+
=================================
|
30
|
+
|
31
|
+
If the listener is for the server the class must be into the module
|
32
|
+
Trinidad::Lyfecycle::Server but if it's for the web application it must be into
|
33
|
+
the module Trinidad::Lyfecycle::WebApp. The class must include the java class
|
34
|
+
LifecycleListener and must contain the method `lifecycleEvent(event)`. For
|
35
|
+
example:
|
36
|
+
|
37
|
+
module Trinidad
|
38
|
+
module Lifecycle
|
39
|
+
module WebApp
|
40
|
+
import org.apache.catalina.LifecycleListener
|
41
|
+
import org.apache.catalina.Lifecycle
|
42
|
+
|
43
|
+
class WebAppListener
|
44
|
+
include LifecycleListener
|
45
|
+
|
46
|
+
def lifecycleEvent(event)
|
47
|
+
if Lifecycle::BEFORE_START_EVENT == event.type
|
48
|
+
# do something before start the web application context
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
As a reference, Trinidad is configured with a lifecycle listener, here is the
|
57
|
+
code:
|
58
|
+
|
59
|
+
http://github.com/calavera/trinidad/blob/master/lib/trinidad/web_app_lifecycle_listener.rb
|
60
|
+
|
61
|
+
You should also be familiar with the Tomcat's life cycle:
|
62
|
+
|
63
|
+
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/Lifecycle.java?view=markup
|
64
|
+
|
65
|
+
== Copyright
|
66
|
+
|
67
|
+
Copyright (c) 2010 David Calavera<calavera@apache.org>. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
task :default => :spec
|
47
|
+
|
48
|
+
require 'spec/rake/spectask'
|
49
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
50
|
+
spec.libs << 'lib' << 'spec'
|
51
|
+
spec.spec_opts = ['--options', 'spec/spec.opts']
|
52
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
desc "Generate RCov test coverage and open in your browser"
|
57
|
+
task :coverage do
|
58
|
+
require 'rcov'
|
59
|
+
sh "rm -fr coverage"
|
60
|
+
sh "rcov test/test_*.rb"
|
61
|
+
sh "open coverage/index.html"
|
62
|
+
end
|
63
|
+
|
64
|
+
require 'rake/rdoctask'
|
65
|
+
Rake::RDocTask.new do |rdoc|
|
66
|
+
rdoc.rdoc_dir = 'rdoc'
|
67
|
+
rdoc.title = "#{name} #{version}"
|
68
|
+
rdoc.rdoc_files.include('README*')
|
69
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
70
|
+
end
|
71
|
+
|
72
|
+
desc "Open an irb session preloaded with this library"
|
73
|
+
task :console do
|
74
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
75
|
+
end
|
76
|
+
|
77
|
+
#############################################################################
|
78
|
+
#
|
79
|
+
# Custom tasks (add your own tasks here)
|
80
|
+
#
|
81
|
+
#############################################################################
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
#############################################################################
|
86
|
+
#
|
87
|
+
# Packaging tasks
|
88
|
+
#
|
89
|
+
#############################################################################
|
90
|
+
|
91
|
+
task :release do
|
92
|
+
unless `git branch` =~ /^\* master$/
|
93
|
+
puts "You must be on the master branch to release!"
|
94
|
+
exit!
|
95
|
+
end
|
96
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
97
|
+
sh "git tag v#{version}"
|
98
|
+
sh "git push origin master"
|
99
|
+
sh "git push --tags"
|
100
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
101
|
+
end
|
102
|
+
|
103
|
+
task :build => :gemspec do
|
104
|
+
sh "mkdir -p pkg"
|
105
|
+
sh "gem build #{gemspec_file}"
|
106
|
+
sh "mv #{gem_file} pkg"
|
107
|
+
end
|
108
|
+
|
109
|
+
task :gemspec => :validate do
|
110
|
+
# read spec file and split out manifest section
|
111
|
+
spec = File.read(gemspec_file)
|
112
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
113
|
+
|
114
|
+
# replace name version and date
|
115
|
+
replace_header(head, :name)
|
116
|
+
replace_header(head, :version)
|
117
|
+
replace_header(head, :date)
|
118
|
+
#comment this out if your rubyforge_project has a different name
|
119
|
+
replace_header(head, :rubyforge_project)
|
120
|
+
|
121
|
+
# determine file list from git ls-files
|
122
|
+
files = `git ls-files`.
|
123
|
+
split("\n").
|
124
|
+
sort.
|
125
|
+
reject { |file| file =~ /^\./ }.
|
126
|
+
reject { |file| file =~ /^(rdoc|pkg|src)/ }.
|
127
|
+
map { |file| " #{file}" }.
|
128
|
+
join("\n")
|
129
|
+
|
130
|
+
# piece file back together and write
|
131
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
132
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
133
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
134
|
+
puts "Updated #{gemspec_file}"
|
135
|
+
end
|
136
|
+
|
137
|
+
task :validate do
|
138
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
139
|
+
unless libfiles.empty?
|
140
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
141
|
+
exit!
|
142
|
+
end
|
143
|
+
unless Dir['VERSION*'].empty?
|
144
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
145
|
+
exit!
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'trinidad_jars'
|
3
|
+
require 'trinidad/extensions'
|
4
|
+
|
5
|
+
module Trinidad
|
6
|
+
module Extensions
|
7
|
+
module Lifecycle
|
8
|
+
VERSION = '0.1.0'
|
9
|
+
|
10
|
+
def init_listeners(context, path, mod_name)
|
11
|
+
Dir.glob("#{path}/*.rb").each do |listener|
|
12
|
+
load listener
|
13
|
+
end
|
14
|
+
|
15
|
+
mod = constantize(mod_name)
|
16
|
+
|
17
|
+
mod.constants.each do |listener|
|
18
|
+
const_listener = mod.const_get(listener)
|
19
|
+
context.add_lifecycle_listener(const_listener.new)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def constantize(mod)
|
24
|
+
names = mod.split('::')
|
25
|
+
names.inject(Object) {|constant, obj| constant.const_get(obj) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
class LifecycleServerExtension < ServerExtension
|
31
|
+
include Lifecycle
|
32
|
+
|
33
|
+
def configure(tomcat)
|
34
|
+
init_listeners(tomcat.server, @options[:path], 'Trinidad::Lifecycle::Server')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class LifecycleWebAppExtension < WebAppExtension
|
39
|
+
include Lifecycle
|
40
|
+
|
41
|
+
def configure(tomcat, app_context)
|
42
|
+
init_listeners(app_context, @options[:path], 'Trinidad::Lifecycle::WebApp')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Trinidad
|
2
|
+
module Lifecycle
|
3
|
+
import org.apache.catalina.LifecycleListener
|
4
|
+
import org.apache.catalina.Lifecycle
|
5
|
+
|
6
|
+
module Server
|
7
|
+
class FakeServerListener
|
8
|
+
include LifecycleListener
|
9
|
+
|
10
|
+
def lifecycleEvent(event)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module WebApp
|
16
|
+
class FakeWebAppListener
|
17
|
+
include LifecycleListener
|
18
|
+
|
19
|
+
def lifecycleEvent(event)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'trinidad_lifecycle_extension'
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'trinidad'
|
8
|
+
require 'spec'
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
config.mock_with :mocha
|
12
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe 'Trinidad lifecycle extension' do
|
4
|
+
before(:all) do
|
5
|
+
@options = { :path => File.expand_path('../fixtures', __FILE__) }
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@context = Trinidad::Tomcat::StandardContext.new
|
10
|
+
@tomcat = mock
|
11
|
+
@tomcat.stubs(:server).returns(@context)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when it's a server extension" do
|
15
|
+
subject { Trinidad::Extensions::LifecycleServerExtension.new(@options) }
|
16
|
+
|
17
|
+
it "adds the listener to the tomcat's server context" do
|
18
|
+
subject.configure(@tomcat)
|
19
|
+
@tomcat.server.findLifecycleListeners().should have(1).listener
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when it's a webapp extension" do
|
24
|
+
subject { Trinidad::Extensions::LifecycleWebAppExtension.new(@options) }
|
25
|
+
|
26
|
+
it "adds the listener to the application context" do
|
27
|
+
app_context = Trinidad::Tomcat::StandardContext.new
|
28
|
+
subject.configure(@tomcat, app_context)
|
29
|
+
app_context.findLifecycleListeners().should have(1).listener
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'trinidad_lifecycle_extension'
|
16
|
+
s.version = '0.1.0'
|
17
|
+
s.date = '2010-09-12'
|
18
|
+
s.rubyforge_project = 'trinidad_lifecycle_extension'
|
19
|
+
|
20
|
+
## Make sure your summary is short. The description may be as long
|
21
|
+
## as you like.
|
22
|
+
s.summary = "Add lifecycle listeners to the trinidad's server or applications"
|
23
|
+
s.description = "Add lifecycle listeners to the trinidad's server or the applications that run on it"
|
24
|
+
|
25
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
26
|
+
## better to set the email to an email list or something. If you don't have
|
27
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
28
|
+
s.authors = ["David Calavera"]
|
29
|
+
s.email = 'calavera@apache.org'
|
30
|
+
s.homepage = 'http://github.com/calavera/trinidad_lifecycle_extension'
|
31
|
+
|
32
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
33
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
34
|
+
s.require_paths = %w[lib]
|
35
|
+
|
36
|
+
## Specify any RDoc options here. You'll want to add your README and
|
37
|
+
## LICENSE files to the extra_rdoc_files list.
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.extra_rdoc_files = %w[README LICENSE]
|
40
|
+
|
41
|
+
## List your runtime dependencies here. Runtime dependencies are those
|
42
|
+
## that are needed for an end user to actually USE your code.
|
43
|
+
# s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"])
|
44
|
+
|
45
|
+
## List your development dependencies here. Development dependencies are
|
46
|
+
## those that are only needed during development
|
47
|
+
# s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
|
48
|
+
|
49
|
+
## Leave this section as-is. It will be automatically generated from the
|
50
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
51
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
52
|
+
# = MANIFEST =
|
53
|
+
s.files = %w[
|
54
|
+
LICENSE
|
55
|
+
README
|
56
|
+
Rakefile
|
57
|
+
lib/trinidad_lifecycle_extension.rb
|
58
|
+
spec/fixtures/fake_lifecycle_listener.rb
|
59
|
+
spec/spec.opts
|
60
|
+
spec/spec_helper.rb
|
61
|
+
spec/trinidad_lifecycle_extension_spec.rb
|
62
|
+
trinidad_lifecycle_extension.gemspec
|
63
|
+
]
|
64
|
+
# = MANIFEST =
|
65
|
+
|
66
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
67
|
+
## matches what you actually use.
|
68
|
+
s.test_files = s.files.select { |path| path =~ /^spec\/*_spec\.rb/ }
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trinidad_lifecycle_extension
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- David Calavera
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-12 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Add lifecycle listeners to the trinidad's server or the applications that run on it
|
23
|
+
email: calavera@apache.org
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
30
|
+
- LICENSE
|
31
|
+
files:
|
32
|
+
- LICENSE
|
33
|
+
- README
|
34
|
+
- Rakefile
|
35
|
+
- lib/trinidad_lifecycle_extension.rb
|
36
|
+
- spec/fixtures/fake_lifecycle_listener.rb
|
37
|
+
- spec/spec.opts
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/trinidad_lifecycle_extension_spec.rb
|
40
|
+
- trinidad_lifecycle_extension.gemspec
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/calavera/trinidad_lifecycle_extension
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: trinidad_lifecycle_extension
|
71
|
+
rubygems_version: 1.3.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Add lifecycle listeners to the trinidad's server or applications
|
75
|
+
test_files: []
|
76
|
+
|