com.clustercontrol.port.dialog.PortCreateDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.port.dialog.PortCreateDialog.java

Source

/*
    
Copyright (C) 2006 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.port.dialog;

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.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
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.bean.HinemosModuleConstant;
import com.clustercontrol.bean.RequiredFieldColorConstant;
import com.clustercontrol.monitor.run.dialog.CommonMonitorNumericDialog;
import com.clustercontrol.monitor.util.MonitorSettingEndpointWrapper;
import com.clustercontrol.port.bean.PortRunCountConstant;
import com.clustercontrol.port.bean.PortRunIntervalConstant;
import com.clustercontrol.port.bean.ProtocolConstant;
import com.clustercontrol.port.bean.ProtocolMessage;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.WidgetTestUtil;
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.PortCheckInfo;

/**
 * ????
 *
 * @version 4.0.0
 * @since 2.4.0
 */
public class PortCreateDialog extends CommonMonitorNumericDialog {

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

    // ----- instance  ----- //
    /**  */
    private Text m_textTimeout = null;

    /** TCP?? */
    private Button m_radioTCP = null;

    /**  */
    private Button m_radioService = null;

    /**  */
    private Combo m_comboService = null;

    /** ?? */
    private Text m_textPortNo = null;

    /**  */
    private Text m_textRunCount = null;

    /**  */
    private Text m_textRunInterval = null;

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

    /**
     * ?????
     *
     * @param parent
     *            ?
     */
    public PortCreateDialog(Shell parent) {
        super(parent, null);
    }

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

        this.monitorId = monitorId;
        this.updateFlg = updateFlg;
    }

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

    /**
     * ????
     *
     * @param parent
     *            ?
     */
    @Override
    protected void customizeDialog(Composite parent) {
        // ????
        item1 = Messages.getString("response.time.milli.sec");
        item2 = Messages.getString("response.time.milli.sec");

        super.customizeDialog(parent);
        m_numericValueInfo.setInfoWarnText("0", "1000", "1000", "3000");

        // 
        shell.setText(Messages.getString("dialog.port.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 = HALF_MARGIN;
        layout.marginHeight = HALF_MARGIN;
        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"));

        /*
         * 
         */
        // TCP??
        this.m_radioTCP = new Button(groupCheckRule, SWT.RADIO);
        WidgetTestUtil.setTestId(this, "tcp", m_radioTCP);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_radioTCP.setLayoutData(gridData);
        this.m_radioTCP.setText(Messages.getString("tcp.connect.only"));
        this.m_radioTCP.setSelection(true);

        // 
        this.m_radioService = new Button(groupCheckRule, SWT.RADIO);
        WidgetTestUtil.setTestId(this, "service", m_radioService);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_radioService.setLayoutData(gridData);
        this.m_radioService.setText(Messages.getString("service.protocol"));
        this.m_radioService.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                setEnabledComboService(m_radioService.getSelection());
            }
        });

        //
        this.m_comboService = new Combo(groupCheckRule, SWT.DROP_DOWN | SWT.READ_ONLY);
        WidgetTestUtil.setTestId(this, "service", m_comboService);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_comboService.setLayoutData(gridData);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_FTP);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_SMTP);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_SMTPS);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_POP3);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_POP3S);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_IMAP);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_IMAPS);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_NTP);
        this.m_comboService.add(ProtocolMessage.STRING_PROTOCOL_DNS);
        //this.m_comboService.setEnabled(false);

        //
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "space1", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TEXT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);

        /*
         * ??
         */
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "portnumber", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("port.number") + " : ");
        // 
        this.m_textPortNo = new Text(groupCheckRule, SWT.BORDER);
        WidgetTestUtil.setTestId(this, "protno", m_textPortNo);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_textPortNo.setLayoutData(gridData);
        this.m_textPortNo.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "space2", label);
        gridData = new GridData();
        gridData.horizontalSpan = HALF_UNIT - (WIDTH_TITLE_LONG + WIDTH_VALUE_LONG);
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);

        /*
         * 
         */
        //
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "space3", label);
        gridData = new GridData();
        gridData.horizontalSpan = MIN_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);

        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "labelruncount", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE + MIN_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("trial.run.count") + " : ");

        // 
        this.m_textRunCount = new Text(groupCheckRule, SWT.BORDER);
        WidgetTestUtil.setTestId(this, "textruncount", m_textRunCount);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_textRunCount.setLayoutData(gridData);
        this.m_textRunCount.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        // ??
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "labelcount", label);
        gridData = new GridData();
        gridData.horizontalSpan = HALF_UNIT - (WIDTH_TITLE + WIDTH_VALUE + WIDTH_VALUE_LONG);
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("count"));

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

        // 
        this.m_textRunInterval = new Text(groupCheckRule, SWT.BORDER);
        WidgetTestUtil.setTestId(this, "runinterval", m_textRunInterval);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_textRunInterval.setLayoutData(gridData);
        this.m_textRunInterval.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        // ??
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "labelmillisec1", label);
        gridData = new GridData();
        gridData.horizontalSpan = HALF_UNIT - (WIDTH_TITLE_LONG + WIDTH_VALUE_LONG);
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("milli.sec"));

        /*
         * 
         */
        //
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "spacetimeout", label);
        gridData = new GridData();
        gridData.horizontalSpan = MIN_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        // 
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "labeltimeout", label);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_TITLE + MIN_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("time.out") + " : ");

        // 
        this.m_textTimeout = new Text(groupCheckRule, SWT.BORDER);
        WidgetTestUtil.setTestId(this, "timeout", m_textTimeout);
        gridData = new GridData();
        gridData.horizontalSpan = WIDTH_VALUE_LONG;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_textTimeout.setLayoutData(gridData);
        this.m_textTimeout.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        // ??
        label = new Label(groupCheckRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "labelmillisec2", label);
        gridData = new GridData();
        gridData.horizontalSpan = HALF_UNIT - (WIDTH_TITLE + WIDTH_VALUE + WIDTH_VALUE_LONG);
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("milli.sec"));

        // ????
        this.itemName.setText(Messages.getString("response.time"));

        // ????
        this.measure.setText(Messages.getString("time.msec"));

        // 
        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(monitorId);
            } catch (InvalidRole_Exception e) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
                throw new InternalError(e.getMessage());
            } catch (Exception e) {
                // Manager???????????
                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);

    }

    protected void setEnabledComboService(boolean enable) {
        this.m_comboService.setEnabled(enable);
    }

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

        // 
        if (this.m_textPortNo.getEnabled() && "".equals(this.m_textPortNo.getText())) {
            this.m_textPortNo.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.m_textPortNo.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
        if (this.m_textTimeout.getEnabled() && "".equals(this.m_textTimeout.getText())) {
            this.m_textTimeout.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.m_textTimeout.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
    }

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

        this.inputData = monitor;

        // ? port
        PortCheckInfo portInfo = monitor.getPortCheckInfo();
        if (portInfo.getServiceId() == null || portInfo.getServiceId().equals(ProtocolConstant.TYPE_PROTOCOL_TCP)) {
            this.m_radioTCP.setSelection(true);
            this.m_radioService.setSelection(false);
            setEnabledComboService(m_radioService.getSelection());
        } else {
            this.m_radioTCP.setSelection(false);
            this.m_radioService.setSelection(true);
            setEnabledComboService(m_radioService.getSelection());
            this.m_comboService.setText(ProtocolMessage.typeToString(portInfo.getServiceId()));
        }
        if (portInfo.getPortNo() != null) {
            this.m_textPortNo.setText(portInfo.getPortNo().toString());
        }
        if (portInfo.getRunCount() != null) {
            this.m_textRunCount.setText(portInfo.getRunCount().toString());
        }
        if (portInfo.getRunInterval() != null) {
            this.m_textRunInterval.setText(portInfo.getRunInterval().toString());
        }
        // 
        if (portInfo.getTimeout() != null) {
            this.m_textTimeout.setText(portInfo.getTimeout().toString());
        }

        m_numericValueInfo.setInputData(monitor);

    }

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

        // port
        monitorInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_PORT);

        // ? port
        PortCheckInfo portInfo = new PortCheckInfo();

        /** ?? */
        if (this.m_radioTCP.getSelection()) {
            portInfo.setServiceId(ProtocolConstant.TYPE_PROTOCOL_TCP);
        } else if (this.m_radioService.getSelection()) {/* ??? */
            if (this.m_comboService.getText() != null && !"".equals((this.m_comboService.getText()).trim())) {
                portInfo.setServiceId(ProtocolMessage.stringToType(this.m_comboService.getText()));
            }
        }
        /** ?? */
        if (this.m_textPortNo.getText() != null && !"".equals((this.m_textPortNo.getText()).trim())) {
            try {
                Integer portNo = Integer.valueOf(this.m_textPortNo.getText().trim());
                portInfo.setPortNo(portNo);
            } catch (NumberFormatException e) {
                this.setValidateResult(Messages.getString("message.hinemos.1"),
                        Messages.getString("message.port.8"));
                return null;
            }
        }
        /**  */

        if (!"".equals((this.m_textRunCount.getText()).trim())) {

            try {
                Integer runcount = Integer.parseInt(this.m_textRunCount.getText().trim());
                portInfo.setRunCount(runcount);
            } catch (NumberFormatException e) {
                this.setValidateResult(Messages.getString("message.hinemos.1"),
                        Messages.getString("message.port.1"));
                return null;
            }
        }
        /**  */

        if (!"".equals((this.m_textRunInterval.getText()).trim())) {

            try {
                Integer runinterval = Integer.parseInt(this.m_textRunInterval.getText().trim());
                portInfo.setRunInterval(runinterval);
            } catch (NumberFormatException e) {
                this.setValidateResult(Messages.getString("message.hinemos.1"),
                        Messages.getString("message.port.2"));
                return null;
            }
        }
        /**  */
        if (!"".equals((this.m_textTimeout.getText()).trim())) {

            try {
                Integer timeout = Integer.parseInt(this.m_textTimeout.getText().trim());
                portInfo.setTimeout(timeout);
            } catch (NumberFormatException e) {
                this.setValidateResult(Messages.getString("message.hinemos.1"),
                        Messages.getString("message.port.3"));
                return null;
            }
        }
        monitorInfo.setPortCheckInfo(portInfo);

        // ??
        validateResult = m_numericValueInfo.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;
        if (info != null) {
            String[] args = { info.getMonitorId(), getManagerName() };
            MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper.getWrapper(getManagerName());
            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;
    }

    /**
     * MonitorInfo?????
     *
     * @see com.clustercontrol.dialog.CommonMonitorDialog#setInfoInitialValue()
     */
    @Override
    protected void setInfoInitialValue(MonitorInfo monitor) {

        super.setInfoInitialValue(monitor);

        PortCheckInfo portCheckInfo = new PortCheckInfo();
        // ??
        portCheckInfo.setPortNo(ProtocolConstant.DEFAULT_PORT_NUM_PROTOCOL_TCP);
        // 
        portCheckInfo.setRunCount(PortRunCountConstant.TYPE_COUNT_01);
        // 
        portCheckInfo.setRunInterval(PortRunIntervalConstant.TYPE_SEC_01);
        // 
        portCheckInfo.setTimeout(TIMEOUT_SEC);
        monitor.setPortCheckInfo(portCheckInfo);
    }

}