jmx4r 0.0.1 → 0.0.2

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/README.txt CHANGED
@@ -1 +1,18 @@
1
- jmx4r is a JMX library for JRuby
1
+ jmx4r is a JMX library for JRuby.
2
+
3
+ It can be used to write simple Ruby scripts running on JRuby[http://jruby.org]
4
+ to manage remote Java applications (e.g. JBoss[http://www.jboss.org],
5
+ Tomcat[http://tomcat.apache.org/]) using
6
+ JMX[http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/].
7
+
8
+ == Examples
9
+
10
+ To trigger a garbage collection on a Java application:
11
+
12
+ require 'rubygems'
13
+ require 'jmx4r'
14
+
15
+ JMX::MBean.establish_connection :host => "localhost", :port => 3000
16
+ memory = JMX::MBean.find_by_name "java.lang:type=Memory"
17
+ memory.gc
18
+
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.1"
9
+ version = "0.0.2"
10
10
 
11
11
  task :default => [:test]
12
12
 
@@ -22,6 +22,7 @@ Rake::RDocTask.new do |rdoc|
22
22
  rdoc.main = "README.txt"
23
23
  rdoc.rdoc_dir = "doc/html"
24
24
  rdoc.title = "jmx4r Documentation"
25
+ rdoc.options << "-S"
25
26
  end
26
27
 
27
28
  spec = Gem::Specification.new do |spec|
@@ -44,7 +45,7 @@ spec = Gem::Specification.new do |spec|
44
45
  spec.rubyforge_project = "jmx4r"
45
46
  spec.homepage = "http://code.google.com/p/jmx4r/"
46
47
  spec.description = <<END_DESC
47
- TODO
48
+ jmx4r is a JMX library for JRuby
48
49
  END_DESC
49
50
  end
50
51
 
@@ -57,6 +58,6 @@ desc "Show library's code statistics"
57
58
  task :stats do
58
59
  require 'code_statistics'
59
60
  CodeStatistics.new( ["jmx4r", "lib"],
60
- ["Functionals", "examples"],
61
+ ["Examples", "examples"],
61
62
  ["Units", "test"] ).to_s
62
63
  end
data/lib/jconsole.rb CHANGED
@@ -1,14 +1,21 @@
1
1
 
2
2
  module JConsole
3
- def JConsole.start(port=3000)
3
+ def JConsole.start(args={})
4
+ port = args[:port] || 3000
5
+ pwd_file = args[:pwd_file]
6
+ access_file = args[:access_file]
7
+
4
8
  cmd =<<-EOCMD.split("\n").join(" ")
5
9
  jconsole
6
10
  -J-Dcom.sun.management.jmxremote
7
11
  -J-Dcom.sun.management.jmxremote.port=#{port}
8
12
  -J-Dcom.sun.management.jmxremote.ssl=false
9
- -J-Dcom.sun.management.jmxremote.authenticate=false
13
+ -J-Dcom.sun.management.jmxremote.authenticate=#{!pwd_file.nil?}
10
14
  EOCMD
11
-
15
+ if pwd_file
16
+ cmd << " -J-Dcom.sun.management.jmxremote.password.file=#{pwd_file}"
17
+ cmd << " -J-Dcom.sun.management.jmxremote.access.file=#{access_file}"
18
+ end
12
19
  Thread.start { system cmd }
13
20
  sleep 2
14
21
  end
data/lib/jmx4r.rb CHANGED
@@ -64,9 +64,17 @@ module JMX
64
64
  def self.establish_connection(args={})
65
65
  host = args[:host] || "localhost"
66
66
  port = args[:port] || 3000
67
+ username = args[:username]
68
+ password = args[:password]
69
+
67
70
  @@mbsc ||= begin
68
71
  url = "service:jmx:rmi:///jndi/rmi://#{host}:#{port}/jmxrmi"
69
- connector = JMXConnectorFactory::connect JMXServiceURL.new(url), HashMap.new
72
+ env = HashMap.new
73
+ if !username.nil? and username.length > 0
74
+ credentials = [username, password]
75
+ env.put JMXConnector::CREDENTIALS, credentials.to_java(:String)
76
+ end
77
+ connector = JMXConnectorFactory::connect JMXServiceURL.new(url), env
70
78
  connector.getMBeanServerConnection
71
79
  end
72
80
  end
@@ -0,0 +1,44 @@
1
+ require 'java'
2
+
3
+ class MBeanBase < java.lang.Object
4
+ include_package 'javax.management'
5
+ include DynamicMBean
6
+
7
+ attr_reader :attributes
8
+
9
+ def initialize
10
+ @attributes = []
11
+ end
12
+
13
+ def getMBeanInfo
14
+ begin
15
+ mbean_attrs = @attributes.map do |attr|
16
+ MBeanAttributeInfo.new attr.to_s, attr.to_s, "java.lang.String", true, false, false
17
+ end
18
+ MBeanInfo.new(self.class.to_s, self.class.to_s,
19
+ mbean_attrs.to_java('javax.management.MBeanAttributeInfo'),
20
+ #[].to_java('javax.management.MBeanAttributeInfo'),
21
+ [].to_java('javax.management.MBeanConstructorInfo'),
22
+ [].to_java('javax.management.MBeanOperationInfo'),
23
+ [].to_java('javax.management.MBeanNotificationInfo'))
24
+ rescue
25
+ puts $!
26
+ end
27
+ end
28
+ end
29
+
30
+ class TestMBean < MBeanBase
31
+ #mbean_attr_reader :hello
32
+
33
+ def initialize
34
+ super
35
+ @hello = "world"
36
+ end
37
+ end
38
+
39
+ require 'remote_server'
40
+ server = RemoteServer.new
41
+ server.start
42
+ mbean = TestMBean.new
43
+ mbean.attributes << :hello
44
+ server.connector_server.getMBeanServer.register_mbean mbean, ObjectName.new("jmx4r:type=Test")
@@ -0,0 +1,33 @@
1
+ require 'java'
2
+ include_class 'javax.management.ObjectName'
3
+
4
+ class RemoteServer
5
+ include_package 'java.rmi.registry'
6
+ include_package 'java.lang.management'
7
+ include_package 'javax.management.remote'
8
+ include_package 'javax.management'
9
+
10
+ attr_reader :connector_server
11
+
12
+ def start(port=3000)
13
+
14
+ LocateRegistry.createRegistry(port);
15
+
16
+ mbs = ManagementFactory.platform_mbean_server
17
+ url = JMXServiceURL.new "service:jmx:rmi:///jndi/rmi://:#{port}/jmxrmi"
18
+ @connector_server = JMXConnectorServerFactory.newJMXConnectorServer url, nil , mbs
19
+ @connector_server.start
20
+ end
21
+
22
+ def stop
23
+ @connector_server.stop
24
+ end
25
+ end
26
+
27
+ include_class 'javax.management.StandardMBean'
28
+
29
+ module Test
30
+ def hello
31
+ puts "hello"
32
+ end
33
+ end
data/test/tc_auth.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "test/unit"
2
+
3
+ require "jmx4r"
4
+ require "jconsole"
5
+
6
+ class TestAuthentication < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @username = "jmx4r.user"
10
+ @password = "jmx4r.password"
11
+
12
+ pwd_path = "/tmp/jmx4r.password"
13
+ @pwd_file = File.new(pwd_path, "w")
14
+ @pwd_file.puts "#{@username} #{@password}"
15
+ @pwd_file.close
16
+
17
+ # pwd file must be readable only by user
18
+ # but somehow File.chmod is not working
19
+ # with JRuby
20
+ `chmod 0600 #{@pwd_file.path}`
21
+
22
+ access_path = "/tmp/jmx4r.access"
23
+ @access_file = File.new(access_path, "w")
24
+ @access_file.puts "#{@username} readwrite"
25
+ @access_file.close
26
+ # access file must be readable only by user
27
+ `chmod 0600 #{@access_file.path}`
28
+
29
+ JConsole::start :pwd_file => @pwd_file.path, :access_file => @access_file.path
30
+ end
31
+
32
+ def teardown
33
+ JMX::MBean.remove_connection
34
+ JConsole::stop
35
+ File.delete @pwd_file.path if File.file? @pwd_file.path
36
+ File.delete @access_file.path if File.file? @access_file.path
37
+ end
38
+
39
+ def test_establish_auth_connection_with_correct_credentials
40
+ JMX::MBean.establish_connection :username => @username, :password => @password
41
+ end
42
+
43
+ def test_establish_auth_connection_with_incorrect_credentials
44
+ assert_raise(NativeException) {
45
+ JMX::MBean.establish_connection :username => "not a valid user name", :password => @password
46
+ }
47
+ end
48
+
49
+ def test_establish_auth_connection_with_no_credentials
50
+ assert_raise(NativeException) {
51
+ JMX::MBean.establish_connection
52
+ }
53
+ end
54
+ end
@@ -27,8 +27,7 @@ class TestConnection < Test::Unit::TestCase
27
27
  end
28
28
 
29
29
  def test_establish_connection_with_custom_port
30
- JConsole::start 3001
30
+ JConsole::start :port => 3001
31
31
  JMX::MBean.establish_connection :port => 3001
32
32
  end
33
-
34
33
  end
data/test/toto.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'java'
2
+
3
+ class A < java.lang.Object
4
+ def A.dsl_method(*args)
5
+ puts args.join(', ')
6
+ end
7
+ end
8
+
9
+ class B < A
10
+ dsl_method :a, :b
11
+ end
12
+
13
+ b = B.new
14
+
15
+
data/test/ts_all.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "test/unit"
2
2
 
3
3
  require "tc_connection"
4
+ require "tc_auth"
4
5
  require "tc_attributes"
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.1
6
+ version: 0.0.2
7
7
  post_install_message:
8
- date: 2007-06-24 22:00:00 +00:00
8
+ date: 2007-06-26 22:00:00 +00:00
9
9
  files:
10
10
  - examples/class_loading.rb
11
11
  - examples/jvm_mngmt.rb
@@ -15,7 +15,11 @@ files:
15
15
  - lib/jmx4r.rb
16
16
  - test/tc_connection.rb
17
17
  - test/tc_attributes.rb
18
+ - test/remote_server.rb
18
19
  - test/ts_all.rb
20
+ - test/tc_auth.rb
21
+ - test/mbean_base.rb
22
+ - test/toto.rb
19
23
  - Rakefile
20
24
  - README.txt
21
25
  - LICENSE.txt
@@ -45,7 +49,7 @@ specification_version: 1
45
49
  test_files:
46
50
  - test/ts_all.rb
47
51
  dependencies: []
48
- description: TODO
52
+ description: jmx4r is a JMX library for JRuby
49
53
  authors:
50
54
  - Jeff Mesnil
51
55
  email: jmesnil@gmail.com