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);
 
  37     private static final Coder coder = new StandardCoder();
 
  44      * Extracts an object's identity by invoking {@link Object#toString()} and returning
 
  45      * the portion starting with "@". Extraction is done on-demand, when toString() is
 
  46      * called on the result. This is typically used when logging.
 
  48      * @param object object whose identity is to be extracted
 
  49      * @return an object that will extract the source object's identity when this object's
 
  50      *         toString() method is called
 
  52     public static Object ident(Object object) {
 
  53         return new DelayedIdentString(object);
 
  57      * Runs a function and logs a message if it throws an exception. Does <i>not</i>
 
  58      * re-throw the exception.
 
  60      * @param function function to be run
 
  61      * @param exceptionMessage message to log if an exception is thrown
 
  62      * @param exceptionArgs arguments to be passed to the logger
 
  64     public static void runFunction(Runnable function, String exceptionMessage, Object... exceptionArgs) {
 
  68         } catch (RuntimeException ex) {
 
  69             // create a new array containing the original arguments plus the exception
 
  70             Object[] allArgs = Arrays.copyOf(exceptionArgs, exceptionArgs.length + 1);
 
  71             allArgs[exceptionArgs.length] = ex;
 
  73             logger.warn(exceptionMessage, allArgs);
 
  78      * Translates parameters from one class to another, typically from a Map to a POJO or
 
  81      * @param identifier identifier of the actor/operation being translated; used to build
 
  82      *        an exception message
 
  83      * @param source source object to be translated
 
  84      * @param clazz target class
 
  85      * @return the translated object
 
  87     public static <T> T translate(String identifier, Object source, Class<T> clazz) {
 
  89             return coder.convert(source, clazz);
 
  91         } catch (CoderException | RuntimeException e) {
 
  92             throw new IllegalArgumentException("cannot translate parameters for " + identifier, e);
 
  97      * Translates parameters to a Map.
 
  99      * @param identifier identifier of the actor/operation being translated; used to build
 
 100      *        an exception message
 
 101      * @param source source parameters
 
 102      * @return the parameters, as a Map
 
 104     @SuppressWarnings("unchecked")
 
 105     public static Map<String, Object> translateToMap(String identifier, Object source) {
 
 106         return translate(identifier, source, LinkedHashMap.class);