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 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
23 package org.openecomp.mso.asdc.util;
27 import java.lang.reflect.InvocationHandler;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Modifier;
31 import java.lang.reflect.Proxy;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
37 import org.openecomp.mso.logger.MsoLogger;
38 import org.openecomp.sdc.api.notification.INotificationData;
41 public class NotificationLogging implements InvocationHandler {
43 private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
45 protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
47 private static InvocationHandler handler = new InvocationHandler() {
49 public Object invoke(Object arg0, Method arg1, Object[] 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);
61 public static InvocationHandler getHandler() {
68 private NotificationLogging() {}
70 private static final String[] GETTER_PREFIXES = { "get", "is" };
72 public static String logNotification(INotificationData iNotif) {
77 Class<? extends INotificationData> clazz = iNotif.getClass();
79 Method[] declaredMethods = clazz.getDeclaredMethods();
81 if (declaredMethods == null || declaredMethods.length == 0) {
82 return "EMPTY"; // No declared methods in this class !!!
85 StringBuilder buffer = new StringBuilder("ASDC Notification:");
86 buffer.append(System.lineSeparator());
88 for (Method m : declaredMethods) {
89 if ((m != null) && isGetter(m)) {
90 for (String prefix : GETTER_PREFIXES) {
91 if (m.getName().startsWith(prefix)) {
92 buffer.append(m.getName().substring(prefix.length()));
97 buffer.append(testNull(m.invoke(iNotif, (Object[])null)));
98 } catch (IllegalAccessException | IllegalArgumentException
99 | InvocationTargetException e) {
100 LOGGER.debug("Exception :"+e);
101 buffer.append("UNREADABLE");
103 buffer.append(System.lineSeparator());
107 return buffer.toString();
110 private static final boolean isGetter(Method method) {
112 // Must start with a valid (and known) prefix
113 boolean prefixFound = false;
114 for (String prefix : GETTER_PREFIXES) {
115 if (method.getName().startsWith(prefix)) {
124 // Must not take any input arguments
125 if (method.getParameterTypes().length != 0) {
129 // Must not have return type 'void'
130 if (void.class.equals(method.getReturnType())) {
135 if (!Modifier.isPublic(method.getModifiers())) {
142 private static String testNull(Object object) {
143 if (object == null) {
145 } else if (object instanceof Integer) {
146 return object.toString();
147 } else if (object instanceof String) {
148 return (String) object;
150 return "Type not recognized";
154 private static void registerForLog(INotificationData objectToLog) {
155 INotificationData proxy = (INotificationData) Proxy.newProxyInstance(
156 INotificationData.class.getClassLoader(),
157 new Class[] { INotificationData.class },
158 NotificationLogging.getHandler());
159 objectMethodsToLog.put(proxy, new ArrayList<Method>());
162 private static <T> void methodToLog(T methodCall) {
167 public Object invoke(Object proxy, Method method, Object[] args)
169 // TODO Auto-generated method stub