Let's look at the manual Transparent Activation implementation. This example will help you to understand how TA is implemented under the hood.
We will take the example class from the Activation chapter and modify it to enable TA:
Activatable
interface (bind
method)_activator
variable to keep the current activator;activate()
method;activate()
method each time field objects are required.01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
03
package com.db4odoc.taexamples; 04
05
import com.db4o.activation.*; 06
import com.db4o.ta.*; 07
08
public class SensorPanelTA /*must implement Activatable for TA*/implements Activatable { 09
10
private Object _sensor; 11
12
private SensorPanelTA _next; 13
14
/*activator registered for this class*/ 15
transient Activator _activator; 16
17
public SensorPanelTA() { 18
// default constructor for instantiation 19
} 20
21
public SensorPanelTA(int value) { 22
_sensor = new Integer(value); 23
} 24
25
/*Bind the class to the specified object container, create the activator*/ 26
public void bind(Activator activator) { 27
if (_activator == activator) { 28
return; 29
} 30
if (activator != null && _activator != null) { 31
throw new IllegalStateException(); 32
} 33
_activator = activator; 34
} 35
36
/*Call the registered activator to activate the next level, 37
* the activator remembers the objects that were already 38
* activated and won't activate them twice. 39
*/ 40
public void activate(ActivationPurpose purpose) { 41
if (_activator == null) 42
return; 43
_activator.activate(purpose); 44
} 45
46
public SensorPanelTA getNext() { 47
/*activate direct members*/ 48
activate(ActivationPurpose.READ); 49
return _next; 50
} 51
52
public Object getSensor() { 53
/*activate direct members*/ 54
activate(ActivationPurpose.READ); 55
return _sensor; 56
} 57
58
public SensorPanelTA createList(int length) { 59
return createList(length, 1); 60
} 61
62
public SensorPanelTA createList(int length, int first) { 63
int val = first; 64
SensorPanelTA root = newElement(first); 65
SensorPanelTA list = root; 66
while (--length > 0) { 67
list._next = newElement(++val); 68
list = list._next; 69
} 70
return root; 71
} 72
73
protected SensorPanelTA newElement(int value) { 74
return new SensorPanelTA(value); 75
} 76
77
public String toString() { 78
return "Sensor #" + getSensor(); 79
} 80
}
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:
1private static Configuration configureTA() { 2
Configuration configuration = Db4o.newConfiguration(); 3
// add TA support 4
configuration.add(new TransparentActivationSupport()); 5
// activate TA diagnostics to reveal the classes that are not TA-enabled. 6
activateDiagnostics(configuration); 7
return configuration; 8
}
We can test TA using the configuration above:
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.store(list); 09
} finally { 10
closeDatabase(); 11
} 12
} 13
}
01private static void testActivation() { 02
storeSensorPanel(); 03
Configuration configuration = configureTA(); 04
05
ObjectContainer container = database(configuration); 06
if (container != null) { 07
try { 08
ObjectSet result = container.queryByExample(new SensorPanelTA(1)); 09
listResult(result); 10
if (result.size() > 0) { 11
SensorPanelTA sensor = (SensorPanelTA) result.get(0); 12
// the object is a linked list, so each call to next() 13
// will need to activate a new object 14
SensorPanelTA next = sensor.getNext(); 15
while (next != null) { 16
System.out.println(next); 17
next = next.getNext(); 18
} 19
} 20
} finally { 21
closeDatabase(); 22
} 23
} 24
}