2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider;
23 import java.util.Arrays;
24 import java.util.LinkedHashMap;
26 import org.onap.policy.common.utils.coder.Coder;
27 import org.onap.policy.common.utils.coder.CoderException;
28 import org.onap.policy.common.utils.coder.StandardCoder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
36 private static final Logger logger = LoggerFactory.getLogger(Util.class);
43 * Extracts an object's identity by invoking {@link Object#toString()} and returning
44 * the portion starting with "@". Extraction is done on-demand, when toString() is
45 * called on the result. This is typically used when logging.
47 * @param object object whose identity is to be extracted
48 * @return an object that will extract the source object's identity when this object's
49 * toString() method is called
51 public static Object ident(Object object) {
52 return new DelayedIdentString(object);
56 * Runs a function and logs a message if it throws an exception. Does <i>not</i>
57 * re-throw the exception.
59 * @param function function to be run
60 * @param exceptionMessage message to log if an exception is thrown
61 * @param exceptionArgs arguments to be passed to the logger
63 public static void runFunction(Runnable function, String exceptionMessage, Object... exceptionArgs) {
67 } catch (RuntimeException ex) {
68 // create a new array containing the original arguments plus the exception
69 Object[] allArgs = Arrays.copyOf(exceptionArgs, exceptionArgs.length + 1);
70 allArgs[exceptionArgs.length] = ex;
72 logger.warn(exceptionMessage, allArgs);
77 * Translates parameters from one class to another, typically from a Map to a POJO or
80 * @param identifier identifier of the actor/operation being translated; used to build
81 * an exception message
82 * @param source source object to be translated
83 * @param clazz target class
84 * @return the translated object
86 public static <T> T translate(String identifier, Object source, Class<T> clazz) {
87 Coder coder = new StandardCoder();
90 String json = coder.encode(source);
91 return coder.decode(json, clazz);
93 } catch (CoderException | RuntimeException e) {
94 throw new IllegalArgumentException("cannot translate parameters for " + identifier, e);
99 * Translates parameters to a Map.
101 * @param identifier identifier of the actor/operation being translated; used to build
102 * an exception message
103 * @param source source parameters
104 * @return the parameters, as a Map
106 @SuppressWarnings("unchecked")
107 public static Map<String, Object> translateToMap(String identifier, Object source) {
108 if (source == null) {
112 return translate(identifier, source, LinkedHashMap.class);