2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.client.impl.core;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
31 import java.util.concurrent.TimeoutException;
33 /** Handles sync requests
35 class SyncRequestResponseHandler<T> extends AbstractRequestResponseHandler {
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;
42 SyncRequestResponseHandler(String corrID,
43 ICoreResponseHandler callback,
44 CoreManager coreManager){
45 super(corrID, callback, coreManager);
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
54 synchronized void runTask(String response, String type) {
56 responseObject = ((ICoreSyncResponseHandler) businessCallback).onResponse(response, type);
57 } catch (CoreException e) {
60 if(responseObject != null || coreException != null) {
67 * Returns response. goes sleep until coming either timeout event or complete response
69 public synchronized <T> T getResponse() throws CoreException, TimeoutException {
71 if(!isResponseReceived()){
74 if (coreException != null) {
77 if ( timeoutException != null) {
78 throw timeoutException;
81 } catch (InterruptedException e) {
82 Thread.currentThread().interrupt();
83 throw new CoreException(e);
85 coreManager.unregisterHandler(corrID);
86 coreManager.cancelTimer(corrID);
88 return (T) responseObject;
92 * indicates if a response received
95 private boolean isResponseReceived() {
96 return responseObject != null;
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);