Simple Example

Let's look how to enable Transparent Activation in practice. We will take the example class from the Activation chapter and modify it to enable TA:


SensorPanelTA.java
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.taexamples; 04 05import com.db4o.ObjectContainer; 06import com.db4o.activation.Activator; 07import com.db4o.ta.Activatable; 08 09public class SensorPanelTA /*must implement Activatable for TA*/implements Activatable { 10 11 private Object _sensor; 12 13 private SensorPanelTA _next; 14 15 /*activator registered for this class*/ 16 transient Activator _activator; 17 18 public SensorPanelTA() { 19 // default constructor for instantiation 20 } 21 22 public SensorPanelTA(int value) { 23 _sensor = new Integer(value); 24 } 25 26 /*Bind the class to the specified object container, create the activator*/ 27 public void bind(Activator activator) { 28 if (null != _activator) { 29 throw new IllegalStateException(); 30 } 31 _activator = activator; 32 } 33 34 /*Call the registered activator to activate the next level, 35 * the activator remembers the objects that were already 36 * activated and won't activate them twice. 37 */ 38 protected void activate() { 39 if (_activator == null) 40 return; 41 _activator.activate(); 42 } 43 44 public SensorPanelTA getNext() { 45 /*activate direct members*/ 46 activate(); 47 return _next; 48 } 49 50 public Object getSensor() { 51 /*activate direct members*/ 52 activate(); 53 return _sensor; 54 } 55 56 public SensorPanelTA createList(int length) { 57 return createList(length, 1); 58 } 59 60 public SensorPanelTA createList(int length, int first) { 61 int val = first; 62 SensorPanelTA root = newElement(first); 63 SensorPanelTA list = root; 64 while (--length > 0) { 65 list._next = newElement(++val); 66 list = list._next; 67 } 68 return root; 69 } 70 71 protected SensorPanelTA newElement(int value) { 72 return new SensorPanelTA(value); 73 } 74 75 public String toString() { 76 return "Sensor #" + getSensor(); 77 } 78}

As you can see from the example class we can call activate() to activate the field objects. However, transparent activation is currently not available directly on field variables, you will have to wrap them into methods.

Let's create a configuration for transparent activation:

 

TAExample.java: configureTA
01private static Configuration configureTA() { 02 Configuration configuration = Db4o.newConfiguration(); 03 // set normal activation to 0 04 configuration.activationDepth(0); 05 // add TA support 06 configuration.add(new TransparentActivationSupport()); 07 // activate TA diagnostics to reveal the classes that are not TA-enabled. 08 // activateDiagnostics(configuration); 09 return configuration; 10 }

We can test TA using the configuration above:

 

TAExample.java: storeSensorPanel
01private static void storeSensorPanel() { 02 new File(DB4O_FILE_NAME).delete(); 03 ObjectContainer container = database(Db4o.newConfiguration()); 04 if (container != null) { 05 try { 06 // create a linked list with length 10 07 SensorPanelTA list = new SensorPanelTA().createList(10); 08 container.set(list); 09 } finally { 10 closeDatabase(); 11 } 12 } 13 }
TAExample.java: testActivation
01private static void testActivation() { 02 storeSensorPanel(); 03 Configuration configuration = configureTA(); 04 05 ObjectContainer container = database(configuration); 06 if (container != null) { 07 try { 08 System.out.println("Zero activation depth"); 09 ObjectSet result = container.get(new SensorPanelTA(1)); 10 listResult(result); 11 if (result.size() > 0) { 12 SensorPanelTA sensor = (SensorPanelTA) result.get(0); 13 // the object is a linked list, so each call to next() 14 // will need to activate a new object 15 SensorPanelTA next = sensor.getNext(); 16 while (next != null) { 17 System.out.println(next); 18 next = next.getNext(); 19 } 20 } 21 } finally { 22 closeDatabase(); 23 } 24 } 25 }