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