2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
22 package org.openecomp.mso.asdc.util;
26 import java.lang.reflect.InvocationHandler;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
30 import java.lang.reflect.Proxy;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
36 import org.openecomp.sdc.api.notification.INotificationData;
39 public class NotificationLogging implements InvocationHandler {
41 private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
43 private static InvocationHandler handler = new InvocationHandler() {
45 public Object invoke(Object arg0, Method arg1, Object[] arg2)
47 List<Method> methods = objectMethodsToLog.get(arg0);
48 if ((methods == null) || (methods.isEmpty())) {
49 // Do nothing for now...
53 return arg1.invoke(arg0, arg2);
57 public static InvocationHandler getHandler() {
64 private NotificationLogging() {}
66 private static final String[] GETTER_PREFIXES = { "get", "is" };
68 public static String logNotification(INotificationData iNotif) {
73 Class<? extends INotificationData> clazz = iNotif.getClass();
75 Method[] declaredMethods = clazz.getDeclaredMethods();
77 if (declaredMethods == null || declaredMethods.length == 0) {
78 return "EMPTY"; // No declared methods in this class !!!
81 StringBuffer buffer = new StringBuffer("ASDC Notification:");
82 buffer.append(System.lineSeparator());
84 for (Method m : declaredMethods) {
85 if ((m != null) && isGetter(m)) {
86 for (String prefix : GETTER_PREFIXES) {
87 if (m.getName().startsWith(prefix)) {
88 buffer.append(m.getName().substring(prefix.length()));
93 buffer.append(testNull(m.invoke(iNotif, (Object[])null)));
94 } catch (IllegalAccessException | IllegalArgumentException
95 | InvocationTargetException e) {
96 buffer.append("UNREADABLE");
98 buffer.append(System.lineSeparator());
102 return buffer.toString();
105 private static final boolean isGetter(Method method) {
107 // Must start with a valid (and known) prefix
108 boolean prefixFound = false;
109 for (String prefix : GETTER_PREFIXES) {
110 if (method.getName().startsWith(prefix)) {
119 // Must not take any input arguments
120 if (method.getParameterTypes().length != 0) {
124 // Must not have return type 'void'
125 if (void.class.equals(method.getReturnType())) {
130 if (!Modifier.isPublic(method.getModifiers())) {
137 private static String testNull(Object object) {
138 if (object == null) {
140 } else if (object instanceof Integer) {
141 return object.toString();
142 } else if (object instanceof String) {
143 return (String) object;
145 return "Type not recognized";
149 private static void registerForLog(INotificationData objectToLog) {
150 INotificationData proxy = (INotificationData) Proxy.newProxyInstance(
151 INotificationData.class.getClassLoader(),
152 new Class[] { INotificationData.class },
153 NotificationLogging.getHandler());
154 objectMethodsToLog.put(proxy, new ArrayList<Method>());
157 private static <T> void methodToLog(T methodCall) {
162 public Object invoke(Object proxy, Method method, Object[] args)
164 // TODO Auto-generated method stub