View Javadoc

1   package org.xvsm.core.notifications;
2   
3   import java.net.URI;
4   import java.util.List;
5   import java.util.Properties;
6   
7   import org.xvsm.core.AtomicEntry;
8   import org.xvsm.core.Capi;
9   import org.xvsm.core.ContainerRef;
10  import org.xvsm.core.Entry;
11  import org.xvsm.core.Tuple;
12  import org.xvsm.core.aspect.GlobalAspect;
13  import org.xvsm.interfaces.ICapi;
14  import org.xvsm.internal.ContainerManager;
15  import org.xvsm.internal.exceptions.AspectNotOkException;
16  import org.xvsm.internal.exceptions.AspectRescheduleException;
17  import org.xvsm.internal.exceptions.AspectSkipException;
18  import org.xvsm.internal.exceptions.FatalException;
19  import org.xvsm.internal.exceptions.XCoreException;
20  import org.xvsm.selectors.Selector;
21  import org.xvsm.transactions.Transaction;
22  
23  public class NotificationAspect extends GlobalAspect {
24  	/***
25  	 * 
26  	 */
27  	private static final long serialVersionUID = 102376212575295265L;
28  	private ContainerRef cref = null;
29  	private ContainerRef ncref = null;
30  	private static ICapi capi;
31  	static {
32  		try {
33  			capi = new Capi();
34  		} catch (Exception e) {
35  			throw new FatalException(e);
36  		}
37  	}
38  
39  	@Override
40  	public void finalize() throws Throwable {
41  		super.finalize();
42  		if (this.ncref != null) {
43  			ContainerManager.getInstance().deleteContainer(null, ncref);
44  		}
45  	}
46  
47  	public NotificationAspect() {
48  		try {
49  			capi = new Capi();
50  		} catch (Exception e) {
51  			throw new FatalException(e);
52  		}
53  	}
54  
55  	public NotificationAspect(ContainerRef cref, ContainerRef ncref) {
56  		this();
57  		super.properties.put("cref", new AtomicEntry<URI>(cref.asURI()));
58  		super.properties.put("ncref", new AtomicEntry<URI>(ncref.asURI()));
59  
60  		this.cref = cref;
61  		this.ncref = ncref;
62  	}
63  
64  	@Override
65  	public void postRead(ContainerRef cref, Transaction tx,
66  			List<Entry> entries, List<Selector> selectors,
67  			Properties contextProperties) throws AspectNotOkException,
68  			AspectRescheduleException, AspectSkipException {
69  		try {
70  			capi.write(ncref, 0, tx, this.createTuple(Operation.Read, entries));
71  		} catch (XCoreException e) {
72  			throw new FatalException(e);
73  		}
74  	}
75  
76  	@Override
77  	public void postTake(ContainerRef cref, Transaction tx,
78  			List<Selector> selectors, List<Entry> taken,
79  			Properties contextProperties) throws AspectNotOkException,
80  			AspectRescheduleException, AspectSkipException {
81  		try {
82  			capi.write(ncref, 0, tx, this.createTuple(Operation.Take, taken));
83  		} catch (XCoreException e) {
84  			throw new FatalException(e);
85  		}
86  	}
87  
88  	@Override
89  	public void postDestroy(ContainerRef cref, Transaction tx,
90  			List<Selector> selectors, List<Entry> deleted,
91  			Properties contextProperties) throws AspectNotOkException,
92  			AspectRescheduleException, AspectSkipException {
93  		try {
94  			capi.write(ncref, 0, tx, this.createTuple(Operation.Destroy,
95  					deleted));
96  		} catch (XCoreException e) {
97  			throw new FatalException(e);
98  		}
99  	}
100 
101 	@Override
102 	public void postShift(ContainerRef cref, Transaction tx,
103 			List<Entry> entries, List<Entry> shifted,
104 			Properties contextProperties) throws AspectNotOkException,
105 			AspectRescheduleException, AspectSkipException {
106 		try {
107 			capi
108 					.write(ncref, 0, tx, this.createTuple(Operation.Shift,
109 							entries));
110 		} catch (XCoreException e) {
111 			throw new FatalException(e);
112 		}
113 	}
114 
115 	@Override
116 	public void postWrite(ContainerRef cref, Transaction tx,
117 			List<Entry> entries, Properties contextProperties)
118 			throws AspectNotOkException, AspectRescheduleException,
119 			AspectSkipException {
120 		try {
121 			capi
122 					.write(ncref, 0, tx, this.createTuple(Operation.Write,
123 							entries));
124 		} catch (XCoreException e) {
125 			throw new FatalException(e);
126 		}
127 	}
128 
129 	@Override
130 	public void setProperties(Properties props) {
131 		this.cref = new ContainerRef((URI) ((AtomicEntry<?>) props.get("cref"))
132 				.getValue());
133 		this.ncref = new ContainerRef((URI) ((AtomicEntry<?>) props
134 				.get("ncref")).getValue());
135 		this.properties = props;
136 	}
137 
138 	@Override
139 	public Properties getProperties() {
140 		return this.properties;
141 	}
142 
143 	private Tuple createTuple(Operation operation, List<Entry> entries) {
144 		Tuple t = new Tuple(new AtomicEntry<String>(operation.toString()));
145 		Tuple t2 = new Tuple();
146 		for (Entry e : entries) {
147 			t2.add(e.getFlatCopy());
148 		}
149 		t.add(t2);
150 		return t;
151 	}
152 }