jmx4r 0.0.3 → 0.0.4

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.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require "rubygems"
6
6
 
7
7
  dir = File.dirname(__FILE__)
8
8
  lib = File.join(dir, "lib", "jmx4r.rb")
9
- version = "0.0.3"
9
+ version = "0.0.4"
10
10
 
11
11
  task :default => [:test]
12
12
 
@@ -17,7 +17,7 @@ Rake::TestTask.new do |test|
17
17
  end
18
18
 
19
19
  Rake::RDocTask.new do |rdoc|
20
- rdoc.rdoc_files.include( "README.txt", "LICENSE.txt",
20
+ rdoc.rdoc_files.include( "README.txt", "LICENSE.txt", "AUTHORS.txt",
21
21
  "lib/" )
22
22
  rdoc.main = "README.txt"
23
23
  rdoc.rdoc_dir = "doc/html"
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env jruby
2
2
  require 'rubygems'
3
3
  require 'jmx4r'
4
4
 
@@ -4,18 +4,18 @@ require 'jmx4r'
4
4
 
5
5
  def display header, memory_usage
6
6
  puts header
7
- memory_usage.composite_type.key_set.each do |type|
8
- puts "\t#{type} : #{memory_usage.get type}"
7
+ memory_usage.sort.each do |key, value|
8
+ puts "\t#{key} : #{value}"
9
9
  end
10
10
  end
11
11
  memory = JMX::MBean.find_by_name "java.lang:type=Memory"
12
12
 
13
13
  display "Heap Memory Usage", memory.heap_memory_usage
14
- display "Non Heap Memory Usage", memory.heap_memory_usage
14
+ display "Non Heap Memory Usage", memory.non_heap_memory_usage
15
15
 
16
16
  if ARGV.length == 1 and ARGV[0] == "gc"
17
17
  puts "trigger a garbage collection"
18
18
  memory.gc
19
19
  display "Heap Memory Usage", memory.heap_memory_usage
20
- display "Non Heap Memory Usage", memory.heap_memory_usage
20
+ display "Non Heap Memory Usage", memory.non_heap_memory_usage
21
21
  end
@@ -25,8 +25,8 @@ def display_memory_usages (ports)
25
25
 
26
26
  ports.each do |port|
27
27
  memory = JMX::MBean.find_by_name "java.lang:type=Memory", :port => port
28
- heap_used = memory.heap_memory_usage.get("used")
29
- non_heap_used = memory.non_heap_memory_usage.get("used")
28
+ heap_used = memory.heap_memory_usage["used"]
29
+ non_heap_used = memory.non_heap_memory_usage["used"]
30
30
  puts "| localhost:#{port} |#{heap_used.to_s.rjust(15)} |#{non_heap_used.to_s.rjust(15)} |"
31
31
  end
32
32
 
@@ -4,6 +4,8 @@
4
4
  # is used as the target remote Java application manageable by JMX
5
5
  # (we do not used it for its JMX capability, just because it is a ready to
6
6
  # use Java application available with every JDK).
7
+ #
8
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
7
9
  module JConsole
8
10
 
9
11
  # Start a new instance of jconsole which is accessible on port 3000.
@@ -15,10 +15,17 @@ class String
15
15
  end
16
16
 
17
17
  module JMX
18
+ require 'open_data_helper'
19
+ require 'jruby'
20
+
18
21
  class MBean
19
- include_package 'javax.management'
20
- include_package 'javax.management.remote'
21
22
  include_class 'java.util.HashMap'
23
+ include_class 'javax.management.Attribute'
24
+ include_class 'javax.management.ObjectName'
25
+ include_class 'javax.management.remote.JMXConnector'
26
+ include_class 'javax.management.remote.JMXConnectorFactory'
27
+ include_class 'javax.management.remote.JMXServiceURL'
28
+ JThread = java.lang.Thread
22
29
 
23
30
  attr_reader :object_name, :operations, :attributes
24
31
 
@@ -100,26 +107,50 @@ module JMX
100
107
  # Create a connection to a remote MBean server.
101
108
  #
102
109
  # The args accepts 4 keys:
103
- # [:host] the host of the MBean server (defaults to "localhost")
104
- # [:port] the port of the MBean server (defaults to 3000)
105
- # [:username] the name of the user (if the MBean server requires authentication).
106
- # No default
107
- # [:password] the password of the user (if the MBean server requires authentication).
108
- # No default
110
+ #
111
+ # [:host] the host of the MBean server (defaults to "localhost")
112
+ # [:port] the port of the MBean server (defaults to 3000)
113
+ # [:username] the name of the user (if the MBean server requires authentication).
114
+ # No default
115
+ # [:password] the password of the user (if the MBean server requires authentication).
116
+ # No default
117
+ # [:credentials] custom credentials (if the MBean server requires authentication).
118
+ # No default. It has precedence over :username and :password (i.e. if
119
+ # :credentials is specified, :username & :password are ignored)
120
+ #
109
121
  def self.create_connection(args={})
110
122
  host= args[:host] || "localhost"
111
123
  port = args[:port] || 3000
112
124
  username = args[:username]
113
125
  password = args[:password]
126
+ credentials = args[:credentials]
114
127
 
115
128
  url = "service:jmx:rmi:///jndi/rmi://#{host}:#{port}/jmxrmi"
129
+
130
+ unless credentials
131
+ if !username.nil? and username.length > 0
132
+ user_password_credentials = [username, password]
133
+ credentials = user_password_credentials.to_java(:String)
134
+ end
135
+ end
136
+
116
137
  env = HashMap.new
117
- if !username.nil? and username.length > 0
118
- credentials = [username, password]
119
- env.put JMXConnector::CREDENTIALS, credentials.to_java(:String)
138
+ env.put(JMXConnector::CREDENTIALS, credentials) if credentials
139
+
140
+ # the context class loader is set to JRuby's classloader when
141
+ # creating the JMX Connection so that classes loaded using
142
+ # JRuby "require" (and not from its classpath) can also be
143
+ # accessed (see issue #6)
144
+ begin
145
+ context_class_loader = JThread.current_thread.context_class_loader
146
+ JThread.current_thread.context_class_loader = JRuby.runtime.getJRubyClassLoader
147
+
148
+ connector = JMXConnectorFactory::connect JMXServiceURL.new(url), env
149
+ connector.getMBeanServerConnection
150
+ ensure
151
+ # ... and we reset the previous context class loader
152
+ JThread.current_thread.context_class_loader = context_class_loader
120
153
  end
121
- connector = JMXConnectorFactory::connect JMXServiceURL.new(url), env
122
- connector.getMBeanServerConnection
123
154
  end
124
155
 
125
156
  # Returns an array of MBeans corresponding to all the MBeans
@@ -128,7 +159,11 @@ module JMX
128
159
  #
129
160
  # The args accepts the same keys than #create_connection and an
130
161
  # additional one:
162
+ #
131
163
  # [:connection] a MBean server connection (as returned by #create_connection)
164
+ # No default. It has precedence over :host and :port (i.e if
165
+ # :connection is specified, :host and :port are ignored)
166
+ #
132
167
  def self.find_all_by_name(name, args={})
133
168
  object_name = ObjectName.new(name)
134
169
  mbsc = args[:connection] || MBean.connection(args)
@@ -0,0 +1,30 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+ #
3
+ # This file adds methods to CompositeData proxies so that they can behave like
4
+ # regular (read-only) Ruby Hash
5
+ require 'java'
6
+
7
+ JavaUtilities.extend_proxy('javax.management.openmbean.CompositeDataSupport') do
8
+ include Enumerable
9
+ def each
10
+ self.get_composite_type.key_set.each do |k|
11
+ yield(k,self.get(k))
12
+ end
13
+ self
14
+ end
15
+
16
+ def key?(k)
17
+ self.contains_key k
18
+ end
19
+ alias has_key? key?
20
+ alias include? key?
21
+ alias member? key?
22
+
23
+ def keys
24
+ self.get_composite_type.key_set
25
+ end
26
+
27
+ def [](key)
28
+ self.get key
29
+ end
30
+ end
@@ -1,3 +1,5 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
1
3
  require "test/unit"
2
4
 
3
5
  require "jmx4r"
@@ -1,3 +1,5 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
1
3
  require "test/unit"
2
4
 
3
5
  require "jmx4r"
@@ -39,6 +41,13 @@ class TestAuthentication < Test::Unit::TestCase
39
41
  def test_establish_auth_connection_with_correct_credentials
40
42
  JMX::MBean.establish_connection :username => @username, :password => @password
41
43
  end
44
+
45
+ # test that using the :credentials key to pass the username/password
46
+ # credentials is working the same way than passing :username/:password
47
+ def test_establish_auth_connection_with_custom_credentials
48
+ credentials = [@username, @password].to_java(:String)
49
+ JMX::MBean.establish_connection :credentials => credentials
50
+ end
42
51
 
43
52
  def test_establish_auth_connection_with_invalid_username
44
53
  assert_raise(NativeException) {
@@ -0,0 +1,57 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
3
+ require "test/unit"
4
+
5
+ require "jmx4r"
6
+ require "jconsole"
7
+
8
+ class TestCompositeData < Test::Unit::TestCase
9
+ def setup
10
+ JConsole::start
11
+ memory = JMX::MBean.find_by_name "java.lang:type=Memory"
12
+ # heap_memory_usage is a CompositeData
13
+ @heap = memory.heap_memory_usage
14
+ end
15
+
16
+ def teardown
17
+ @heap = nil
18
+ JMX::MBean.remove_connection
19
+ JConsole::stop
20
+ end
21
+
22
+ # use #map to check that CompositeData includes Enumerable
23
+ def test_enumerable_composite_data
24
+ expected_headers = ["init", "committed", "used", "max"].sort
25
+ headers = @heap.map { |k, v| k }.sort
26
+ assert_equal expected_headers, headers
27
+ end
28
+
29
+ def test_composite_data_keys
30
+ expected_headers = ["init", "committed", "used", "max"].sort
31
+ headers = @heap.keys.sort
32
+ assert_equal expected_headers, headers
33
+ end
34
+
35
+ def test_composite_data_key_aliases
36
+ assert @heap.key?("used")
37
+ assert @heap.has_key?("used")
38
+ assert @heap.include?("used")
39
+ assert @heap.member?("used")
40
+ end
41
+
42
+ def test_composite_data_as_hash
43
+ used = @heap.get "used"
44
+ used_from_hash = @heap["used"]
45
+ assert_equal used, used_from_hash
46
+ end
47
+
48
+ def test_composite_data_as_hash_with_known_key
49
+ assert_equal true, @heap.key?("used")
50
+ used = @heap["used"]
51
+ end
52
+
53
+ def test_composite_data_as_hash_with_unknown_key
54
+ assert_equal false, @heap.key?("unknown")
55
+ assert_raise(NativeException) { @heap["unknown"] }
56
+ end
57
+ end
@@ -1,3 +1,5 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
1
3
  require "test/unit"
2
4
 
3
5
  require "jmx4r"
@@ -1,3 +1,5 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
1
3
  require "test/unit"
2
4
 
3
5
  require "jmx4r"
@@ -1,6 +1,9 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
1
3
  require "test/unit"
2
4
 
3
5
  require "tc_connection"
4
6
  require "tc_auth"
5
7
  require "tc_multiple_connections"
6
8
  require "tc_attributes"
9
+ require "tc_composite_data"
metadata CHANGED
@@ -3,9 +3,9 @@ homepage: http://code.google.com/p/jmx4r/
3
3
  extensions: []
4
4
  executables: []
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
6
+ version: 0.0.4
7
7
  post_install_message:
8
- date: 2007-07-02 22:00:00 +00:00
8
+ date: 2007-07-26 22:00:00 +00:00
9
9
  files:
10
10
  - examples/class_loading.rb
11
11
  - examples/jvm_mngmt.rb
@@ -14,8 +14,10 @@ files:
14
14
  - examples/memory_on_many_nodes.rb
15
15
  - lib/jconsole.rb
16
16
  - lib/jmx4r.rb
17
+ - lib/open_data_helper.rb
17
18
  - test/tc_attributes.rb
18
19
  - test/tc_auth.rb
20
+ - test/tc_composite_data.rb
19
21
  - test/tc_connection.rb
20
22
  - test/tc_multiple_connections.rb
21
23
  - test/ts_all.rb