weblogic-jmx4r 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module JMX
2
+ VERSION = '0.1.9'
3
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright 2008 Jeff Mesnil (http://jmesnil.net)
2
+ #
3
+ # This file adds methods to ObjectName proxies
4
+ require 'java'
5
+
6
+ JavaUtilities.extend_proxy('javax.management.ObjectName') do
7
+ def key?(k)
8
+ self.contains_key k
9
+ end
10
+ alias has_key? key?
11
+ alias include? key?
12
+ alias member? key?
13
+
14
+ def keys
15
+ self.get_key_property_list.key_set
16
+ end
17
+
18
+ def [](key)
19
+ self.get_key_property key
20
+ end
21
+ end
@@ -0,0 +1,50 @@
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.CompositeData') 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
+
31
+ def method_missing(name, *args)
32
+ key = name.to_s.camel_case
33
+ super unless containsKey(key)
34
+ get(name.to_s.camel_case)
35
+ end
36
+
37
+ def respond_to?(symbol, include_private = false)
38
+ containsKey(symbol.to_s.camel_case) || super
39
+ end
40
+ end
41
+
42
+ JavaUtilities.extend_proxy('javax.management.openmbean.TabularData') do
43
+ include Enumerable
44
+ def each
45
+ self.values.each do |value|
46
+ yield value
47
+ end
48
+ self
49
+ end
50
+ end
@@ -0,0 +1,44 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
3
+ require "test/unit"
4
+
5
+ require "jmx4r"
6
+ require "jconsole"
7
+
8
+ class TestAttribute < Test::Unit::TestCase
9
+ import java.lang.management.ManagementFactory
10
+
11
+ def setup
12
+ @memory = JMX::MBean.find_by_name "java.lang:type=Memory", :connection => ManagementFactory.platform_mbean_server
13
+ end
14
+
15
+ def teardown
16
+ JMX::MBean.remove_connection
17
+ end
18
+
19
+ def test_unknwown_attribute
20
+ assert_raise(NoMethodError) { @memory.unknown_attribute }
21
+ end
22
+
23
+ def test_readable_attribute
24
+ assert_equal false, @memory.verbose
25
+ end
26
+
27
+ def test_writable_attribute
28
+ assert_equal false, @memory.verbose
29
+ @memory.verbose = true
30
+ assert_equal true, @memory.verbose
31
+ @memory.verbose = false
32
+ end
33
+
34
+ def test_non_writable_attribute
35
+ assert_raise(NoMethodError) { @memory.object_pending_finalization_count = -1 }
36
+ end
37
+
38
+ def test_non_overlapping_attributes
39
+ assert_raise(NoMethodError) { @memory.logger_names }
40
+ logging = JMX::MBean.find_by_name "java.util.logging:type=Logging", :connection => ManagementFactory.platform_mbean_server
41
+ assert_raise(NoMethodError) { logging.verbose }
42
+ assert_raise(NoMethodError) { @memory.logger_names }
43
+ end
44
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
3
+ require "test/unit"
4
+
5
+ require "jmx4r"
6
+ require "jconsole"
7
+
8
+ class TestAuthentication < Test::Unit::TestCase
9
+ import java.lang.SecurityException
10
+
11
+ def setup
12
+ @username = "jmx4r.user"
13
+ @password = "jmx4r.password"
14
+
15
+ pwd_path = "/tmp/jmx4r.password"
16
+ @pwd_file = File.new(pwd_path, "w")
17
+ @pwd_file.puts "#{@username} #{@password}"
18
+ @pwd_file.close
19
+
20
+ # pwd file must be readable only by user
21
+ # but somehow File.chmod is not working
22
+ # with JRuby
23
+ `chmod 0600 #{@pwd_file.path}`
24
+
25
+ access_path = "/tmp/jmx4r.access"
26
+ @access_file = File.new(access_path, "w")
27
+ @access_file.puts "#{@username} readwrite"
28
+ @access_file.close
29
+ # access file must be readable only by user
30
+ `chmod 0600 #{@access_file.path}`
31
+
32
+ JConsole::start :pwd_file => @pwd_file.path, :access_file => @access_file.path
33
+ end
34
+
35
+ def teardown
36
+ JMX::MBean.remove_connection
37
+ JConsole::stop
38
+ File.delete @pwd_file.path if File.file? @pwd_file.path
39
+ File.delete @access_file.path if File.file? @access_file.path
40
+ end
41
+
42
+ def test_establish_auth_connection_with_correct_credentials
43
+ JMX::MBean.establish_connection :username => @username, :password => @password
44
+ end
45
+
46
+ # test that using the :credentials key to pass the username/password
47
+ # credentials is working the same way than passing :username/:password
48
+ def test_establish_auth_connection_with_custom_credentials
49
+ credentials = [@username, @password].to_java(:String)
50
+ JMX::MBean.establish_connection :credentials => credentials
51
+ end
52
+
53
+ def test_establish_auth_connection_with_invalid_username
54
+ assert_raise(SecurityException) {
55
+ JMX::MBean.establish_connection :username => "invalid user name", :password => @password
56
+ }
57
+ end
58
+
59
+ def test_establish_auth_connection_with_invalid_password
60
+ assert_raise(SecurityException) {
61
+ JMX::MBean.establish_connection :username => @username, :password => "invalid password"
62
+ }
63
+ end
64
+
65
+ def test_establish_auth_connection_with_no_credentials
66
+ assert_raise(SecurityException) {
67
+ JMX::MBean.establish_connection
68
+ }
69
+ end
70
+ end
@@ -0,0 +1,77 @@
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
+ import java.lang.management.ManagementFactory
10
+ import javax.management.openmbean.InvalidKeyException
11
+
12
+ def setup
13
+ memory = JMX::MBean.find_by_name "java.lang:type=Memory", :connection => ManagementFactory.platform_mbean_server
14
+ # heap_memory_usage is a CompositeData
15
+ @heap = memory.heap_memory_usage
16
+ end
17
+
18
+ def teardown
19
+ @heap = nil
20
+ JMX::MBean.remove_connection
21
+ end
22
+
23
+ # use #map to check that CompositeData includes Enumerable
24
+ def test_enumerable_composite_data
25
+ expected_headers = ["init", "committed", "used", "max"].sort
26
+ headers = @heap.map { |k, v| k }.sort
27
+ assert_equal expected_headers, headers
28
+ end
29
+
30
+ def test_composite_data_keys
31
+ expected_headers = ["init", "committed", "used", "max"].sort
32
+ headers = @heap.keys.sort
33
+ assert_equal expected_headers, headers
34
+ end
35
+
36
+ def test_composite_data_key_aliases
37
+ assert @heap.key?("used")
38
+ assert @heap.has_key?("used")
39
+ assert @heap.include?("used")
40
+ assert @heap.member?("used")
41
+ end
42
+
43
+ def test_composite_data_method_missing
44
+ assert @heap.used
45
+
46
+ def @heap.containsKey(key)
47
+ "camelCaseAttributeName" == key
48
+ end
49
+
50
+ def @heap.get(key)
51
+ return "value" if "camelCaseAttributeName" == key
52
+ raise("should not happen")
53
+ end
54
+
55
+ assert_equal "value", @heap.camel_case_attribute_name
56
+
57
+ assert_raises NoMethodError do
58
+ @heap.unknown_attribute
59
+ end
60
+ end
61
+
62
+ def test_composite_data_as_hash
63
+ used = @heap.get "used"
64
+ used_from_hash = @heap["used"]
65
+ assert_equal used, used_from_hash
66
+ end
67
+
68
+ def test_composite_data_as_hash_with_known_key
69
+ assert_equal true, @heap.key?("used")
70
+ used = @heap["used"]
71
+ end
72
+
73
+ def test_composite_data_as_hash_with_unknown_key
74
+ assert_equal false, @heap.key?("unknown")
75
+ assert_raise(InvalidKeyException) { @heap["unknown"] }
76
+ end
77
+ end
@@ -0,0 +1,92 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
2
+
3
+ require "test/unit"
4
+
5
+ require "jmx4r"
6
+ require "jconsole"
7
+
8
+ class TestConnection < Test::Unit::TestCase
9
+ import java.io.IOException
10
+
11
+ def teardown
12
+ JMX::MBean.remove_connection
13
+ end
14
+
15
+ def test_establish_connection_with_bad_port
16
+ assert_raise(IOException) {
17
+ JMX::MBean.establish_connection :port => 9999
18
+ }
19
+ end
20
+
21
+ def test_establish_connection_with_bad_host
22
+ assert_raise(IOException) {
23
+ JMX::MBean.establish_connection :host => "not a valid host"
24
+ }
25
+ end
26
+
27
+ def test_establish_connection
28
+ begin
29
+ JConsole::start
30
+ connection = JMX::MBean.establish_connection
31
+ assert(connection.getMBeanCount > 0)
32
+ ensure
33
+ JConsole::stop
34
+ end
35
+ end
36
+
37
+ def test_remove_connection
38
+ begin
39
+ JConsole::start
40
+ connection = JMX::MBean.establish_connection
41
+ JMX::MBean.remove_connection
42
+ assert_raise(IOException) {
43
+ connection.getMBeanCount
44
+ }
45
+ ensure
46
+ JConsole::stop
47
+ end
48
+ end
49
+
50
+ def test_establish_connection_with_custom_port
51
+ begin
52
+ JConsole::start :port => 3001
53
+ JMX::MBean.establish_connection :port => 3001
54
+ ensure
55
+ JConsole::stop 3001
56
+ end
57
+ end
58
+
59
+ def test_establish_connection_with_custom_url
60
+ begin
61
+ JConsole::start :port => 3001
62
+ url = "service:jmx:rmi:///jndi/rmi://localhost:3001/jmxrmi"
63
+ JMX::MBean.establish_connection :url => url
64
+ ensure
65
+ JConsole::stop 3001
66
+ end
67
+ end
68
+
69
+ def test_establish_connection_with_custom_url_overrides_host_and_port
70
+ begin
71
+ JConsole::start :port => 3001
72
+ good_url = "service:jmx:rmi:///jndi/rmi://localhost:3001/jmxrmi"
73
+ bad_port = 3000
74
+ # specifying a :url discards the :port & :host parameters
75
+ JMX::MBean.establish_connection :port => bad_port, :url => good_url
76
+ ensure
77
+ JConsole::stop 3001
78
+ end
79
+ end
80
+
81
+ def test_establish_connection_local
82
+ begin
83
+ JConsole::start :port => 0
84
+ connection = JMX::MBean.establish_connection \
85
+ :command => /jconsole/i
86
+ assert(connection.getMBeanCount > 0)
87
+ ensure
88
+ JConsole::stop 0
89
+ end
90
+ end
91
+
92
+ end
@@ -0,0 +1,157 @@
1
+ # Copyright 2008 Jeff Mesnil (http://jmesnil.net)
2
+
3
+ require "test/unit"
4
+ require "jmx4r"
5
+
6
+
7
+ class TestDynamicMBean < Test::Unit::TestCase
8
+
9
+ import java.lang.management.ManagementFactory
10
+ import javax.management.ObjectName
11
+ import javax.management.Attribute
12
+
13
+ class AttributeTypesMBean < JMX::DynamicMBean
14
+ rw_attribute :string_attr, :string, "a String attribute"
15
+ rw_attribute :byte_attr, :byte, "a Byte attribute"
16
+ rw_attribute :int_attr, :int, "a Integer attribute"
17
+ rw_attribute :long_attr, :long, "a Long attribute"
18
+ rw_attribute :float_attr, :float, "a Float attribute"
19
+ rw_attribute :double_attr, :double, "a Double attribute"
20
+ rw_attribute :list_attr, :list, "a List attribute"
21
+ rw_attribute :map_attr, :map, "a Map attribute"
22
+ rw_attribute :set_attr, :set, "a Set attribute"
23
+ rw_attribute :boolean_attr, :boolean, "a Boolean attribute"
24
+ end
25
+
26
+ def test_attribute_types
27
+ mbean = AttributeTypesMBean.new
28
+ object_name = ObjectName.new "jmx4r:name=AttributeTypesMBean"
29
+ mbean_server = ManagementFactory.platform_mbean_server
30
+ mbean_server.register_mbean mbean, object_name
31
+
32
+ mbean = JMX::MBean.find_by_name object_name.to_s, :connection => mbean_server
33
+ mbean.string_attr = "test"
34
+ assert_equal("test", mbean.string_attr)
35
+
36
+ mbean.byte_attr = 9
37
+ assert_equal(9, mbean.byte_attr)
38
+
39
+ mbean.int_attr = 23
40
+ assert_equal(23, mbean.int_attr)
41
+
42
+ mbean.long_attr = 33
43
+ assert_equal(33, mbean.long_attr)
44
+
45
+ mbean.float_attr = 91.0
46
+ assert_equal(91.0, mbean.float_attr)
47
+
48
+ mbean.float_attr = 7.0
49
+ assert_equal(7.0, mbean.float_attr)
50
+
51
+ mbean.list_attr = [1, 2, 3]
52
+ assert_equal([1, 2, 3], mbean.list_attr.to_a)
53
+
54
+ mbean.set_attr = [1, 2, 3]
55
+ assert_equal([1, 2, 3].sort, mbean.list_attr.to_a.sort)
56
+
57
+ mbean.map_attr = { "a" => 1, "b" => 2}
58
+ assert_equal({ "a" => 1, "b" => 2}.to_a.sort, mbean.map_attr.to_a.sort)
59
+
60
+ mbean.boolean_attr = true
61
+ assert_equal(true, mbean.boolean_attr)
62
+
63
+ mbean_server.unregister_mbean object_name
64
+ end
65
+
66
+ class OperationInvocationMBean < JMX::DynamicMBean
67
+ operation "reverse the string passed in parameter"
68
+ parameter :string, "arg", "a String to reverse"
69
+ returns :string
70
+ def reverse(arg)
71
+ arg.reverse
72
+ end
73
+ end
74
+
75
+ def test_operation_invocation
76
+ mbean = OperationInvocationMBean.new
77
+ object_name = ObjectName.new "jmx4r:name=OperationInvocationMBean"
78
+ mbean_server = ManagementFactory.platform_mbean_server
79
+ mbean_server.register_mbean mbean, object_name
80
+
81
+ mbean = JMX::MBean.find_by_name object_name.to_s, :connection => mbean_server
82
+ assert_equal("oof", mbean.reverse("foo"))
83
+
84
+ mbean_server.unregister_mbean object_name
85
+ end
86
+
87
+ class Foo < JMX::DynamicMBean
88
+ rw_attribute :foo_attr, :string
89
+
90
+ operation
91
+ parameter :string
92
+ returns :string
93
+ def foo(arg)
94
+ "foo #{arg}"
95
+ end
96
+ end
97
+
98
+ class Bar < JMX::DynamicMBean
99
+ rw_attribute :bar_attr, :string
100
+
101
+ operation
102
+ parameter :string
103
+ returns :string
104
+ def bar(arg)
105
+ "bar #{arg}"
106
+ end
107
+ end
108
+
109
+ def test_separate_dynamic_beans_have_separate_operations_and_attributes
110
+ mbean_server = ManagementFactory.platform_mbean_server
111
+ foo_object_name = ObjectName.new "jmx4r:name=foo"
112
+ bar_object_name = ObjectName.new "jmx4r:name=bar"
113
+ mbean_server.register_mbean Foo.new, foo_object_name
114
+ mbean_server.register_mbean Bar.new, bar_object_name
115
+
116
+ foo_mbean = JMX::MBean.find_by_name foo_object_name.to_s, :connection => mbean_server
117
+ assert_equal "foo test", foo_mbean.foo("test")
118
+ assert_raise(NoMethodError){
119
+ foo_mbean.bar("test")
120
+ }
121
+ foo_mbean.foo_attr = "test"
122
+ assert_equal "test", foo_mbean.foo_attr
123
+ assert_raise(NoMethodError){
124
+ foo_mbean.bar_attr = "test"
125
+ }
126
+ bar_mbean = JMX::MBean.find_by_name bar_object_name.to_s, :connection => mbean_server
127
+ assert_equal "bar test", bar_mbean.bar("test")
128
+ assert_raise(NoMethodError) {
129
+ bar_mbean.foo("test")
130
+ }
131
+ bar_mbean.bar_attr = "test"
132
+ assert_equal "test", bar_mbean.bar_attr
133
+ assert_raise(NoMethodError){
134
+ bar_mbean.foo_attr = "test"
135
+ }
136
+
137
+ mbean_server.unregister_mbean foo_object_name
138
+ mbean_server.unregister_mbean bar_object_name
139
+ end
140
+
141
+ def test_get_attributes
142
+ foo = Foo.new
143
+ foo.foo_attr = "value"
144
+
145
+ object_name = ObjectName.new "jmx4r:name=baz"
146
+ mbean_server = ManagementFactory.platform_mbean_server
147
+ mbean_server.register_mbean foo, object_name
148
+
149
+ foo_mbean = JMX::MBean.find_by_name object_name.to_s, :connection => mbean_server
150
+ attrs = mbean_server.get_attributes object_name, ["foo_attr"].to_java(:string)
151
+ assert attrs[0].kind_of? Attribute
152
+ assert_equal foo.foo_attr, attrs[0].value
153
+
154
+ mbean_server.unregister_mbean object_name
155
+
156
+ end
157
+ end