e7a65766e453ed7fa9ff1b71899a937bc19f19ee
[appc.git] / appc-client / client-lib / src / main / java / org / openecomp / appc / client / impl / core / SyncRequestResponseHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.client.impl.core;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.util.concurrent.TimeoutException;
31
32 /** Handles sync requests
33  */
34 class SyncRequestResponseHandler<T> extends AbstractRequestResponseHandler {
35
36     private final EELFLogger LOG = EELFManager.getInstance().getLogger(SyncRequestResponseHandler.class);
37     private T responseObject = null;
38     private CoreException coreException = null;
39     private TimeoutException timeoutException = null;
40
41     SyncRequestResponseHandler(String corrID,
42                                ICoreResponseHandler callback,
43                                CoreManager coreManager){
44         super(corrID, callback, coreManager);
45     }
46
47     /**
48      *  Calls API callback for getting response object. in case of complete response notifies consumer
49      *  thread for receiving response
50      * @param response - Response
51      * @param type - Type of Response
52      */
53     synchronized void runTask(String response, String type) {
54         try {
55             responseObject = ((ICoreSyncResponseHandler) businessCallback).onResponse(response, type);
56         } catch (CoreException e) {
57             coreException = e;
58         }
59         if(responseObject != null || coreException != null) {
60             notify();
61         }
62     }
63
64
65     /**
66      * Returns response. goes sleep until coming either timeout event or complete response
67      */
68     public synchronized  <T> T getResponse() throws CoreException, TimeoutException {
69         try{
70             if(!isResponseReceived()){
71                 wait();
72             }
73             if (coreException != null) {
74                 throw coreException;
75             }
76             if ( timeoutException != null) {
77                 throw timeoutException;
78             }
79
80         } catch (InterruptedException e) {
81             throw new CoreException(e);
82         } finally{
83             coreManager.unregisterHandler(corrID);
84             coreManager.cancelTimer(corrID);
85         }
86         return (T) responseObject;
87     }
88
89     /**
90      * indicates if a response received
91      * @return
92      */
93     private boolean isResponseReceived() {
94         return responseObject != null;
95     }
96
97     @Override
98     public synchronized void onTimeOut() {
99         LOG.error("sync response handler on timeout correlation ID <" + corrID + ">.");
100         timeoutException = new TimeoutException("timeout for request with correlation-id " + corrID);
101         notify();
102     }
103
104
105
106
107 }