2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.client.impl.core;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.concurrent.TimeoutException;
31 /** Handles sync requests
33 class SyncRequestResponseHandler<T> extends AbstractRequestResponseHandler {
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;
40 SyncRequestResponseHandler(String corrID,
41 ICoreResponseHandler callback,
42 CoreManager coreManager){
43 super(corrID, callback, coreManager);
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
52 synchronized void runTask(String response, String type) {
54 responseObject = ((ICoreSyncResponseHandler) businessCallback).onResponse(response, type);
55 } catch (CoreException e) {
58 if(responseObject != null || coreException != null) {
65 * Returns response. goes sleep until coming either timeout event or complete response
67 public synchronized <T> T getResponse() throws CoreException, TimeoutException {
69 if(!isResponseReceived()){
72 if (coreException != null) {
75 if ( timeoutException != null) {
76 throw timeoutException;
79 } catch (InterruptedException e) {
80 throw new CoreException(e);
82 coreManager.unregisterHandler(corrID);
83 coreManager.cancelTimer(corrID);
85 return (T) responseObject;
89 * indicates if a response received
92 private boolean isResponseReceived() {
93 return responseObject != null;
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);