com.clustercontrol.winevent.dialog.WinEventDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.winevent.dialog.WinEventDialog.java

Source

/*
    
Copyright (C) 2013 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.
    
 */
package com.clustercontrol.winevent.dialog;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.WidgetTestUtil;
import com.clustercontrol.bean.HinemosModuleConstant;
import com.clustercontrol.bean.RequiredFieldColorConstant;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.monitor.run.dialog.CommonMonitorStringDialog;
import com.clustercontrol.monitor.util.MonitorSettingEndpointWrapper;
import com.clustercontrol.util.Messages;
import com.clustercontrol.winevent.util.WinEventUtil;
import com.clustercontrol.ws.monitor.InvalidRole_Exception;
import com.clustercontrol.ws.monitor.MonitorDuplicate_Exception;
import com.clustercontrol.ws.monitor.MonitorInfo;
import com.clustercontrol.ws.monitor.WinEventCheckInfo;

/**
 * Windows?<BR>
 *
 * @version 4.1.0
 * @since 4.1.0
 */
public class WinEventDialog extends CommonMonitorStringDialog {

    // 
    private static Log m_log = LogFactory.getLog(WinEventDialog.class);

    // ----- instance  ----- //
    private Button levelCritical = null;
    private Button levelWarning = null;
    private Button levelVerbose = null;
    private Button levelError = null;
    private Button levelInformational = null;
    private Text logName = null;
    private Text source = null;
    private Text eventId = null;
    private Text category = null;
    private Text keywords = null;
    /** ??? */
    private String managerName = null;

    // -----  ----- //

    /**
     * ?????
     *
     * @param parent
     *            ?
     */
    public WinEventDialog(Shell parent) {
        super(parent, null);
        logLineFlag = true;
    }

    /**
     * ????
     *
     * @param parent ?
     * @param managerName ???
     * @param monitorId ?ID
     * @param updateFlg ????true:?false:?
     */
    public WinEventDialog(Shell parent, String managerName, String monitorId, boolean updateFlg) {
        super(parent, managerName);

        logLineFlag = true;
        this.managerName = managerName;
        this.monitorId = monitorId;
        this.updateFlg = updateFlg;
    }

    // ----- instance  ----- //

    /**
     * ????
     *
     * @param parent ?
     */
    @Override
    protected void customizeDialog(Composite parent) {

        super.customizeDialog(parent);

        // 
        shell.setText(Messages.getString("dialog.winevent.create.modify"));

        // ????
        Label label = null;
        // ????
        GridData gridData = null;

        /*
         * ????
         */
        Group groupCheckRule = new Group(groupRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "checkrule", groupCheckRule);
        GridLayout layout = new GridLayout(1, true);
        layout.marginWidth = 5;
        layout.marginHeight = 5;
        layout.numColumns = BASIC_UNIT;
        groupCheckRule.setLayout(layout);
        gridData = new GridData();
        gridData.horizontalSpan = BASIC_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        groupCheckRule.setLayoutData(gridData);
        groupCheckRule.setText(Messages.getString("check.rule"));

        /*
         * 
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "wineventlevel", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("winevent.level") + " : ");

        // (?)
        this.levelCritical = new Button(groupCheckRule, SWT.CHECK);
        WidgetTestUtil.setTestId(this, "criticalcheck", levelCritical);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.levelCritical.setLayoutData(gridData);
        this.levelCritical.setText(Messages.getString("winevent.level.critical"));

        // ()
        this.levelWarning = new Button(groupCheckRule, SWT.CHECK);
        WidgetTestUtil.setTestId(this, "warningcheck", levelWarning);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.levelWarning.setLayoutData(gridData);
        this.levelWarning.setText(Messages.getString("winevent.level.warning"));

        // ()
        this.levelVerbose = new Button(groupCheckRule, SWT.CHECK);
        WidgetTestUtil.setTestId(this, "verbosecheck", levelVerbose);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.levelVerbose.setLayoutData(gridData);
        this.levelVerbose.setText(Messages.getString("winevent.level.verbose"));

        // ()
        this.levelError = new Button(groupCheckRule, SWT.CHECK);
        WidgetTestUtil.setTestId(this, "errorcheck", levelError);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.levelError.setLayoutData(gridData);
        this.levelError.setText(Messages.getString("winevent.level.error"));

        // ()
        this.levelInformational = new Button(groupCheckRule, SWT.CHECK);
        WidgetTestUtil.setTestId(this, "infomationalcheck", levelInformational);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.levelInformational.setLayoutData(gridData);
        this.levelInformational.setText(Messages.getString("winevent.level.informational"));

        /*
         *  
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "wineveltlog", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("winevent.log") + " : ");
        // 
        this.logName = new Text(groupCheckRule, SWT.BORDER | SWT.LEFT);
        WidgetTestUtil.setTestId(this, "logname", logName);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TEXT_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.logName.setLayoutData(gridData);
        this.logName.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        /*
         *  
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "wineventsource", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("winevent.source") + " : ");
        // 
        this.source = new Text(groupCheckRule, SWT.BORDER | SWT.LEFT);
        WidgetTestUtil.setTestId(this, "source", source);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TEXT_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.source.setLayoutData(gridData);
        this.source.setMessage(Messages.getString("message.winevent.1"));

        /*
         * ID
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "wineventid", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("winevent.id") + " : ");
        // 
        this.eventId = new Text(groupCheckRule, SWT.BORDER | SWT.LEFT);
        WidgetTestUtil.setTestId(this, "eventid", eventId);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TEXT_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.eventId.setLayoutData(gridData);
        this.eventId.setMessage(Messages.getString("message.winevent.2"));

        /*
         * ?
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "wineventcategory", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("winevent.category") + " : ");
        // 
        this.category = new Text(groupCheckRule, SWT.BORDER | SWT.LEFT);
        WidgetTestUtil.setTestId(this, "category", category);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TEXT_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.category.setLayoutData(gridData);
        this.category.setMessage(Messages.getString("message.winevent.3"));

        /*
         * 
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "keywords", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("winevent.keywords") + " : ");
        // 
        this.keywords = new Text(groupCheckRule, SWT.BORDER | SWT.LEFT);
        WidgetTestUtil.setTestId(this, "keywords", keywords);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TEXT_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.keywords.setLayoutData(gridData);
        this.keywords.setMessage(Messages.getString("message.winevent.4"));

        // ?????
        this.m_monitorRule.setRunIntervalEnabled(false);

        // 
        this.adjustDialog();

        // ?
        MonitorInfo info = null;
        if (this.monitorId == null) {
            // ???
            info = new MonitorInfo();
            this.setInfoInitialValue(info);
        } else {
            // ????
            try {
                MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper.getWrapper(managerName);
                info = wrapper.getMonitor(this.monitorId);
            } catch (InvalidRole_Exception e) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
                throw new InternalError(e.getMessage());
            } catch (Exception e) {
                // ?
                m_log.warn("customizeDialog(), " + HinemosMessage.replace(e.getMessage()), e);
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.hinemos.failure.unexpected") + ", "
                                + HinemosMessage.replace(e.getMessage()));
                throw new InternalError(e.getMessage());
            }
        }
        this.setInputData(info);
        this.update();
    }

    /**
     * ?
     *
     */
    @Override
    public void update() {
        super.update();

        // ??????
        if ("".equals(this.logName.getText())) {
            this.logName.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.logName.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
    }

    /**
     * ?????
     *
     * @param monitor ????
     */
    @Override
    protected void setInputData(MonitorInfo monitor) {

        super.setInputData(monitor);
        this.inputData = monitor;

        // ? Windows
        WinEventCheckInfo checkInfo = monitor.getWinEventCheckInfo();
        if (checkInfo == null) {
            checkInfo = new WinEventCheckInfo();

            // 
            // ???
            checkInfo.setLevelCritical(true);
            checkInfo.setLevelWarning(true);
            checkInfo.setLevelVerbose(false);
            checkInfo.setLevelError(true);
            checkInfo.setLevelInformational(false);

            // Application, System
            checkInfo.getLogName().add("Application");
            checkInfo.getLogName().add("System");
        }
        this.levelCritical.setSelection(checkInfo.isLevelCritical());
        this.levelWarning.setSelection(checkInfo.isLevelWarning());
        this.levelVerbose.setSelection(checkInfo.isLevelVerbose());
        this.levelError.setSelection(checkInfo.isLevelError());
        this.levelInformational.setSelection(checkInfo.isLevelInformational());
        if (checkInfo.getLogName() != null)
            this.logName.setText(listToCommaSeparatedString(checkInfo.getLogName()));
        if (checkInfo.getSource() != null)
            this.source.setText(listToCommaSeparatedString(checkInfo.getSource()));
        if (checkInfo.getEventId() != null)
            this.eventId.setText(listToCommaSeparatedString(checkInfo.getEventId()));
        if (checkInfo.getCategory() != null)
            this.category.setText(listToCommaSeparatedString(checkInfo.getCategory()));
        if (checkInfo.getKeywords() != null)
            this.keywords.setText(keywordLongToString(checkInfo.getKeywords()));

        m_stringValueInfo.setInputData(monitor);
    }

    /**
     * ??????
     *
     * @return ????
     */
    @Override
    protected MonitorInfo createInputData() {
        super.createInputData();
        if (validateResult != null) {
            return null;
        }

        // Windows
        monitorInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_WINEVENT);

        // ? Windows
        WinEventCheckInfo winEventInfo = new WinEventCheckInfo();
        winEventInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_WINEVENT);
        winEventInfo.setMonitorId(monitorInfo.getMonitorId());
        winEventInfo.setLevelCritical(this.levelCritical.getSelection());
        winEventInfo.setLevelWarning(this.levelWarning.getSelection());
        winEventInfo.setLevelVerbose(this.levelVerbose.getSelection());
        winEventInfo.setLevelError(this.levelError.getSelection());
        winEventInfo.setLevelInformational(this.levelInformational.getSelection());

        for (String logName : commaSeparatedStringToStringList(this.logName.getText())) {
            winEventInfo.getLogName().add(logName);
        }

        for (String source : commaSeparatedStringToStringList(this.source.getText())) {
            winEventInfo.getSource().add(source);
        }

        try {
            for (Integer id : commaSeparatedStringToIntegerList(this.eventId.getText())) {
                winEventInfo.getEventId().add(id);
            }
        } catch (HinemosUnknown e) {
            setValidateResult(Messages.getString("message.hinemos.1"),
                    HinemosMessage.replace(e.getMessage()) + "\n" + Messages.getString("winevent.id"));
        }

        try {
            for (Integer category : commaSeparatedStringToIntegerList(this.category.getText())) {
                winEventInfo.getCategory().add(category);
            }
        } catch (HinemosUnknown e) {
            setValidateResult(Messages.getString("message.hinemos.1"),
                    HinemosMessage.replace(e.getMessage()) + "\n" + Messages.getString("winevent.category"));
        }

        if (this.keywords.getText() != null && !"".equals(this.keywords.getText())) {
            for (Long keyword : keywordStringToLongList(this.keywords.getText())) {
                winEventInfo.getKeywords().add(keyword);
            }
        }

        if (validateResult != null) {
            return null;
        }

        monitorInfo.setWinEventCheckInfo(winEventInfo);

        // ??
        validateResult = m_stringValueInfo.createInputData(monitorInfo);
        if (validateResult != null) {
            return null;
        }

        // ??
        validateResult = m_notifyInfo.createInputData(monitorInfo);
        if (validateResult != null) {
            if (validateResult.getID() == null) { // ID
                if (!displayQuestion(validateResult)) {
                    validateResult = null;
                    return null;
                }
            } else { // ?
                return null;
            }
        }

        return monitorInfo;
    }

    /**
     * ?????
     *
     * @return true?false
     *
     * @see com.clustercontrol.dialog.CommonDialog#action()
     */
    @Override
    protected boolean action() {
        boolean result = false;

        MonitorInfo info = this.inputData;
        String managerName = this.getManagerName();

        if (info != null) {
            String[] args = { info.getMonitorId(), managerName };
            MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper.getWrapper(managerName);
            if (!this.updateFlg) {
                // ???
                try {
                    result = wrapper.addMonitor(info);

                    if (result) {
                        MessageDialog.openInformation(null, Messages.getString("successful"),
                                Messages.getString("message.monitor.33", args));
                    } else {
                        MessageDialog.openError(null, Messages.getString("failed"),
                                Messages.getString("message.monitor.34", args));
                    }
                } catch (MonitorDuplicate_Exception e) {
                    // ID????????
                    MessageDialog.openInformation(null, Messages.getString("message"),
                            Messages.getString("message.monitor.53", args));

                } catch (Exception e) {
                    String errMessage = "";
                    if (e instanceof InvalidRole_Exception) {
                        // ??????
                        MessageDialog.openInformation(null, Messages.getString("message"),
                                Messages.getString("message.accesscontrol.16"));
                    } else {
                        errMessage = ", " + HinemosMessage.replace(e.getMessage());
                    }

                    MessageDialog.openError(null, Messages.getString("failed"),
                            Messages.getString("message.monitor.34", args) + errMessage);
                }
            } else {
                // ??
                String errMessage = "";
                try {
                    result = wrapper.modifyMonitor(info);
                } catch (InvalidRole_Exception e) {
                    // ??????
                    MessageDialog.openInformation(null, Messages.getString("message"),
                            Messages.getString("message.accesscontrol.16"));
                } catch (Exception e) {
                    errMessage = ", " + HinemosMessage.replace(e.getMessage());
                }

                if (result) {
                    MessageDialog.openInformation(null, Messages.getString("successful"),
                            Messages.getString("message.monitor.35", args));
                } else {
                    MessageDialog.openError(null, Messages.getString("failed"),
                            Messages.getString("message.monitor.36", args) + errMessage);
                }
            }
        }

        return result;
    }

    private static String listToCommaSeparatedString(List<?> list) {
        if (list != null) {
            String string = list.toString();
            string = string.replace("[", "");
            string = string.replace("]", "");
            return string;
        } else {
            return null;
        }
    }

    private static List<Integer> commaSeparatedStringToIntegerList(String string) throws HinemosUnknown {
        String[] numbers = string.split("\\s*,\\s*");
        ArrayList<Integer> list = new ArrayList<Integer>();
        try {
            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] != null && !"".equals(numbers[i])) {
                    list.add(Integer.parseInt(numbers[i]));
                }
            }
        } catch (NumberFormatException e) {
            throw new HinemosUnknown(Messages.getString("message.winevent.6"), e);
        }
        return list;
    }

    private static List<Long> keywordStringToLongList(String keywords) {
        ArrayList<Long> longList = new ArrayList<Long>();

        List<String> keywordList = commaSeparatedStringToStringList(keywords);
        for (String keyword : keywordList) {
            // ???
            if (WinEventUtil.containsKeywordString(keyword)) {
                longList.add(WinEventUtil.getKeywordLong(keyword));
            } else {
                // ??
                try {
                    longList.add(Long.parseLong(keyword));
                }
                // ??
                catch (NumberFormatException e) {
                    MessageDialog.openInformation(null, Messages.getString("message"),
                            Messages.getString("message.winevent.7"));

                    m_log.error("unknown keyword : " + keyword);
                    throw e;
                }
            }

        }
        return longList;
    }

    private static String keywordLongToString(List<Long> keywordList) {
        ArrayList<String> list = new ArrayList<String>();
        for (Long keyword : keywordList) {
            String keywordStr = WinEventUtil.getKeywordString(keyword);

            // Keyword?long??String????????????
            if (keywordStr != null) {
                list.add(keywordStr);
            }
            // Keyword?long??String???????long??????????
            else {
                list.add(keyword.toString());
            }
        }
        return listToCommaSeparatedString(list);
    }

    private static List<String> commaSeparatedStringToStringList(String string) {
        String[] strings = string.split("\\s*,\\s*");
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i] != null && !"".equals(strings[i])) {
                list.add(strings[i]);
            }
        }
        return list;
    }
}