JRockit Management Console ユーザーズ ガイド (JRockit 5.0 R26)

     前  次    目次     
コンテンツの開始位置

カスタム通知アクションおよび制約の追加

Management Console を初めて起動すると、consolesettings.xml というファイルがホーム ディレクトリの ¥ManagementConsole ディレクトリに作成されます。このファイルにはいろいろなエントリがありますが、その中にデフォルトのアクションと制約のデプロイメント エントリも含まれています。Management Console ではカスタムの通知アクションと制約を作成することができますが、それらもこのファイルに格納されます。追加されたアクションと制約は Management Console の [Notification] タブに表示されます。

この付録の内容は以下のとおりです。

 


カスタム アクションの作成

カスタム アクションを作成および実装するために必要な手順について以下に説明します。この手順では、出力アクションを作成します。

  1. ManagementConsole.jar をビルド パスに追加します。
  2. この .jar<jrockit_home>/console ディレクトリにあります。

  3. AbstractNotificationAction のサブクラスを作成します。このクラスは NotificationEvent を受け取ります。
  4. handleNotificationEvent を実装します。
  5. public void handleNotificationEvent(NotificationEvent event)

    exportToXml および initializeFromXml メソッドをオーバーライドして、アクション設定を XML に格納することもできます。

  6. AbstractNotificationActionEditor のサブクラスを作成して、設定の編集に使用するグラフィカルなエディタを作成します。アクションに編集可能な設定がない場合は、com.jrockit.console.notification.ui.NotificationActionEditorEmpty を使用します。
  7. 抽象メソッドを実装します。
  8. protected void storeToObject(Describable object);
    protected void initializeEditor(Describable object);
  9. consolesettings.xml ファイルを編集して、<registry_entry> 要素の下に新しいアクションを指定します。
  10. クラスパスに新しいクラスを追加します。
  11. コンソールを実行します。

新しいアクションが、Management Console の通知セクションにある新しいルールのダイアログ ボックスで使用できるようになります (「新しいルールの作成」を参照してください)。

 


通知アクションの作成と実装 - 例

この節では、アクションの作成方法と実装方法の実際の例を示します。実装すると、パラメータを入力できるテキスト フィールドが [Notification] タブに表示されます。以下の見出しにある手順番号は、「カスタム アクションの作成」の手順を示しています。

注意 : この例では、ManagementConsole.jar がビルド パスに追加されていることを前提としています (手順 1)。

アクションの作成 (手順 2)

最初に、AbstractNotificationAction のサブクラスを作成します。コード リスト A-1 を参照してください。このクラスは NotificationEvent を受け取ります。

コード リスト A-1 パラメータ化したアクションの作成
package com.example.actions;
import org.w3c.dom.Element;
import com.jrockit.console.notification.*;
import com.jrockit.console.util.XmlToolkit;
/**
* パラメータ化したアクションの作成方法を示すテスト クラス
*
* @author Marcus Hirt
*/
public class MyTestAction extends AbstractNotificationAction
{
private final static String TEST_SETTING = "test_param";
public final static String DEFAULT_VALUE = "default value";
private String m_parameter = DEFAULT_VALUE;
      /**
* @see com.jrockit.console.notification.NotificationAction#
* handleNotificationEvent(NotificationEvent)
*/
public void handleNotificationEvent(NotificationEvent event)
   {
System.out.println("===MyTestAction with param: " +
getParameter() + "======");
System.out.println(NotificationToolkit.prettyPrint(event));
}
   /**
* @see com.jrockit.console.util.XmlEnabled#exportToXml
* (Element)
*/
   public void exportToXml(Element node)
{
XmlToolkit.setSetting(node, TEST_SETTING, m_parameter);
}

/**
* @see com.jrockit.console.util.XmlEnabled#initializeFromXml
* (Element)
*/
public void initializeFromXml(Element node)
{
      m_parameter = XmlToolkit.getSetting(node, TEST_SETTING,
DEFAULT_VALUE);
}
      /**
* パラメータを返す
*
* @return some parameter.
*/
   public String getParameter()
{
return m_parameter;
}
   /**
* パラメータを設定
*
* @param parameter the value to set the parameter to.
*/
public void setParameter(String parameter)
{
m_parameter = parameter;
}
}

handleNotificationEvent() の実装 (手順 3)

AbstractNotificationAction のサブクラスを作成するときに、抽象メソッド handleNotificationEvent() を実装しました。このメソッドは入ってくるイベントに作用するものであり、目的のアクションを表現したコードを記述する場所です。イベントから、JVM から利用できる機能にアクセスできる getSource() を通じて RJMXConnectorModule にアクセスすることができます。

アクション エディタの作成 (手順 4)

次に、AbstractNotificationActionEditor のサブクラスを作成して、設定の編集に使用するグラフィカルなエディタを作成します。コード リスト A-2 にその方法を示します。

コード リスト A-2 アクション エディタの作成
package com.example.actions;
import java.awt.*;
import javax.swing.*;
import com.jrockit.console.notification.Describable;
import com.jrockit.console.notification.ui.AbstractNotification
ActionEditor;
/**
* 簡単なテスト エディタ。パラメータを入力できるテキスト フィールドを表示する
* (GridbagLayout を使用すると、よりよいレイアウト結果が得られる)
*
*
* @author Marcus Hirt
*/
public class MyTestActionEditor extends AbstractNotificationActionEditor
{
private JTextField m_parameterField = new
JTextField(MyTestAction.DEFAULT_VALUE);
   /**
* MyTestActionEditor のコンストラクタ
*/
public MyTestActionEditor()
{
super();
setName("MyTestAction settings");
add(new JLabel("Param:"), BorderLayout.WEST);
add(m_parameterField, BorderLayout.CENTER);
setMinimumSize(new Dimension(140,0));
}
/**
* @see com.jrockit.console.notification.ui.Abstract
* Editor#initializeEditor(com.jrockit.console.notification.
* Describable)
*/
protected void initializeEditor(Describable action)
{
m_parameterField.setText(((MyTestAction) action).
getParameter());
}
/**
* @see com.jrockit.console.notification.ui.AbstractEditor#
* storeToObject(com.jrockit.console.notification.Describable)
*/
protected void storeToObject(Describable action)
{
      ((MyTestAction)action).setParameter(m_parameterField.
getText());
}
}

抽象メソッドの実装 (手順 5)

アクション エディタを作成した上記の手順では、コード リスト A-3 の 2 つの抽象メソッド (initializeEditor() および storeToObject()) を実装しました。

コード リスト A-3 抽象メソッドの実装
  */
protected void initializeEditor(Describable action)
{
m_parameterField.setText(((MyTestAction) action).
getParameter());
}
/**
* @see com.jrockit.console.notification.ui.AbstractEditor#
* storeToObject(com.jrockit.console.notification.Describable)
*/
protected void storeToObject(Describable action)
{
      ((MyTestAction)action).setParameter(m_parameterField.
getText());
}

デプロイメント エントリへの新しいアクションの追加 (手順 6)

アクションとエディタを Management Console に表示するには、consolesettings.xml で、<notification_actions> 要素の下のデプロイメント エントリにそのアクションを追加する必要があります。コード リスト A-4 を参照してください。

コード リスト A-4 デプロイメント エントリへの新しいアクションの追加
<registry_entry>
<entry_class>
com.example.actions.MyTestAction
</entry_class>
<entry_name>
Test action
</entry_name>
<entry_description>
Test action, dynamically added.
</entry_description>
<entry_editor_class>
com.example.actions.MyTestActionEditor
</entry_editor_class>
</registry_entry>

新しいアクション エディタの表示 (手順 7 および 8)

最後に、新しいクラスをクラスパスに追加し、コンソールを起動します。[Notification] タブに移動すると、タブ上に新しいエディタが表示されます。

 


カスタム制約の作成

カスタムの制約を作成するには、「カスタム アクションの作成」で説明したものと同じ手順を使用します。ただし、以下のように実装する必要があります。

boolean validate(NotificationEvent event)

このコードの代わりに、次のコードを実装します。

void handleNotificationEvent(NotificationEvent event)

  ページの先頭       前  次