adaptation 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1 +1,2 @@
1
- * 0.0.1 (18 June 2007)
1
+ * 0.0.3
2
+ - changed script/mom to mom as a standalone executable (like adaptation)
data/bin/breakpointer ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/../config/boot'
3
+ require 'commands/breakpointer'
data/bin/mom CHANGED
@@ -1,8 +1,46 @@
1
- #!/usr/bin/env ruby
2
- unless defined? ADAPTOR_ROOT
3
- ADAPTOR_ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
4
- end
5
1
  require 'rubygems'
6
2
  require 'adaptation'
3
+ require 'adaptation/mom'
4
+
5
+ DEFAULT_HOST = "127.0.0.1"
6
+ DEFAULT_PORT = "8080"
7
+
8
+ # search mom configuration file.
9
+ # - first by specified parameter
10
+ # - second in /etc/adaptation (*nix systems)
11
+ # - third in the gem itself
12
+
13
+ if File.exists?(File.dirname(__FILE__) + '/../configs/mom.yml')
14
+ file = File.dirname(__FILE__) + '/../configs/mom.yml'
15
+ end
16
+
17
+ if File.exists?('/etc/adaptation/mom.yml')
18
+ file = '/etc/adaptation/mom.yml'
19
+ end
7
20
 
8
- require 'commands/mom'
21
+ if %w(--file -f).include? ARGV.first
22
+ file = ARGV[1]
23
+ end
24
+
25
+ #if %w(--environment -e).include? ARGV.first
26
+ # environment = ARGV[1]
27
+ #end
28
+ environment = "development"
29
+
30
+ if file.nil?
31
+ config = {"mom" => {"type" => "druby", "host" => DEFAULT_HOST, "port" => DEFAULT_PORT}}
32
+ else
33
+ config = YAML::load(File.open(file))[environment]
34
+ end
35
+
36
+ Signal.trap("INT") { puts "Shutting down MOM server (#{config["mom"]["type"]})"; exit }
37
+
38
+ case config["mom"]["type"]
39
+ when "druby"
40
+ mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
41
+ mom = Adaptation::Mom::Mom.new mom_uri
42
+ mom.start
43
+ #when "xmlblaster"
44
+ else
45
+ puts "Unknown MOM server type: #{config["mom"]["type"]}"
46
+ end
data/bin/mom~ ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'adaptation'
3
+ require 'adaptation/mom'
4
+
5
+ DEFAULT_HOST = "127.0.0.1"
6
+ DEFAULT_PORT = "8080"
7
+
8
+ # search mom configuration file.
9
+ # - first by specified parameter
10
+ # - second in /etc/adaptation (*nix systems)
11
+ # - third in the gem itself
12
+
13
+ if File.exists?(File.dirname(__FILE__) + '/../configs/mom.yml')
14
+ file = File.dirname(__FILE__) + '/../configs/mom.yml'
15
+ end
16
+
17
+ if File.exists?('/etc/adaptation/mom.yml')
18
+ file = '/etc/adaptation/mom.yml'
19
+ end
20
+
21
+ if %w(--file -f).include? ARGV.first
22
+ file = ARGV[1]
23
+ end
24
+
25
+ #if %w(--environment -e).include? ARGV.first
26
+ # environment = ARGV[1]
27
+ #end
28
+ environment = "development"
29
+
30
+ if file.nil?
31
+ config = {"mom" => {"type" => "druby", "host" => DEFAULT_HOST, "port" => DEFAULT_PORT}}
32
+ else
33
+ config = YAML::load(File.open(file))[environment]
34
+ end
35
+
36
+ Signal.trap("INT") { puts "Shutting down MOM server (#{config["mom"]["type"]})"; exit }
37
+
38
+ case config["mom"]["type"]
39
+ when "druby"
40
+ mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
41
+ mom = Adaptation::Mom::Mom.new mom_uri
42
+ mom.start
43
+ #when "xmlblaster"
44
+ else
45
+ puts "Unknown MOM server type: #{config["mom"]["type"]}"
46
+ end
data/configs/mom.yml CHANGED
@@ -1,8 +1,10 @@
1
- mom:
2
- type: druby
3
- port: 8080
4
- host: 127.0.0.1
5
- subscriber:
6
- port: 8081
7
- host: 127.0.0.1
8
- topics: all
1
+ development:
2
+ mom:
3
+ type: druby
4
+ port: 8080
5
+ host: 127.0.0.1
6
+ subscriber:
7
+ type: druby
8
+ port: 8081
9
+ host: 127.0.0.1
10
+ topics: all
@@ -0,0 +1,6 @@
1
+ development:
2
+ group: group_id
3
+ application: APPLICATION_ID
4
+ test:
5
+ group: group_id
6
+ application: APPLICATION_ID
@@ -1,11 +1,26 @@
1
1
  unless defined? ADAPTOR_ROOT
2
2
  ADAPTOR_ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
3
3
  end
4
- require 'drb'
5
4
  require 'yaml'
6
5
 
7
6
  config = YAML::load(File.open("#{ADAPTOR_ROOT}/config/mom.yml"))
8
- mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
9
7
 
10
- mom = DRbObject.new(nil, mom_uri)
11
- mom.publish ARGV[1], ARGV[0]
8
+ case config["mom"]["type"]
9
+ when "druby"
10
+ require 'drb'
11
+
12
+ mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
13
+
14
+ mom = DRbObject.new(nil, mom_uri)
15
+ mom.publish ARGV[1], ARGV[0]
16
+
17
+ when "xmlblaster"
18
+ require "rubygems"
19
+ require "adaptation/xmlblaster_client"
20
+
21
+ xbc = XmlblasterClient.new(config["mom"]["host"], config["mom"]["port"])
22
+ xbc.login( "OAP_USER", "OAP_PASS" )
23
+ xbc.publish( "<key oid='#{ARGV[0]}' contentMime='text/xml'/>", ARGV[1] , "<qos></qos>" )
24
+ xbc.logout
25
+ end
26
+
@@ -1,6 +1,9 @@
1
1
  unless defined? ADAPTOR_ROOT
2
2
  ADAPTOR_ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
3
3
  end
4
+
5
+ $:.unshift("#{ADAPTOR_ROOT}/app/adaptors")
6
+
4
7
  require 'rubygems'
5
8
  require 'adaptation'
6
9
  require "adaptation/test/test_help"
@@ -4,7 +4,7 @@ module Adaptation
4
4
 
5
5
  def initialize
6
6
  $environment = "development"
7
- $config = YAML::load(File.open("config/settings.yml"))[$environment]
7
+ $config = YAML::load(File.open("#{ADAPTOR_ROOT}/config/settings.yml"))[$environment]
8
8
 
9
9
  # require all adaptors
10
10
  Dir["#{ADAPTOR_ROOT}/app/adaptors/*.rb"].each do |f|
@@ -22,8 +22,8 @@ module Adaptation
22
22
  end
23
23
 
24
24
  # connect with database -> this could also be avoided?
25
- if File.exists?("config/database.yml")
26
- development_configurations = YAML::load(File.open("config/database.yml"))[$environment]
25
+ if File.exists?("#{ADAPTOR_ROOT}/config/database.yml")
26
+ development_configurations = YAML::load(File.open("#{ADAPTOR_ROOT}/config/database.yml"))[$environment]
27
27
  ActiveRecord::Base.configurations.update("development" => development_configurations)
28
28
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[$environment])
29
29
  end
@@ -0,0 +1,85 @@
1
+ require 'drb'
2
+ require 'yaml'
3
+
4
+ module Adaptation
5
+
6
+ module Mom
7
+
8
+ class DrubySubscriber
9
+
10
+ def initialize subscriber_uri, mom_uri, topics
11
+ @subscriber_uri = subscriber_uri
12
+ @mom_uri = mom_uri
13
+ @topics = topics
14
+ @adaptors_list = Array.new
15
+ end
16
+
17
+ def process message, topic
18
+ if ( (@topics.include?(topic)) or (@topics == "all") )
19
+ system("ruby public/dispatch.rb #{message}")
20
+ end
21
+ puts "#{topic} => #{message}"
22
+ end
23
+
24
+ def subscription_result subscribed
25
+ if subscribed
26
+ puts "Subscribed to mom (#{@mom_uri}). Listening at #{@subscriber_uri}"
27
+ end
28
+ end
29
+
30
+ # TODO: Think what exactly is this subscriber:
31
+ # - path to file
32
+ # - combination of a path and a set of conditions <- looks better...
33
+ # - ...
34
+ def note_me_down adaptor
35
+ unless @adaptors_list.include? adaptor
36
+ @adaptors_list << adaptor
37
+ puts "Added adaptor: #{adaptor}"
38
+ end
39
+ end
40
+
41
+ def start
42
+ uri_owner = false
43
+ begin
44
+ # try to start the subscriber service,
45
+ # using the uri specified in config/mom.yml
46
+ DRb.start_service(@subscriber_uri, self)
47
+
48
+ # subscribe that uri to the mom
49
+ mom = DRbObject.new(nil, @mom_uri)
50
+ mom.subscribe @subscriber_uri
51
+ uri_owner = true
52
+ rescue Exception => e
53
+ # desired uri already in use...
54
+ # if the process using it is a subscriber, this
55
+ # shouldn't be a problem
56
+ uri_owner = false
57
+ end
58
+
59
+ # try to tell to the subscriber using the uri
60
+ # (may be this self instance of DrubySubscriber),
61
+ # that we want to be executed when the MOM
62
+ # calls its process method
63
+ begin
64
+ subscriber = DRbObject.new(nil, @subscriber_uri)
65
+ routes_file = File.expand_path(File.dirname(__FILE__) + '/../config/routes.rb')
66
+ subscriber.note_me_down routes_file
67
+ rescue Exception => e
68
+ # the process using @subscriber_uri is not
69
+ # an instance of DrubySubsciber...
70
+ puts "Couldn't start or find subscriber at #{@subscriber_uri}:"
71
+ puts "#{e}"
72
+ return
73
+ end
74
+
75
+ if uri_owner
76
+ DRb.thread.join # Don't exit just yet!
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,79 @@
1
+ require 'drb'
2
+ require 'yaml'
3
+
4
+ module Adaptation
5
+
6
+ module Mom
7
+
8
+ class DrubySubscriber
9
+
10
+ def initialize subscriber_uri, mom_uri, topics
11
+ @subscriber_uri = subscriber_uri
12
+ @mom_uri = mom_uri
13
+ @topics = topics
14
+ @adaptors_list = Array.new
15
+ end
16
+
17
+ def process message, topic
18
+ if ( (@topics.include?(topic)) or (@topics == "all") )
19
+ system("ruby public/dispatch.rb #{message}")
20
+ end
21
+ puts "#{topic} => #{message}"
22
+ end
23
+
24
+ def subscription_result subscribed
25
+ if subscribed
26
+ puts "Subscribed to mom (#{@mom_uri}). Listening at #{@subscriber_uri}"
27
+ end
28
+ end
29
+
30
+ # TODO: Think what exactly is this subscriber:
31
+ # - path to file
32
+ # - combination of a path and a set of conditions <- looks better...
33
+ # - ...
34
+ def note_me_down adaptor
35
+ unless @adaptors_list.include? adaptor
36
+ @adaptors_list << adaptor
37
+ puts "Added adaptor: #{adaptor}"
38
+ end
39
+ end
40
+
41
+ def start
42
+ begin
43
+ # try to start the subscriber service,
44
+ # using the uri specified in config/mom.yml
45
+ DRb.start_service(@subscriber_uri, self)
46
+
47
+ # subscribe that uri to the mom
48
+ mom = DRbObject.new(nil, @mom_uri)
49
+ mom.subscribe @subscriber_uri
50
+ rescue Exception => e
51
+ # desired uri already in use...
52
+ # if the process using it is a subscriber, this
53
+ # shouldn't be a problem
54
+ end
55
+
56
+ # try to tell to the subscriber using the uri
57
+ # (may be this self instance of DrubySubscriber),
58
+ # that we want to be executed when the MOM
59
+ # calls its process method
60
+ begin
61
+ subscriber = DRbObject.new(nil, @subscriber_uri)
62
+ routes_file = File.expand_path(File.dirname(__FILE__) + '/../config/routes.rb')
63
+ subscriber.note_me_down routes_file
64
+ rescue Exception => e
65
+ # the process using @subscriber_uri is not
66
+ # an instance of DrubySubsciber...
67
+ puts "Couldn't start or find subscriber at #{@subscriber_uri}:"
68
+ puts "#{e}"
69
+ return
70
+ end
71
+
72
+ DRb.thread.join # Don't exit just yet!
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end