jsparrow 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  JSparrow is a JMS client based on JRuby. Previously it has been called Sparrow, but changed his name because has another project with the same name.
4
4
 
5
- WARNING: I'm sorry but any time I break backward compatibility. However, always is for make a better API.
5
+ I'm sorry but any time I break backward compatibility. However, always is for make a better API.
6
6
 
7
7
  === Install
8
8
 
@@ -38,7 +38,7 @@ WARNING: OpenJMS will be used as JMS provider, but any other could be used with
38
38
 
39
39
  3) Create the client and start it
40
40
 
41
- jms_client = JSparrow::Connection.new_client
41
+ jms_client = new_jsparrow_client
42
42
  jms_client.start
43
43
 
44
44
  4) OK! Now you can send and receive messages right now!
@@ -78,7 +78,7 @@ Or, if you don't want write code, only do it:
78
78
 
79
79
  For more informations, read the specs! ;)
80
80
 
81
- http://github.com/leandrosilva/jsparrow/tree/master/spec/
81
+ http://github.com/leandrosilva/jsparrow/tree/master/spec
82
82
 
83
83
  == Copyright
84
84
 
data/Rakefile CHANGED
@@ -45,5 +45,10 @@ rescue LoadError
45
45
  end
46
46
  end
47
47
 
48
+ require 'spec/rake/spectask'
49
+ Spec::Rake::SpecTask.new do |spec|
50
+ spec.spec_files = FileList['spec/**/*_spec.rb']
51
+ spec.spec_opts = %w(-fs --color)
52
+ end
48
53
 
49
54
  task :default => :test
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 1
4
- :patch: 3
4
+ :patch: 4
5
5
  :build:
@@ -1,253 +1,5 @@
1
- # Classes Java usadas nesse arquivo
2
1
  import 'java.util.Hashtable'
3
2
  import 'javax.naming.InitialContext'
4
3
 
5
- module JSparrow
6
- module Connection
7
-
8
- class << self
9
- #
10
- # Metodo usado para configurar a conexao.
11
- #
12
- def configure(&block)
13
- @@configuration = Configuration.new
14
-
15
- class_eval(&block)
16
-
17
- @@configuration
18
- end
19
-
20
- #
21
- # Metodo usado para obter a configuracao para conexao com o provedor de JMS.
22
- #
23
- def configuration
24
- @@configuration
25
- end
26
-
27
- #
28
- # Use:
29
- #
30
- # use_jms_client_jar "path/to/name_of_the_client_jar_file.jar"
31
- #
32
- def use_jms_client_jar(client_jar)
33
- configuration.jms_client_jar = client_jar
34
- end
35
-
36
- #
37
- # Use:
38
- #
39
- # use_jndi_properties :a_jndi_property_name_in_lower_case => "a_value_of_property",
40
- # :other_jndi_property_name_in_lower_case => "other_value_of_property"
41
- #
42
- def use_jndi_properties(jndi_properties = {})
43
- configuration.jndi_properties = jndi_properties
44
- end
45
-
46
- #
47
- # Use:
48
- #
49
- # enable_connection_factories :queue_connection_factory => "jndi_name_of_queue_connection_factory",
50
- # :topic_connection_factory => "jndi_name_of_topic_connection_factory"
51
- #
52
- def enable_connection_factories(jndi_names = {})
53
- configuration.enabled_connection_factories = jndi_names
54
- end
55
-
56
- #
57
- # Use:
58
- #
59
- # enable_queues :a_queue_name_in_lower_case => "jndi_name_of_a_queue",
60
- # :other_queue_name_in_lower_case => "jndi_name_of_other_queue"
61
- #
62
- def enable_queues(jndi_names = {})
63
- configuration.enabled_queues = jndi_names
64
- end
65
-
66
- #
67
- # Use:
68
- #
69
- # enable_topics :a_topic_name_in_lower_case => "jndi_name_of_a_topic",
70
- # :other_topic_name_in_lower_case => "jndi_name_of_other_topic"
71
- #
72
- def enable_topics(jndi_names = {})
73
- configuration.enabled_topics = jndi_names
74
- end
75
-
76
- #
77
- # Metodo usado para criar um novo Client JMS.
78
- #
79
- def new_client
80
- Client.new(new_connection)
81
- end
82
-
83
- #
84
- # Metodo usado para criar um novo Listener de mensagens JMS.
85
- #
86
- def new_listener(listener_spec)
87
- listener_spec[:as].new(new_connection)
88
- end
89
-
90
- # --- Private methods --- #
91
- private
92
-
93
- #
94
- # Metodo usado para criar uma nova Connection
95
- #
96
- def new_connection
97
- jndi_context_builder = JNDI::ContextBuilder.new(configuration.jms_client_jar, configuration.jndi_properties)
98
-
99
- connection = Base.new(configuration, jndi_context_builder)
100
- end
101
- end
102
-
103
- #
104
- # Classe base para estabelecer conexao com o provedor JMS via JNDI.
105
- #
106
- class Base
107
- attr_reader :configuration
108
-
109
- def initialize(configuration, jndi_context_builder)
110
- @configuration = configuration
111
- @jndi_context_builder = jndi_context_builder
112
-
113
- # Foi estabelecida?
114
- @opened = false
115
- end
116
-
117
- def is_opened?
118
- @opened
119
- end
120
-
121
- def open
122
- raise InvalidStateError.new('opened', 'open') if is_opened?
123
-
124
- begin
125
- @jndi_context = @jndi_context_builder.build
126
- rescue => cause
127
- raise InitializationError.new(@configuration, cause)
128
- end
129
-
130
- @opened = true
131
- end
132
-
133
- def is_closed?
134
- not @opened
135
- end
136
-
137
- def close
138
- raise InvalidStateError.new('closed', 'close') if is_closed?
139
-
140
- @jndi_context.close
141
-
142
- @opened = false
143
- end
144
-
145
- def lookup_resources(resources = {})
146
- lookuped_resource = {}
147
-
148
- return lookuped_resource unless resources
149
-
150
- resources.each do |name, jndi_name|
151
- lookuped_resource[name] = lookup_resource(jndi_name)
152
- end
153
-
154
- lookuped_resource
155
- end
156
-
157
- def lookup_resource(jndi_name)
158
- @jndi_context.lookup(jndi_name)
159
- end
160
- end
161
-
162
- #
163
- # Configuracoes necessarias para que clientes JMS se conetem
164
- # ao provedor de mensageria via contexto JNDI.
165
- #
166
- class Configuration
167
- attr_accessor :jms_client_jar, :jndi_properties,
168
- :enabled_connection_factories, :enabled_queues, :enabled_topics
169
- end
170
-
171
- #
172
- # Erro para quando uma conexao esta num estado invalido para uma operacao (open ou close).
173
- #
174
- class InvalidStateError < StandardError
175
- attr_reader :state, :operation
176
-
177
- def initialize(state, operation)
178
- super("Could not did #{operation} because connection is #{state}.")
179
-
180
- @state = state
181
- @operation = operation
182
- end
183
- end
184
-
185
- #
186
- # Erro para quando nao for possivel estabelecer conexao com o provedor JMS.
187
- #
188
- class InitializationError < StandardError
189
- attr_reader :configuration, :cause
190
-
191
- def initialize(configuration, cause)
192
- super("Could not open connection to JMS provider. Verify the config's config.")
193
-
194
- @configuration = configuration
195
- @cause = cause
196
- end
197
- end
198
- end
199
-
200
- module JNDI
201
-
202
- #
203
- # Builder para construcao de contexto JNDI para conexao com o provedor
204
- # de JMS.
205
- #
206
- class ContextBuilder
207
- attr_accessor :jms_client_jar, :jndi_properties
208
-
209
- def initialize(jms_client_jar, jndi_properties)
210
- @jms_client_jar = jms_client_jar
211
- @jndi_properties = jndi_properties
212
- end
213
-
214
- #
215
- # Constroi um contexto JNDI inicial a partir das configuracoes atuais.
216
- #
217
- def build
218
- # Carrega a biblioteca cliente do servidor de aplicacoes
219
- require @jms_client_jar
220
-
221
- InitialContext.new(to_jndi_environment_hashtable)
222
- end
223
-
224
- # --- Private methods --- #
225
- private
226
-
227
- #
228
- # Cria um Hashtable Java contendo as configuracoes atuais.
229
- #
230
- def to_jndi_environment_hashtable
231
- jndi_env = Hashtable.new
232
-
233
- jndi_env.put(
234
- InitialContext::INITIAL_CONTEXT_FACTORY,
235
- @jndi_properties[:initial_context_factory])
236
-
237
- jndi_env.put(
238
- InitialContext::PROVIDER_URL,
239
- @jndi_properties[:provider_url])
240
-
241
- jndi_env.put(
242
- InitialContext::SECURITY_PRINCIPAL,
243
- @jndi_properties[:security_principal]) if @jndi_properties[:security_principal]
244
-
245
- jndi_env.put(
246
- InitialContext::SECURITY_CREDENTIALS,
247
- @jndi_properties[:security_credentials]) if @jndi_properties[:security_credentials]
248
-
249
- jndi_env
250
- end
251
- end
252
- end
253
- end
4
+ require 'connection/configuration'
5
+ require 'connection/provider'
@@ -0,0 +1,88 @@
1
+ module JSparrow
2
+ module Connection
3
+
4
+ #
5
+ # Connection configuration to connect the JMS provider.
6
+ #
7
+ class Configuration
8
+ attr_accessor :jms_client_jar, :jndi_properties,
9
+ :enabled_connection_factories, :enabled_queues, :enabled_topics
10
+ end
11
+
12
+ #
13
+ # Class methods to configure the connection with the JMS provider.
14
+ #
15
+ class << self
16
+
17
+ def configure(&block)
18
+ @@configuration = Configuration.new
19
+
20
+ class_eval(&block)
21
+
22
+ @@configuration
23
+ end
24
+
25
+ def configuration
26
+ @@configuration
27
+ end
28
+
29
+ #
30
+ # Example:
31
+ #
32
+ # use_jms_client_jar "path/to/name_of_the_client_jar_file.jar"
33
+ #
34
+ def use_jms_client_jar(jms_client_jar)
35
+ configuration.jms_client_jar = jms_client_jar
36
+ end
37
+
38
+ #
39
+ # Example:
40
+ #
41
+ # use_jndi_properties :a_jndi_property_name_in_lower_case => "a_value_of_property",
42
+ # :other_jndi_property_name_in_lower_case => "other_value_of_property"
43
+ #
44
+ def use_jndi_properties(jndi_properties = {})
45
+ configuration.jndi_properties = jndi_properties
46
+ end
47
+
48
+ #
49
+ # Example:
50
+ #
51
+ # enable_connection_factories :queue_connection_factory => "jndi_name_of_queue_connection_factory",
52
+ # :topic_connection_factory => "jndi_name_of_topic_connection_factory"
53
+ #
54
+ def enable_connection_factories(jndi_names = {})
55
+ configuration.enabled_connection_factories = jndi_names
56
+ end
57
+
58
+ #
59
+ # Example:
60
+ #
61
+ # enable_queues :a_queue_name_in_lower_case => "jndi_name_of_a_queue",
62
+ # :other_queue_name_in_lower_case => "jndi_name_of_other_queue"
63
+ #
64
+ def enable_queues(jndi_names = {})
65
+ configuration.enabled_queues = jndi_names
66
+ end
67
+
68
+ #
69
+ # Example:
70
+ #
71
+ # enable_topics :a_topic_name_in_lower_case => "jndi_name_of_a_topic",
72
+ # :other_topic_name_in_lower_case => "jndi_name_of_other_topic"
73
+ #
74
+ def enable_topics(jndi_names = {})
75
+ configuration.enabled_topics = jndi_names
76
+ end
77
+ end
78
+
79
+ #
80
+ # Factory method.
81
+ #
82
+ def self.new
83
+ jndi_context_builder = JNDI::ContextBuilder.new(configuration.jms_client_jar, configuration.jndi_properties)
84
+
85
+ connection = Provider.new(configuration, jndi_context_builder)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,91 @@
1
+ module JSparrow
2
+ module Connection
3
+
4
+ #
5
+ # Class for establish connection with JMS provider throught JNDI.
6
+ #
7
+ class Provider
8
+ attr_reader :configuration
9
+
10
+ def initialize(configuration, jndi_context_builder)
11
+ @configuration = configuration
12
+ @jndi_context_builder = jndi_context_builder
13
+
14
+ # Foi estabelecida?
15
+ @opened = false
16
+ end
17
+
18
+ def is_opened?
19
+ @opened
20
+ end
21
+
22
+ def open
23
+ raise InvalidStateError.new('opened', 'open') if is_opened?
24
+
25
+ begin
26
+ @jndi_context = @jndi_context_builder.build
27
+ rescue => cause
28
+ raise InitializationError.new(@configuration, cause)
29
+ end
30
+
31
+ @opened = true
32
+ end
33
+
34
+ def is_closed?
35
+ not @opened
36
+ end
37
+
38
+ def close
39
+ raise InvalidStateError.new('closed', 'close') if is_closed?
40
+
41
+ @jndi_context.close
42
+
43
+ @opened = false
44
+ end
45
+
46
+ def lookup_resources(resources = {})
47
+ lookuped_resource = {}
48
+
49
+ return lookuped_resource unless resources
50
+
51
+ resources.each do |name, jndi_name|
52
+ lookuped_resource[name] = lookup_resource(jndi_name)
53
+ end
54
+
55
+ lookuped_resource
56
+ end
57
+
58
+ def lookup_resource(jndi_name)
59
+ @jndi_context.lookup(jndi_name)
60
+ end
61
+ end
62
+
63
+ #
64
+ # Error to signal invalid state on open or close operation.
65
+ #
66
+ class InvalidStateError < StandardError
67
+ attr_reader :state, :operation
68
+
69
+ def initialize(state, operation)
70
+ super("Could not did #{operation} because connection is #{state}.")
71
+
72
+ @state = state
73
+ @operation = operation
74
+ end
75
+ end
76
+
77
+ #
78
+ # Error to signal impossibility to connect the JMS provider.
79
+ #
80
+ class InitializationError < StandardError
81
+ attr_reader :configuration, :cause
82
+
83
+ def initialize(configuration, cause)
84
+ super("Could not open connection to JMS provider. Verify the config's config.")
85
+
86
+ @configuration = configuration
87
+ @cause = cause
88
+ end
89
+ end
90
+ end
91
+ end