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.



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

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



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

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



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

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



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

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

.define_getter(name, type, reader) ⇒ Object



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

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



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

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



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

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

.metadataObject

Thread local storage for the derived bean



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

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?



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

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 ++



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

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



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

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


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

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"


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

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


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

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"


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

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"


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

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)



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

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

#getAttributes(attributes) ⇒ Object



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

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

#getMBeanInfoObject



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

def getMBeanInfo
  @info
end

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



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

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

#setAttribute(attribute) ⇒ Object



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

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

#setAttributes(attributes) ⇒ Object



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

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

#toStringObject Also known as: to_s, inspect



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

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