1 | package de.uka.ipd.sdq.probespec.framework; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collections; |
5 | import java.util.List; |
6 | |
7 | /** |
8 | * This class represents the execution context of a request. Normally the |
9 | * execution context of a request is the thread which processes this request. |
10 | * Then the thread's id is a unique identifier for this execution context. |
11 | * <p> |
12 | * A RequestContext simply wraps an identifier that represents the execution |
13 | * context clearly. The thread id is such an identifier. |
14 | * <p> |
15 | * In order to represent a sequence of requests so that request1 spawned |
16 | * request2, which in turn spawned request3 and so forth, a parent context can |
17 | * be specified by using constructor |
18 | * {@link #RequestContext(String, RequestContext)}. In this way the |
19 | * execution of forks can be represented. |
20 | * |
21 | * @author Faber |
22 | * @author Philipp Merkle |
23 | * |
24 | */ |
25 | public class RequestContext { |
26 | |
27 | public static final RequestContext EMPTY_REQUEST_CONTEXT = new RequestContext(""); |
28 | |
29 | // unique identifier of the request context |
30 | private String requestContextId; |
31 | |
32 | private RequestContext parentContext; |
33 | |
34 | private List<RequestContext> childContexts; |
35 | |
36 | /** |
37 | * Default Constructor. |
38 | * |
39 | * @param requestContextId |
40 | * the unique identifier of the request context |
41 | */ |
42 | public RequestContext(String requestContextId) { |
43 | this(requestContextId, null); |
44 | } |
45 | |
46 | /** |
47 | * Constructor to be used when there is a parent context. |
48 | * |
49 | * @param requestContextId |
50 | * the unique identifier of the request context |
51 | * @param parentContext |
52 | * the parent context of the context which is to be constructed |
53 | */ |
54 | public RequestContext(String requestContextId, RequestContext parentContext) { |
55 | this.requestContextId = requestContextId; |
56 | this.parentContext = parentContext; |
57 | |
58 | if (parentContext != null) { |
59 | parentContext.addChildContext(this); |
60 | } |
61 | |
62 | if (this.requestContextId == null) { |
63 | this.requestContextId = ""; |
64 | } |
65 | } |
66 | |
67 | /** |
68 | * Returns the unique identifier of the request context represented by this |
69 | * object. |
70 | * |
71 | * @return the request context identifier |
72 | */ |
73 | public String getRequestContextId() { |
74 | return requestContextId; |
75 | } |
76 | |
77 | public RequestContext getParentContext() { |
78 | return parentContext; |
79 | } |
80 | |
81 | private void addChildContext(RequestContext context) { |
82 | if (childContexts == null) { |
83 | childContexts = new ArrayList<RequestContext>(); |
84 | } |
85 | childContexts.add(context); |
86 | } |
87 | |
88 | public List<RequestContext> getChildContexts() { |
89 | if (childContexts == null) { |
90 | return null; |
91 | } |
92 | return Collections.unmodifiableList(childContexts); |
93 | } |
94 | |
95 | @Override |
96 | public int hashCode() { |
97 | final int prime = 31; |
98 | int result = 1; |
99 | result = prime * result |
100 | + ((parentContext == null) ? 0 : parentContext.hashCode()); |
101 | result = prime * result |
102 | + ((requestContextId == null) ? 0 : requestContextId.hashCode()); |
103 | return result; |
104 | } |
105 | |
106 | @Override |
107 | public boolean equals(Object obj) { |
108 | if (this == obj) { |
109 | return true; |
110 | } |
111 | if (obj == null) { |
112 | return false; |
113 | } |
114 | if (!(obj instanceof RequestContext)) { |
115 | return false; |
116 | } |
117 | RequestContext other = (RequestContext) obj; |
118 | if (parentContext == null) { |
119 | if (other.parentContext != null) { |
120 | return false; |
121 | } |
122 | } else if (!parentContext.equals(other.parentContext)) { |
123 | return false; |
124 | } |
125 | if (requestContextId == null) { |
126 | if (other.requestContextId != null) { |
127 | return false; |
128 | } |
129 | } else if (!requestContextId.equals(other.requestContextId)) { |
130 | return false; |
131 | } |
132 | return true; |
133 | } |
134 | |
135 | /** |
136 | * Appends the specified addition to the request context id and returns a |
137 | * new {@link RequestContext} containing the extended request context id. |
138 | * The parent context will be retained. |
139 | * |
140 | * @param addition |
141 | * the String to append |
142 | * @return |
143 | */ |
144 | public RequestContext append(String addition) { |
145 | return new RequestContext(getRequestContextId() + addition, |
146 | getParentContext()); |
147 | } |
148 | |
149 | public RequestContext rootContext() { |
150 | RequestContext context = this; |
151 | while (context.getParentContext() != null) { |
152 | context = context.getParentContext(); |
153 | } |
154 | return context; |
155 | } |
156 | |
157 | @Override |
158 | public String toString() { |
159 | return parentContext != null ? parentContext.toString() + " called " |
160 | + "<" + requestContextId + ">" : "<" + requestContextId + ">"; |
161 | } |
162 | |
163 | } |