Java tutorial
package com.zte.spring.service; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import org.apache.catalina.util.ParameterMap; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.zte.databinder.ClientBinder; import com.zte.databinder.DeptBinder; import com.zte.databinder.GroupBinder; import com.zte.databinder.NewsBinder; import com.zte.databinder.TenantBinder; import com.zte.databinder.UserBinder; import com.zte.databinder.UserPermBinder; import com.zte.im.bean.Customization; import com.zte.im.bean.Param; import com.zte.im.bean.Role; import com.zte.im.bean.SysLog; import com.zte.im.mybatis.bean.UserPermission; import com.zte.im.mybatis.bean.my.UcTenantMy; import com.zte.im.service.ISysLogService; import com.zte.im.service.impl.SysLogServiceImpl; @Component @Aspect public class LogService { private final static Logger logger = LoggerFactory.getLogger(LogService.class); public static Map<String, String> logMethodsMap = new HashMap<String, String>(); public static Map<String, String> paramDescMap = new HashMap<String, String>(); static { /* * ? */ logMethodsMap.put("login", ""); logMethodsMap.put("logout", ""); logMethodsMap.put("editPass", "?"); logMethodsMap.put("addUser", ""); logMethodsMap.put("removeUser", ""); logMethodsMap.put("updateUser", ""); logMethodsMap.put("importUser", ""); logMethodsMap.put("exportUser", ""); logMethodsMap.put("addGroup", ""); logMethodsMap.put("editGroup", ""); logMethodsMap.put("delGroup", ""); logMethodsMap.put("addDept", ""); logMethodsMap.put("updateDept", ""); logMethodsMap.put("delDept", ""); logMethodsMap.put("updateUserPerm", "??"); logMethodsMap.put("saveNews", ""); logMethodsMap.put("delNews", ""); logMethodsMap.put("saveOrUpdateRole", ""); logMethodsMap.put("removeRole", ""); logMethodsMap.put("addClient", ""); logMethodsMap.put("editClient", ""); logMethodsMap.put("removeClient", ""); logMethodsMap.put("updateSysParam", "?"); logMethodsMap.put("updateContactParam", "?"); logMethodsMap.put("updateParam", "?"); logMethodsMap.put("saveOrUpdateParam", "?"); logMethodsMap.put("removeParam", "?"); logMethodsMap.put("editTenant", "??"); logMethodsMap.put("updateTenant", "??"); logMethodsMap.put("updateTenantCustomization", "?"); /* * ??? */ paramDescMap.put("name", "??"); paramDescMap.put("password", "?"); paramDescMap.put("deptManage", "?"); paramDescMap.put("userManage", "?"); paramDescMap.put("userPrivManage", "??"); paramDescMap.put("userAbleManage", "???"); paramDescMap.put("userImport", ""); paramDescMap.put("userExport", ""); paramDescMap.put("publishNews", "?"); paramDescMap.put("editNews", ""); paramDescMap.put("removeNews", ""); paramDescMap.put("updateCompInfo", "??"); paramDescMap.put("roleManage", "?"); paramDescMap.put("updateSystemParam", "?"); paramDescMap.put("updateContactParam", "?"); paramDescMap.put("defineParam", ""); paramDescMap.put("licenseManage", "License?"); paramDescMap.put("clientManage", "?"); paramDescMap.put("0", "?"); paramDescMap.put("1", ""); paramDescMap.put("LDAP", "LDAP"); paramDescMap.put("DATABASE", "?"); paramDescMap.put("THIRD", "?"); paramDescMap.put("number", ""); paramDescMap.put("date", ""); paramDescMap.put("string", ""); } ISysLogService logService = SysLogServiceImpl.getInstance(); @Pointcut("execution(* com.zte.controller..*.*(..))") public void methodCachePointcut() { } @Around("methodCachePointcut()") public Object around(ProceedingJoinPoint point) throws Throwable { Object result = null; String methodName = point.getSignature().getName(); // ? if (methodName.equals("login")) { result = point.proceed(); } if (logMethodsMap.containsKey(methodName)) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); String loginName = (String) request.getSession().getAttribute("userid"); String roleName = (String) request.getSession().getAttribute("uname"); String clientIP = (String) request.getSession().getAttribute("clientIP"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Calendar ca = Calendar.getInstance(); String operDate = df.format(ca.getTime()); StringBuffer operDetail = new StringBuffer(); operDetail.append(roleName).append(loginName).append("(").append(clientIP).append(")"); Object[] method_param = point.getArgs(); // ?? // ???? if (!methodName.equals("addUser") && !methodName.equals("saveNews") && !methodName.equals("saveOrUpdateRole") && !methodName.equals("saveOrUpdateParam") && !methodName.equals("updateParam")) { operDetail.append(logMethodsMap.get(methodName)); } operDetail.append(getOperContent(method_param, methodName)); SysLog log = new SysLog(); log.setType("oper"); log.setCreatime(operDate); log.setUname(loginName); log.setUid(""); log.setOper(logMethodsMap.get(methodName)); log.setContent(operDetail.toString()); logger.info(log.getContent()); logService.saveLog(log); } if (!methodName.equals("login")) { result = point.proceed(); } return result; } /** * Java????(insert?update)? ?? * * @param args * @param mName * @return */ public String getOperContent(Object[] args, String mName) { if (args == null || args.length == 0) { return null; } StringBuffer content = new StringBuffer(); for (int i = 0; i < args.length; i++) { Object arg = args[i]; logger.info(arg.getClass().toString()); if (arg instanceof String) { if (StringUtils.isNotBlank((String) arg)) { content.append(",?").append(i + 1).append(":").append(arg); } } else if (arg instanceof DeptBinder) { return getContentByDeptBinder((DeptBinder) arg); } else if (arg instanceof Param) { return getContentByParam((Param) arg, mName); } else if (arg instanceof ClientBinder) { return getContentByClientBinder((ClientBinder) arg); } else if (arg instanceof Role) { return getContentByRole((Role) arg, mName); } else if (arg instanceof NewsBinder) { return getContentByNewsBinder((NewsBinder) arg, mName); } else if (arg instanceof UserPermBinder) { return getContentByUserPermBinder((UserPermBinder) arg); } else if (arg instanceof GroupBinder) { return getContentByGroupBinder((GroupBinder) arg); } else if (arg instanceof TenantBinder) { return getContentByTenantBinder((TenantBinder) arg); } else if (arg instanceof UcTenantMy) { return getContentByUcTenantMy((UcTenantMy) arg); } else if (arg instanceof Customization) { return getContentByCustomization((Customization) arg); } else if (arg instanceof UserBinder) { return getContentByUserBinder((UserBinder) arg); } else { return getContentByParameters(arg); } } return content.toString(); } private String getContentByCustomization(Customization arg) { StringBuffer content = new StringBuffer(); if (arg != null) { handleLogField(content, arg.getServerLogo(), ",?LOGO:"); handleLogField(content, arg.getServerLoginLogo(), ",?LOGO:"); handleLogField(content, arg.getServerManagerName(), ",?????:"); handleLogField(content, arg.getServerCopyright(), ",???:"); handleLogField(content, arg.getClientLogo(), ",LOGO:"); handleLogField(content, arg.getClientName(), ",??:"); handleLogField(content, arg.getClientCopyright(), ",?:"); handleLogField(content, arg.getClientHomePage(), ",?:"); handleLogField(content, arg.getClientContact(), ",??:"); } return content.toString(); } private String getContentByDeptBinder(DeptBinder arg) { StringBuffer content = new StringBuffer(); if (arg != null) { handleLogField(content, arg.getGname(), ",??:"); handleLogField(content, arg.getDeptName(), ",:"); handleLogField(content, arg.getDeptDesc(), ",??:"); handleLogField(content, arg.getSequ() != null ? arg.getSequ().toString() : null, ",??:"); } return content.toString(); } private String getContentByParam(Param arg, String method) { StringBuffer content = new StringBuffer(); if (arg != null) { if (method.equals("saveOrUpdateParam")) { content.append(arg.getId() == null ? "?," : "?,"); handleLogField(content, arg.getParamName(), "???:"); handleLogField(content, arg.getParamType(), ",?:", true); } else if (arg.getId() != null) { if (arg.getId().equals("system")) { content.append("?,"); handleLogField(content, arg.getPwdExpire(), "?:", true); handleLogField(content, arg.getPwdPeriod(), ",?:"); handleLogField(content, arg.getPwdLength(), ",?:", true); handleLogField(content, arg.getPwdMinLength(), ",??:"); handleLogField(content, arg.getPwdMaxLength(), ",?:"); handleLogField(content, arg.getPwdCheck(), ",?:", true); handleLogField(content, arg.getPwdFirstCheck(), ",?:", true); handleLogField(content, arg.getLoginCheck(), ",??:", true); handleLogField(content, arg.getIpCheck(), ",?IP?:", true); handleLogField(content, arg.getLoginAuthType(), ",??:", true); handleLogField(content, arg.getLdapUrl(), ",LDAP?URL:"); handleLogField(content, arg.getBaseDN(), ",baseDN:"); handleLogField(content, arg.getDomain(), ",??:"); } else if (arg.getId().equals("contact")) { content.append("?,"); handleLogField(content, arg.getContactOrgName(), "????:"); handleLogField(content, arg.getSyncLDAP(), ",??LDAP:", true); handleLogField(content, arg.getLdapUrl(), ",LDAP?URL:"); handleLogField(content, arg.getContactBaseDN(), ",BaseDN:"); handleLogField(content, arg.getProtectedPropNames(), ",??:"); handleLogField(content, arg.getEditablePropNames(), ",?:"); } else { content.append(","); handleLogField(content, arg.getParamName(), "???:"); } } else { logger.error("wrong param id:{}" + arg.getId()); } } return content.toString(); } private String getContentByClientBinder(ClientBinder arg) { StringBuffer content = new StringBuffer(); if (arg != null) { content.append(","); handleLogField(content, arg.getCname(), "??:"); handleLogField(content, arg.getCtype(), ",:"); handleLogField(content, arg.getVersion(), ",:"); } return content.toString(); } /** * Role??. * * @param arg * @param mName * @return */ private String getContentByRole(Role r, String method) { StringBuffer content = new StringBuffer(); if (r != null) { if (method.equals("saveOrUpdateRole")) { content.append(StringUtils.isEmpty(r.getRoleId()) ? "," : ","); } else { content.append(","); } handleLogField(content, r.getRoleName(), "??:"); handleLogField(content, r.getDesc(), ",??:"); handleLogField(content, r.getPrivileges(), ",:"); if (r.getUserList() != null && r.getUserList().size() > 0) { content.append(",?:"); for (int i = 0; i < r.getUserList().size(); i++) { content.append(r.getUserList().get(i).getName()); if (i < r.getUserList().size() - 1) { content.append("?"); } } } } return content.toString(); } private void handleLogField(StringBuffer content, String param, String fieldStart) { handleLogField(content, param, fieldStart, false); } /** * * @param content * @param param * @param fieldStart * @param useDesc * true??? */ private void handleLogField(StringBuffer content, String param, String fieldStart, boolean useDesc) { if (useDesc) { content.append( param != null ? fieldStart + (paramDescMap.containsKey(param) ? paramDescMap.get(param) : param) : ""); } else { content.append(param != null ? fieldStart + param : ""); } } private void handleLogField(StringBuffer content, List<String> params, String fieldStart) { if (params != null && params.size() > 0) { content.append(fieldStart); for (int i = 0; i < params.size(); i++) { String param = params.get(i); content.append(paramDescMap.containsKey(param) ? paramDescMap.get(param) : param); if (i < params.size() - 1) { content.append("?"); } } } } /** * ?NewsBinder?. * * @param upb * @return */ private String getContentByNewsBinder(NewsBinder nb, String method) { StringBuffer content = new StringBuffer(); if (nb != null) { if (method.equals("saveNews")) { content.append(StringUtils.isEmpty(nb.getNews_id()) ? "," : ","); } else { content.append(","); } content.append(nb.getNews_title() != null ? ":" + nb.getNews_title() : ""); content.append(nb.getNews_con() != null ? ",:" + nb.getNews_con() : ""); } return content.toString(); } /** * ?UserPermBinder??. * * @param upb * @return */ private String getContentByUserPermBinder(UserPermBinder upb) { StringBuffer content = new StringBuffer(); if (upb != null) { content.append(","); content.append(upb.getUname() != null ? "??:" + upb.getUname() : ""); if (upb.getPermList() != null && upb.getPermList().size() > 0) { Collections.sort(upb.getPermList()); Integer permType = null; for (UserPermission up : upb.getPermList()) { if (permType == null || permType != up.getPermType()) { content.append(",").append(getPermTypeDesc(up.getPermType())).append(":") .append(up.getPermName()); permType = up.getPermType(); } else { content.append("?").append(up.getPermName()); } } } } return content.toString(); } /** * ?????. * * @param permType * @return */ private String getPermTypeDesc(int permType) { if (permType == 1) return "???"; if (permType == 2) return "??"; if (permType == 3) return "??"; return ""; } /** * ?GroupBinder??. * * @param gb * @return */ private String getContentByGroupBinder(GroupBinder gb) { StringBuffer content = new StringBuffer(); if (gb != null) { content.append(","); content.append(gb.getGname() != null ? "??:" + gb.getGname() : ""); content.append(gb.getGpname() != null ? ",:" + gb.getGpname() : ""); content.append(gb.getGdesc() != null ? ",??:" + gb.getGdesc() : ""); content.append(gb.getSequ() != null ? ",??:" + gb.getSequ() : ""); } return content.toString(); } /** * ?UserBinder?. * * @param ub * @return */ private String getContentByUserBinder(UserBinder ub) { StringBuffer content = new StringBuffer(); if (content != null) { content.append(StringUtils.isEmpty(ub.getId()) ? "," : ","); content.append(ub.getUid() != null ? "?:" + ub.getUid() : ""); content.append(ub.getCn() != null ? ",??:" + ub.getCn() : ""); content.append(ub.getDeptName() != null ? ",:" + ub.getDeptName() : ""); content.append(ub.getSexDisplay() != null ? ",:" + ub.getSexDisplay() : ""); content.append(ub.getPosDisplay() != null ? ",?:" + ub.getPosDisplay() : ""); content.append(ub.getBirthday() != null ? ",:" + ub.getBirthday() : ""); content.append(ub.getMobile() != null ? ",:" + ub.getMobile() : ""); content.append(ub.getMail() != null ? ",:" + ub.getMail() : ""); content.append(ub.getL() != null ? ",:" + ub.getL() : ""); content.append(ub.getPassword() != null ? ",?:" + ub.getPassword() : ""); content.append(ub.getPhotoUrl() != null ? ",?:" + ub.getPhotoUrl() : ""); content.append(ub.getWeixin() != null ? ",:" + ub.getWeixin() : ""); content.append(ub.getPhotoSign() != null ? ",??:" + ub.getPhotoSign() : ""); content.append(ub.getWeibo() != null ? ",?:" + ub.getWeibo() : ""); content.append(ub.getHobby() != null ? ",:" + ub.getHobby() : ""); content.append(ub.getHometelephone() != null ? ",?:" + ub.getHometelephone() : ""); content.append(ub.getQq() != null ? ",QQ:" + ub.getQq() : ""); content.append(ub.getFax() != null ? ",?:" + ub.getFax() : ""); content.append(ub.getAddress() != null ? ",?:" + ub.getAddress() : ""); content.append(ub.getInfo() != null ? ",:" + ub.getInfo() : ""); } return content.toString(); } /** * ?TenantBinder??. * * @param tb * @return */ private String getContentByTenantBinder(TenantBinder tb) { StringBuffer content = new StringBuffer(); if (tb != null) { content.append(","); content.append(tb.getTname() != null ? "???:" + tb.getTname() : ""); content.append(tb.getTpid() != null ? ",??:" + tb.getTpid() : ""); content.append(tb.getPhotoImg() != null ? ",?Logo:" + tb.getPhotoImg() : ""); content.append(tb.getTlinkman() != null ? ",??:" + tb.getTlinkman() : ""); content.append(tb.getTmobile() != null ? ",??:" + tb.getTmobile() : ""); content.append(tb.getTmail() != null ? ",?:" + tb.getTmail() : ""); content.append(tb.getTtel() != null ? ",?:" + tb.getTtel() : ""); content.append(tb.getTguimo() != null ? ",?:" + tb.getTguimo() : ""); content.append(tb.getTprov() != null ? ",:" + tb.getTprov() : ""); content.append(tb.getTcity() != null ? tb.getTcity() : ""); content.append(tb.getTaddress() != null ? ",?:" + tb.getTaddress() : ""); content.append(tb.getOpass() != null ? ",?:" + tb.getOpass() : ""); content.append(tb.getPass() != null ? ",?:" + tb.getPass() : ""); } return content.toString(); } /** * ?UcTenantMy??. * * @param tb * @return */ private String getContentByUcTenantMy(UcTenantMy tb) { StringBuffer content = new StringBuffer(); if (tb != null) { content.append(","); content.append(tb.getName() != null ? "???:" + tb.getName() : ""); content.append(tb.getPlatformId() != null ? ",??:" + tb.getPlatformId() : ""); content.append(tb.getPhotoImg() != null ? ",?Logo:" + tb.getPhotoImg() : ""); content.append(tb.getLinkman() != null ? ",??:" + tb.getLinkman() : ""); content.append(tb.getMobile() != null ? ",??:" + tb.getMobile() : ""); content.append(tb.getMail() != null ? ",?:" + tb.getMail() : ""); content.append(tb.getTel() != null ? ",?:" + tb.getTel() : ""); content.append(tb.getGuimo() != null ? ",?:" + tb.getGuimo() : ""); content.append(tb.getProv() != null ? ",:" + tb.getProv() : ""); content.append(tb.getCity() != null ? tb.getCity() : ""); content.append(tb.getAddress() != null ? ",?:" + tb.getAddress() : ""); } return content.toString(); } /** * ????. * * @param info * @return */ private String getContentByParameters(Object info) { StringBuffer content = new StringBuffer(); // ? Method[] methods = info.getClass().getDeclaredMethods(); // ??get for (Method method : methods) { String methodName = method.getName(); // ?get if (methodName.indexOf("get") == -1) {// ?get continue;// ?? } // ?? if (methodName.equals("getParameterMap")) { Object rsValue = null; try { // get? rsValue = method.invoke(info); logger.info(rsValue.getClass() + ""); if (rsValue instanceof ParameterMap) { @SuppressWarnings("unchecked") ParameterMap<String, String[]> map = (ParameterMap<String, String[]>) rsValue; if (map.size() > 0) { content.append(","); } int j = 0; for (Entry<String, String[]> entry : map.entrySet()) { String key = entry.getKey(); if (paramDescMap.containsKey(key)) { } String[] vals = entry.getValue(); content.append(paramDescMap.containsKey(key) ? paramDescMap.get(key) : key); content.append(":"); if (vals.length > 1) { content.append("["); } for (int i = 0; i < vals.length; i++) { content.append(vals[i]); if (i < vals.length - 1) { content.append(","); } } if (vals.length > 1) { content.append("]"); } if (j < map.size() - 1) { content.append(","); } j++; } } } catch (Exception e) { logger.error("", e); continue; } } } return content.toString(); } }