Java tutorial
/******************************************************************************* * ENdoSnipe 5.3 - (https://github.com/endosnipe) * * The MIT License (MIT) * * Copyright (c) 2015 Acroquest Technology Co.,Ltd. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ package jp.co.acroquest.endosnipe.web.explorer.entity; import java.text.DecimalFormat; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.management.AttributeChangeNotification; import javax.management.Notification; import javax.management.NotificationListener; import jp.co.acroquest.endosnipe.common.entity.ItemType; import jp.co.acroquest.endosnipe.common.util.StringUtil; import jp.co.acroquest.endosnipe.communicator.entity.Body; import jp.co.acroquest.endosnipe.communicator.entity.ResponseBody; import jp.co.acroquest.endosnipe.communicator.entity.Telegram; import jp.co.acroquest.endosnipe.communicator.entity.TelegramConstants; import org.apache.commons.lang.StringUtils; /** * ?????<br /> * * @author hitramatsu * */ public class InvocationModel implements Comparable<InvocationModel>, NotificationListener, TelegramConstants { /** ?? */ private static final int INDEX_METHODNAME = 1; /** ?? */ private static final int INDEX_CLASSNAME = 0; /** ??/CPU/USER? */ private static final DecimalFormat AVERAGE_FORMAT = new DecimalFormat("0.000"); /** ????????? */ public static final long THRESHOLD_NOT_SPECIFIED = -1; /** ?? */ private ComponentModel component_; /** Javelin */ private String targetAddress_; /** ?? */ private String className_; /** ?? */ private String methodName_; /** */ private Date date_; /** ??? */ private long count_; /** ?????: */ private long accumulatedMinimum_; /** ?????: */ private long accumulatedMaximum_; /** ?????? */ private long accumulatedTotal_; /** ??CPU???: */ private long accumulatedCpuMinimum_; /** ??CPU???: */ private long accumulatedCpuMaximum_; /** ???CPU??? */ private long accumulatedCpuTotal_; /** ??CPU???: */ private long accumulatedUserMinimum_; /** ??CPU???: */ private long accumulatedUserMaximum_; /** ???CPU??? */ private long accumulatedUserTotal_; /** ????: */ private long minimum_; /** ????: */ private long maximum_; /** ????? */ private long total_; /** ?CPU???: */ private long cpuMinimum_; /** ?CPU???: */ private long cpuMaximum_; /** ??CPU??? */ private long cpuTotal_; /** ?USER???: */ private long userMinimum_; /** ?USER???: */ private long userMaximum_; /** ??USER??? */ private long userTotal_; /** ?? */ private long throwableCount_; /** ?? */ private long methodStallCount_; /** HTTP? */ private long httpStatusCount_; /** */ private boolean target_ = true; /** ??? <code>true</code> ?????? <code>false</code> */ private boolean transactionGraphOutput_ = false; /** ???? */ private List<Throwable> throwableList_; /** ??? */ private long alarmThreshold_ = THRESHOLD_NOT_SPECIFIED; /** ???CPU */ private long alarmCpuThreshold_ = THRESHOLD_NOT_SPECIFIED; /** ???????? <code>true</code> */ private boolean modifiedByUser_ = false; /** ? */ private static final String EXCEED_THRESHOLD_ALARM = "Alarm:EXCEED_THRESHOLD"; /** */ private static final int MILLIS = 1000; /** * ??????<br /> * * @return ?? */ public String getClassName() { return this.className_; } /** * ?????<br /> * * @param className ?? */ public void setClassName(final String className) { this.className_ = className; } /** * URL????<br /> * * @return URL */ public String getTargetAddress() { return this.targetAddress_; } /** * URL???<br /> * * @param targetAddress URL */ public void setTargetAddress(final String targetAddress) { this.targetAddress_ = targetAddress; } /** * ?????<br /> * * @return ? */ public long getCount() { return this.count_; } /** * ????<br /> * * @param count ? */ public void setCount(final long count) { this.count_ = count; } /** * ????????????<br /> * ??? 1 ???????? <code>0</code> ???<br /> * * @return ?? */ public String getAccumulatedAverage() { return AVERAGE_FORMAT.format(getAccumulatedAverageDouble()); } /** * ?double????<br /> * * @return ? */ public double getAccumulatedAverageDouble() { double result; if (getCount() == 0) { result = 0; } else { result = (double) getAccumulatedTotal() / getCount(); } return result; } /** * ??????<br /> * * @return ?? */ public long getAccumulatedTotal() { return this.accumulatedTotal_; } /** * ?????<br /> * * @param accumulatedTotal ?? */ public void setAccumulatedTotal(final long accumulatedTotal) { this.accumulatedTotal_ = accumulatedTotal; } /** * ??????<br /> * * @return ?? */ public long getAccumulatedMaximum() { return this.accumulatedMaximum_; } /** * ?????<br /> * * @param accumulatedMaximum ?? */ public void setAccumulatedMaximum(final long accumulatedMaximum) { this.accumulatedMaximum_ = accumulatedMaximum; } /** * ???????<br /> * * @return ??? */ public long getAccumulatedMinimum() { return this.accumulatedMinimum_; } /** * ??????<br /> * * @param accumulatedMinimum ??? */ public void setAccumulatedMinimum(final long accumulatedMinimum) { this.accumulatedMinimum_ = accumulatedMinimum; } /** * ????????????<br /> * ??? 1 ???????? <code>0</code> ???<br /> * * @return ?? */ public String getAccumulatedCpuAverage() { return AVERAGE_FORMAT.format(getAccumulatedCpuAverageDouble()); } /** * ?double????<br /> * * @return ? */ public double getAccumulatedCpuAverageDouble() { double result; if (getCount() == 0) { result = 0; } else { result = (double) getAccumulatedCpuTotal() / getCount(); } return result; } /** * ?CPU?????<br /> * * @return ?CPU? */ public long getAccumulatedCpuTotal() { return this.accumulatedCpuTotal_; } /** * ?CPU????<br /> * * @param accumulatedCpuTotal ?CPU? */ public void setAccumulatedCpuTotal(final long accumulatedCpuTotal) { this.accumulatedCpuTotal_ = accumulatedCpuTotal; } /** * ?CPU??????<br /> * * @return ?CPU?? */ public long getAccumulatedCpuMinimum() { return this.accumulatedCpuMinimum_; } /** * ?CPU?????<br /> * * @param accumulatedCpuMinimum ?CPU?? */ public void setAccumulatedCpuMinimum(final long accumulatedCpuMinimum) { this.accumulatedCpuMinimum_ = accumulatedCpuMinimum; } /** * ?CPU?????<br /> * * @return ?CPU? */ public long getAccumulatedCpuMaximum() { return this.accumulatedCpuMaximum_; } /** * ?CPU????<br /> * * @param accumulatedCpuMaximum ?CPU? */ public void setAccumulatedCpuMaximum(final long accumulatedCpuMaximum) { this.accumulatedCpuMaximum_ = accumulatedCpuMaximum; } /** * ????????????<br /> * ??? 1 ???????? <code>0</code> ???<br /> * * @return ?? */ public String getAccumulatedUserAverage() { return AVERAGE_FORMAT.format(getAccumulatedUserAverageDouble()); } /** * ?double????<br /> * * @return ? */ public double getAccumulatedUserAverageDouble() { double result; if (getCount() == 0) { result = 0; } else { result = (double) getAccumulatedUserTotal() / getCount(); } return result; } /** * ??????<br /> * * @return ?? */ public long getAccumulatedUserTotal() { return this.accumulatedUserTotal_; } /** * ?????<br /> * * @param accumulatedUserTotal ?? */ public void setAccumulatedUserTotal(final long accumulatedUserTotal) { this.accumulatedUserTotal_ = accumulatedUserTotal; } /** * ???????<br /> * * @return ??? */ public long getAccumulatedUserMinimum() { return this.accumulatedUserMinimum_; } /** * ??????<br /> * * @param accumulatedUserMinimum ??? */ public void setAccumulatedUserMinimum(final long accumulatedUserMinimum) { this.accumulatedUserMinimum_ = accumulatedUserMinimum; } /** * ??????<br /> * * @return ?? */ public long getAccumulatedUserMaximum() { return this.accumulatedUserMaximum_; } /** * ?????<br /> * * @param accumulatedUserMaximum ?? */ public void setAccumulatedUserMaximum(final long accumulatedUserMaximum) { this.accumulatedUserMaximum_ = accumulatedUserMaximum; } /** * ????????????<br /> * ??? 1 ???????? <code>0</code> ???<br /> * * @return ?? */ public String getAverage() { return AVERAGE_FORMAT.format(getAverageDouble()); } /** * ?double????<br /> * * @return ? */ public double getAverageDouble() { double result; if (getCount() == 0) { result = 0; } else { result = (double) getTotal() / getCount(); } return result; } /** * ??????<br /> * * @return ?? */ public long getTotal() { return this.total_; } /** * ?????<br /> * * @param total ?? */ public void setTotal(final long total) { this.total_ = total; } /** * ??????<br /> * * @return ?? */ public long getMaximum() { return this.maximum_; } /** * ?????<br /> * * @param maximum ?? */ public void setMaximum(final long maximum) { this.maximum_ = maximum; } /** * ???????<br /> * * @return ??? */ public long getMinimum() { return this.minimum_; } /** * ??????<br /> * * @param minimum ??? */ public void setMinimum(final long minimum) { this.minimum_ = minimum; } /** * ?CPU?????<br /> * * @return ?CPU? */ public long getCpuTotal() { return this.cpuTotal_; } /** * ?CPU????<br /> * * @param cpuTotal ?CPU? */ public void setCpuTotal(final long cpuTotal) { this.cpuTotal_ = cpuTotal; } /** * ?CPU??????<br /> * * @return ?CPU?? */ public long getCpuMinimum() { return this.cpuMinimum_; } /** * ?CPU?????<br /> * * @param cpuMinimum ?CPU?? */ public void setCpuMinimum(final long cpuMinimum) { this.cpuMinimum_ = cpuMinimum; } /** * ?CPU?????<br /> * * @return ?CPU? */ public long getCpuMaximum() { return this.cpuMaximum_; } /** * ?CPU????<br /> * * @param cpuMaximum ?CPU? */ public void setCpuMaximum(final long cpuMaximum) { this.cpuMaximum_ = cpuMaximum; } /** * ?? CPU ??????????<br /> * * ??? 1 ???????? <code>0</code> ???<br /> * * @return ?? CPU */ public String getCpuAverage() { double result; if (getCount() == 0) { result = 0; } else { result = (double) getCpuTotal() / getCount(); } return AVERAGE_FORMAT.format(result); } /** * ??????<br /> * * @return ?? */ public long getUserMinimum() { return this.userMinimum_; } /** * ?????<br /> * * @param userMinimum ?? */ public void setUserMinimum(final long userMinimum) { this.userMinimum_ = userMinimum; } /** * ?????<br /> * * @return ? */ public long getUserMaximum() { return this.userMaximum_; } /** * ?????<br /> * * @param userMaximum ? */ public void setUserMaximum(final long userMaximum) { this.userMaximum_ = userMaximum; } /** * ?????<br /> * * @return ? */ public long getUserTotal() { return this.userTotal_; } /** * ????<br /> * * @param userTotal ? */ public void setUserTotal(final long userTotal) { this.userTotal_ = userTotal; } /** * ?? USER ??????????<br /> * * ??? 1 ???????? <code>0</code> ???<br /> * * @return ?? USER */ public String getUserAverage() { double result; if (getCount() == 0) { result = 0; } else { result = (double) getUserTotal() / getCount(); } return AVERAGE_FORMAT.format(result); } /** * ????<br /> * * @return */ public long getThrowableCount() { return this.throwableCount_; } /** * ???<br /> * * @param throwableCount */ public void setThrowableCount(final long throwableCount) { this.throwableCount_ = throwableCount; } /** * ????????<br /> * * @return ???? */ public List<Throwable> getThrowableList() { return this.throwableList_; } /** * ???????<br /> * * @param throwableList ???? */ public void setThrowableList(final List<Throwable> throwableList) { this.throwableList_ = throwableList; } /** * ????<br /> * * @return */ public long getMethodStallCount() { return this.methodStallCount_; } /** * ???<br /> * * @param methodStall */ public void setMethodStallCount(final long methodStall) { this.methodStallCount_ = methodStall; } /** * ??????<br /> * * @return ?? */ public ComponentModel getComponent() { return this.component_; } /** * ?????<br /> * * @param component ?? */ public void setComponent(final ComponentModel component) { this.component_ = component; } /** * ??????<br /> * * @return ?? */ public String getMethodName() { return this.methodName_; } /** * ?????<br /> * * @param methodName ?? */ public void setMethodName(final String methodName) { this.methodName_ = methodName; } /** * ?????<br /> * * @return ? */ public long getAlarmThreshold() { return this.alarmThreshold_; } /** * ????<br /> * * @param alarmThreshold ? */ public void setAlarmThreshold(final long alarmThreshold) { this.alarmThreshold_ = alarmThreshold; } /** * ?CPU?????<br /> * * @return ?CPU? */ public long getAlarmCpuThreshold() { return this.alarmCpuThreshold_; } /** * ?CPU????<br /> * * @param alarmCpuThreshold ?CPU? */ public void setAlarmCpuThreshold(final long alarmCpuThreshold) { this.alarmCpuThreshold_ = alarmCpuThreshold; } /** * ????<br /> * * @return */ public Date getDate() { return this.date_; } /** * ???<br /> * * @param date */ public void setDate(final Date date) { this.date_ = date; } /** * ??????<br /> * * @return ????<code>true</code> */ public boolean isTarget() { return this.target_; } /** * ?????????<br /> * * @param target ????<code>true</code> */ public void setTarget(final boolean target) { this.target_ = target; } /** * ????????<br /> * * @return ??????<code>true</code> */ public boolean isResponseGraphOutput() { return this.transactionGraphOutput_; } /** * ???????????<br /> * * @param transactionGraphOutput ??????<code>true</code> */ public void setResponseGraphOutput(final boolean transactionGraphOutput) { this.transactionGraphOutput_ = transactionGraphOutput; } /** * ?????????????<br /> * * @return ???????? <code>true</code> */ public boolean isModifiedByUser() { return this.modifiedByUser_; } /** * ?????????????<br /> * * @param modified ???????? <code>true</code> */ public void setModifiedByUser(final boolean modified) { this.modifiedByUser_ = modified; } public void setHttpStatusCount(final long httpStatusCount) { httpStatusCount_ = httpStatusCount; } public long getHttpStatusCount() { return httpStatusCount_; } /** * {@inheritDoc} */ @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("??="); builder.append(getCount()); builder.append(", ??="); builder.append(getAverage()); builder.append(", ?="); builder.append(getMaximum()); builder.append(", ??="); builder.append(getMinimum()); builder.append(", ="); builder.append(getThrowableCount()); builder.append(", ="); builder.append(isTarget()); builder.append(", ?="); builder.append(isResponseGraphOutput()); return builder.toString(); } /** * {@inheritDoc} */ public int compareTo(final InvocationModel obj) { return this.getMethodName().compareTo(obj.getMethodName()); } /** * {@inheritDoc} * * View ???????????<br /> */ @Override public boolean equals(final Object obj) { if (obj instanceof InvocationModel) { InvocationModel invocation = (InvocationModel) obj; String className = invocation.getClassName(); String methodName = invocation.getMethodName(); String average = invocation.getAverage(); long maximum = invocation.getMaximum(); boolean equal = (getClassName().equals(className) && getMethodName().equals(methodName) && getAverage().equals(average) && getMaximum() == maximum); return equal; } return false; } /** * {@inheritDoc} */ @Override public int hashCode() { return getMethodName().hashCode(); } /** * {@inheritDoc} */ public void handleNotification(final Notification notification, final Object handback) { if (notification instanceof AttributeChangeNotification) { String alarmMsg = ((AttributeChangeNotification) notification).getMessage(); if (EXCEED_THRESHOLD_ALARM.equals(alarmMsg) == true) { handleExceedThresholdAlarm((AttributeChangeNotification) notification); } } throw new RuntimeException("aaaa"); } private void handleExceedThresholdAlarm(final AttributeChangeNotification notification) { //setAverage((Long)notification.getOldValue()); setMaximum((Long) notification.getNewValue()); this.component_.setExceededThresholdAlarm(this.methodName_); } /** * ? {@link InvocationModel} ?????<br /> * * @param telegram * @param alarmThreshold ? * @param warningThreshold ? * @return {@link InvocationModel} ? */ public static InvocationModel[] createFromTelegram(final Telegram telegram, final long alarmThreshold, final long warningThreshold) { Map<String, InvocationModel> invocationMap = new HashMap<String, InvocationModel>(); Body[] objBody = telegram.getObjBody(); String strClassMethodName = null; for (int index = 0; index < objBody.length; index++) { ResponseBody responseBody = (ResponseBody) objBody[index]; if (responseBody.getStrObjName() == null) { continue; } // ??? // InvocationModel?? String strItemName = ((ResponseBody) objBody[index]).getStrItemName(); Object[] objTempArr = ((ResponseBody) objBody[index]).getObjItemValueArr(); // ???0???? if (objTempArr.length == 0) { continue; } if (StringUtils.isEmpty(responseBody.getStrObjName()) == false) { strClassMethodName = responseBody.getStrObjName(); } InvocationModel invocation = invocationMap.get(strClassMethodName); if (invocation == null) { // ?????InvocationModel???? invocation = new InvocationModel(); invocation.setDate(new Date()); // ?????????? if (strClassMethodName == null) { continue; } List<String> strClassMethodNameArr = StringUtil.split(strClassMethodName, CLASSMETHOD_SEPARATOR); String strClassName = "unknown"; String strMethodName = "unknown"; if (strClassMethodNameArr.size() > INDEX_METHODNAME) { strClassName = strClassMethodNameArr.get(INDEX_CLASSNAME); strMethodName = strClassMethodNameArr.get(INDEX_METHODNAME); } invocation.setClassName(strClassName); invocation.setMethodName(strMethodName); invocationMap.put(strClassMethodName, invocation); } Long value = 0L; String valid = "false"; if (responseBody.getByteItemMode() == ItemType.ITEMTYPE_LONG) { value = (Long) objTempArr[0]; } else if (responseBody.getByteItemMode() == ItemType.ITEMTYPE_STRING) { valid = (String) objTempArr[0]; } // ?? if (ITEMNAME_CALL_COUNT.equals(strItemName)) { value = getValidValue(value); invocation.setCount(value); } // ? else if (ITEMNAME_ACCUMULATED_MAXIMUM_INTERVAL.equals(strItemName)) { value = getValidValue(value); invocation.setAccumulatedMaximum(value); } // ?? else if (ITEMNAME_ACCUMULATED_MINIMUM_INTERVAL.equals(strItemName)) { value = getValidValue(value); invocation.setAccumulatedMinimum(value); } // ?? else if (ITEMNAME_ACCUMULATED_TOTAL_INTERVAL.equals(strItemName)) { value = getValidValue(value); invocation.setAccumulatedTotal(value); } // CPU else if (ITEMNAME_ACCUMULATED_MAXIMUM_CPU_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setAccumulatedCpuMaximum(value); } // ?CPU else if (ITEMNAME_ACCUMULATED_MINIMUM_CPU_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setAccumulatedCpuMinimum(value); } // ?CPU else if (ITEMNAME_ACCUMULATED_TOTAL_CPU_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setAccumulatedCpuTotal(value); } // else if (ITEMNAME_ACCUMULATED_MAXIMUM_USER_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setAccumulatedUserMaximum(value); } // ? else if (ITEMNAME_ACCUMULATED_MINIMUM_USER_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setAccumulatedUserMinimum(value); } // ? else if (ITEMNAME_ACCUMULATED_TOTAL_USER_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setAccumulatedUserTotal(value); } // ? else if (ITEMNAME_MAXIMUM_INTERVAL.equals(strItemName)) { value = getValidValue(value); invocation.setMaximum(value); } // ?? else if (ITEMNAME_MINIMUM_INTERVAL.equals(strItemName)) { value = getValidValue(value); invocation.setMinimum(value); } // ?? else if (ITEMNAME_TOTAL_INTERVAL.equals(strItemName)) { value = getValidValue(value); invocation.setTotal(value); } // CPU else if (ITEMNAME_MAXIMUM_CPU_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setCpuMaximum(value); } // ?CPU else if (ITEMNAME_MINIMUM_CPU_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setCpuMinimum(value); } // ?CPU else if (ITEMNAME_TOTAL_CPU_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setCpuTotal(value); } // USER else if (ITEMNAME_MAXIMUM_USER_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setUserMaximum(value); } // ?USER else if (ITEMNAME_MINIMUM_USER_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setUserMinimum(value); } // ?USER else if (ITEMNAME_TOTAL_USER_INTERVAL.equals(strItemName)) { value = getValidMegaValue(value); invocation.setUserTotal(value); } // else if (ITEMNAME_JAVAPROCESS_EXCEPTION_OCCURENCE_COUNT.equals(strItemName)) { value = getValidValue(value); invocation.setThrowableCount(value); } // else if (ITEMNAME_JAVAPROCESS_METHOD_STALL_COUNT.equals(strItemName)) { value = getValidValue(value); invocation.setMethodStallCount(value); } // ??? ??????? // CHECKSTYLE:OFF else if (ITEMNAME_ALL_CALLER_NAMES.equals(strItemName)) { // } // CHECKSTYLE:ON else if (ITEMNAME_TARGET.equals(strItemName)) { invocation.setTarget(Boolean.valueOf(valid)); } // ??????? else if (ITEMNAME_TRANSACTION_GRAPH.equals(strItemName)) { invocation.setResponseGraphOutput(Boolean.valueOf(valid)); } // TAT else if (ITEMNAME_ALARM_THRESHOLD.equals(strItemName)) { value = getValidValue(value); invocation.setAlarmThreshold(value); } // CPU else if (ITEMNAME_ALARM_CPU_THRESHOLD.equals(strItemName)) { value = getValidValue(value); invocation.setAlarmCpuThreshold(value); } /* else if (ITEMNAME_JAVAPROCESS_HTTP_EXCEPTION.equals(strItemName)) { value = getValidValue(value); invocation.setHttpStatusCount(value); } */ } Collection<InvocationModel> invocationList = invocationMap.values(); return invocationList.toArray(new InvocationModel[invocationList.size()]); } private static Long getValidMegaValue(Long value) { if (value < 0) { value = Long.valueOf(-1); } else { value /= (MILLIS * MILLIS); } return value; } private static Long getValidValue(Long value) { if (value < 0) { value = Long.valueOf(-1); } return value; } }