torquebox-core 2.3.2-java → 3.0.0.beta1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # This is free software; you can redistribute it and/or modify it
4
+ # under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation; either version 2.1 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # This software is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this software; if not, write to the Free
15
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
+
18
+ require 'edn'
19
+
20
+ module TorqueBox
21
+ module Codecs
22
+ module EDN
23
+ class << self
24
+
25
+ def encode(data)
26
+ data.to_edn
27
+ end
28
+
29
+ def decode(data)
30
+ ::EDN.read(data) unless data.nil?
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,61 @@
1
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # This is free software; you can redistribute it and/or modify it
4
+ # under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation; either version 2.1 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # This software is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this software; if not, write to the Free
15
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
+
18
+
19
+ module TorqueBox
20
+ module Codecs
21
+ module JSON
22
+ class << self
23
+
24
+ # @api private
25
+ def require_json
26
+ # We can't ship our own json, as it may collide with the gem
27
+ # requirement for the app.
28
+ if !defined?( ::JSON )
29
+ begin
30
+ require 'json'
31
+ rescue LoadError => ex
32
+ raise RuntimeError.new( "Unable to load the json gem. Verify that is installed and in your Gemfile (if using Bundler)" )
33
+ end
34
+ end
35
+ end
36
+
37
+ def encode(data)
38
+ require_json
39
+ begin
40
+ if ( data.respond_to?( :as_json ) )
41
+ data = data.as_json
42
+ end
43
+ ::JSON.fast_generate( data ) unless data.nil?
44
+ rescue ::JSON::GeneratorError
45
+ ::JSON.dump(data)
46
+ end
47
+ end
48
+
49
+ def decode(data)
50
+ require_json
51
+ begin
52
+ ::JSON.parse( data, :symbolize_names => true ) unless data.nil?
53
+ rescue ::JSON::ParserError
54
+ ::JSON.load(data)
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # This is free software; you can redistribute it and/or modify it
4
+ # under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation; either version 2.1 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # This software is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this software; if not, write to the Free
15
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
+
18
+
19
+ module TorqueBox
20
+ module Codecs
21
+ module Marshal
22
+ class << self
23
+
24
+ def encode(data)
25
+ ::Marshal.dump(data) unless data.nil?
26
+ end
27
+
28
+ def decode(data)
29
+ ::Marshal.restore(data) unless data.nil?
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # This is free software; you can redistribute it and/or modify it
4
+ # under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation; either version 2.1 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # This software is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this software; if not, write to the Free
15
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
+
18
+ require 'base64'
19
+
20
+ module TorqueBox
21
+ module Codecs
22
+ module MarshalBase64
23
+ class << self
24
+
25
+ def encode(data)
26
+ Base64.encode64(::Marshal.dump(data)) unless data.nil?
27
+ end
28
+
29
+ def decode(data)
30
+ ::Marshal.restore(Base64.decode64(data)) unless data.nil?
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # This is free software; you can redistribute it and/or modify it
4
+ # under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation; either version 2.1 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # This software is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this software; if not, write to the Free
15
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
+
18
+ require 'base64'
19
+
20
+ module TorqueBox
21
+ module Codecs
22
+ module MarshalSmart
23
+ class << self
24
+
25
+ # @api private
26
+ MARSHAL_MARKER = "_|marshalled|_"
27
+
28
+ def encode(object)
29
+ case object
30
+ when String, Numeric, true, false, nil
31
+ object
32
+ else
33
+ if object.respond_to?(:java_object)
34
+ object
35
+ else
36
+ MARSHAL_MARKER + Base64.encode64(::Marshal.dump(object))
37
+ end
38
+ end
39
+ end
40
+
41
+ def decode(object)
42
+ if object.is_a?(String) && object.start_with?(MARSHAL_MARKER)
43
+ object = ::Marshal.load(Base64.decode64(object.sub(MARSHAL_MARKER, '')))
44
+ end
45
+ object
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,65 @@
1
+ # Copyright 2008-2013 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # This is free software; you can redistribute it and/or modify it
4
+ # under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation; either version 2.1 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # This software is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this software; if not, write to the Free
15
+ # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
+ # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
+
18
+ # These codecs don't depend on anything outside Ruby stdlib
19
+ require 'torquebox/codecs/marshal'
20
+ require 'torquebox/codecs/marshal_base64'
21
+ require 'torquebox/codecs/marshal_smart'
22
+
23
+ # These codecs depend on external gems - attempt to load them
24
+ # but ignore any load errors and we'll lazily try again later
25
+ require 'torquebox/codecs/json' rescue nil
26
+ require 'torquebox/codecs/edn' rescue nil
27
+
28
+ module TorqueBox
29
+ module Codecs
30
+ class << self
31
+
32
+ def [](key)
33
+ case key
34
+ when :edn
35
+ # This is only so any issues requiring the edn codec bubble
36
+ # up when it gets used
37
+ require 'torquebox/codecs/edn' unless defined?(TorqueBox::Codecs::EDN)
38
+ TorqueBox::Codecs::EDN
39
+ when :json
40
+ # This is only so any issues requiring the json codec bubble
41
+ # up when it gets used
42
+ require 'torquebox/codecs/json' unless defined?(TorqueBox::Codecs::JSON)
43
+ TorqueBox::Codecs::JSON
44
+ when :marshal
45
+ TorqueBox::Codecs::Marshal
46
+ when :marshal_base64
47
+ MarshalBase64
48
+ when :marshal_smart
49
+ MarshalSmart
50
+ else
51
+ raise "Unsupported codec #{key}"
52
+ end
53
+ end
54
+
55
+ def encode(data, encoding)
56
+ self[encoding].encode(data)
57
+ end
58
+
59
+ def decode(data, encoding)
60
+ self[encoding].decode(data)
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -15,26 +15,47 @@
15
15
  # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
16
  # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
17
 
18
+ require 'torquebox/msc'
18
19
  require 'torquebox/registry'
20
+ require 'torquebox/service_registry'
19
21
 
20
22
  module TorqueBox
21
- module Injectors
22
23
 
23
- def self.analyze_and_inject(&block)
24
- fetch( 'runtime-injection-analyzer' ).analyze_and_inject( block )
24
+ class InjectionError < StandardError
25
+ end
26
+
27
+ def self.fetch(something)
28
+ unless TorqueBox::Registry.has_key?(something.to_s)
29
+ handler_registry = TorqueBox::ServiceRegistry['torquebox.core.injection.injectable-handler-registry']
30
+ # handler_registry should only be nil when running outside of
31
+ # TorqueBox so we just return the nil value and skip everything
32
+ # else to facilitate testing outside the container
33
+ return nil if handler_registry.nil?
34
+ handler = handler_registry.get_handler(something)
35
+ raise InjectionError.new("Invalid injection - #{something}") if handler.nil?
36
+ injectable = handler.handle(something, true)
37
+ service_name = injectable.get_service_name(TorqueBox::Registry['service-target'],
38
+ TorqueBox::Registry['deployment-unit'])
39
+ service = TorqueBox::ServiceRegistry.registry.getService(service_name)
40
+ raise InjectionError.new("Service not found for injection - #{something}") if service.nil?
41
+ state = TorqueBox::MSC.wait_for_service_to_start(service, 45)
42
+ raise InjectionError.new("Injected service failed to start - #{service_name}") if state != 'UP'
43
+ value = service.value
44
+ raise InjectionError.new("Injected service had no value - #{service_name}") if value.nil?
45
+ value = value.convert(JRuby.runtime) if value.respond_to?(:convert)
46
+ TorqueBox::Registry.merge!(something.to_s => value)
25
47
  end
48
+ TorqueBox::Registry[something.to_s]
49
+ end
50
+
51
+ module Injectors
26
52
 
27
53
  def fetch(something)
28
- TorqueBox::Registry[something.to_s]
54
+ TorqueBox.fetch(something)
29
55
  end
30
- alias_method :inject, :fetch
31
56
  alias_method :__inject__, :fetch
32
-
33
- %w{ msc service cdi jndi queue topic }.each do |type|
34
- define_method("inject_#{type}".to_sym) do |key|
35
- fetch(key)
36
- end
37
57
 
58
+ %w{ msc service cdi jndi queue topic }.each do |type|
38
59
  define_method("fetch_#{type}".to_sym) do |key|
39
60
  fetch(key)
40
61
  end
@@ -42,3 +63,13 @@ module TorqueBox
42
63
 
43
64
  end
44
65
  end
66
+
67
+ # Temporarily workaround TorqueSpec needing an inject(...) method
68
+ # @api private
69
+ module TorqueSpec
70
+ class Daemon
71
+ def inject(something)
72
+ TorqueBox.fetch(something)
73
+ end
74
+ end
75
+ end
@@ -18,6 +18,7 @@
18
18
  require 'torquebox/service_registry'
19
19
 
20
20
  module TorqueBox
21
+ # @api private
21
22
  class Kernel
22
23
 
23
24
  def self.kernel=(kernel)
@@ -19,6 +19,7 @@ require 'logger'
19
19
 
20
20
  module TorqueBox
21
21
 
22
+ # @api private
22
23
  class FallbackLogger < ::Logger
23
24
 
24
25
  def initialize name = nil
@@ -54,6 +55,8 @@ module TorqueBox
54
55
  define_method(method) { true }
55
56
  end
56
57
 
58
+ # The minimum log level to actually log, with debug being the lowest
59
+ # and fatal the highest
57
60
  attr_accessor :level
58
61
 
59
62
  def add(severity, message, progname, &block)
@@ -65,6 +68,16 @@ module TorqueBox
65
68
  @logger.send(delegate, *params)
66
69
  end
67
70
 
71
+ # @!method debug(message)
72
+ # @param [String] message The message to log
73
+ # @!method info(message)
74
+ # @param [String] message The message to log
75
+ # @!method warn(message)
76
+ # @param [String] message The message to log
77
+ # @!method error(message)
78
+ # @param [String] message The message to log
79
+ # @!method fatal(message)
80
+ # @param [String] message The message to log
68
81
  def method_missing(method, *args, &block)
69
82
  delegate = method
70
83
  is_boolean = false
data/lib/torquebox/msc.rb CHANGED
@@ -15,19 +15,18 @@
15
15
  # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
16
  # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
17
 
18
- require 'torquebox/injectors'
18
+ require 'java'
19
19
 
20
20
  module TorqueBox
21
21
  class MSC
22
22
  class << self
23
- include TorqueBox::Injectors
24
23
 
25
24
  def service_registry
26
- fetch('service-registry')
25
+ TorqueBox::Registry['service-registry']
27
26
  end
28
27
 
29
28
  def deployment_unit
30
- fetch('deployment-unit')
29
+ TorqueBox::Registry['deployment-unit']
31
30
  end
32
31
 
33
32
  def service_names
@@ -37,6 +36,63 @@ module TorqueBox
37
36
  def get_service(service_name)
38
37
  service_registry.get_service(service_name)
39
38
  end
39
+
40
+ # Returns (or yields over the items) of list of
41
+ # services with (canonical) name matching provided
42
+ # regular expression.
43
+ #
44
+ # @param [Regexp] Regular expression to match the name
45
+ #
46
+ # @param Optional block. If block is provided it'll
47
+ # iterate over the values. If there is no block
48
+ # given this method returns list of services
49
+ def get_services(regexp, &block)
50
+ services = []
51
+
52
+ TorqueBox::MSC.service_names.each do |name|
53
+ if name.canonical_name =~ regexp
54
+ service = TorqueBox::MSC.get_service(name)
55
+ services << service unless service.nil?
56
+ end
57
+ end
58
+
59
+ if block
60
+ services.each do |s|
61
+ yield s
62
+ end
63
+ else
64
+ services
65
+ end
66
+ end
67
+
68
+ def java_web_context
69
+ war_meta_data = deployment_unit.get_attachment(org.jboss.as.web.deployment.WarMetaData::ATTACHMENT_KEY)
70
+ return nil if war_meta_data.nil? # no web component in this application
71
+ jboss_web_meta_data = war_meta_data.getMergedJBossWebMetaData
72
+ virtual_host = jboss_web_meta_data.virtual_hosts.first || 'default-host'
73
+ context_path = jboss_web_meta_data.context_root
74
+ context_path = "/#{context_path}" unless context_path.start_with?('/')
75
+ deployment_service_name = org.jboss.msc.service.ServiceName.parse("jboss.web.deployment")
76
+ service_name = deployment_service_name.append(virtual_host).append(context_path)
77
+ get_service(service_name).value
78
+ end
79
+
80
+ # Wait for the given MSC service to start.
81
+ #
82
+ # @param [org.jboss.msc.service.Service] MSC service to wait on
83
+ #
84
+ # @return String the service state after waiting - one of DOWN,
85
+ # STARTING, START_FAILED, UP, STOPPING, or REMOVED. This should
86
+ # be UP unless something went wrong.
87
+ def wait_for_service_to_start(service, seconds_to_wait)
88
+ unless service.state.to_s == 'UP'
89
+ listener = org.torquebox.core.gem.MSCServiceListener.new(service)
90
+ service.add_listener(listener)
91
+ service.set_mode(org.jboss.msc.service.ServiceController::Mode::ACTIVE)
92
+ listener.wait_for_start_or_failure(seconds_to_wait)
93
+ end
94
+ service.state.to_s
95
+ end
40
96
  end
41
97
  end
42
98
  end
@@ -19,8 +19,9 @@ require 'thread'
19
19
 
20
20
  module TorqueBox
21
21
  class Registry
22
+ # @api private
22
23
  MUTEX = Mutex.new
23
-
24
+
24
25
  class << self
25
26
  def merge!(hash)
26
27
  MUTEX.synchronize do
@@ -33,7 +34,13 @@ module TorqueBox
33
34
  MUTEX.synchronize do
34
35
  value = registry[key]
35
36
  end
36
- return value
37
+ value
38
+ end
39
+
40
+ def has_key?(key)
41
+ MUTEX.synchronize do
42
+ registry.has_key?(key)
43
+ end
37
44
  end
38
45
 
39
46
  def registry