Handled interrupted exception Rechecked
[appc.git] / appc-client / client-lib / src / main / java / org / onap / appc / client / impl / core / SyncRequestResponseHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2019 IBM
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.client.impl.core;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.util.concurrent.TimeoutException;
32
33 /** Handles sync requests
34  */
35 class SyncRequestResponseHandler<T> extends AbstractRequestResponseHandler {
36
37     private final EELFLogger LOG = EELFManager.getInstance().getLogger(SyncRequestResponseHandler.class);
38     private T responseObject = null;
39     private CoreException coreException = null;
40     private TimeoutException timeoutException = null;
41
42     SyncRequestResponseHandler(String corrID,
43                                ICoreResponseHandler callback,
44                                CoreManager coreManager){
45         super(corrID, callback, coreManager);
46     }
47
48     /**
49      *  Calls API callback for getting response object. in case of complete response notifies consumer
50      *  thread for receiving response
51      * @param response - Response
52      * @param type - Type of Response
53      */
54     synchronized void runTask(String response, String type) {
55         try {
56             responseObject = ((ICoreSyncResponseHandler) businessCallback).onResponse(response, type);
57         } catch (CoreException e) {
58             coreException = e;
59         }
60         if(responseObject != null || coreException != null) {
61             notify();
62         }
63     }
64
65
66     /**
67      * Returns response. goes sleep until coming either timeout event or complete response
68      */
69     public synchronized  <T> T getResponse() throws CoreException, TimeoutException {
70         try{
71             if(!isResponseReceived()){
72                 wait();
73             }
74             if (coreException != null) {
75                 throw coreException;
76             }
77             if ( timeoutException != null) {
78                 throw timeoutException;
79             }
80
81         } catch (InterruptedException e) {
82             Thread.currentThread().interrupt();
83             throw new CoreException(e);
84         } finally{
85             coreManager.unregisterHandler(corrID);
86             coreManager.cancelTimer(corrID);
87         }
88         return (T) responseObject;
89     }
90
91     /**
92      * indicates if a response received
93      * @return
94      */
95     private boolean isResponseReceived() {
96         return responseObject != null;
97     }
98
99     @Override
100     public synchronized void onTimeOut() {
101         LOG.error("sync response handler on timeout correlation ID <" + corrID + ">.");
102         timeoutException = new TimeoutException("timeout for request with correlation-id " + corrID);
103         notify();
104     }
105
106
107
108
109 }