Java tutorial
package com.jiangyifen.ec2.ui.mgr.system.tabsheet; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.StringReader; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Random; import org.apache.commons.lang3.StringUtils; import com.jiangyifen.ec2.entity.SerialNumber; import com.jiangyifen.ec2.globaldata.license.LicenseManager; import com.jiangyifen.ec2.service.common.CommonService; import com.jiangyifen.ec2.ui.util.NotificationUtil; import com.jiangyifen.ec2.utils.SpringContextHolder; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.GridLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; import com.vaadin.ui.TextArea; import com.vaadin.ui.VerticalLayout; @SuppressWarnings("serial") public class SystemLicence extends VerticalLayout { private final String VALUE_COL_COLOR = "blue"; // grid ? ? private final String DESCRIPTION_COL_COLOR = "#608525"; // grid ??? ? private GridLayout licence_glo; /** * ?? (-xxxx-xxxx)xxxx ?? */ public static String serialNumber; private static CommonService commonService; private Label csrAmountValue_lb; private Label expiredDateValue_lb; //lc ?? static { commonService = SpringContextHolder.getBean("commonService"); List<SerialNumber> list = commonService.getEntitiesByJpql("select s from SerialNumber as s"); if (list.size() != 0) { serialNumber = list.get(0).getSerialNumber(); } else { serialNumber = new SimpleDateFormat("yyyy-MMdd").format(new Date()) + "-" + (new Random().nextInt(8999) + 1000) + "-" + (new Random().nextInt(8999) + 1000); SerialNumber number = new SerialNumber(); number.setSerialNumber(serialNumber); //? commonService.save(number); } } public SystemLicence() { this.setMargin(true); this.setSizeUndefined(); licence_glo = new GridLayout(3, 5); licence_glo.setMargin(true); licence_glo.setSpacing(true); licence_glo.setSizeUndefined(); this.addComponent(licence_glo); int row_index = 0; int column_index = 0; // Label key_lb = new Label("<b>?</b>", Label.CONTENT_XHTML); key_lb.setWidth("-1px"); licence_glo.addComponent(key_lb, (column_index++), row_index); licence_glo.setComponentAlignment(key_lb, Alignment.MIDDLE_LEFT); Label value_lb = new Label("<font color='blue'>?</font>", Label.CONTENT_XHTML); value_lb.setWidth("-1px"); licence_glo.addComponent(value_lb, (column_index++), row_index); licence_glo.setComponentAlignment(value_lb, Alignment.MIDDLE_LEFT); Label description_lb = new Label("<font color='" + DESCRIPTION_COL_COLOR + "'>??</font>", Label.CONTENT_XHTML); description_lb.setWidth("-1px"); licence_glo.addComponent(description_lb, (column_index++), row_index); licence_glo.setComponentAlignment(description_lb, Alignment.MIDDLE_LEFT); // grid ??? row_index++; column_index = 0; // ?? Label csrAmount_lb = new Label("<b>??</b>", Label.CONTENT_XHTML); csrAmount_lb.setWidth("-1px"); licence_glo.addComponent(csrAmount_lb, (column_index++), row_index); csrAmountValue_lb = new Label("<font color='" + VALUE_COL_COLOR + "'>" + 0 + "</font>", Label.CONTENT_XHTML); csrAmountValue_lb.setWidth("-1px"); licence_glo.addComponent(csrAmountValue_lb, (column_index++), row_index); Label csrAmountDescription_lb = new Label( "<font color='" + DESCRIPTION_COL_COLOR + "'>????!!</font>", Label.CONTENT_XHTML); csrAmountDescription_lb.setWidth("-1px"); licence_glo.addComponent(csrAmountDescription_lb, (column_index++), row_index); // grid ??? row_index++; column_index = 0; // Label expiredDate_lb = new Label("<b></b>", Label.CONTENT_XHTML); expiredDate_lb.setWidth("-1px"); licence_glo.addComponent(expiredDate_lb, (column_index++), row_index); expiredDateValue_lb = new Label("<font color='" + VALUE_COL_COLOR + "'>invalid</font>", Label.CONTENT_XHTML); expiredDateValue_lb.setWidth("-1px"); licence_glo.addComponent(expiredDateValue_lb, (column_index++), row_index); Label expiredDateDescription_lb = new Label("<font color='" + DESCRIPTION_COL_COLOR + "'>??!!</font>", Label.CONTENT_XHTML); expiredDateDescription_lb.setWidth("-1px"); licence_glo.addComponent(expiredDateDescription_lb, (column_index++), row_index); // grid ??? row_index++; column_index = 0; //?? Label licence_lb = new Label("<b>? ?</b>", Label.CONTENT_XHTML); licence_lb.setWidth("-1px"); licence_glo.addComponent(licence_lb, (column_index++), row_index); Label licenceValue_lb = new Label("<font color='" + VALUE_COL_COLOR + "'>" + serialNumber + "</font>", Label.CONTENT_XHTML); // Label licenceValue_lb = new Label("<font color='blue'>"+GlobalVariable.CONCURRENT_MAX_CSR+"</font>", Label.CONTENT_XHTML); licenceValue_lb.setWidth("-1px"); licence_glo.addComponent(licenceValue_lb, (column_index++), row_index); Label licenceDescription_lb = new Label( "<font color='" + DESCRIPTION_COL_COLOR + "'>???!!</font>", Label.CONTENT_XHTML); licenceDescription_lb.setWidth("-1px"); licence_glo.addComponent(licenceDescription_lb, (column_index++), row_index); this.addComponent(updateLicenseComponent()); //?? refreshLicenseInfo(); } /** * added by chb 20140520 * @return */ private VerticalLayout updateLicenseComponent() { VerticalLayout licenseUpdateLayout = new VerticalLayout(); final VerticalLayout textAreaPlaceHolder = new VerticalLayout(); final HorizontalLayout buttonPlaceHolder = new HorizontalLayout(); buttonPlaceHolder.setSpacing(true); // final Button updateButton = new Button("License"); updateButton.setData("show"); // final Button cancelButton = new Button("?"); // final TextArea licenseTextArea = new TextArea(); licenseTextArea.setColumns(30); licenseTextArea.setRows(5); licenseTextArea.setWordwrap(true); licenseTextArea.setInputPrompt("??"); //Layout buttonPlaceHolder.addComponent(updateButton); licenseUpdateLayout.addComponent(textAreaPlaceHolder); licenseUpdateLayout.addComponent(buttonPlaceHolder); // cancelButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { textAreaPlaceHolder.removeAllComponents(); buttonPlaceHolder.removeComponent(cancelButton); updateButton.setData("show"); updateButton.setCaption("License"); } }); updateButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { if (((String) event.getButton().getData()).equals("show")) { textAreaPlaceHolder.removeAllComponents(); textAreaPlaceHolder.addComponent(licenseTextArea); buttonPlaceHolder.addComponent(cancelButton); event.getButton().setData("updateAndHide"); event.getButton().setCaption("??"); } else if (((String) event.getButton().getData()).equals("updateAndHide")) { StringReader stringReader = new StringReader( StringUtils.trimToEmpty((String) licenseTextArea.getValue())); Properties props = new Properties(); try { props.load(stringReader); } catch (IOException e) { e.printStackTrace(); } stringReader.close(); // Boolean isValidvalidateLicense(licenseTextArea.getValue()); String license_date = props.getProperty(LicenseManager.LICENSE_DATE); String license_count = props.getProperty(LicenseManager.LICENSE_COUNT); String license_localmd5 = props.getProperty(LicenseManager.LICENSE_LOCALMD5); // Boolean isMatch = regexMatchCheck(license_date, license_count, license_localmd5); if (isMatch) { Map<String, String> licenseMap = new HashMap<String, String>(); //?? // String license_count = (String)props.get(LicenseManager.LICENSE_COUNT); license_count = license_count == null ? "" : license_count; licenseMap.put(LicenseManager.LICENSE_COUNT, license_count); //? // String license_date = (String)props.get(LicenseManager.LICENSE_DATE); license_date = license_date == null ? "" : license_date; licenseMap.put(LicenseManager.LICENSE_DATE, license_date); //??? // String license_localmd5 = (String)props.get(LicenseManager.LICENSE_LOCALMD5); license_localmd5 = license_localmd5 == null ? "" : license_localmd5; licenseMap.put(LicenseManager.LICENSE_LOCALMD5, license_localmd5); //?License Map<String, String> resultMap = LicenseManager.licenseValidate(licenseMap); String validateResult = resultMap.get(LicenseManager.LICENSE_VALIDATE_RESULT); if (LicenseManager.LICENSE_VALID.equals(validateResult)) { //continue } else { NotificationUtil.showWarningNotification(SystemLicence.this, "License ?License"); return; } try { URL resourceurl = SystemLicence.class.getResource(LicenseManager.LICENSE_FILE); //System.err.println("chb: SystemLicense"+resourceurl.getPath()); OutputStream fos = new FileOutputStream(resourceurl.getPath()); props.store(fos, "license"); } catch (Exception e) { e.printStackTrace(); NotificationUtil.showWarningNotification(SystemLicence.this, "License "); return; } textAreaPlaceHolder.removeAllComponents(); buttonPlaceHolder.removeComponent(cancelButton); updateButton.setData("show"); updateButton.setCaption("License"); LicenseManager.loadLicenseFile(LicenseManager.LICENSE_FILE.substring(1)); // LicenseManager.loadLicenseFile(licenseFilename) refreshLicenseInfo(); NotificationUtil.showWarningNotification(SystemLicence.this, "License ?,?"); } else { NotificationUtil.showWarningNotification(SystemLicence.this, "License ??"); } } } /** * license ? * @param license_date * @param license_count * @param license_localmd5 * @return */ private Boolean regexMatchCheck(String license_date, String license_count, String license_localmd5) { if (StringUtils.isEmpty(license_date) || StringUtils.isEmpty(license_count) || StringUtils.isEmpty(license_localmd5)) { return false; } String date_regex = "^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}$"; //? String count_regex = "^\\d+$"; //? String md5_32_regex = "^\\w{32}$"; //? return license_localmd5.matches(md5_32_regex) && license_date.matches(date_regex) && license_count.matches(count_regex); } }); return licenseUpdateLayout; } /** * License? */ private void refreshLicenseInfo() { // Label value_lb = new Label("<font color='blue'>?</font>", Label.CONTENT_XHTML); Map<String, String> licenseMap = LicenseManager.licenseValidate(); String licenseCount = licenseMap.get(LicenseManager.LICENSE_COUNT); String licenseDate = licenseMap.get(LicenseManager.LICENSE_DATE); csrAmountValue_lb.setValue("<font color='" + VALUE_COL_COLOR + "'>" + licenseCount + "</font>");//, Label.CONTENT_XHTML); expiredDateValue_lb.setValue("<font color='" + VALUE_COL_COLOR + "'>" + licenseDate + "</font>");//, Label.CONTENT_XHTML); // Label licenceValue_lb = new Label("<font color='"+VALUE_COL_COLOR+"'>"+serialNumber+"</font>", Label.CONTENT_XHTML); } }