01: /*
02: * Created on Sep 10, 2006
03: */
04: package uk.org.ponder.rsf.preservation;
05:
06: import uk.org.ponder.beanutil.BeanLocator;
07: import uk.org.ponder.beanutil.BeanModelAlterer;
08: import uk.org.ponder.beanutil.WriteableBeanLocator;
09: import uk.org.ponder.errorutil.ThreadErrorState;
10: import uk.org.ponder.messageutil.TargettedMessageList;
11: import uk.org.ponder.rsf.state.TokenStateHolder;
12: import uk.org.ponder.stringutil.StringList;
13: import uk.org.ponder.util.Logger;
14:
15: /** An "exact" BCPS that preserves to precisely named paths within
16: * the target TSH, rather than trying to maximise speed and compactness as
17: * the traditional BCPS.
18: * @author Antranig Basman (amb26@ponder.org.uk)
19: *
20: */
21: public class ExactBeanCopyPreservationStrategy implements
22: StatePreservationStrategy, GenericBeanCopyPreservationStrategy {
23: private StringList beannames;
24: private TokenStateHolder holder;
25: private BeanModelAlterer alterer;
26: private StringList targetkeys;
27: private boolean haskeys;
28:
29: public void setPreservingBeans(StringList beannames) {
30: this .beannames = beannames;
31: }
32:
33: public void setTargetPreservingKeys(StringList targetkeys) {
34: this .targetkeys = targetkeys;
35: }
36:
37: public void setTokenStateHolder(TokenStateHolder holder) {
38: this .holder = holder;
39: }
40:
41: public TokenStateHolder getTokenStateHolder() {
42: return holder;
43: }
44:
45: public void setBeanModelAlterer(BeanModelAlterer alterer) {
46: this .alterer = alterer;
47: }
48:
49: public int preserveImmediate(BeanLocator source,
50: StringList beannames, String tokenid) {
51: int cbeans = 0;
52: for (int i = 0; i < beannames.size(); ++i) {
53: String beanname = beannames.stringAt(i);
54: String targetkey = haskeys ? beanname : targetkeys
55: .stringAt(i);
56: Object bean = alterer.getBeanValue(beanname, source, null);
57: if (bean != null) {
58: holder.putTokenState(targetkey, bean);
59: ++cbeans;
60: Logger.log.info("BeanCopy preserved to path "
61: + targetkey + ": " + bean);
62: }
63: }
64: return cbeans;
65: }
66:
67: public int preserve(BeanLocator source, String tokenid) {
68: return preserveImmediate(source, beannames, tokenid);
69: }
70:
71: public int restore(WriteableBeanLocator target, String tokenid) {
72: TargettedMessageList messages = ThreadErrorState
73: .getErrorState().messages;
74: int cbeans = 0;
75: for (int i = 0; i < targetkeys.size(); ++i) {
76: String key = targetkeys.stringAt(i);
77: Object bean = holder.getTokenState(key);
78: if (bean != null) {
79: String path = beannames.stringAt(i);
80: alterer.setBeanValue(path, target, bean, messages,
81: false);
82: Logger.log.info("BeanCopy restored value " + bean
83: + " from key " + key + " to path " + path);
84: ++cbeans;
85: }
86: }
87: return cbeans;
88: }
89:
90: public void clear(String tokenid) {
91: for (int i = 0; i < targetkeys.size(); ++i) {
92: String key = targetkeys.stringAt(i);
93: holder.clearTokenState(key);
94: }
95: }
96:
97: }
|