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.



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

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

Class Method Details

.all_attributesObject

All attributes up the inheritance chain



125
126
127
128
129
130
# File 'lib/jmx/dynamic_mbean.rb', line 125

def self.all_attributes
  ancestors.inject([]) do |sum, clazz|
    sum.concat(clazz.attributes) if clazz.respond_to? :attributes
    sum
  end
end

.all_operationsObject

All operations up the inheritance chain



133
134
135
136
137
138
# File 'lib/jmx/dynamic_mbean.rb', line 133

def self.all_operations
  ancestors.inject([]) do |sum, clazz|
    sum.concat(clazz.operations) if clazz.respond_to? :operations
    sum
  end
end

.attributesObject

All attributes in this class



120
121
122
# File 'lib/jmx/dynamic_mbean.rb', line 120

def self.attributes
  [:attrs] ||= []
end

.define_getter(name, type, reader) ⇒ Object



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

def self.define_getter(name, type, reader)
  # 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 }

  reader = name.to_s unless reader
  attr_reader reader unless instance_methods.include?(reader)
  define_method("jmx_get_#{name.downcase}") do
    javax.management.Attribute.new name, value_proc.call(__send__(reader))
  end
end

.define_setter(name, type, writer) ⇒ Object



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

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

  writer = name.to_s + '=' unless writer
  attr_writer name.to_s unless instance_methods.include?(writer)
  define_method("jmx_set_#{name.downcase}") do |value|
    __send__ writer, value_converter.call(value)
  end
end

.inherited(cls) ⇒ Object



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

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

.metadataObject

Thread local storage for the derived bean



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

def self.
  @@metadata ||= {}
  @@metadata[object_id] ||= {}
end

.method_added(name) ⇒ Object

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



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

def self.method_added(name) #:nodoc:
  return if [:op].nil?
  [:op].name = name
  operations << [:op].to_jmx
  [: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 ++



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

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

.operationsObject

All operations in this class



141
142
143
# File 'lib/jmx/dynamic_mbean.rb', line 141

def self.operations
  [: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


214
215
216
# File 'lib/jmx/dynamic_mbean.rb', line 214

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

.r_attribute(name, type, description, reader = nil) ⇒ 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"


180
181
182
183
184
# File 'lib/jmx/dynamic_mbean.rb', line 180

def self.r_attribute(name, type, description, reader=nil)
  name = name.to_s
  attributes << JMX::Attribute.new(name, type, description, true, false).to_jmx
  define_getter name, type, reader
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


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

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

.rw_attribute(name, type, description, reader = nil, writer = nil) ⇒ 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"


170
171
172
173
174
175
# File 'lib/jmx/dynamic_mbean.rb', line 170

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

.w_attribute(name, type, description, writer = nil) ⇒ 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"


189
190
191
192
193
# File 'lib/jmx/dynamic_mbean.rb', line 189

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

Instance Method Details

#getAttribute(attribute) ⇒ Object

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



242
243
244
# File 'lib/jmx/dynamic_mbean.rb', line 242

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

#getAttributes(attributes) ⇒ Object



246
247
248
# File 'lib/jmx/dynamic_mbean.rb', line 246

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

#getMBeanInfoObject



250
251
252
# File 'lib/jmx/dynamic_mbean.rb', line 250

def getMBeanInfo
  @info
end

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



254
255
256
# File 'lib/jmx/dynamic_mbean.rb', line 254

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

#setAttribute(attribute) ⇒ Object



258
259
260
# File 'lib/jmx/dynamic_mbean.rb', line 258

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

#setAttributes(attributes) ⇒ Object



262
263
264
# File 'lib/jmx/dynamic_mbean.rb', line 262

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

#toStringObject Also known as: to_s, inspect



266
267
268
# File 'lib/jmx/dynamic_mbean.rb', line 266

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