com.clustercontrol.infra.dialog.CommandModuleDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.infra.dialog.CommandModuleDialog.java

Source

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

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.graphics.Point;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.clustercontrol.bean.PropertyDefineConstant;
import com.clustercontrol.bean.RequiredFieldColorConstant;
import com.clustercontrol.dialog.CommonDialog;
import com.clustercontrol.dialog.ValidateResult;
import com.clustercontrol.infra.bean.AccessMethodConstant;
import com.clustercontrol.infra.bean.AccessMethodMessage;
import com.clustercontrol.infra.util.InfraEndpointWrapper;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.ws.infra.CommandModuleInfo;
import com.clustercontrol.ws.infra.HinemosUnknown_Exception;
import com.clustercontrol.ws.infra.InfraManagementDuplicate_Exception;
import com.clustercontrol.ws.infra.InfraManagementInfo;
import com.clustercontrol.ws.infra.InfraManagementNotFound_Exception;
import com.clustercontrol.ws.infra.InfraModuleInfo;
import com.clustercontrol.ws.infra.InvalidRole_Exception;
import com.clustercontrol.ws.infra.InvalidSetting_Exception;
import com.clustercontrol.ws.infra.InvalidUserPass_Exception;
import com.clustercontrol.ws.infra.NotifyDuplicate_Exception;
import com.clustercontrol.ws.infra.NotifyNotFound_Exception;

/**
 * [??]??
 *
 * @version 5.0.0
 * @since 5.0.0
 */
public class CommandModuleDialog extends CommonDialog {

    // 
    private static Log m_log = LogFactory.getLog(CommandModuleDialog.class);
    /** []*/
    private InfraManagementInfo infraInfo;
    private CommandModuleInfo moduleInfo;

    /**
     * ???
     * ??????????????
     * ??????????
     */
    private final int DIALOG_WIDTH = 12;
    /** ? */
    private final int TITLE_WIDTH = 4;
    /** ? */
    private final int FORM_WIDTH = 6;
    /*
     * 
     */
    /** ID */
    private Text m_moduleId = null;
    /** ?? */
    private Text m_moduleName = null;
    /**  */
    private Text m_commandExec = null;
    /** ? */
    private Text m_commandCheck = null;
    /** ?????? */
    private Button m_precheckFlg = null;
    /** ??????*/
    private Button m_proceedIfFailFlg = null;
    /** ?*/
    private Button m_validFlg = null;
    /** SSH */
    private Button m_methodSSH = null;
    /** WinRM */
    private Button m_methodWinRM = null;
    /** */
    private Label m_label = null;
    /**  */
    private Shell m_shell = null;

    /**
     * ?MODE_ADD = 0;
     * MODE_MODIFY = 1;
     * MODE_COPY = 3;
     * */
    private int mode;
    /** ??? */
    private String managerName = null;
    /** ?ID*/
    private String moduleId = null;
    /** ID */
    private String managementId = null;
    /** ?-1*/
    private int index = -1;

    /**
     * 
     * ?
     * @param parent
     * @param id
     */
    public CommandModuleDialog(Shell parent, String managerName, String managementId) {
        super(parent);
        this.managerName = managerName;
        this.managementId = managementId;
    }

    public CommandModuleDialog(Shell parent, String managerName, String managementId, String moduleId, int mode) {
        super(parent);
        this.managerName = managerName;
        this.managementId = managementId;
        this.moduleId = moduleId;
        this.mode = mode;
    }

    /**
     * ????
     *
     * @param parent ?
     */
    @Override
    protected void customizeDialog(Composite parent) {
        m_shell = this.getShell();
        parent.getShell().setText(Messages.getString("dialog.infra.module.command"));
        /**
         * 
         * ?????
         */
        GridLayout baseLayout = new GridLayout(1, true);
        baseLayout.marginWidth = 10;
        baseLayout.marginHeight = 10;
        baseLayout.numColumns = DIALOG_WIDTH;
        //?
        parent.setLayout(baseLayout);

        GridData gridData = null;

        Composite commandModuleComposite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(1, true);
        layout.marginWidth = 5;
        layout.marginHeight = 5;
        layout.numColumns = DIALOG_WIDTH;
        commandModuleComposite.setLayout(layout);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        commandModuleComposite.setLayoutData(gridData);

        /*
         * ID
         */
        Label labelModuleId = new Label(commandModuleComposite, SWT.LEFT);
        gridData = new GridData();
        gridData.horizontalSpan = TITLE_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        labelModuleId.setText(Messages.getString("infra.module.id") + " : ");
        labelModuleId.setLayoutData(gridData);
        m_moduleId = new Text(commandModuleComposite, SWT.BORDER);
        gridData = new GridData();
        gridData.horizontalSpan = FORM_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_moduleId.setLayoutData(gridData);
        this.m_moduleId.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        /*
         * ??
         */
        Label labelModuleName = new Label(commandModuleComposite, SWT.LEFT);
        gridData = new GridData();
        gridData.horizontalSpan = TITLE_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        labelModuleName.setText(Messages.getString("infra.module.name") + " : ");
        labelModuleName.setLayoutData(gridData);
        this.m_moduleName = new Text(commandModuleComposite, SWT.BORDER);
        gridData = new GridData();
        gridData.horizontalSpan = FORM_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_moduleName.setLayoutData(gridData);
        this.m_moduleName.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        /*
         * 
         */
        Label labelSelectMethod = new Label(commandModuleComposite, SWT.LEFT);
        gridData = new GridData();
        gridData.horizontalSpan = TITLE_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        labelSelectMethod.setText(Messages.getString("infra.module.exec.method") + " : ");
        labelSelectMethod.setLayoutData(gridData);
        //SSH
        m_methodSSH = new Button(commandModuleComposite, SWT.RADIO);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalIndent = 15;
        m_methodSSH.setText(AccessMethodMessage.STRING_SSH);
        m_methodSSH.setLayoutData(gridData);
        //WinRM
        m_methodWinRM = new Button(commandModuleComposite, SWT.RADIO);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalIndent = 15;
        m_methodWinRM.setText(AccessMethodMessage.STRING_WINRM);
        m_methodWinRM.setLayoutData(gridData);

        // 
        m_label = new Label(commandModuleComposite, SWT.NONE);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_label.setLayoutData(gridData);

        /*
         * ??????????
         */
        m_precheckFlg = new Button(commandModuleComposite, SWT.CHECK | SWT.TOP);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_precheckFlg.setText(Messages.getString("infra.module.check.command.run.before"));
        m_precheckFlg.setLayoutData(gridData);
        //??????
        Label labelSkipExec = new Label(commandModuleComposite, SWT.LEFT | SWT.WRAP);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalIndent = 10;
        labelSkipExec.setText(Messages.getString("infra.module.check.command.run.before.supple"));
        labelSkipExec.setLayoutData(gridData);

        /*
         * ??????????????????
         */
        m_proceedIfFailFlg = new Button(commandModuleComposite, SWT.CHECK);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_proceedIfFailFlg.setText(Messages.getString("infra.module.inexec.after.exec.error"));
        m_proceedIfFailFlg.setLayoutData(gridData);

        // 
        m_label = new Label(commandModuleComposite, SWT.NONE);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_label.setLayoutData(gridData);

        /*
         * 
         */
        Label labelCommandExec = new Label(commandModuleComposite, SWT.LEFT);
        gridData = new GridData();
        gridData.horizontalSpan = TITLE_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        labelCommandExec.setText(Messages.getString("infra.module.exec.command") + " : ");
        labelCommandExec.setLayoutData(gridData);
        m_commandExec = new Text(commandModuleComposite, SWT.BORDER);
        gridData = new GridData();
        gridData.horizontalSpan = 10;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalIndent = 25;
        m_commandExec.setLayoutData(gridData);
        m_commandExec.setToolTipText(Messages.getString("infra.command.tooltip"));
        m_commandExec.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                update();
            }
        });

        /*
         * ?
         */
        Label labelCommandCheck = new Label(commandModuleComposite, SWT.LEFT);
        gridData = new GridData();
        gridData.horizontalSpan = TITLE_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        labelCommandCheck.setText(Messages.getString("infra.module.check.command") + " : ");
        labelCommandCheck.setLayoutData(gridData);
        m_commandCheck = new Text(commandModuleComposite, SWT.BORDER);
        gridData = new GridData();
        gridData.horizontalSpan = 10;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalIndent = 25;
        m_commandCheck.setLayoutData(gridData);
        m_commandCheck.setToolTipText(Messages.getString("infra.command.tooltip"));
        m_commandCheck.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                update();
            }
        });

        // 
        m_label = new Label(commandModuleComposite, SWT.NONE);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_label.setLayoutData(gridData);

        /*
         * ??
         */
        Label line = new Label(commandModuleComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalSpan = DIALOG_WIDTH;
        line.setLayoutData(gridData);

        /*
         * 
         */
        m_validFlg = new Button(commandModuleComposite, SWT.CHECK);
        gridData = new GridData();
        gridData.horizontalSpan = DIALOG_WIDTH;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_validFlg.setText(Messages.getString("setting.valid.confirmed"));
        m_validFlg.setLayoutData(gridData);

        // 
        this.adjustDialog();

        //??
        setInputData();
        update();

    }

    /**
     * ???
     *
     */
    private void adjustDialog() {
        // ?
        // ????????????????????
        m_shell.pack();
        m_shell.setSize(new Point(500, m_shell.getSize().y));

        // ???
        Display display = m_shell.getDisplay();
        m_shell.setLocation((display.getBounds().width - m_shell.getSize().x) / 2,
                (display.getBounds().height - m_shell.getSize().y) / 2);
    }

    /**
     * ?
     *
     */
    public void update() {
        /*
         *  
         */
        //ID
        if ("".equals(this.m_moduleId.getText())) {
            this.m_moduleId.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.m_moduleId.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
        //??
        if ("".equals(this.m_moduleName.getText())) {
            this.m_moduleName.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.m_moduleName.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
        //
        if ("".equals(this.m_commandExec.getText())) {
            this.m_commandExec.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.m_commandExec.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
        //?
        if ("".equals(this.m_commandCheck.getText())) {
            this.m_commandCheck.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            this.m_commandCheck.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
    }

    /**
     * ??????
     *
     * @see com.clustercontrol.infra.bean.InfraManagementInfo
     */
    private void setInputData() {
        InfraManagementInfo info = null;
        CommandModuleInfo module = null;
        try {
            InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(this.managerName);
            info = wrapper.getInfraManagement(this.managementId);
        } catch (InfraManagementNotFound_Exception | HinemosUnknown_Exception | InvalidRole_Exception
                | InvalidUserPass_Exception | NotifyNotFound_Exception e) {
            m_log.error(managementId + " InfraManagerInfo is null");
        }

        if (mode == PropertyDefineConstant.MODE_ADD | mode == PropertyDefineConstant.MODE_COPY) {
            // ?????
            moduleInfo = new CommandModuleInfo();
            List<InfraModuleInfo> modules = info.getModuleList();
            modules.add(moduleInfo);
        } else if (mode == PropertyDefineConstant.MODE_MODIFY) {
            // ???
            for (InfraModuleInfo tmpModule : info.getModuleList()) {
                if (tmpModule.getModuleId().equals(moduleId)) {
                    moduleInfo = (CommandModuleInfo) tmpModule;
                }
            }
        }

        // ?????
        if (moduleId != null) {
            for (InfraModuleInfo tmpModule : info.getModuleList()) {
                if (tmpModule.getModuleId().equals(moduleId)) {
                    index = info.getModuleList().indexOf(tmpModule);
                    break;
                }
            }
            module = (CommandModuleInfo) info.getModuleList().get(index);
            //   ID
            m_moduleId.setText(module.getModuleId());
            if (mode == PropertyDefineConstant.MODE_MODIFY) {
                m_moduleId.setEnabled(false);
            }
            //   ??
            m_moduleName.setText(module.getName());
            //   
            if (module.getAccessMethodType() == AccessMethodConstant.TYPE_WINRM) {
                m_methodWinRM.setSelection(true);
            } else {
                m_methodSSH.setSelection(true);
            }

            //   ???
            m_precheckFlg.setSelection(module.isPrecheckFlg());
            //   ???????
            m_proceedIfFailFlg.setSelection(module.isStopIfFailFlg());
            //   ???
            m_commandCheck.setText(module.getCheckCommand());
            //   ??
            m_commandExec.setText(module.getExecCommand());
            //   ?
            m_validFlg.setSelection(module.isValidFlg());

        } else {
            // ???(default)
            m_validFlg.setSelection(true);
            m_methodSSH.setSelection(true);
        }
        //   ????
        infraInfo = info;
    }

    /**
     * ??????
     *
     * @return ?
     *
     * @see com.clustercontrol.infra.bean.ExecutableModuleInfo
     *
     */
    private void createInputData() {

        //ID?
        moduleInfo.setModuleId(m_moduleId.getText());

        //???
        moduleInfo.setName(m_moduleName.getText());

        //??
        if (m_methodSSH.getSelection()) {
            moduleInfo.setAccessMethodType(AccessMethodConstant.TYPE_SSH);
        } else {
            moduleInfo.setAccessMethodType(AccessMethodConstant.TYPE_WINRM);
        }

        //??????????
        moduleInfo.setPrecheckFlg(m_precheckFlg.getSelection());

        //??????????????
        moduleInfo.setStopIfFailFlg(m_proceedIfFailFlg.getSelection());

        //?
        moduleInfo.setExecCommand(m_commandExec.getText());

        //??
        moduleInfo.setCheckCommand(m_commandCheck.getText());

        //?
        moduleInfo.setValidFlg(m_validFlg.getSelection());
    }

    /**
     * ?
     *
     * @return ?
     */
    @Override
    protected String getOkButtonText() {
        return Messages.getString("register");
    }

    /**
     * ?
     *
     * @return ?
     */
    @Override
    protected String getCancelButtonText() {
        return Messages.getString("cancel");
    }

    @Override
    protected ValidateResult validate() {
        if ("".equals((m_moduleId.getText()).trim())) {
            return createValidateResult(Messages.getString("message.hinemos.1"), Messages.getString(
                    "message.infra.specify.item", new Object[] { Messages.getString("infra.module.id") }));
        }
        if ("".equals((m_moduleName.getText()).trim())) {
            return createValidateResult(Messages.getString("message.hinemos.1"), Messages.getString(
                    "message.infra.specify.item", new Object[] { Messages.getString("infra.module.name") }));
        }
        if ("".equals((this.m_commandExec.getText()).trim())) {
            return createValidateResult(Messages.getString("message.hinemos.1"),
                    Messages.getString("message.infra.specify.item",
                            new Object[] { Messages.getString("infra.module.exec.command") }));
        }
        if ("".equals((this.m_commandCheck.getText()).trim())) {
            return createValidateResult(Messages.getString("message.hinemos.1"),
                    Messages.getString("message.infra.specify.item",
                            new Object[] { Messages.getString("infra.module.check.command") }));
        }
        return super.validate();
    }

    /**
     * ValidateResult????
     * @id messageBox??
     * @message messageBox???
     */
    protected ValidateResult createValidateResult(String id, String message) {
        ValidateResult validateResult = new ValidateResult();
        validateResult.setValid(false);
        validateResult.setID(id);
        validateResult.setMessage(message);

        return validateResult;
    }

    @Override
    protected boolean action() {
        boolean result = false;
        createInputData();
        String action = null;
        if (infraInfo != null) {
            if (mode == PropertyDefineConstant.MODE_ADD | mode == PropertyDefineConstant.MODE_COPY) {
                // ???
                action = Messages.getString("add");
            } else if (mode == PropertyDefineConstant.MODE_MODIFY) {
                // ??
                action = Messages.getString("modify");
            }

            try {
                InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(this.managerName);
                wrapper.modifyInfraManagement(infraInfo);
                action += "(" + this.managerName + ")";
                result = true;
                MessageDialog.openInformation(null, Messages.getString("successful"),
                        Messages.getString("message.infra.action.result",
                                new Object[] { Messages.getString("infra.module"), action,
                                        Messages.getString("successful"), m_moduleId.getText() }));
            } catch (InfraManagementDuplicate_Exception e) {
                // ID?
                MessageDialog.openInformation(null, Messages.getString("message"), Messages
                        .getString("message.infra.module.duplicate", new String[] { m_moduleId.getText() }));
            } catch (InvalidRole_Exception e) {
                // ???
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
            } catch (InfraManagementNotFound_Exception | NotifyDuplicate_Exception | NotifyNotFound_Exception
                    | HinemosUnknown_Exception | InvalidUserPass_Exception | InvalidSetting_Exception e) {
                m_log.info(
                        "action() modifyInfraManagement : " + e.getMessage() + " (" + e.getClass().getName() + ")");
                MessageDialog.openError(null, Messages.getString("failed"),
                        Messages.getString("message.infra.action.result",
                                new Object[] { Messages.getString("infra.module"), action,
                                        Messages.getString("failed"),
                                        m_moduleId.getText() + "\n" + HinemosMessage.replace(e.getMessage()) }));
            } catch (Exception e) {
                m_log.info(
                        "action() modifyInfraManagement : " + e.getMessage() + " (" + e.getClass().getName() + ")");
                MessageDialog.openError(null, Messages.getString("failed"),
                        Messages.getString("message.infra.action.result",
                                new Object[] { Messages.getString("infra.module"), action,
                                        Messages.getString("failed"),
                                        m_moduleId.getText() + "\n" + HinemosMessage.replace(e.getMessage()) }));
            }
        } else {
            m_log.error("inputData InfraManagerInfo is null");
            MessageDialog.openError(null, Messages.getString("failed"),
                    Messages.getString("message.infra.action.result",
                            new Object[] { Messages.getString("infra.module"), action, Messages.getString("failed"),
                                    m_moduleId.getText() }));
        }
        return result;
    }
}