All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scouter.server.plugin.PluginHelper Maven / Gradle / Ivy

/*
 *  Copyright 2015 the original author or authors.
 *  @https://github.com/scouter-project/scouter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package scouter.server.plugin;

import scouter.lang.AlertLevel;
import scouter.lang.TextTypes;
import scouter.lang.conf.ConfigDesc;
import scouter.lang.conf.Internal;
import scouter.lang.conf.ParamDesc;
import scouter.lang.pack.AlertPack;
import scouter.server.Logger;
import scouter.server.core.AlertCore;
import scouter.server.db.TextRD;
import scouter.server.plugin.alert.RealCounter;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Utility class for script plugin
 * Created by gunlee on 2017. 8. 18.
 * @since v1.7.3
 */
public class PluginHelper {
	private static final String NO_DATE = "00000000";
	private static Map reflCache = Collections.synchronizedMap(new LinkedHashMap(100));

	private static PluginHelper instance =new PluginHelper();
	private PluginHelper() {}

	public static PluginHelper getInstance() {
		return instance;
	}

	@ConfigDesc("logging")
	public void log(Object c) {
		Logger.println(c);
	}

	@ConfigDesc("System.out.println")
	public void println(Object c) {
		System.out.println(c);
	}

	@ConfigDesc("get NumberFormatter set fraction 1")
	public NumberFormat getNumberFormatter() {
		return getNumberFormatter(1);
	}

	@ConfigDesc("get NumberFormatter set the fraction")
	@ParamDesc("int fractionDigits")
	public NumberFormat getNumberFormatter(int fractionDigits) {
		NumberFormat f = NumberFormat.getInstance();
		f.setMaximumFractionDigits(fractionDigits);
		return f;
	}

	@ConfigDesc("format the number as fraction 1")
	public String formatNumber(float f) {
		return formatNumber(f, 1);
	}

	@ConfigDesc("format the number as given fraction")
	@ParamDesc("float f, int fractionDigits")
	public String formatNumber(float f, int fractionDigits) {
		return getNumberFormatter(fractionDigits).format(f);
	}

	@ConfigDesc("format the number as fraction 1")
	public String formatNumber(double v) {
		return formatNumber(v, 1);
	}

	@ConfigDesc("format the number as given fraction")
	@ParamDesc("double v, int fractionDigits")
	public String formatNumber(double v, int fractionDigits) {
		return getNumberFormatter(fractionDigits).format(v);
	}

	@ConfigDesc("format the number as fraction 1")
	public String formatNumber(int v) {
		return formatNumber(v, 1);
	}

	@ConfigDesc("format the number as given fraction")
	@ParamDesc("int v, int fractionDigits")
	public String formatNumber(int v, int fractionDigits) {
		return getNumberFormatter(fractionDigits).format(v);
	}

	@ConfigDesc("format the number as fraction 1")
	public String formatNumber(long v) {
		return formatNumber(v, 1);
	}

	@ConfigDesc("format the number as given fraction")
	@ParamDesc("long v, int fractionDigits")
	public String formatNumber(long v, int fractionDigits) {
		return getNumberFormatter(fractionDigits).format(v);
	}

	@ConfigDesc("alert as info level")
	@ParamDesc("int objHash, String objType, String title, String message")
	public void alertInfo(int objHash, String objType, String title, String message) {
		alert(AlertLevel.INFO, objHash, objType, title, message);
	}

	@ConfigDesc("alert as warn level")
	@ParamDesc("int objHash, String objType, String title, String message")
	public void alertWarn(int objHash, String objType, String title, String message) {
		alert(AlertLevel.WARN, objHash, objType, title, message);
	}

	@ConfigDesc("alert as error level")
	@ParamDesc("int objHash, String objType, String title, String message")
	public void alertError(int objHash, String objType, String title, String message) {
		alert(AlertLevel.ERROR, objHash, objType, title, message);
	}

	@ConfigDesc("alert as fatal level")
	@ParamDesc("int objHash, String objType, String title, String message")
	public void alertFatal(int objHash, String objType, String title, String message) {
		alert(AlertLevel.FATAL, objHash, objType, title, message);
	}

	@Internal
	public void alert(byte level, int objHash, String objType, String title, String message) {
		AlertPack p = new AlertPack();
		p.time = System.currentTimeMillis();
		p.level = level;
		p.objHash = objHash;
		p.objType = objType;
		p.title = title;
		p.message = message;
		AlertCore.add(p);
	}

	@Internal
	public String getErrorString(int hash) {
		return getErrorString(NO_DATE, hash);
	}
	@Internal
	public String getErrorString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.ERROR, hash);
	}

	@Internal
	public String getApicallString(int hash) {
		return getApicallString(NO_DATE, hash);
	}
	@Internal
	public String getApicallString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.APICALL, hash);
	}

	@Internal
	public String getMethodString(int hash) {
		return getMethodString(NO_DATE, hash);
	}
	@Internal
	public String getMethodString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.METHOD, hash);
	}

	@Internal
	public String getServiceString(int hash) {
		return getServiceString(NO_DATE, hash);
	}
	@Internal
	public String getServiceString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.SERVICE, hash);
	}

	@Internal
	public String getSqlString(int hash) {
		return getSqlString(NO_DATE, hash);
	}
	@Internal
	public String getSqlString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.SQL, hash);
	}

	@Internal
	public String getObjectString(int hash) {
		return getObjectString(NO_DATE, hash);
	}
	@Internal
	public String getObjectString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.OBJECT, hash);
	}

	@Internal
	public String getRefererString(int hash) {
		return getRefererString(NO_DATE, hash);
	}
	@Internal
	public String getRefererString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.REFERER, hash);
	}

	@Internal
	public String getUserAgentString(int hash) {
		return getUserAgentString(NO_DATE, hash);
	}
	@Internal
	public String getUserAgentString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.USER_AGENT, hash);
	}

	@Internal
	public String getUserGroupString(int hash) {
		return getUserGroupString(NO_DATE, hash);
	}
	@Internal
	public String getUserGroupString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.GROUP, hash);
	}

	@Internal
	public String getCityString(int hash) {
		return getCityString(NO_DATE, hash);
	}
	@Internal
	public String getCityString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.CITY, hash);
	}

	@Internal
	public String getLoginString(int hash) {
		return getLoginString(NO_DATE, hash);
	}
	@Internal
	public String getLoginString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.LOGIN, hash);
	}

	@Internal
	public String getDescString(int hash) {
		return getDescString(NO_DATE, hash);
	}
	@Internal
	public String getDescString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.DESC, hash);
	}

	@Internal
	public String getWebString(int hash) {
		return getWebString(NO_DATE, hash);
	}
	@Internal
	public String getWebString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.WEB, hash);
	}

	@Internal
	public String getHashMsgString(int hash) {
		return getHashMsgString(NO_DATE, hash);
	}
	@Internal
	public String getHashMsgString(String yyyymmdd, int hash) {
		return TextRD.getString(yyyymmdd, TextTypes.HASH_MSG, hash);
	}

	@Internal
	@ConfigDesc("reflection util")
	public Object invokeMethod(Object o, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
		Object[] objs = {};
		return invokeMethod(o, methodName, objs);
	}

	@Internal
	@ConfigDesc("reflection util")
	public Object invokeMethod(Object o, String methodName, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
		int argsSize = args.length;
		StringBuilder signature = new StringBuilder(o.getClass().getName()).append(":").append(methodName).append("():");

		Class[] argClazzes = new Class[argsSize];

		for(int i=0; i:");

		for(int i=0; i parameterTypeNames;
		public String returnTypeName;
	}

	private static List pluginHelperDesc;

	public static synchronized List getPluginHelperDesc() {
		if (pluginHelperDesc != null) {
			return pluginHelperDesc;
		}
		List descList = new ArrayList();
		Method[] methods = PluginHelper.class.getDeclaredMethods();
		for (Method method : methods) {
			int mod = method.getModifiers();
			if (Modifier.isStatic(mod) == false && Modifier.isPublic(mod)) {
				Deprecated deprecated = method.getAnnotation(Deprecated.class);
				Internal internal = method.getAnnotation(Internal.class);
				if (deprecated != null || internal != null) {
					continue;
				}

				List typeClassNameList = new ArrayList();

				Class[] clazzes = method.getParameterTypes();
				ParamDesc paramDesc = method.getAnnotation(ParamDesc.class);
				if (paramDesc != null) {
					typeClassNameList.add(paramDesc.value());
				} else {
					for (Class clazz : clazzes) {
						typeClassNameList.add(clazz.getName());
					}
				}
				ConfigDesc configDesc = method.getAnnotation(ConfigDesc.class);

				PluginHelper.Desc desc = new PluginHelper.Desc();
				desc.methodName = method.getName();
				desc.returnTypeName = method.getReturnType().getName();
				if (configDesc != null) {
					desc.desc = configDesc.value();
				}
				desc.parameterTypeNames = typeClassNameList;
				descList.add(desc);
			}
		}
		pluginHelperDesc = descList;
		return pluginHelperDesc;
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy