2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.client.impl.core;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.util.concurrent.TimeoutException;
32 /** Handles sync requests
34 class SyncRequestResponseHandler<T> extends AbstractRequestResponseHandler {
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;
41 SyncRequestResponseHandler(String corrID,
42 ICoreResponseHandler callback,
43 CoreManager coreManager){
44 super(corrID, callback, coreManager);
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
53 synchronized void runTask(String response, String type) {
55 responseObject = ((ICoreSyncResponseHandler) businessCallback).onResponse(response, type);
56 } catch (CoreException e) {
59 if(responseObject != null || coreException != null) {
66 * Returns response. goes sleep until coming either timeout event or complete response
68 public synchronized <T> T getResponse() throws CoreException, TimeoutException {
70 if(!isResponseReceived()){
73 if (coreException != null) {
76 if ( timeoutException != null) {
77 throw timeoutException;
80 } catch (InterruptedException e) {
81 throw new CoreException(e);
83 coreManager.unregisterHandler(corrID);
84 coreManager.cancelTimer(corrID);
86 return (T) responseObject;
90 * indicates if a response received
93 private boolean isResponseReceived() {
94 return responseObject != null;
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);