Java tutorial
package com.momathink.common.tools; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.NetworkInterface; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.log4j.Logger; import com.jfinal.kit.PathKit; import com.jfinal.kit.StrKit; import com.momathink.common.tools.securitys.Des3Encryption; import com.momathink.crm.mediator.model.Organization; /** * ?? * @author dufuzhong */ public class ToolMonitor extends Thread { private static Logger log = Logger.getLogger(ToolMonitor.class); private static Date expirationDate = null; //---------------------------------------?? ---------------------------------------------------------- /** * ???ids * varchar(32) 32 */ private final String column_customerIds = "monitor.customerIds"; /** * ??1=?,2= * int(2) null */ private final String column_monitorTyp = "monitor.monitorTyp"; /** * ???ip * varchar(50) 50 */ private final String column_serverIP = "monitor.serverIP"; /** * ??MAC? * varchar(50) 50 */ private final String column_addressMAC = "monitor.addressMAC"; /** * ?? * varchar(255) 255 */ private final String column_projectPath = "monitor.projectPath"; /** * ??? * datetime null */ private final String column_operationDate = "monitor.operationDate"; /** * ??? * text 65535 */ private final String column_organization = "organization"; //--------------------------------------- ---------------------------------------------------------- /** * ???ids * varchar(32) 32 */ private static final String visits_column_customerIds = "visits.customerIds"; /** * ??1= * int(2) null */ private static final String visits_column_visitsTyp = "visits.visitsTyp"; /** * ??? * varchar(50) 50 */ private static final String visits_column_website = "visits.website"; /** * ??? * datetime null */ private static final String visits_column_operationDate = "visits.operationDate"; //----------------------------------------??--------------------------------------------------------- /** * ?? */ private static String authorization_code = "9c9af6c6f19d4ff898f37a89b46d7ba8";//?,? /** * ? ? */ private static String moma_url = ""; /** ? ?? ? */ public static final String parameter_InterfaceAddress = "/crm/monitor/report"; /** ? ? ? */ public static final String parameter_visitsLogin = "/crm/visits/login"; private String type = "1";// /** * ? MoMA ? */ public static final String report_start = "1"; /*** MoMA ? */ public static final String report_timing = "2"; /** * MoMA */ public static final String report_login = "3"; public ToolMonitor(String type) { this.type = type; } public void run() { if (report_start.equals(type)) { refresh(); momaPost(); } else if (report_timing.equals(type)) { momaPost(); } else if (report_login.equals(type)) { loginPost(); } } //------------------------------------------------------------------------------------------------- /** * MoMA ? */ private void momaPost() { Map<String, String> monitor = new HashMap<String, String>(); try { monitor.put(column_customerIds, authorization_code); monitor.put(column_monitorTyp, report_start); monitor.put(column_serverIP, getRealIp()); monitor.put(column_addressMAC, getMACAddr()); monitor.put(column_projectPath, PathKit.getWebRootPath()); monitor.put(column_operationDate, getDate()); //? monitor.put(column_organization, Organization.dao.getOrganizationMessage().toJson()); } catch (Exception e) { e.printStackTrace(); } if (StrKit.notBlank(moma_url)) doPost(moma_url + parameter_InterfaceAddress, monitor); } /** * MoMA */ private void loginPost() { Map<String, String> monitor = new HashMap<String, String>(); try { monitor.put(visits_column_customerIds, authorization_code); monitor.put(visits_column_visitsTyp, "1"); monitor.put(visits_column_website, PathKit.getWebRootPath()); monitor.put(visits_column_operationDate, getDate()); } catch (Exception e) { e.printStackTrace(); } if (StrKit.notBlank(moma_url)) doPost(moma_url + parameter_visitsLogin, monitor); } /** * MAC? * @return : "00 00 00 00 00 00" */ private String getMACAddr() { String mac = ""; try { NetworkInterface netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); byte[] macAddr = netInterface.getHardwareAddress(); String macByte = ""; // ???? for (byte b : macAddr) { macByte = Integer.toHexString((int) (b & 0xff)); if (macByte.length() == 1) { macByte = "0" + macByte; } mac = mac + " " + macByte; } } catch (Exception e) { } return mac.substring(1); } /** * ?ip * @return : "111.111.111.111" */ private String getRealIp() { String netip = null;// IP try { Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// ?IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP netip = ip.getHostAddress(); finded = true; break; } } } } catch (Exception e) { } return netip; } /** * ??? */ private String getDate() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(new Date()); } /** * HTTP POST?HTML * * @param url * URL? * @param params * ?,?null * @return ?HTML */ private String doPost(String url, Map<String, String> params) { StringBuffer result = new StringBuffer(); HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); // Http Post? if (params != null) { NameValuePair[] data = new NameValuePair[params.size()]; int i = 0; for (Map.Entry<String, String> entry : params.entrySet()) { data[i++] = new NameValuePair(entry.getKey(), entry.getValue()); } method.setRequestBody(data); } try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8")); String str = null; while ((str = reader.readLine()) != null) { result.append(str); } } } catch (IOException e) { } finally { method.releaseConnection(); } return result.toString(); } /** * ? * @param date ,(?:) * @return =true =false */ public static boolean whetherExpired(Date date) { log.info("?: ?" + (expirationDate) + " ?" + (date)); boolean result = false; if (null == date || null == expirationDate || date.equals(expirationDate)) {//|| ToolDateTime.format(expirationDate, ToolDateTime.pattern_ymd).equals(ToolDateTime.format(date,ToolDateTime.pattern_ymd)) result = true; } else { //b.before(a)ba??true result = expirationDate.before(date); } log.info("?: " + (result) + "(=true =false)"); return result; } /** * ??? */ public static boolean refresh() { try { Organization organization = Organization.dao.getOrganizationMessage(); String licensekey = organization.getStr("LICENSEKEY"); String expirationDateKeys = organization.getStr("EXPIRATIONDATEKEYS"); log.info("??:" + (authorization_code) + " ?" + (expirationDateKeys)); if (StrKit.isBlank(expirationDateKeys) && StrKit.isBlank(authorization_code)) return false; authorization_code = licensekey.trim(); expirationDateKeys = decryptFromHex(expirationDateKeys.trim());// if (null != expirationDateKeys) { String[] keys = expirationDateKeys.split(",");//? 0,1?? // expirationDate = ToolDateTime.getDate(keys[0], ToolDateTime.pattern_ymd); organization.set("expirationDate", expirationDate).update(); //?? moma_url = keys[1]; log.info("???"); return true; } expirationDate = null; return false; } catch (Exception e) { log.info("???"); e.printStackTrace(); expirationDate = null; return false; } } /** * * @param key * @param value * @return */ private static String decryptFromHex(String value) throws Exception { return Des3Encryption.decryptFromHex(authorization_code.substring(0, 24), value); } }