Exercise 1.5: Using a runtime library for more complex probe logic

Before you begin, it's a good idea to read Exercise 1.4: Creating more advanced probes.

With Probekit, it's easy to write simple probes with simple Java code fragments. As you saw in Exercise 1.4, you can even do some sophisticated things with basic probes. But sometimes you want to do something even more complex, something you can't do with a simple probe. In situations like this, instead of trying to fit all the logic you want to execute into probe fragments, you can create a "runtime support library" for your probes. Then you can have your probe fragments make calls into this library, where all the real action is.

If you decide to use a runtime library, it's sometimes necessary to use reflection to call its methods. This is because the generated probe classes are loaded by the bootstrap class loader, and cannot directly access classes that are loaded by the system class loader.

Here is an example of an "entry" fragment that calls a method entryHandler in the runtime library:

Assume that there is a class com.sample.ProbeSupport with a static method entryHandler that you want to call. Assume that entryHandler takes no arguments. Also, assume that the class can be loaded by the system class loader.

try {
  String className = "com.sample.ProbeSupport";
  ClassLoader sysLoader = ClassLoader.getSystemClassLoader();
  Class cls = Class.forName(className, true, sysLoader);
  java.lang.reflect.Method mth = cls.getMethod("entryHandler", null);
  mth.invoke(null, null);
}
catch (Throwable t) {
  System.out.println("Probe error while attempting reflection:");
  t.printStackTrace();
}

To save time on each invocation, you can use the fragment at class scope to declare a static java.lang.reflect.Method object and initialize it just once.

Finish your tutorial by reviewing the materials in the Summary.

Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.