93aa15c59a605a9cc79520d272b827a0dcc0cc51
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / bpmn / common / workflow / service / WorkflowContext.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.workflow.service;\r
22 \r
23 import java.util.concurrent.Delayed;\r
24 import java.util.concurrent.TimeUnit;\r
25 \r
26 import org.jboss.resteasy.spi.AsynchronousResponse;\r
27 \r
28 /**\r
29  * @version 1.0\r
30  * Workflow context object used to send timeout response, if workflow instance does not write the response in time\r
31  */\r
32 public class WorkflowContext implements Delayed {\r
33         private final String processKey;\r
34         private final String requestId;\r
35         private final AsynchronousResponse asynchronousResponse;\r
36         private final long startTime;\r
37         private final long timeout;\r
38         \r
39         public WorkflowContext(String processKey, String requestId,\r
40                         AsynchronousResponse asynchronousResponse, long timeout) {\r
41                 this.processKey = processKey;\r
42                 this.requestId = requestId;\r
43                 this.asynchronousResponse = asynchronousResponse;\r
44                 this.timeout = timeout;\r
45                 this.startTime = System.currentTimeMillis();\r
46         }\r
47         \r
48         public String getRequestId() {\r
49                 return requestId;\r
50         }\r
51 \r
52         public String getProcessKey() {\r
53                 return processKey;\r
54         }\r
55 \r
56         public AsynchronousResponse getAsynchronousResponse() {\r
57                 return asynchronousResponse;\r
58         }\r
59 \r
60         public long getTimeout() {\r
61                 return timeout;\r
62         }\r
63 \r
64         public long getStartTime() {\r
65                 return startTime;\r
66         }\r
67 \r
68         /**\r
69          * Required implementation by Delay queue\r
70          * Returns the elapsed time for this context\r
71          */\r
72         @Override\r
73         public long getDelay(TimeUnit unit) {\r
74                 // 0 or negative means this object is considered to be expired\r
75                 return unit.convert(startTime + timeout - System.currentTimeMillis(), TimeUnit.MILLISECONDS);\r
76         }\r
77 \r
78         /**\r
79          * Required implementation by Delay queue\r
80          * Compares the object to determine whether the object can be marked as expired\r
81          */\r
82         @Override\r
83         public int compareTo(Delayed object) {\r
84                 WorkflowContext that = (WorkflowContext) object;\r
85                 long thisEndTime = startTime + timeout;\r
86                 long thatEndTime = that.startTime + that.timeout;\r
87 \r
88                 if (thisEndTime < thatEndTime) {\r
89                         return -1;\r
90                 } else if (thisEndTime > thatEndTime) {\r
91                         return 1;\r
92                 } else {\r
93                         return 0;\r
94                 }\r
95         }\r
96 }\r