hope 0.1.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +11 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/bin/hope-web +16 -0
- data/hope.gemspec +34 -0
- data/lib/hope.rb +103 -0
- data/lib/hope/core_ext/object.rb +78 -0
- data/lib/hope/engine.rb +189 -0
- data/lib/hope/event_type.rb +22 -0
- data/lib/hope/jars/antlr-runtime-3.2.jar +0 -0
- data/lib/hope/jars/cglib-nodep-2.2.jar +0 -0
- data/lib/hope/jars/commons-logging-1.1.1.jar +0 -0
- data/lib/hope/jars/esper-4.3.0.jar +0 -0
- data/lib/hope/jars/log4j-1.2.16.jar +0 -0
- data/lib/hope/jars/msgpack-0.6.0-devel.jar +0 -0
- data/lib/hope/listener/base.rb +40 -0
- data/lib/hope/server.rb +43 -0
- data/lib/hope/server/app.rb +40 -0
- data/lib/hope/server/public/css/Aristo/images/bg_fallback.png +0 -0
- data/lib/hope/server/public/css/Aristo/images/icon_sprite.png +0 -0
- data/lib/hope/server/public/css/Aristo/images/progress_bar.gif +0 -0
- data/lib/hope/server/public/css/Aristo/images/slider_handles.png +0 -0
- data/lib/hope/server/public/css/Aristo/images/ui-icons_222222_256x240.png +0 -0
- data/lib/hope/server/public/css/Aristo/images/ui-icons_454545_256x240.png +0 -0
- data/lib/hope/server/public/css/Aristo/jquery-ui-1.8.7.custom.css +703 -0
- data/lib/hope/server/public/css/hope.css +235 -0
- data/lib/hope/server/public/favicon.ico +0 -0
- data/lib/hope/server/public/hope.js +995 -0
- data/lib/hope/server/public/js/backbone-0.3.3.js +1011 -0
- data/lib/hope/server/public/js/backbone-0.5.1.js +1149 -0
- data/lib/hope/server/public/js/hope.js +993 -0
- data/lib/hope/server/public/js/inflection.js +656 -0
- data/lib/hope/server/public/js/jquery-1.6.1.js +8936 -0
- data/lib/hope/server/public/js/jquery-ui-1.8.14.custom.min.js +789 -0
- data/lib/hope/server/public/js/underscore-1.1.6.js +807 -0
- data/lib/hope/server/resources/engine.rb +60 -0
- data/lib/hope/server/resources/source.rb +22 -0
- data/lib/hope/server/resources/statement.rb +62 -0
- data/lib/hope/server/views/app.erb +24 -0
- data/lib/hope/source.rb +20 -0
- data/lib/hope/source/base.rb +30 -0
- data/lib/hope/source/sub.rb +24 -0
- data/lib/hope/source/twitter.rb +123 -0
- data/lib/hope/statement.rb +73 -0
- data/lib/hope/version.rb +3 -0
- metadata +241 -0
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Stephane Bellity
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= hope
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to hope
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Stephane Bellity. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/bin/hope-web
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env jruby
|
2
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'thin'
|
6
|
+
require 'sinatra'
|
7
|
+
require 'hope'
|
8
|
+
require 'hope/server'
|
9
|
+
|
10
|
+
|
11
|
+
EM::run {
|
12
|
+
Kernel.trap("INT") { puts "Got INT signal"; EM.stop; exit(0) }
|
13
|
+
Kernel.trap("TERM") { puts "Got TERM signal"; EM.stop; exit(0) }
|
14
|
+
Hope::configure(ARGV[0]) unless ARGV[0].nil?
|
15
|
+
Hope::Server::App.run!
|
16
|
+
}
|
data/hope.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/hope/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'hope'
|
6
|
+
s.version = Hope::VERSION.dup
|
7
|
+
s.summary = %q{jruby wrapper for Esper (Stream Event Processing)}
|
8
|
+
s.description = %q{jruby wrapper for Esper (Stream Event Processing) }
|
9
|
+
s.homepage = 'https://github.com/sbellity/hope'
|
10
|
+
s.authors = ['Stephane Bellity']
|
11
|
+
s.email = ['sbellity@gmail.com']
|
12
|
+
|
13
|
+
s.executables = 'hope-web'
|
14
|
+
s.files = Dir.glob('{bin,lib}/**/*') + %w(README.rdoc LICENSE.txt Gemfile hope.gemspec)
|
15
|
+
|
16
|
+
s.platform = 'java'
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
|
19
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'sinatra', '~> 1.0.0'
|
22
|
+
s.add_runtime_dependency 'json-jruby', '~> 1.4'
|
23
|
+
s.add_runtime_dependency 'em-zeromq', '~> 0.2.1'
|
24
|
+
s.add_runtime_dependency 'thin'
|
25
|
+
|
26
|
+
|
27
|
+
s.add_development_dependency 'ZenTest', '~> 4.5'
|
28
|
+
s.add_development_dependency 'rake', '~> 0.9.2'
|
29
|
+
s.add_development_dependency 'rspec', '~> 2.6'
|
30
|
+
s.add_development_dependency 'rcov', '~> 0.9.9'
|
31
|
+
s.add_development_dependency 'eco', '~> 1.0.0'
|
32
|
+
# s.add_development_dependency 'therubyrhino', '~> 1.72.8'
|
33
|
+
s.add_development_dependency 'watchr'
|
34
|
+
end
|
data/lib/hope.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'json'
|
2
|
+
require "digest/md5"
|
3
|
+
|
4
|
+
require 'em-zeromq'
|
5
|
+
|
6
|
+
require 'hope/jars/esper-4.3.0.jar'
|
7
|
+
require 'hope/jars/commons-logging-1.1.1.jar'
|
8
|
+
require 'hope/jars/antlr-runtime-3.2.jar'
|
9
|
+
require 'hope/jars/cglib-nodep-2.2.jar'
|
10
|
+
require 'hope/jars/log4j-1.2.16.jar'
|
11
|
+
require 'hope/jars/msgpack-0.6.0-devel.jar'
|
12
|
+
|
13
|
+
java_import org.msgpack.MessagePack
|
14
|
+
|
15
|
+
require 'hope/core_ext/object'
|
16
|
+
|
17
|
+
require "hope/version"
|
18
|
+
require "hope/engine"
|
19
|
+
require "hope/statement"
|
20
|
+
require "hope/event_type"
|
21
|
+
require "hope/source"
|
22
|
+
require "hope/source/base"
|
23
|
+
require "hope/source/sub"
|
24
|
+
# require "hope/source/twitter"
|
25
|
+
|
26
|
+
require "hope/listener/base"
|
27
|
+
|
28
|
+
require 'hope/server'
|
29
|
+
|
30
|
+
module Hope
|
31
|
+
include Java
|
32
|
+
|
33
|
+
def self.ctx
|
34
|
+
@ctx ||= EM::ZeroMQ::Context.new(1)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.pub
|
38
|
+
@pub ||= ctx.bind ZMQ::PUB, 'ipc://hope'
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.config
|
42
|
+
@config ||= {}
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.configure config_file, &block
|
46
|
+
@config = YAML::load_file(config_file)
|
47
|
+
Hope::Source
|
48
|
+
# Add Sources
|
49
|
+
@config["sources"].each do |sn, src_opts|
|
50
|
+
begin
|
51
|
+
src_type = src_opts.delete("type")
|
52
|
+
src_klass = src_type.constantize
|
53
|
+
source = src_type.constantize.new(sn, src_opts)
|
54
|
+
rescue => err
|
55
|
+
puts "Error creating source #{src_type}: #{err}"
|
56
|
+
raise err
|
57
|
+
end
|
58
|
+
end unless config["sources"].nil?
|
59
|
+
|
60
|
+
if @config["reload_config"].to_i > 0
|
61
|
+
EM::PeriodicTimer.new(@config["reload_config"].to_i) do
|
62
|
+
new_config_hash = Digest::MD5.hexdigest(open(config_file).read)
|
63
|
+
self.load_engines(YAML::load_file(config_file)['engines']) if new_config_hash != @config_hash
|
64
|
+
@config_hash = new_config_hash
|
65
|
+
end
|
66
|
+
else
|
67
|
+
self.load_engines @config['engines'] unless @config['engines'].nil?
|
68
|
+
end
|
69
|
+
|
70
|
+
yield if block_given?
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.load_engines engines_config
|
74
|
+
engines_config.each do |uri, econf|
|
75
|
+
if e = Hope::Engine.get(uri)
|
76
|
+
e.stop
|
77
|
+
e.reset
|
78
|
+
else
|
79
|
+
# Create engine
|
80
|
+
puts "Creating engine with config: #{@config['engines_cfg']}"
|
81
|
+
e = Hope::Engine.new(uri, @config['engines_cfg'])
|
82
|
+
# Subscribe to named sources
|
83
|
+
econf['subscriptions'].each do |sub|
|
84
|
+
e.subscribe(sub)
|
85
|
+
end if econf['subscriptions']
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# Add and configure Statements
|
90
|
+
econf['statements'].each do |sn, st|
|
91
|
+
s = e.add_epl st['epl'], sn
|
92
|
+
st['listeners'].each do |listener_name, listener_opts|
|
93
|
+
listener_klass = listener_opts.delete('class').constantize
|
94
|
+
listener_args = listener_opts.delete('args')
|
95
|
+
s.add_listener listener_klass.new("#{listener_name}-#{rand(1000000)}", *listener_args)
|
96
|
+
end if st['listeners']
|
97
|
+
end if econf['statements']
|
98
|
+
e.start
|
99
|
+
end unless engines_config.nil?
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# Shamelessly stolen from activesupport-3.0.9
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
5
|
+
# For example, "", " ", +nil+, [], and {} are blank.
|
6
|
+
#
|
7
|
+
# This simplifies:
|
8
|
+
#
|
9
|
+
# if !address.nil? && !address.empty?
|
10
|
+
#
|
11
|
+
# ...to:
|
12
|
+
#
|
13
|
+
# if !address.blank?
|
14
|
+
def blank?
|
15
|
+
respond_to?(:empty?) ? empty? : !self
|
16
|
+
end
|
17
|
+
|
18
|
+
# An object is present if it's not blank.
|
19
|
+
def present?
|
20
|
+
!blank?
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns object if it's #present? otherwise returns nil.
|
24
|
+
# object.presence is equivalent to object.present? ? object : nil.
|
25
|
+
#
|
26
|
+
# This is handy for any representation of objects where blank is the same
|
27
|
+
# as not present at all. For example, this simplifies a common check for
|
28
|
+
# HTTP POST/query parameters:
|
29
|
+
#
|
30
|
+
# state = params[:state] if params[:state].present?
|
31
|
+
# country = params[:country] if params[:country].present?
|
32
|
+
# region = state || country || 'US'
|
33
|
+
#
|
34
|
+
# ...becomes:
|
35
|
+
#
|
36
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
37
|
+
def presence
|
38
|
+
self if present?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class NilClass #:nodoc:
|
43
|
+
def blank?
|
44
|
+
true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class FalseClass #:nodoc:
|
49
|
+
def blank?
|
50
|
+
true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class TrueClass #:nodoc:
|
55
|
+
def blank?
|
56
|
+
false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Array #:nodoc:
|
61
|
+
alias_method :blank?, :empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
class Hash #:nodoc:
|
65
|
+
alias_method :blank?, :empty?
|
66
|
+
end
|
67
|
+
|
68
|
+
class String #:nodoc:
|
69
|
+
def blank?
|
70
|
+
self !~ /\S/
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class Numeric #:nodoc:
|
75
|
+
def blank?
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
data/lib/hope/engine.rb
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
module Hope
|
2
|
+
|
3
|
+
def self.engines
|
4
|
+
@engines ||= {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.register_engine eng
|
8
|
+
self.engines[eng.uri] = eng
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.unregister_engine eng
|
12
|
+
self.engines.delete eng.uri
|
13
|
+
end
|
14
|
+
|
15
|
+
class Engine
|
16
|
+
|
17
|
+
java_import com.espertech.esper.client.EPServiceProviderManager
|
18
|
+
java_import com.espertech.esper.client.Configuration
|
19
|
+
|
20
|
+
attr_reader :provider, :uri
|
21
|
+
|
22
|
+
def self.get uri=nil
|
23
|
+
Hope.engines[uri]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.all
|
27
|
+
EPServiceProviderManager.getProviderURIs.to_a
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize uri=nil, config_file=Hope.config['engines_cfg']
|
31
|
+
puts "Init engine #{uri} with config: #{config_file}"
|
32
|
+
@uri = uri || "default"
|
33
|
+
Hope.register_engine(self)
|
34
|
+
@configuration = Configuration.new
|
35
|
+
if config_file
|
36
|
+
if File.exists?(config_file)
|
37
|
+
@configuration.configure(config_file)
|
38
|
+
else
|
39
|
+
puts "I cant find this config file: #{config_file}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
provider
|
44
|
+
@sub = Hope.ctx.connect ZMQ::SUB, "ipc://hope", self if EM.reactor_running?
|
45
|
+
@received = 0
|
46
|
+
@subscriptions = []
|
47
|
+
@registered_sources = {}
|
48
|
+
@registered_types = {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def on_readable(socket, messages)
|
52
|
+
puts "Received event from #{socket}"
|
53
|
+
@received += 1
|
54
|
+
src_name, msg = messages.map(&:copy_out_string)
|
55
|
+
if src = self.register_source(src_name)
|
56
|
+
evts = src.parse(msg)
|
57
|
+
|
58
|
+
Array(evts).map do |e|
|
59
|
+
puts ">> sendEvent: #{e.inspect}"
|
60
|
+
self.sendEvent(e)
|
61
|
+
end
|
62
|
+
else
|
63
|
+
puts "Error: SOURCE #{src_name}, not registered !"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def register_source src_name
|
68
|
+
return @registered_sources[src_name] if @registered_sources[src_name]
|
69
|
+
src = Hope::Source.sources[src_name]
|
70
|
+
return false if src.nil?
|
71
|
+
src.class.event_types.each do |event_type|
|
72
|
+
# puts "Adding eventType to engine #{self.uri}: #{event_type.name}:\n #{event_type.properties.inspect}"
|
73
|
+
self.add_event_type(event_type)
|
74
|
+
end
|
75
|
+
@registered_sources[src] = src
|
76
|
+
src
|
77
|
+
end
|
78
|
+
|
79
|
+
def subscribe source_name
|
80
|
+
return true if @subscriptions.include?(source_name)
|
81
|
+
@sub.subscribe source_name
|
82
|
+
@subscriptions << source_name
|
83
|
+
register_source(source_name)
|
84
|
+
end
|
85
|
+
|
86
|
+
def unsubscribe source_name
|
87
|
+
@subscriptions - [source_name]
|
88
|
+
@sub.unsubscribe source_name
|
89
|
+
end
|
90
|
+
|
91
|
+
# Provider API
|
92
|
+
def provider
|
93
|
+
if uri.nil?
|
94
|
+
@provider ||= EPServiceProviderManager.getDefaultProvider(@configuration)
|
95
|
+
else
|
96
|
+
@provider ||= EPServiceProviderManager.getProvider(uri, @configuration)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def destroy
|
101
|
+
unless provider.destroyed?
|
102
|
+
provider.destroy
|
103
|
+
Hope.unregister_engine(self)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def destroyed?
|
108
|
+
provider.destroyed?
|
109
|
+
end
|
110
|
+
|
111
|
+
def reset
|
112
|
+
# @subscriptions.each { |sub| self.unsubscribe(sub) }
|
113
|
+
statements.map do |st|
|
114
|
+
st.remove_all_listeners
|
115
|
+
st.destroy
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Admin API
|
120
|
+
def admin
|
121
|
+
provider.getEPAdministrator
|
122
|
+
end
|
123
|
+
|
124
|
+
def statement_names
|
125
|
+
admin.getStatementNames.to_a
|
126
|
+
end
|
127
|
+
|
128
|
+
def statements
|
129
|
+
statement_names.map { |n| Hope::Statement.new(admin.getStatement(n)) }
|
130
|
+
end
|
131
|
+
|
132
|
+
def statement stmt_name
|
133
|
+
s = admin.getStatement stmt_name
|
134
|
+
Statement.new(s) unless s.nil?
|
135
|
+
end
|
136
|
+
|
137
|
+
def add_epl epl, name=nil
|
138
|
+
name = nil if name.blank?
|
139
|
+
Hope::Statement.new admin.createEPL(epl, name)
|
140
|
+
end
|
141
|
+
|
142
|
+
def add_pattern pattern, name=nil
|
143
|
+
name = nil if name.blank?
|
144
|
+
Hope::Statement.new admin.createPattern(pattern, name)
|
145
|
+
end
|
146
|
+
|
147
|
+
def add_event_type event_type
|
148
|
+
return if @registered_types[event_type.name]
|
149
|
+
@registered_types[event_type.name] = event_type.schema
|
150
|
+
add_epl(event_type.schema)
|
151
|
+
end
|
152
|
+
|
153
|
+
def stop
|
154
|
+
admin.stopAllStatements
|
155
|
+
end
|
156
|
+
|
157
|
+
def start
|
158
|
+
admin.startAllStatements
|
159
|
+
end
|
160
|
+
|
161
|
+
# Runtime API
|
162
|
+
def runtime
|
163
|
+
provider.getEPRuntime
|
164
|
+
end
|
165
|
+
|
166
|
+
def sendEvent(e, type=nil)
|
167
|
+
if type
|
168
|
+
runtime.sendEvent(e, type)
|
169
|
+
else
|
170
|
+
runtime.sendEvent(e)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# Misc
|
175
|
+
def serializable_hash
|
176
|
+
{
|
177
|
+
:id => uri,
|
178
|
+
:received => @received,
|
179
|
+
:statements => statements.map(&:serializable_hash),
|
180
|
+
:subscriptions => @subscriptions
|
181
|
+
}
|
182
|
+
end
|
183
|
+
|
184
|
+
def to_json
|
185
|
+
serializable_hash.to_json
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|