1 package org.xvsm.internal.tasks;
2
3 import java.io.Serializable;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.util.List;
7
8 import org.xvsm.core.ContainerRef;
9 import org.xvsm.core.aspect.AspectContext;
10 import org.xvsm.core.aspect.IAspect;
11 import org.xvsm.core.aspect.IPoint;
12 import org.xvsm.core.aspect.LocalIPoint;
13 import org.xvsm.interfaces.container.IContainer;
14 import org.xvsm.internal.ContainerManager;
15 import org.xvsm.internal.EventProcessingPool;
16 import org.xvsm.internal.GlobalAspectManager;
17 import org.xvsm.internal.exceptions.AspectNotOkException;
18 import org.xvsm.internal.exceptions.AspectRescheduleException;
19 import org.xvsm.internal.exceptions.AspectSkipException;
20 import org.xvsm.internal.exceptions.FatalException;
21 import org.xvsm.internal.exceptions.InvalidContainerException;
22
23 /***
24 *
25 * @author Christian Schreiber, Michael Proestler
26 *
27 */
28 public class AspectTask extends Task implements Serializable {
29
30 /***
31 *
32 */
33 private static final long serialVersionUID = 6173238989593091613L;
34
35 /***
36 * The type of the aspect operations. Maybe null if not needed by the
37 * operation.
38 */
39 private AspectTaskType type;
40
41 /***
42 * The aspect itselft. Maybe null if not needed by the operation.
43 */
44 private IAspect aspect;
45
46 /***
47 * The integration points, where the aspect should work.
48 */
49 private List<IPoint> points;
50
51 /***
52 * The {@link ContainerRef} where the aspect should be added.
53 */
54 private ContainerRef cref;
55
56 /***
57 * the uri of the aspect.
58 */
59 private URI aspectURI;
60
61 /***
62 * The constructor for local aspects.
63 *
64 * @param type
65 * The {@link AspectTaskType}
66 * @param cref
67 * The {@link ContainerRef} where the aspect should be added.
68 * @param aspect
69 * The Aspect itself.
70 * @param points
71 * The integration points of the aspect.
72 */
73 public AspectTask(AspectTaskType type, ContainerRef cref, IAspect aspect,
74 List<IPoint> points) {
75 super();
76 this.type = type;
77 this.cref = cref;
78 this.aspect = aspect;
79 this.points = points;
80 }
81
82 public AspectTask(AspectTaskType type, ContainerRef cref, URI uri,
83 List<IPoint> points) {
84 super();
85 this.type = type;
86 this.cref = cref;
87 this.aspectURI = uri;
88 this.points = points;
89 }
90
91 public AspectTask(AspectTaskType type, URI uri, List<IPoint> points) {
92 super();
93 this.type = type;
94 this.aspectURI = uri;
95 this.points = points;
96 }
97
98 /***
99 * The constructor for global aspects.
100 *
101 * @param type
102 * The {@link AspectTaskType}
103 * @param aspect
104 * The Aspect itself.
105 * @param points
106 * The integration points of the aspect.
107 */
108 public AspectTask(AspectTaskType type, IAspect aspect, List<IPoint> points) {
109 super();
110 this.type = type;
111 this.aspect = aspect;
112 this.points = points;
113 }
114
115 /***
116 * {@inheritDoc}
117 */
118
119 public void run() {
120 Thread.currentThread().setName(this.getClass().getName());
121 String id = null;
122 switch (this.type) {
123 case ADD:
124 AspectContext acontext = new AspectContext(
125 LocalIPoint.PreAddAspect, this.points, this.aspect, this
126 .getAspectContext());
127 boolean skip = false;
128 try {
129 GlobalAspectManager.execute(acontext);
130 } catch (AspectNotOkException e) {
131 this.setResult(e);
132 return;
133 } catch (AspectRescheduleException e) {
134 EventProcessingPool.getInstance().execute(this);
135 } catch (AspectSkipException e) {
136 skip = true;
137 }
138 if (!skip) {
139 if (this.cref != null) {
140
141 IContainer container = null;
142 try {
143 container = ContainerManager.getInstance()
144 .getContainer(null, cref);
145 } catch (InvalidContainerException e) {
146 throw new FatalException(e);
147 }
148
149 id = container.addAspects(acontext.getIpoints(), acontext
150 .getAspect(), this.getAspectContext());
151 } else {
152
153 for (IPoint p : this.points) {
154 id = GlobalAspectManager.addAspect(p, this.aspect);
155 }
156 }
157 }
158
159 try {
160 acontext.setLocalIPoint(LocalIPoint.PostAddAspect);
161 GlobalAspectManager.execute(acontext);
162 } catch (Exception e) {
163 this.setResult(new FatalException(
164 "Aspect result not allowed here. " + e));
165 }
166 break;
167 case DELETE:
168 acontext = new AspectContext(LocalIPoint.PreRemoveAspect,
169 this.points, this.aspect, this.getAspectContext());
170 skip = false;
171 try {
172 GlobalAspectManager.execute(acontext);
173 } catch (AspectNotOkException e) {
174 this.setResult(e);
175 return;
176 } catch (AspectRescheduleException e) {
177 EventProcessingPool.getInstance().execute(this);
178 } catch (AspectSkipException e) {
179 skip = true;
180 }
181 if (!skip) {
182 if (this.cref != null) {
183
184 for (IPoint p : this.points) {
185 try {
186 IContainer container = ContainerManager
187 .getInstance().getContainer(null, cref);
188 container.removeAspect(p, this.aspectURI, this
189 .getAspectContext());
190 } catch (InvalidContainerException e) {
191 throw new FatalException(e);
192 }
193 }
194 } else {
195
196 for (IPoint p : this.points) {
197 GlobalAspectManager.removeAspect(p, this.aspectURI);
198 }
199 }
200 }
201 try {
202 acontext.setLocalIPoint(LocalIPoint.PostRemoveAspect);
203 GlobalAspectManager.execute(acontext);
204 } catch (Exception e) {
205 this.setResult(new FatalException(
206 "Aspect result not allowed here. " + e));
207 }
208 break;
209 default:
210 }
211
212 try {
213 if (this.cref == null) {
214 this.setResult(new URI("/aspects/" + id));
215 } else if (this.cref.getSite() == null) {
216 this.setResult(new URI("/aspects/" + id));
217 } else {
218 this.setResult(new URI(this.getCref().getSite() + "/aspects/"
219 + id));
220 }
221 } catch (URISyntaxException e) {
222 this.setResult(e);
223 }
224 }
225
226 /***
227 * @return the type
228 */
229 public AspectTaskType getType() {
230 return type;
231 }
232
233 /***
234 * @return the aspect
235 */
236 public IAspect getAspect() {
237 return aspect;
238 }
239
240 /***
241 * @return the points
242 */
243 public List<IPoint> getPoints() {
244 return points;
245 }
246
247 /***
248 * @return the cref
249 */
250 public ContainerRef getCref() {
251 return cref;
252 }
253
254 /***
255 * @return the aspectURI
256 */
257 public URI getAspectURI() {
258 return aspectURI;
259 }
260
261 }