Java tutorial
package cn.schina.dbfw.service; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang.SystemUtils; import net.ServiceNativeGmon; import cn.schina.dbfw.common.lang.ServiceException; import cn.schina.dbfw.config.Globals; import cn.schina.dbfw.pojo.AuditLog; import cn.schina.dbfw.pojo.LicenseInfo; import cn.schina.dbfw.util.SystemAuditResultSingelton; public class SystemLicenseSrever { /** * licence * * @param is * @return * @throws Exception */ public LicenseInfo uploadLicenseInfo(InputStream is) throws Exception { if (!System.getProperty("os.name").equals("Linux")) { return getLicenseInfoFinal(); } String path = "/home/dbfw/dbfw/etc/"; String name = "License.swk"; path = path + "/" + name; boolean success = true; FileOutputStream fos = null; try { File file = new File(path);// if (file.exists()) { file.delete(); } fos = new FileOutputStream(path); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); } fos.flush();// ? } catch (Exception e) { success = false; e.printStackTrace(); throw new ServiceException( "??\r\n" + e.getLocalizedMessage(), e); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } try { is.close(); } catch (IOException e) { e.printStackTrace(); } AuditLog auditLog = new AuditLog(); auditLog.setEventDesc("License? "); auditLog.setDesigner("->??"); auditLog.setMovement(""); auditLog.setEventResult(success == true ? "1" : "2"); auditLog.setAlterBefore("?" + name); SystemAuditResultSingelton.getInstance().insertAuditLog(auditLog); } return getLicenseInfoFinal(); } /** * ?license? * * @return * @throws ParseException */ public static LicenseInfo getLicenseInfo() throws Exception { LicenseInfo info = new LicenseInfo(); String runtime = ""; if ("Linux".equals(info.getSystemType())) { String cmdShell = "/bin/sh"; String cmdOption = "-c"; String cmdParamPrefix = " cat /proc/uptime| awk -F. " + "'{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;" + "run_minute=($1 % 3600)/60;run_second=$1 % 60;" + "printf(\"?%d%d%d\",run_days,run_hour,run_minute)}'"; // System.out.println("" + cmdParamPrefix); String[] cmd = { cmdShell, cmdOption, cmdParamPrefix }; runtime = runCmd(cmd); } else { System.out.println("" + info.getSystemType()); } info.setRunTime(runtime); Globals.reloadConfigs(); String ret = ""; if ("Linux".equals(info.getSystemType())) { ret = ServiceNativeGmon.ODC_ED_GetLicenseInfo(Globals.GMON_IP, Globals.GMON_PORT); System.out.println(ret); } String[] licVal = ret.split("\\|"); if (licVal.length != 6) { info.setDBSum(0); info.setEndTime("?"); // info.setRunTime(""); info.setSeriesNumber(getSeriesNumber()); info.setUnitType(""); info.setHelp_version_info("?"); } else { /** * ?license? ? * audtype|dbcount * |devtype|time|diskid??? audtype?TR? * dbcount??? devtype?32 * time??YYYYMMDD00000000 diskid??crc */ SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat formatDate1 = new SimpleDateFormat("yyyyMMdd"); String licType = ""; if (licVal[0].equals("T")) { licType = ""; } else if (licVal[0].equals("S")) { licType = ""; } else if (licVal[0].equals("R")) { licType = "?"; } info.setHelp_version_info(licType + "(" + licVal[1] + "?)"); info.setDBSum(Integer.parseInt(licVal[1])); info.setUnitType(licVal[2]); info.setEndTime(licVal[3].equals("00000000") ? "" : (" " + formatDate.format(formatDate1.parse(licVal[3])))); info.setSeriesNumber(getSeriesNumber()); if (!licVal[3].equals("00000000")) { Date now = formatDate1.parse(formatDate1.format(new Date()));// ? if (formatDate1.parse(licVal[3]).getTime() - now.getTime() < 0) { info.setEndTime( "(" + formatDate.format(formatDate1.parse(licVal[3])) + ")"); } } if (licVal[5].equals("0")) { info.setHelp_version_info("false"); } } return info; } /** * ?license? * * @return * @throws ParseException */ public static LicenseInfo getLicenseInfoFinal() throws Exception { if (ifNsfocus() == 1) { return getNsfocusLicenseInfoFinal(); } LicenseInfo info = new LicenseInfo(); String runtime = ""; if (info.getSystemType().equals("Linux")) { String cmdShell = "/bin/sh"; String cmdOption = "-c"; String cmdParamPrefix = " cat /proc/uptime| awk -F. " + "'{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;" + "run_minute=($1 % 3600)/60;run_second=$1 % 60;" + "printf(\"?%d%d%d\",run_days,run_hour,run_minute)}'"; // System.out.println("" + cmdParamPrefix); String[] cmd = { cmdShell, cmdOption, cmdParamPrefix }; runtime = runCmd(cmd); } else { System.out.println("" + info.getSystemType()); } info.setRunTime(runtime); Globals.reloadConfigs(); String ret = ""; if ("Linux".equals(info.getSystemType())) { ret = ServiceNativeGmon.ODC_ED_GetLicenseInfo(Globals.GMON_IP, Globals.GMON_PORT); System.out.println(ret); } else { ret = "T|100|TEST-500-3|20200101|2FF1-8024-078C-A560|1|20141210||2"; } String[] licVal = ret.split("\\|"); if (licVal.length != 9) { info.setStartTime(""); info.setEndTime(""); info.setUser(""); info.setAudtype("?"); info.setCk_lic_ret("?"); } else { /** * ?license? ? * audtype|dbcount * |devtype|time|diskid??? audtype?TR? * dbcount??? devtype?32 * time??YYYYMMDD00000000 diskid??crc * */ // /** * * audtype? dbcount??? devtype? * starttime? endtime? diskid?id user??? * mod? ck_lic_ret: * * audtype|dbcount|devtype|endtime|diskid|ck_lic_ret|starttime|user| * mod */ SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat formatDate1 = new SimpleDateFormat("yyyyMMdd"); String licType = ""; if (licVal[0].equals("T")) { licType = ""; } else if (licVal[0].equals("S")) { licType = ""; } else if (licVal[0].equals("R")) { licType = "?"; } info.setAudtype(licType); info.setDbcount(Integer.parseInt(licVal[1])); info.setDevtype(licVal[2]); info.setEndTime(licVal[3].equals("00000000") ? "" : (formatDate.format(formatDate1.parse(licVal[3])))); info.setDiskid(getSeriesNumber()); info.setCk_lic_ret(Integer.parseInt(licVal[5]) == 1 ? "" : ""); info.setStartTime(licVal[6].equals("00000000") ? "" : formatDate.format(formatDate1.parse(licVal[6]))); info.setUser(licVal[7]); // ?? int dbAddressCount = info.getDbcount() == 1 ? 64 : info.getDbcount(); info.setMod_help(Integer.parseInt(licVal[8]) == 1 ? ("?(" + dbAddressCount + ")") : ("??(" + info.getDbcount() + ")")); if (!licVal[3].equals("00000000")) { Date now = formatDate1.parse(formatDate1.format(new Date()));// ? if (formatDate1.parse(licVal[3]).getTime() - now.getTime() < 0) { info.setCk_lic_ret(""); info.setEndTime( "(" + formatDate.format(formatDate1.parse(licVal[3])) + ")"); } } if (licVal[5].equals("0")) { info.setHelp_version_info("false"); } } return info; } /** * DB? * * @return */ public static int getDBsum() { Globals.reloadConfigs(); int saveDB = 0; if (System.getProperty("os.name").equals("Linux")) { saveDB = ServiceNativeGmon.ODC_ED_GetLicenseDBCount(Globals.GMON_IP, Globals.GMON_PORT); if (saveDB < 0) { String errMsg = Globals.ERROR_MSG_MAP.get(saveDB); if (errMsg == null) throw new ServiceException("[" + saveDB + "]?"); else throw new ServiceException("[" + saveDB + "]" + errMsg); } } return saveDB; } /** * ?? * * @return */ public static String getSeriesNumber() { Globals.reloadConfigs(); String ret = ""; if (System.getProperty("os.name").equals("Linux")) { ret = ServiceNativeGmon.ODC_ED_GetLicenseDiskCRC(Globals.GMON_IP, Globals.GMON_PORT); } System.out.println(Globals.GMON_IP + "-----" + Globals.GMON_PORT + "---------" + ret); return ret; } private static String runCmd(String[] cmd) throws IOException { Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } String result = sb.toString(); return result; } /** * * <b>?</b><br/> * * @author <b>LCL</b><br/> * @date 2016-8-3 * * @return */ public static int ifNsfocus() { int returnValure = 0; try { if (SystemUtils.IS_OS_LINUX) { String[] cmd = { "/bin/sh", "-c", " cat /etc/producttype " }; String value = runCmd(cmd).trim(); if (value.contains("Nsfocus")) { returnValure = 1; } } } catch (Exception e) { e.printStackTrace(); } return returnValure; } /** * ?license? * * @return * @throws ParseException */ public static LicenseInfo getNsfocusLicenseInfoFinal() throws Exception { LicenseInfo info = new LicenseInfo(); String systemType = System.getProperty("os.name"); String runtime = ""; if (systemType.equals("Linux")) { String cmdShell = "/bin/sh"; String cmdOption = "-c"; String cmdParamPrefix = " cat /proc/uptime| awk -F. " + "'{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;" + "run_minute=($1 % 3600)/60;run_second=$1 % 60;" + "printf(\"?%d%d%d\",run_days,run_hour,run_minute)}'"; String[] cmd = { cmdShell, cmdOption, cmdParamPrefix }; runtime = runCmd(cmd); } else { System.out.println("" + systemType); } info.setRunTime(runtime); Globals.reloadConfigs(); String ret = ""; if (systemType.equals("Linux")) { ret = ServiceNativeGmon.ODC_ED_GetLicenseInfo(Globals.GMON_IP, Globals.GMON_PORT); } else if (systemType.indexOf("Windows") > -1) { ret = "4|FCB5-5AB6-F481-FABB|1|5|user|20140101|20151231|10|1|1";// } String[] licVal = ret.split("\\|"); if (licVal.length != 10) { info.setStartTime(""); info.setEndTime(""); info.setUser(""); info.setAudtype("?"); } else { /** * audtype|hardw_id|modcode|ewkcount|lssue_usr|starttime|endtime| * dbcount|instcount|ck_lic_ret audtype, ?1-4 * 1.?/2.?/3.?/4.?? hardw_id, ? modcode, 1-32 * ,1:??,2:? ewkcount, ?? lssue_usr, ?? starttime, * endtime, ? dbcount ??? instcount ? ck_lic_ret * ??123 license?????4 ? */ SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatDate1 = new SimpleDateFormat("yyyyMMdd"); info.setVersionInfo(Integer.parseInt(licVal[0])); switch (Integer.parseInt(licVal[0])) {// ? case 1: info.setAudtype("?"); break; case 2: info.setAudtype("?"); break; case 3: info.setAudtype("?"); break; case 4: info.setAudtype("??"); break; default: break; } info.setHardw_id(licVal[1]);// ? // 1:??,2:? info.setModcode(Integer.parseInt(licVal[2])); info.setEwkcount(Integer.parseInt(licVal[3]));// ?? info.setUser(licVal[4]);// ?? info.setStartTime(formatDate.format(formatDate1.parse(licVal[5])));// info.setEndTime(formatDate.format(formatDate1.parse(licVal[6])));// ? info.setDbcount(Integer.parseInt(licVal[7]));// DB?? info.setInstcount(Integer.parseInt(licVal[8]));// ? int mode_info = Integer.parseInt(licVal[2]); String mode_help = ""; for (int i = 0; i <= 31; i++) { if ((mode_info & (1 << i)) > 0) { switch (i) { case 0: mode_help = mode_help + "???"; break; case 1: mode_help = mode_help + "??"; break; default: break; } } } if (mode_help.length() > 1) { mode_help = mode_help.substring(0, mode_help.length() - 1); } info.setMod_help(mode_help + ("(" + info.getDbcount() + "?)")); // 1 23 license????? info.setCk_lic_ret_im(Integer.parseInt(licVal[9])); if (Integer.parseInt(licVal[9]) != 0) { info.setCk_lic_ret(""); } // ? if (licVal[9].equals("3") && licVal[0].equals("4")) { Date now = formatDate1.parse(formatDate1.format(new Date()));// ? if (formatDate1.parse(licVal[6]).getTime() - now.getTime() < 0) { info.setEndTime( "(" + formatDate.format(formatDate1.parse(licVal[6])) + ")"); } if (formatDate1.parse(licVal[5]).getTime() - now.getTime() > 0) { info.setStartTime(formatDate.format(formatDate1.parse(licVal[5])) + ""); info.setClientInfo("1"); } } else if (!licVal[0].equals("4") && licVal[9].equals("2")) { Date now = formatDate1.parse(formatDate1.format(new Date()));// ? if (formatDate1.parse(licVal[6]).getTime() - now.getTime() < 0) { info.setEndTime( "(" + formatDate.format(formatDate1.parse(licVal[6])) + ")"); } if (formatDate1.parse(licVal[5]).getTime() - now.getTime() > 0) { info.setStartTime(formatDate.format(formatDate1.parse(licVal[5])) + ""); info.setClientInfo("1"); } } else if (licVal[0].equals("4") && licVal[9].equals("2")) { Date now = formatDate1.parse(formatDate1.format(new Date()));// ? if (formatDate1.parse(licVal[5]).getTime() - now.getTime() > 0) { info.setStartTime(formatDate.format(formatDate1.parse(licVal[5])) + ""); info.setClientInfo("1"); } } } return info; } }