1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.protocol.wire.operation;
17
18 import static fulmine.util.Utils.COLON;
19 import static fulmine.util.Utils.logException;
20
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.IdentityHashMap;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.Map.Entry;
27
28 import org.apache.commons.lang.SystemUtils;
29
30 import fulmine.context.IPermissionProfile;
31 import fulmine.util.collection.CollectionFactory;
32
33
34
35
36
37
38 public class BasicOperation implements IOperationScope
39 {
40 private final Set<Object> scope = CollectionFactory.newSet(1);
41
42 private final Set<Integer> processed = CollectionFactory.newSet(1);
43
44 private final Set<Integer> processing = CollectionFactory.newSet(1);
45
46 private final Set<Integer> failed = CollectionFactory.newSet(1);
47
48 private final IPermissionProfile permissionProfile;
49
50 private final Map<Object, Exception> exceptions =
51 new IdentityHashMap<Object, Exception>(1);
52
53 public BasicOperation(IPermissionProfile permissionProfile)
54 {
55 super();
56 this.permissionProfile = permissionProfile;
57 }
58
59 public void exception(Object object, Exception e)
60 {
61 this.failed.add(Integer.valueOf(System.identityHashCode(object)));
62 this.exceptions.put(object, e);
63 }
64
65 public void exiting(Object object, boolean processed)
66 {
67 final Integer identityHashCode =
68 Integer.valueOf(System.identityHashCode(object));
69 this.processing.remove(identityHashCode);
70 if (processed)
71 {
72 this.processed.add(identityHashCode);
73 this.scope.add(object);
74 }
75 }
76
77 public Map<Object, Exception> getExceptions()
78 {
79 return this.exceptions;
80 }
81
82 public Set<Object> getScope()
83 {
84 return this.scope;
85 }
86
87 public boolean include(Object object)
88 {
89
90 final Integer identityHashCode =
91 Integer.valueOf(System.identityHashCode(object));
92 if (this.processed.contains(identityHashCode))
93 {
94 return false;
95 }
96 if (this.processing.contains(identityHashCode))
97 {
98 return false;
99 }
100 if (this.failed.contains(identityHashCode))
101 {
102 return false;
103 }
104 this.processing.add(identityHashCode);
105 return true;
106 }
107
108 public void validate()
109 {
110 if (this.exceptions.size() > 0)
111 {
112 StringBuilder buffer = new StringBuilder();
113 buffer.append(SystemUtils.LINE_SEPARATOR);
114 final Set<Entry<Object, Exception>> entrySet =
115 this.exceptions.entrySet();
116 int i = 0;
117 int size = this.exceptions.size();
118 for (Entry<Object, Exception> entry : entrySet)
119 {
120 try
121 {
122 {
123 i++;
124 buffer.append("Exception " + i + " of " + size + COLON).append(
125 SystemUtils.LINE_SEPARATOR);
126 try
127 {
128 buffer.append(entry.getKey());
129 }
130 catch (Exception e)
131 {
132 buffer.append("<object unknown>");
133 }
134 StringWriter writer = new StringWriter();
135 PrintWriter pw = new PrintWriter(writer);
136 entry.getValue().printStackTrace(pw);
137 buffer.append(SystemUtils.LINE_SEPARATOR).append(
138 "threw ").append(SystemUtils.LINE_SEPARATOR).append(
139 writer.getBuffer().toString());
140 buffer.append(
141 "------------------------------------------------").append(
142 SystemUtils.LINE_SEPARATOR);
143 }
144 }
145 catch (Exception e)
146 {
147 logException(null, entry, e);
148 }
149 }
150 throw new IllegalStateException(buffer.toString());
151 }
152 }
153
154 public IPermissionProfile getPermissionProfile()
155 {
156 return this.permissionProfile;
157 }
158 }