Class: RubyDynamicMBean

Inherits:
Object
  • Object
show all
Includes:
JMX::JavaTypeAware
Defined in:
lib/jmx/dynamic_mbean.rb

Overview

The Ruby-Java JMX utilities work throughout the DynamicMBean concept. Creators of Ruby based MBeans must inherit this class (RubyDynamicMBean) in their own bean classes and then register them with a JMX mbean server.

Here is an example:
     class MyMBean < RuybDynamicMBean
       rw_attribute :status, :string, "Status information for this process"

       operation "Shutdown this process"
       parameter :string, "user_name", "Name of user requesting shutdown"
       returns :string
       def shutdown(user_name)
          "shutdown requests more time"
       end
     end

Once you have defined your bean class you can start declaring attributes and operations.

Attributes come in three flavors: read, write, and read write. Simmilar to the attr* helpers, there are helpers that are used to create management attributes. Use r_attribute, w_attribute, and rw_attribute to declare attributes, and the operation, returns, and parameter helpers to define a management operation. Creating attributes with the *_attribute convention ALSO creates ruby accessors (it invokes the attr_accessor/attr_reader/attr_writer ruby helpers) to create ruby methods like: user_name= and username. So in your ruby code you can treat the attributes as “regular” ruby accessors

Constant Summary

Constants included from JMX::JavaTypeAware

JMX::JavaTypeAware::SIMPLE_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JMX::JavaTypeAware

to_java_type, to_ruby

Constructor Details

#initialize(name, description) ⇒ RubyDynamicMBean

when creating a dynamic MBean we need to provide it with a name and a description.



209
210
211
212
213
# File 'lib/jmx/dynamic_mbean.rb', line 209

def initialize(name, description)
  operations = self.class.operations.to_java MBeanOperationInfo
  attributes = self.class.attributes.to_java MBeanAttributeInfo
  @info = MBeanInfo.new name, description, attributes, nil, operations, nil
end

Class Method Details

.attributesObject

:nodoc:



118
119
120
# File 'lib/jmx/dynamic_mbean.rb', line 118

def self.attributes #:nodoc:
  Thread.current[:attrs] ||= []
end

.define_getter(name, type) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/jmx/dynamic_mbean.rb', line 126

def self.define_getter(name, type)
  # FIXME: Our to_java_type needs to do something saner
  java_type = begin; to_java_type(type); rescue; nil; end
  value_proc = java_type ? proc { |value| java_type.new value } : proc { |value| Java.ruby_to_java value }

  define_method("jmx_get_#{name.downcase}") do
    javax.management.Attribute.new name, value_proc.call(instance_variable_get('@' + name))
  end
end

.define_setter(name, type) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/jmx/dynamic_mbean.rb', line 136

def self.define_setter(name, type)
  value_converter = JMX::JavaTypeAware.to_ruby(type)

  define_method("jmx_set_#{name.downcase}") do |value| 
    instance_variable_set '@' + name, value_converter.call(value)
  end    
end

.inherited(cls) ⇒ Object



105
106
107
# File 'lib/jmx/dynamic_mbean.rb', line 105

def self.inherited(cls)
  cls.send(:include, javax.management.DynamicMBean)
end

.method_added(name) ⇒ Object

TODO: preserve any original method_added? TODO: Error handling here when it all goes wrong?



111
112
113
114
115
116
# File 'lib/jmx/dynamic_mbean.rb', line 111

def self.method_added(name) #:nodoc:
  return if Thread.current[:op].nil?
  Thread.current[:op].name = name
  operations << Thread.current[:op].to_jmx
  Thread.current[:op] = nil
end

.operation(description) ⇒ Object

Use the operation method to declare the start of an operation It takes as an argument the description for the operation

operation "Used to start the service"
def start
end

– Last operation wins if more than one ++



183
184
185
186
# File 'lib/jmx/dynamic_mbean.rb', line 183

def self.operation(description)
  # Wait to error check until method_added so we can know method name
  Thread.current[:op] = JMX::Operation.new description
end

.operationsObject

:nodoc:



122
123
124
# File 'lib/jmx/dynamic_mbean.rb', line 122

def self.operations #:nodoc:
  Thread.current[:ops] ||= []
end

.parameter(type, name = nil, description = nil) ⇒ Object

Used to declare a parameter (you can declare more than one in succession) that is associated with the currently declared operation.

operation "Used to update the name of a service"
parameter :string, "name", "Set the new name of the service"
def start
end


194
195
196
# File 'lib/jmx/dynamic_mbean.rb', line 194

def self.parameter(type, name=nil, description=nil)
  Thread.current[:op].parameters << JMX::Parameter.new(type, name, description)
end

.r_attribute(name, type, description) ⇒ Object

the r_attribute method is used to declare a JMX read only attribute. See the JavaSimpleTypes module for more information about acceptable types usage:

r_attribute :attribute_name, :string, "Description displayed in a JMX console"


158
159
160
161
162
163
# File 'lib/jmx/dynamic_mbean.rb', line 158

def self.r_attribute(name, type, description)
  name = name.to_s
  attributes << JMX::Attribute.new(name, type, description, true, false).to_jmx
  attr_reader name
  define_getter name, type
end

.returns(type) ⇒ Object

Used to declare the return type of the operation

operation "Used to update the name of a service"
parameter :string, "name", "Set the new name of the service"
returns :void
def set_name
end


204
205
206
# File 'lib/jmx/dynamic_mbean.rb', line 204

def self.returns(type)
  Thread.current[:op].return_type = type
end

.rw_attribute(name, type, description) ⇒ Object

the rw_attribute method is used to declare a JMX read write attribute. See the JavaSimpleTypes module for more information about acceptable types usage:

rw_attribute :attribute_name, :string, "Description displayed in a JMX console"


147
148
149
150
151
152
153
# File 'lib/jmx/dynamic_mbean.rb', line 147

def self.rw_attribute(name, type, description)
  name = name.to_s
  attributes << JMX::Attribute.new(name, type, description, true, true).to_jmx
  attr_accessor name
  define_getter name, type
  define_setter name, type
end

.w_attribute(name, type, description) ⇒ Object

the w_attribute method is used to declare a JMX write only attribute. See the JavaSimpleTypes module for more information about acceptable types usage:

w_attribute :attribute_name, :string, "Description displayed in a JMX console"


168
169
170
171
172
173
# File 'lib/jmx/dynamic_mbean.rb', line 168

def self.w_attribute(name, type, description)
  name = name.to_s
  attributes << JMX::Attribute.new(name, type, description, false, true).to_jmx
  attr_writer name
  define_setter name, type
end

Instance Method Details

#getAttribute(attribute) ⇒ Object

Retrieve the value of the requested attribute (where attribute is a javax.management.Attribute class)



216
217
218
# File 'lib/jmx/dynamic_mbean.rb', line 216

def getAttribute(attribute)
  __send__("jmx_get_" + attribute.downcase)
end

#getAttributes(attributes) ⇒ Object



220
221
222
# File 'lib/jmx/dynamic_mbean.rb', line 220

def getAttributes(attributes)
  attributes.inject(AttributeList.new) { |attrs, attribute| attrs.add(getAttribute(attribute)); attrs } 
end

#getMBeanInfoObject



224
225
226
# File 'lib/jmx/dynamic_mbean.rb', line 224

def getMBeanInfo
  @info
end

#invoke(actionName, params = nil, signature = nil) ⇒ Object



228
229
230
# File 'lib/jmx/dynamic_mbean.rb', line 228

def invoke(actionName, params=nil, signature=nil)
  __send__(actionName, *params)
end

#setAttribute(attribute) ⇒ Object



232
233
234
# File 'lib/jmx/dynamic_mbean.rb', line 232

def setAttribute(attribute)
  __send__("jmx_set_#{attribute.name.downcase}", attribute.value)   
end

#setAttributes(attributes) ⇒ Object



236
237
238
# File 'lib/jmx/dynamic_mbean.rb', line 236

def setAttributes(attributes)  
  attributes.each { |attribute| setAttribute attribute}
end

#toStringObject Also known as: to_s, inspect



240
241
242
# File 'lib/jmx/dynamic_mbean.rb', line 240

def toString
  "#@info.class_name: #@info.description"
end