A system management target is defined by extending
com.kabira.platform.management.Target
.
Example 12.1. Defining a management target
// $Revision: 1.1.2.1 $ package com.kabira.snippets.management; import com.kabira.platform.management.Target; import com.kabira.platform.management.ManagementTarget; /** * MyTarget management target definition */ @ManagementTarget(name = "mytarget", description = "a management target") public class MyTarget extends Target { }
Annotations are used to define the user interface to the management target. The annotations used are:
Table 12.1. System management annotations
Annotation | Description |
@ManagementTarget | Define the name and description of a management target. |
@Command | Indicate that a method should be exposed as a management command. |
@Parameter | Define a management command parameter. |
@Default | Define default values for management command parameters. |
The
annotation, and
it usage, is defined as:@ManagementTarget
Example 12.2. @ManagementTarget annotation
package com.kabira.platform.management; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicate that this class implements a Management Target. This * annotation must be applied to any class registered with Target.register(). */ @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface ManagementTarget { /** * The public name of the target. Required. */ String name(); /** * The description of the target. Used to generate command help message. */ String description() default ""; } // // Using the @ManagementTarget annotation // @ManagementTarget(name = "mytarget", description = "a management target") public class MyTarget extends Target { }
The
annotation, and its usage, is defined as:@Command
Example 12.3. @Command annotation
package com.kabira.platform.management; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicate that this method should be exposed as an command. * Commands must return void, or TargetException will be thrown * when the target is registered. */ @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Command { /** * The description of the command. Used to generate command help message. */ String description() default ""; } // // Using the @Command annotation // public class MyTarget extends Target { @Command(description = "a management command") public void setstate() { ... } }
A method exposed as a management command must be:
public
return void
A
is thrown
when the management target is registered if a method with the
TargetException
@Command
annotation does not following these
rules.
The
annotation, and its usage, is defined as:@Default
Example 12.4. @Default annotation
package com.kabira.platform.management; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Defines default values for parameters. * see java.lang.String for descriptions of valid String formats for * the supported parameter types. */ @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Default { /** * Must be set true if the default value is provided. */ boolean provided() default true; /** * The default value to be used. */ String value(); } // // Using the @Default annotation // public class MyTarget extends Target { public void setstate( @Parameter(name = "state", defaultValue = @Default(value = "true")) String state) { ... } }
The
annotation, and its usage, is defined as:@Parameter
Example 12.5. @Parameter annotation
package com.kabira.platform.management; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * ManagementTarget Command parameter. * Must be present on all parameters of a method annotated as a @Command. Valid * parameter types are String, Enum, Boolean, Character, Integer, Byte, Short, * Long, Float, and Double. Other types will cause TargetException to be * thrown when the target is registered. Arrays are not supported. * see java.lang.String for descriptions of valid String formats for * the supported parameter types. */ @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Parameter { /** * The parameter name as exposed to management clients */ String name(); /** * A description of the parameter. Used in the generated help for the * command. */ String description() default ""; /** * Indicates that this parameter must be set by the caller. * If the parameter is not set. the command will fail. * If false, and no default is provided, the value of the parameter will be * null */ boolean required() default false; /** * The default value of the parameter. */ Default defaultValue() default @Default(provided = false, value = ""); } // // Using the @Parameter annotation // public class MyTarget extends Target { public void setstate( @Parameter(name = "state", description="state value", required=true, defaultValue = @Default(value = "true")) String state) { ... } }
All parameters of a method marked with the
@Command
annotation must be marked with the
@Parameter
annotation. The supported parameter
types are:
String
Enum
Boolean
Character
Integer
Byte
Short
Long
Float
Double
A TargetException
is thrown when the management
target is registered if an unsupported parameter type is
detected.
Administrative commands can execute synchronously or asynchronously. In both cases, the command appears synchronous from the caller's perspective. The difference is whether the command implementation completes immediately or requires multiple transactions to complete. Synchronous commands are appropriate for commands that can be immediately executed and do not lock a large number of resources, for example a display command. Asynchronous commands are appropriate for commands that require a large amount of processing, or would lock a large number of objects into a single transaction, for example a bulk load of a file into memory.
A synchronous command is one which calls
Target.commandComplete()
or
Target.commandFailed()
before returning from the
command implementation.
An asynchronous command returns without calling
Target.commandComplete()
or
Target.commandFailed()
from the command
implementation. In this case, the command is considered active until one
of these methods is called sometime later in another transaction. It is
important that one of these methods be called at some later time, or
this command will never complete.
![]() | |
Asynchronous commands must call
|
Administrative commands are only executed following a
successful authentication. The principal that is executing a command can be
determined using
Target.getActivePrincipalName()
.
Access control should be configured for all application
administration targets. This protects the application from unauthorized
access to administrative functions. In general, access to any command
that modifies the state of the running system should only be granted to
the switchadmin
role. Commands that only
display system state should grant access to the switchmonitor
role. See the TIBCO BusinessEvents® Extreme
Administration for details on the
switchadmin
and switchmonitor
roles.
An example security configuration for an administration target is in Example 12.10, “exampletargetsecurity.kcs”.
The following conventions are recommended for management targets:
target names are nouns.
command names are verbs.
target, command, and parameter names are all lower case.
single word target, command, and parameter names are preferred.
there are no spaces, or underscores, used in multi-word target, command, or parameter names.
most targets define a display
command which
returns tabular data describing the target's state.
A management target must be registered before it is available for use. It should be unregistered when the target is no longer needed. Generally all management targets should be registered when an application starts and be unregistered when the application exits.
A new target object instance is created by the management framework for each command invocation. The instance is created in the JVM in which the management target was registered. This means that the command will execute in this JVM.
Since a new target object instance is created for each command, asynchronous commands can store command state in fields in the target object. The target object instance is destroyed by the management framework when the command completes.
This is a complete example of a management target implementation. The example consists of these files:
AnEnum.java
- definition of an enumeration
used as a parameter.
-
target implementation.ExampleTarget.java
ExampleMain.java
- driver to execute
example target.
ExampleTargetLifecycle.java
- initialize
and terminate the example target.
- security definition for example target.exampletargetsecurity.kcs
Example 12.6. AnEnum.java
// $Revision: 1.1.2.1 $ package com.kabira.snippets.management; /** * A simple enum for the example target */ public enum AnEnum { /** * for the money */ One, /** * for the show */ Two, /** * to get ready */ Three, /** * to go! */ Four };
Example 12.7. ExampleTarget.java
// $Revision: 1.1.2.2 $ package com.kabira.snippets.management; import com.kabira.platform.annotation.Asynchronous; import com.kabira.platform.management.Command; import com.kabira.platform.management.Default; import com.kabira.platform.management.ManagementTarget; import com.kabira.platform.management.Parameter; import com.kabira.platform.management.Target; import com.kabira.platform.management.TargetError; /** * An example of an management target */ @ManagementTarget( name = "exampletarget", description = "example management target") public class ExampleTarget extends Target { /** * example display command. return a simple table of data. * * @throws TargetError */ @Command public void display() throws TargetError { String[] names = { "Who", "Where", "What" }; setColumnNames(names); String[] row = { "Colonel Mustard", "Library", "Candlestick" }; addRow(row); commandComplete(); } /** * example of command which fails. Transactions are committed when * commandFailed is called. */ @Command public void setstate() { commandFailed("setstate not supported"); } /** * * example of command which throws an exception. If an exception is thrown, * the command fails, and the transaction is aborted. * * @throws Exception */ @Command public void rollbackexample() throws Exception { throw new Exception("transaction will roll back"); } /** * Example of Parameter annotations * * @param version required String parameter * @param count optional Integer parameter with default value * @param enum123 optional Enum parameter with default value * @param aBool optional Boolean parameter with default value * @param aFloat optional Float parameter with default value */ @Command public void examplecommand( @Parameter(name = "version", description = "version number", required = true) String version, @Parameter(name = "count", description = "number to display", defaultValue = @Default(value = "1")) Integer count, @Parameter(name = "enum123", description = "Pick One Two or Three", defaultValue = @Default(value = "One")) AnEnum enum123, @Parameter(name = "abool", defaultValue = @Default(value = "true")) Boolean aBool, @Parameter(name = "afloat", defaultValue = @Default(value = "123.456")) Float aFloat) { commandComplete(); } /** * Asynchronous command example. asyncCommand() starts the command. * * @throws TargetError */ @Command public void asynccommand() throws TargetError { m_asyncStartTime = System.currentTimeMillis(); asyncCommandComplete(); } /** * Complete the asynchronous command. */ @Asynchronous private void asyncCommandComplete() throws TargetError { long elapsedTime = System.currentTimeMillis() - m_asyncStartTime; String[] names = { "Elapsed Time (Milli-seconds)" }; setColumnNames(names); String[] row = { Long.toString(elapsedTime) }; addRow(row); commandComplete(); } private long m_asyncStartTime; }
Example 12.8. ExampleMain.java
// $Revision: 1.1.2.2 $ package com.kabira.snippets.management; import com.kabira.test.management.Client; import com.kabira.test.management.CommandFailed; import java.util.HashMap; /** * Execute example target * <p> * <h2> Target Nodes</h2> * <ul> <li> <b>domainnode</b>= A </ul> */ public class ExampleMain { /** * @param args None supported * @throws CommandFailed Command execution failed */ public static void main(String[] args) throws CommandFailed { // // Initialize target // ExampleTargetLifecycle.register(); // // Create test client // Client client = new Client("guest", "guest"); // // Execute help on the target // String[] results = client.runCommand("help", ExampleTargetLifecycle.Name, null); // // Display results // for (String s : results) { System.out.println(s); } // // Execute display // results = client.runCommand("display", ExampleTargetLifecycle.Name, null); // // Display results // for (String s : results) { System.out.println(s); } // // Execute the asynchronous command // results = client.runCommand("asynccommand", ExampleTargetLifecycle.Name, null); // // Display results // for (String s : results) { System.out.println(s); } // // Execute examplecommand // HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("version", "1.0"); parameters.put("count", "100"); parameters.put("enum123", "Three"); parameters.put("abool", "true"); parameters.put("afloat", "3.75"); client.runCommand("examplecommand", ExampleTargetLifecycle.Name, parameters); // // Terminate target // ExampleTargetLifecycle.unregister(); } }
Example 12.9. ExampleTargetLifecycle.java
// $Revision: 1.1.2.1 $ package com.kabira.snippets.management; import com.kabira.platform.management.Target; import com.kabira.platform.Transaction; /** * Example target life-cycle implementation */ public class ExampleTargetLifecycle { /** * Target name */ public final static String Name = "exampletarget"; /** * Register MyTarget management target */ public static void register() { new Transaction("Register Target") { @Override protected void run() throws Rollback { Target.register(ExampleTarget.class); } }.execute(); } /** * Un-register MyTarget management target */ public static void unregister() { new Transaction("Un-register Target") { @Override protected void run() throws Rollback { Target.unregister(Name); } }.execute(); } }
Example 12.10. exampletargetsecurity.kcs
// $Revision: 1.1.2.1 $ configuration "exampletargetsecurity" version "1" type "security" { configure security { configure AccessControl { // // this rule locks all elements in // ExampleTargetCommand to prevent unauthenticated // access, and then grants full access to all // elements in the command to the "switchadmin" role. // Rule { name = "com.kabira.platform.management.ExampleTarget"; lockAllElements = true; accessRules = { { roleName = "switchadmin"; permission = AccessAllOperationsAndAttributes; } }; }; // // the remaining rules grant access to specific // operations to the "switchmonitor" role: // Rule { name = "com.kabira.platform.management.ExampleTarget.examplecommand"; accessRules = { { roleName = "switchmonitor"; permission = Execute; } }; }; }; }; };
When main is executed the following is output (annotation added):
Example 12.11. Example Target Output
# # Output from help command # [A] valid commands and parameters for target "exampletarget": [A] [A] display exampletarget [A] [A] asynccommand exampletarget [A] [A] examplecommand exampletarget [A] version=<String> [A] version number [A] [A] [ count=<Integer, default = 1> ] [A] number to display [A] [A] [ enum123=<AnEnum, default = One> ] [A] Pick One Two or Three [A] [A] [ abool=<Boolean, default = true> ] [A] [A] [ afloat=<Float, default = 123.456> ] [A] [A] [A] setstate exampletarget [A] [A] rollbackexample exampletarget [A] [A] [A] Description: [A] [A] example management target # # Output from display command # [A] Who Where What [A] Colonel Mustard Library Candlestick # # Output from asynccommand command # [A] Elapsed Time (Milli-seconds) [A] 3