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;
39 import org.onap.sdc.api.notification.INotificationData;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class NotificationLogging implements InvocationHandler {
46 private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
48 protected static final Logger logger = LoggerFactory.getLogger(NotificationLogging.class);
50 private static InvocationHandler handler = (arg0, arg1, arg2) -> {
51 List<Method> methods = objectMethodsToLog.get(arg0);
52 if ((methods == null) || (methods.isEmpty())) {
53 // Do nothing for now...
57 return arg1.invoke(arg0, arg2);
60 public static InvocationHandler getHandler() {
67 private NotificationLogging() {}
69 private static final String[] GETTER_PREFIXES = { "get", "is" };
71 public static String logNotification(INotificationData iNotif) {
76 Class<? extends INotificationData> clazz = iNotif.getClass();
78 Method[] declaredMethods = clazz.getDeclaredMethods();
80 if (declaredMethods == null || declaredMethods.length == 0) {
81 return "EMPTY"; // No declared methods in this class !!!
84 StringBuilder buffer = new StringBuilder("ASDC Notification:");
85 buffer.append(System.lineSeparator());
87 for (Method m : declaredMethods) {
88 if ((m != null) && isGetter(m)) {
89 for (String prefix : GETTER_PREFIXES) {
90 if (m.getName().startsWith(prefix)) {
91 buffer.append(m.getName().substring(prefix.length()));
96 buffer.append(testNull(m.invoke(iNotif, (Object[])null)));
97 } catch (IllegalAccessException | IllegalArgumentException
98 | InvocationTargetException e) {
99 logger.debug("Exception", e);
100 buffer.append("UNREADABLE");
102 buffer.append(System.lineSeparator());
106 return buffer.toString();
109 private static boolean isGetter(Method method) {
111 // Must start with a valid (and known) prefix
112 boolean prefixFound = false;
113 for (String prefix : GETTER_PREFIXES) {
114 if (method.getName().startsWith(prefix)) {
123 // Must not take any input arguments
124 if (method.getParameterTypes().length != 0) {
128 // Must not have return type 'void'
129 if (void.class.equals(method.getReturnType())) {
134 if (!Modifier.isPublic(method.getModifiers())) {
141 private static String testNull(Object object) {
142 if (object == null) {
144 } else if (object instanceof Integer) {
145 return object.toString();
146 } else if (object instanceof String) {
147 return (String) object;
149 return "Type not recognized";
153 private static void registerForLog(INotificationData objectToLog) {
154 INotificationData proxy = (INotificationData) Proxy.newProxyInstance(
155 INotificationData.class.getClassLoader(),
156 new Class[] { INotificationData.class },
157 NotificationLogging.getHandler());
158 objectMethodsToLog.put(proxy, new ArrayList<>());
161 private static <T> void methodToLog(T methodCall) {
166 public Object invoke(Object proxy, Method method, Object[] args)
168 // TODO Auto-generated method stub