Java tutorial
/* * Copyright 2014 by SCSK Corporation. * * This file is part of PrimeCloud Controller(TM). * * PrimeCloud Controller(TM) 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, either version 2 of the License, or * (at your option) any later version. * * PrimeCloud Controller(TM) 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. * * You should have received a copy of the GNU General Public License * along with PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>. */ package jp.primecloud.auto.ui; import java.util.Arrays; import jp.primecloud.auto.common.constant.PCCConstant; import jp.primecloud.auto.common.status.InstanceStatus; import jp.primecloud.auto.common.status.ZabbixInstanceStatus; import jp.primecloud.auto.config.Config; import jp.primecloud.auto.exception.AutoApplicationException; import jp.primecloud.auto.service.InstanceService; import jp.primecloud.auto.service.ProcessService; import jp.primecloud.auto.service.dto.InstanceDto; import jp.primecloud.auto.service.dto.PlatformDto; import jp.primecloud.auto.ui.DialogConfirm.Buttons; import jp.primecloud.auto.ui.DialogConfirm.Result; import jp.primecloud.auto.ui.util.BeanContext; import jp.primecloud.auto.ui.util.Icons; import jp.primecloud.auto.ui.util.OperationLogger; import jp.primecloud.auto.ui.util.ViewContext; import jp.primecloud.auto.ui.util.ViewMessages; import jp.primecloud.auto.ui.util.ViewProperties; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.CheckBox; import com.vaadin.ui.CssLayout; import com.vaadin.ui.Label; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; import com.vaadin.ui.Window.CloseEvent; @SuppressWarnings("serial") public class ServerButtonsBottom extends CssLayout { private MainView sender; private Button addButton; private Button editButton; private Button deleteButton; private Button startButton; private Button stopButton; private Button startMonitoringButton; private Button stopMonitoringButton; public ServerButtonsBottom(final MainView sender) { this.sender = sender; } @Override public void attach() { setWidth("100%"); setMargin(true); addStyleName("server-buttons"); // Add addButton = new Button(ViewProperties.getCaption("button.addServer")); addButton.setDescription(ViewProperties.getCaption("description.addServer")); addButton.setIcon(Icons.ADD.resource()); addButton.addStyleName("left"); addButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { addButtonClick(event); } }); addComponent(addButton); // Label spacer = new Label(" ", Label.CONTENT_XHTML); spacer.setWidth("30px"); spacer.addStyleName("left"); addComponent(spacer); // Edit editButton = new Button(ViewProperties.getCaption("button.editServer")); editButton.setDescription(ViewProperties.getCaption("description.editServer")); editButton.setWidth("90px"); editButton.setIcon(Icons.EDITMINI.resource()); editButton.addStyleName("left"); editButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { editButtonClick(event); } }); addComponent(editButton); // Delete deleteButton = new Button(ViewProperties.getCaption("button.deleteServer")); deleteButton.setDescription(ViewProperties.getCaption("description.deleteServer")); deleteButton.setWidth("90px"); deleteButton.setIcon(Icons.DELETEMINI.resource()); deleteButton.addStyleName("left"); deleteButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { deleteButtonClick(event); } }); addComponent(deleteButton); // Stop stopButton = new Button(ViewProperties.getCaption("button.stopServer")); stopButton.setDescription(ViewProperties.getCaption("description.stopServer")); stopButton.setWidth("90px"); stopButton.setIcon(Icons.STOPMINI.resource()); stopButton.addStyleName("right"); stopButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { stopButtonClick(event); } }); addComponent(stopButton); // Start startButton = new Button(ViewProperties.getCaption("button.startServer")); startButton.setDescription(ViewProperties.getCaption("description.startServer")); startButton.setWidth("90px"); startButton.setIcon(Icons.PLAYMINI.resource()); startButton.addStyleName("right"); startButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { startButtonClick(event); } }); addComponent(startButton); // ?????? boolean useZabbix = BooleanUtils.toBoolean(Config.getProperty("zabbix.useZabbix")); boolean changeMonitoring = BooleanUtils.toBoolean(Config.getProperty("zabbix.changeMonitoring")); if (useZabbix && changeMonitoring) { // Stop Monitoring stopMonitoringButton = new Button(ViewProperties.getCaption("button.stopMonitoring")); stopMonitoringButton.setDescription(ViewProperties.getCaption("description.stopMonitoring")); stopMonitoringButton.setWidth("150px"); stopMonitoringButton.setIcon(Icons.STOP_MONITORING.resource()); stopMonitoringButton.addStyleName("right"); stopMonitoringButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { stopMonitoringButtonClick(event); } }); addComponent(stopMonitoringButton); // Start Monitoring startMonitoringButton = new Button(ViewProperties.getCaption("button.startMonitoring")); startMonitoringButton.setDescription(ViewProperties.getCaption("description.startMonitoring")); startMonitoringButton.setWidth("150px"); startMonitoringButton.setIcon(Icons.START_MONITORING.resource()); startMonitoringButton.addStyleName("right"); startMonitoringButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { startMonitoringButtonClick(event); } }); addComponent(startMonitoringButton); } initialize(); } public void initialize() { addButton.setEnabled(true); editButton.setEnabled(false); deleteButton.setEnabled(false); startButton.setEnabled(false); stopButton.setEnabled(false); if (startMonitoringButton != null) { startMonitoringButton.setEnabled(false); } if (stopMonitoringButton != null) { stopMonitoringButton.setEnabled(false); } // ReloadStart? startButton.setCaption(ViewProperties.getCaption("button.startServer")); startButton.setDescription(ViewProperties.getCaption("description.startServer")); } public void refresh(InstanceDto instance) { // ????? InstanceStatus status = InstanceStatus.fromStatus(instance.getInstance().getStatus()); // Edit if (status == InstanceStatus.STOPPED || status == InstanceStatus.RUNNING) { editButton.setEnabled(true); } else { editButton.setEnabled(false); } // Delete if (status == InstanceStatus.STOPPED) { deleteButton.setEnabled(true); } else { deleteButton.setEnabled(false); } // Start if (status == InstanceStatus.STOPPED) { startButton.setEnabled(true); } else { startButton.setEnabled(false); } // Stop if (status == InstanceStatus.RUNNING || status == InstanceStatus.WARNING) { stopButton.setEnabled(true); } else { stopButton.setEnabled(false); } // Start Monitoring if (startMonitoringButton != null) { if (status == InstanceStatus.RUNNING) { boolean enabled = false; if (instance.getZabbixInstance() != null) { ZabbixInstanceStatus status2 = ZabbixInstanceStatus .fromStatus(instance.getZabbixInstance().getStatus()); if (status2 == ZabbixInstanceStatus.UN_MONITORING) { enabled = true; } } startMonitoringButton.setEnabled(enabled); } else { startMonitoringButton.setEnabled(false); } } // Stop Monitoring if (stopMonitoringButton != null) { if (status == InstanceStatus.RUNNING) { boolean enabled = false; if (instance.getZabbixInstance() != null) { ZabbixInstanceStatus status2 = ZabbixInstanceStatus .fromStatus(instance.getZabbixInstance().getStatus()); if (status2 == ZabbixInstanceStatus.MONITORING) { enabled = true; } } stopMonitoringButton.setEnabled(enabled); } else { stopMonitoringButton.setEnabled(false); } } } private void refreshTable() { // ???????? Long selectedInstanceNo = null; if (sender.serverPanel.serverTable.getValue() != null) { InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); selectedInstanceNo = instance.getInstance().getInstanceNo(); } int index = sender.serverPanel.serverTable.getCurrentPageFirstItemIndex(); // sender.refreshTable(); // ????????? if (selectedInstanceNo != null) { for (Object itemId : sender.serverPanel.serverTable.getItemIds()) { InstanceDto instance = (InstanceDto) itemId; if (selectedInstanceNo.equals(instance.getInstance().getInstanceNo())) { sender.serverPanel.serverTable.select(itemId); sender.serverPanel.serverTable.setCurrentPageFirstItemIndex(index); break; } } } } private void addButtonClick(ClickEvent event) { WinServerAdd winServerAdd = new WinServerAdd(); winServerAdd.addListener(new Window.CloseListener() { @Override public void windowClose(CloseEvent e) { refreshTable(); } }); getWindow().addWindow(winServerAdd); } private void editButtonClick(Button.ClickEvent event) { InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); WinServerEdit winServerEdit = new WinServerEdit(instance.getInstance().getInstanceNo()); winServerEdit.addListener(new Window.CloseListener() { @Override public void windowClose(CloseEvent e) { refreshTable(); } }); getWindow().addWindow(winServerEdit); } private void deleteButtonClick(Button.ClickEvent event) { final InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); // ? String message = ViewMessages.getMessage("IUI-000015", instance.getInstance().getInstanceName()); DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), message, Buttons.OKCancel); dialog.setCallback(new DialogConfirm.Callback() { @Override public void onDialogResult(Result result) { if (result != Result.OK) { return; } delete(instance.getInstance().getInstanceNo()); } }); getApplication().getMainWindow().addWindow(dialog); } private void delete(Long instanceNo) { // OperationLogger.writeInstance("SERVER", "Delete Server", instanceNo, null); // ? InstanceService instanceService = BeanContext.getBean(InstanceService.class); instanceService.deleteInstance(instanceNo); // ??? sender.serverPanel.serverTable.select(null); // refreshTable(); } private void startButtonClick(Button.ClickEvent event) { final InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); PlatformDto platform = instance.getPlatform(); // AWS?? if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatform().getPlatformType())) { // ???????? if (BooleanUtils.isTrue(platform.getPlatformAws().getVpc()) && StringUtils.isEmpty(instance.getAwsInstance().getSubnetId())) { throw new AutoApplicationException("IUI-000111"); } } // Azure?? else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatform().getPlatformType())) { // ???????? if (StringUtils.isEmpty(instance.getAzureInstance().getSubnetId())) { throw new AutoApplicationException("IUI-000111"); } // ? ProcessService processService = BeanContext.getBean(ProcessService.class); boolean startupErrFlg = processService.checkStartup(platform.getPlatform().getPlatformType(), instance.getAzureInstance().getInstanceName(), instance.getAzureInstance().getInstanceNo()); if (startupErrFlg == true) { // ???????????? // ?No??? throw new AutoApplicationException("IUI-000133"); } } // ? VerticalLayout optionLayout = null; final CheckBox checkBox; String enableService = Config.getProperty("ui.enableService"); if (enableService == null || BooleanUtils.toBoolean(enableService)) { optionLayout = new VerticalLayout(); checkBox = new CheckBox(ViewMessages.getMessage("IUI-000035"), false); checkBox.setImmediate(true); optionLayout.addComponent(checkBox); optionLayout.setComponentAlignment(checkBox, Alignment.MIDDLE_CENTER); } else { checkBox = null; } // ? String actionName = event.getButton().getDescription(); String message = ViewMessages.getMessage("IUI-000013", new Object[] { instance.getInstance().getInstanceName(), actionName }); DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), message, Buttons.OKCancel, optionLayout); dialog.setCallback(new DialogConfirm.Callback() { @Override public void onDialogResult(Result result) { if (result != Result.OK) { return; } boolean startService = (checkBox == null) ? false : (Boolean) checkBox.getValue(); start(instance.getInstance().getInstanceNo(), startService); } }); getApplication().getMainWindow().addWindow(dialog); } private void start(Long instanceNo, boolean startService) { // OperationLogger.writeInstance("SERVER", "Start Server", instanceNo, String.valueOf(startService)); // ? ProcessService processService = BeanContext.getBean(ProcessService.class); processService.startInstances(ViewContext.getFarmNo(), Arrays.asList(instanceNo), startService); // refreshTable(); } private void stopButtonClick(Button.ClickEvent event) { final InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); // ? String message = ViewMessages.getMessage("IUI-000014", instance.getInstance().getInstanceName()); DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), message, Buttons.OKCancel); dialog.setCallback(new DialogConfirm.Callback() { @Override public void onDialogResult(Result result) { if (result != Result.OK) { return; } stop(instance.getInstance().getInstanceNo()); } }); getApplication().getMainWindow().addWindow(dialog); } private void stop(Long instanceNo) { // OperationLogger.writeInstance("SERVER", "Stop Server", instanceNo, null); // ?? ProcessService processService = BeanContext.getBean(ProcessService.class); processService.stopInstances(ViewContext.getFarmNo(), Arrays.asList(instanceNo)); // refreshTable(); } private void startMonitoringButtonClick(Button.ClickEvent event) { final InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); // ? String message = ViewMessages.getMessage("IUI-000121", instance.getInstance().getInstanceName()); DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), message, Buttons.OKCancel); dialog.setCallback(new DialogConfirm.Callback() { @Override public void onDialogResult(Result result) { if (result != Result.OK) { return; } startMonitoring(instance); } }); getApplication().getMainWindow().addWindow(dialog); } private void startMonitoring(InstanceDto instance) { // OperationLogger.writeInstance("SERVER", "Start Monitoring Server", instance.getInstance().getInstanceNo(), instance.getInstance().getInstanceName()); // InstanceService instanceService = BeanContext.getBean(InstanceService.class); instanceService.enableZabbixMonitoring(instance.getInstance().getInstanceNo()); // refreshTable(); } private void stopMonitoringButtonClick(Button.ClickEvent event) { final InstanceDto instance = (InstanceDto) sender.serverPanel.serverTable.getValue(); // ? String message = ViewMessages.getMessage("IUI-000122", instance.getInstance().getInstanceName()); DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), message, Buttons.OKCancel); dialog.setCallback(new DialogConfirm.Callback() { @Override public void onDialogResult(Result result) { if (result != Result.OK) { return; } stopMonitoring(instance); } }); getApplication().getMainWindow().addWindow(dialog); } private void stopMonitoring(InstanceDto instance) { // OperationLogger.writeInstance("SERVER", "Stop Monitoring Server", instance.getInstance().getInstanceNo(), instance.getInstance().getInstanceName()); // InstanceService instanceService = BeanContext.getBean(InstanceService.class); instanceService.disableZabbixMonitoring(instance.getInstance().getInstanceNo()); // refreshTable(); } }