2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
25 package org.onap.so.asdc.util;
29 import java.lang.reflect.InvocationHandler;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.lang.reflect.Modifier;
33 import java.lang.reflect.Proxy;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
38 import org.onap.sdc.api.notification.INotificationData;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class NotificationLogging implements InvocationHandler {
45 private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
47 protected static final Logger logger = LoggerFactory.getLogger(NotificationLogging.class);
49 private static InvocationHandler handler = (arg0, arg1, arg2) -> {
50 List<Method> methods = objectMethodsToLog.get(arg0);
51 if ((methods == null) || (methods.isEmpty())) {
52 // Do nothing for now...
56 return arg1.invoke(arg0, arg2);
59 public static InvocationHandler getHandler() {
66 private NotificationLogging() {}
68 private static final String[] GETTER_PREFIXES = {"get", "is"};
70 public static String logNotification(INotificationData iNotif) {
75 Class<? extends INotificationData> clazz = iNotif.getClass();
77 Method[] declaredMethods = clazz.getDeclaredMethods();
79 if (declaredMethods == null || declaredMethods.length == 0) {
80 return "EMPTY"; // No declared methods in this class !!!
83 StringBuilder buffer = new StringBuilder("ASDC Notification:");
84 buffer.append(System.lineSeparator());
86 for (Method m : declaredMethods) {
87 if ((m != null) && isGetter(m)) {
88 for (String prefix : GETTER_PREFIXES) {
89 if (m.getName().startsWith(prefix)) {
90 buffer.append(m.getName().substring(prefix.length()));
95 buffer.append(testNull(m.invoke(iNotif, (Object[]) null)));
96 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
97 logger.debug("Exception", e);
98 buffer.append("UNREADABLE");
100 buffer.append(System.lineSeparator());
104 return buffer.toString();
107 private static boolean isGetter(Method method) {
109 // Must start with a valid (and known) prefix
110 boolean prefixFound = false;
111 for (String prefix : GETTER_PREFIXES) {
112 if (method.getName().startsWith(prefix)) {
121 // Must not take any input arguments
122 if (method.getParameterTypes().length != 0) {
126 // Must not have return type 'void'
127 if (void.class.equals(method.getReturnType())) {
132 if (!Modifier.isPublic(method.getModifiers())) {
139 private static String testNull(Object object) {
140 if (object == null) {
142 } else if (object instanceof Integer) {
143 return object.toString();
144 } else if (object instanceof String) {
145 return (String) object;
147 return "Type not recognized";
151 private static void registerForLog(INotificationData objectToLog) {
152 INotificationData proxy = (INotificationData) Proxy.newProxyInstance(INotificationData.class.getClassLoader(),
153 new Class[] {INotificationData.class}, NotificationLogging.getHandler());
154 objectMethodsToLog.put(proxy, new ArrayList<>());
157 private static <T> void methodToLog(T methodCall) {
162 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
163 // TODO Auto-generated method stub