TA Enhancement At Loading Time

TA Instrumentation at loading time is the most convenient as the classes do not have to be modified, only a separate runner class should be created to enable special instrumenting classloader to deal with the classes.

Let's look at an example. 

We will use SensorPanel class from the Activation example.

The following configuration should be used (note that reflector set-up is not necessary for the loading time instrumentation):

TAInstrumentationExample.java: configureTA
01private static Configuration configureTA() { 02 Configuration configuration = Db4o.newConfiguration(); 03 configuration.add(new TransparentActivationSupport()); 04 // configure db4o to use instrumenting classloader 05 // This is required for build time optimization! 06 configuration.reflectWith(new JdkReflector( 07 TAInstrumentationExample.class.getClassLoader())); 08 09 return configuration; 10 }

The main method should provide the testing code:

TAInstrumentationExample.java: main
1public static void main(String[] args) { 2 testActivation(); 3 }
TAInstrumentationExample.java: storeSensorPanel
01private static void storeSensorPanel() { 02 new File(DB4O_FILE_NAME).delete(); 03 ObjectContainer container = database(configureTA()); 04 if (container != null) { 05 try { 06 // create a linked list with length 10 07 SensorPanel list = new SensorPanel().createList(10); 08 container.store(list); 09 } finally { 10 closeDatabase(); 11 } 12 } 13 }
TAInstrumentationExample.java: testActivation
01private static void testActivation() { 02 storeSensorPanel(); 03 Configuration configuration = configureTA(); 04 activateDiagnostics(configuration); 05 06 ObjectContainer container = database(configuration); 07 if (container != null) { 08 try { 09 Query query = container.query(); 10 query.constrain(SensorPanel.class); 11 query.descend("_sensor").constrain(new Integer(1)); 12 ObjectSet result = query.execute(); 13 listResult(result); 14 if (result.size() > 0) { 15 SensorPanel sensor = (SensorPanel) result.get(0); 16 SensorPanel next = sensor._next; 17 while (next != null) { 18 System.out.println(next); 19 next = next._next; 20 } 21 } 22 } finally { 23 closeDatabase(); 24 } 25 } 26 }

A separate class should be used to run the instrumented example. This class creates a filter to point to the classes that should be instrumented, in this case ByNameClassFilter is used. You can see other filters in ClassFilter hierarchy. A special BloatClassEdit is created to instruct, which type of instrumentation will be used (InjectTransparentActivationEdit in our case). This configuration together with the path of the classes to be instrumented is passed to Db4oInstrumentationLauncher.

TAInstrumentationRunner.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.taexamples.instrumented; 04 05import java.io.File; 06import java.net.URL; 07 08import com.db4o.instrumentation.classfilter.ByNameClassFilter; 09import com.db4o.instrumentation.core.BloatClassEdit; 10import com.db4o.instrumentation.core.ClassFilter; 11import com.db4o.instrumentation.main.Db4oInstrumentationLauncher; 12import com.db4o.ta.instrumentation.InjectTransparentActivationEdit; 13 14 15public class TAInstrumentationRunner { 16 17 public static void main(String[] args) throws Exception { 18 // list the classes that need to be instrumented 19 ClassFilter filter = new ByNameClassFilter(new String[] { SensorPanel.class.getName() }); 20 // inject TA awareness 21 BloatClassEdit edits[] = new BloatClassEdit[]{new InjectTransparentActivationEdit(filter)}; 22 // get URL for the classloader 23 URL[] urls = { new File("e:\\sb4o\\trunk\\reference\\bin").toURI().toURL() }; 24 Db4oInstrumentationLauncher.launch(edits, urls, TAInstrumentationExample.class.getName(), new String[]{}); 25 26 } 27 // end main 28 29}
You can run the example by running Db4oInstrumentationLauncher, which will start TAInstrumentationExample in a correct configuration.