From 4a6c8a4e3f7b0d01053716432ab91ab2150b35e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kv=C3=ADdera?= Date: Sun, 24 Jan 2021 07:20:35 +0100 Subject: [PATCH] [FISH-759] sort instances in admin console (#5074) * FISH-759 Sort instance list by name * FISH-759 Sort also configurations * FISH-759 Got rid of wildcard import * FISH-759 Updated JDK 1.4 generics to JDK 8 style in ClusterHandler * FISH-759 Updated JDK 1.5 generics to JDK 8 style in ListInstancesCommand * FISH-759 Updated JDK 1.4 generics to JDK 8 style in ListInstancesCommand * FISH-759 Removed unused import * FISH-759 Removed unused variable * FISH-759 Fixed spacing around = * FISH-759 IntelliJ automatic code clean up * FISH-759 Fixed whitespaces --- .../common/handlers/ClusterHandler.java | 472 +++++++++--------- .../admin/cluster/ListInstancesCommand.java | 15 +- 2 files changed, 242 insertions(+), 245 deletions(-) diff --git a/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/handlers/ClusterHandler.java b/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/handlers/ClusterHandler.java index fd57856e0f4..a43cefb8b45 100644 --- a/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/handlers/ClusterHandler.java +++ b/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/handlers/ClusterHandler.java @@ -40,7 +40,6 @@ // Portions Copyright [2017-2020] [Payara Foundation and/or its affiliates] /** - * * @author anilam */ package org.glassfish.admingui.common.handlers; @@ -57,7 +56,11 @@ import org.glassfish.api.admin.InstanceState; import java.net.URLEncoder; -import java.util.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.logging.Level; public class ClusterHandler { @@ -90,47 +93,42 @@ public ClusterHandler() { @HandlerOutput(name = "disableEjb", type = Boolean.class) }) public static void getClusterStatusSummary(HandlerContext handlerCtx) { - Map statusMap = (Map) handlerCtx.getInputValue("statusMap"); - int running=0; - int notRunning=0; - int requireRestart=0; - int unknown = 0; - try{ - for (Iterator it=statusMap.values().iterator(); it.hasNext(); ) { - Object value = it.next(); - if (value.toString().equals(InstanceState.StateType.RUNNING.getDescription())){ + Map statusMap = (Map) handlerCtx.getInputValue("statusMap"); + int running = 0; + int notRunning = 0; + int requireRestart = 0; + try { + for (String value : statusMap.values()) { + if (value.equals(InstanceState.StateType.RUNNING.getDescription())) { running++; - }else - if (value.toString().equals(InstanceState.StateType.NOT_RUNNING.getDescription())){ + } else if (value.equals(InstanceState.StateType.NOT_RUNNING.getDescription())) { notRunning++; - }else - if (value.toString().equals(InstanceState.StateType.RESTART_REQUIRED.getDescription())){ + } else if (value.equals(InstanceState.StateType.RESTART_REQUIRED.getDescription())) { requireRestart++; - }else { - unknown++; + } else { GuiUtil.getLogger().severe("Unknown Status"); } } - handlerCtx.setOutputValue("disableEjb", (notRunning > 0) ? false :true); //refer to bug#6342445 - handlerCtx.setOutputValue("disableStart", (notRunning > 0) ? false :true); - handlerCtx.setOutputValue("disableStop", ( (running+requireRestart) > 0) ? false :true); - handlerCtx.setOutputValue( "numRunning" , (running > 0) ? - GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.number.instance.running", new String[]{""+running, GuiUtil.getCommonMessage("status.image.RUNNING")} ) : ""); + handlerCtx.setOutputValue("disableEjb", notRunning <= 0); //refer to bug#6342445 + handlerCtx.setOutputValue("disableStart", notRunning <= 0); + handlerCtx.setOutputValue("disableStop", (running + requireRestart) <= 0); + handlerCtx.setOutputValue("numRunning", (running > 0) ? + GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.number.instance.running", new String[]{"" + running, GuiUtil.getCommonMessage("status.image.RUNNING")}) : ""); - handlerCtx.setOutputValue( "numNotRunning" , (notRunning > 0) ? - GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.number.instance.notRunning", new String[]{""+notRunning , GuiUtil.getCommonMessage("status.image.NOT_RUNNING")}) : ""); + handlerCtx.setOutputValue("numNotRunning", (notRunning > 0) ? + GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.number.instance.notRunning", new String[]{"" + notRunning, GuiUtil.getCommonMessage("status.image.NOT_RUNNING")}) : ""); - handlerCtx.setOutputValue( "numRequireRestart" , (requireRestart > 0) ? - GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.number.instance.requireRestart", new String[]{""+requireRestart, GuiUtil.getCommonMessage("status.image.REQUIRES_RESTART")}) : ""); - }catch(Exception ex){ + handlerCtx.setOutputValue("numRequireRestart", (requireRestart > 0) ? + GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.number.instance.requireRestart", new String[]{"" + requireRestart, GuiUtil.getCommonMessage("status.image.REQUIRES_RESTART")}) : ""); + } catch (Exception ex) { handlerCtx.setOutputValue("numRunning", GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "cluster.status.unknown")); GuiUtil.getLogger().info(GuiUtil.getCommonMessage("log.error.getClusterStatusSummary") + ex.getLocalizedMessage()); - if (GuiUtil.getLogger().isLoggable(Level.FINE)){ + if (GuiUtil.getLogger().isLoggable(Level.FINE)) { ex.printStackTrace(); } - } - } + } + } @Handler(id = "gf.isDGName", input = { @@ -140,11 +138,11 @@ public static void getClusterStatusSummary(HandlerContext handlerCtx) { @HandlerOutput(name = "exists", type = Boolean.class) }) public static void isDepoymentGroupName(HandlerContext handlerCtx) { - if( ! TargetUtil.isDeploymentGroup((String) handlerCtx.getInputValue("dgName"))){ + if (!TargetUtil.isDeploymentGroup((String) handlerCtx.getInputValue("dgName"))) { GuiUtil.handleError(handlerCtx, GuiUtil.getMessage("msg.NoSuchDG")); - handlerCtx.setOutputValue("exists", false); - }else{ - handlerCtx.setOutputValue("exists", true); + handlerCtx.setOutputValue("exists", false); + } else { + handlerCtx.setOutputValue("exists", true); } } @@ -156,27 +154,27 @@ public static void isDepoymentGroupName(HandlerContext handlerCtx) { @HandlerOutput(name = "exists", type = Boolean.class) }) public static void isClusterName(HandlerContext handlerCtx) { - if( ! TargetUtil.isCluster((String) handlerCtx.getInputValue("clusterName"))){ + if (!TargetUtil.isCluster((String) handlerCtx.getInputValue("clusterName"))) { GuiUtil.handleError(handlerCtx, GuiUtil.getMessage("msg.NoSuchCluster")); - handlerCtx.setOutputValue("exists", false); - }else{ - handlerCtx.setOutputValue("exists", true); + handlerCtx.setOutputValue("exists", false); + } else { + handlerCtx.setOutputValue("exists", true); } } @Handler(id = "gf.isInstanceName", input = { - @HandlerInput(name = "instanceName", type = String.class, required = true) + @HandlerInput(name = "instanceName", type = String.class, required = true) }, output = { - @HandlerOutput(name = "exists", type = Boolean.class) + @HandlerOutput(name = "exists", type = Boolean.class) }) public static void isInstanceName(HandlerContext handlerCtx) { - if( ! TargetUtil.isInstance((String) handlerCtx.getInputValue("instanceName"))){ + if (!TargetUtil.isInstance((String) handlerCtx.getInputValue("instanceName"))) { GuiUtil.handleError(handlerCtx, GuiUtil.getMessage("msg.NoSuchInstance")); - handlerCtx.setOutputValue("exists", false); - }else{ - handlerCtx.setOutputValue("exists", true); + handlerCtx.setOutputValue("exists", false); + } else { + handlerCtx.setOutputValue("exists", true); } } @@ -185,16 +183,16 @@ public static void isInstanceName(HandlerContext handlerCtx) { @HandlerInput(name = "configName", type = String.class, required = true) }, output = { - @HandlerOutput(name = "exists", type = Boolean.class) + @HandlerOutput(name = "exists", type = Boolean.class) }) public static void isConfigName(HandlerContext handlerCtx) { String configName = (String) handlerCtx.getInputValue("configName"); - List config = TargetUtil.getConfigs(); - if(!config.contains(configName)){ + List config = TargetUtil.getConfigs(); + if (!config.contains(configName)) { GuiUtil.handleError(handlerCtx, GuiUtil.getMessage("msg.NoSuchConfig")); - handlerCtx.setOutputValue("exists", false); - }else{ - handlerCtx.setOutputValue("exists", true); + handlerCtx.setOutputValue("exists", false); + } else { + handlerCtx.setOutputValue("exists", true); } } @@ -202,50 +200,50 @@ public static void isConfigName(HandlerContext handlerCtx) { input = { @HandlerInput(name = "rows", type = List.class, required = true)}) public static void saveInstanceWeight(HandlerContext handlerCtx) { - List rows = (List) handlerCtx.getInputValue("rows"); - List errorInstances = new ArrayList(); - Map response = null; + List rows = (List) handlerCtx.getInputValue("rows"); + List errorInstances = new ArrayList<>(); + Map response; String prefix = GuiUtil.getSessionValue("REST_URL") + "/servers/server/"; - for (Map oneRow : rows) { + for (Map oneRow : rows) { String instanceName = (String) oneRow.get("encodedName"); - Map attrsMap = new HashMap(); + Map attrsMap = new HashMap<>(); attrsMap.put("lbWeight", oneRow.get("lbWeight")); - try{ - response = RestUtil.restRequest( prefix+instanceName , attrsMap, "post" , null, false); - }catch (Exception ex){ + try { + response = RestUtil.restRequest(prefix + instanceName, attrsMap, "post", null, false); + } catch (Exception ex) { GuiUtil.getLogger().severe( - GuiUtil.getCommonMessage("LOG_SAVE_INSTANCE_WEIGHT_ERROR" , new Object[]{prefix+instanceName, attrsMap})); + GuiUtil.getCommonMessage("LOG_SAVE_INSTANCE_WEIGHT_ERROR", new Object[]{prefix + instanceName, attrsMap})); response = null; } - if (response ==null){ + if (response == null) { errorInstances.add(instanceName); } } - if (errorInstances.size() > 0){ - String details = GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "instance.error.updateWeight" , new String[]{""+errorInstances}); + if (errorInstances.size() > 0) { + String details = GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "instance.error.updateWeight", new String[]{"" + errorInstances}); GuiUtil.handleError(handlerCtx, details); } - } + } - @Handler(id="gf.removeFromDG", - input= { - @HandlerInput(name = "selectedRows", type = List.class, required = true), - @HandlerInput(name = "dgName", type = String.class, required = true) } ) + @Handler(id = "gf.removeFromDG", + input = { + @HandlerInput(name = "selectedRows", type = List.class, required = true), + @HandlerInput(name = "dgName", type = String.class, required = true)}) public static void removeFromDG(HandlerContext ctx) { String deploymentGroup = (String) ctx.getInputValue("dgName"); - List rows = (List) ctx.getInputValue("selectedRows"); + List> rows = (List>) ctx.getInputValue("selectedRows"); String endPoint = GuiUtil.getSessionValue("REST_URL") + "/deployment-groups/remove-instance-from-deployment-group"; - for (Map oneRow : rows) { + for (Map oneRow : rows) { String instanceName = (String) oneRow.get("name"); - Map attrs = new HashMap<>(2); + Map attrs = new HashMap<>(2); attrs.put("deploymentGroup", deploymentGroup); attrs.put("instance", instanceName); GuiUtil.getLogger().info(endPoint); try { - RestUtil.restRequest(endPoint, attrs, "post", null, false); - }catch (Exception ex){ + RestUtil.restRequest(endPoint, attrs, "post", null, false); + } catch (Exception ex) { GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), ex.getMessage()); return; } @@ -253,57 +251,58 @@ public static void removeFromDG(HandlerContext ctx) { } - @Handler(id = "gf.dgAction", + @Handler(id = "gf.dgAction", input = { @HandlerInput(name = "rows", type = List.class, required = true), @HandlerInput(name = "action", type = String.class, required = true), - @HandlerInput(name = "extraInfo", type = Object.class) }) + @HandlerInput(name = "extraInfo", type = Object.class)}) public static void deploymentGroupAction(HandlerContext handlerCtx) { String action = (String) handlerCtx.getInputValue("action"); - List rows = (List) handlerCtx.getInputValue("rows"); - String errorMsg = null; + List> rows = (List>) handlerCtx.getInputValue("rows"); + String errorMsg = null; String prefix = GuiUtil.getSessionValue("REST_URL") + "/deployment-groups/deployment-group/"; - for (Map oneRow : rows) { + for (Map oneRow : rows) { String dgName = (String) oneRow.get("name"); String endpoint = prefix + dgName + "/" + action; String method = "post"; - if (action.equals("delete-deployment-group")){ + if (action.equals("delete-deployment-group")) { endpoint = prefix + dgName; method = "delete"; } - try{ + try { GuiUtil.getLogger().info(endpoint); - RestUtil.restRequest( endpoint, null, method, null, false); - }catch (Exception ex){ + RestUtil.restRequest(endpoint, null, method, null, false); + } catch (Exception ex) { GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), ex.getMessage()); return; } } - } + } @Handler(id = "gf.clusterAction", input = { @HandlerInput(name = "rows", type = List.class, required = true), @HandlerInput(name = "action", type = String.class, required = true), - @HandlerInput(name = "extraInfo", type = Object.class) }) + @HandlerInput(name = "extraInfo", type = Object.class)}) public static void clusterAction(HandlerContext handlerCtx) { String action = (String) handlerCtx.getInputValue("action"); - List rows = (List) handlerCtx.getInputValue("rows"); - String errorMsg = null; + List> rows = (List>) handlerCtx.getInputValue("rows"); + String errorMsg = null; String prefix = GuiUtil.getSessionValue("REST_URL") + "/clusters/cluster/"; - for (Map oneRow : rows) { + for (Map oneRow : rows) { String clusterName = (String) oneRow.get("name"); String endpoint = prefix + clusterName + "/" + action; String method = "post"; - if (action.equals("delete-cluster")){ + if (action.equals("delete-cluster")) { //need to delete the clustered instance first - Map clusterInstanceMap = (Map)handlerCtx.getInputValue("extraInfo"); - List instanceNameList = (List) clusterInstanceMap.get(clusterName); - for(String instanceName : instanceNameList){ + Map clusterInstanceMap = + (Map) handlerCtx.getInputValue("extraInfo"); + List instanceNameList = (List) clusterInstanceMap.get(clusterName); + for (String instanceName : instanceNameList) { errorMsg = deleteInstance(instanceName); - if (errorMsg != null){ + if (errorMsg != null) { GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), errorMsg); return; } @@ -311,15 +310,15 @@ public static void clusterAction(HandlerContext handlerCtx) { endpoint = prefix + clusterName; method = "delete"; } - try{ + try { GuiUtil.getLogger().info(endpoint); - RestUtil.restRequest( endpoint, null, method, null, false); - }catch (Exception ex){ + RestUtil.restRequest(endpoint, null, method, null, false); + } catch (Exception ex) { GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), ex.getMessage()); return; } } - } + } @Handler(id = "gf.instanceAction", @@ -328,24 +327,24 @@ public static void clusterAction(HandlerContext handlerCtx) { @HandlerInput(name = "action", type = String.class, required = true)}) public static void instanceAction(HandlerContext handlerCtx) { String action = (String) handlerCtx.getInputValue("action"); - List rows = (List) handlerCtx.getInputValue("rows"); + List> rows = (List>) handlerCtx.getInputValue("rows"); String prefix = GuiUtil.getSessionValue("REST_URL") + "/servers/server/"; - for (Map oneRow : rows) { + for (Map oneRow : rows) { String instanceName = (String) oneRow.get("name"); - if(action.equals("delete-instance")){ + if (action.equals("delete-instance")) { String errorMsg = deleteInstance(instanceName); - if (errorMsg != null){ + if (errorMsg != null) { GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), errorMsg); return; } - }else{ + } else { try { String endpoint = prefix + instanceName + "/" + action; GuiUtil.getLogger().info(endpoint); - RestUtil.restRequest(endpoint , null, "post" ,null, false); - } catch (Exception ex){ - String endpoint=prefix + instanceName + "/" + action; + RestUtil.restRequest(endpoint, null, "post", null, false); + } catch (Exception ex) { + String endpoint = prefix + instanceName + "/" + action; GuiUtil.getLogger().severe( GuiUtil.getCommonMessage("LOG_ERROR_INSTANCE_ACTION", new Object[]{endpoint, "null"})); GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), ex.getMessage()); @@ -353,14 +352,14 @@ public static void instanceAction(HandlerContext handlerCtx) { } } } - if(action.equals("stop-instance")){ + if (action.equals("stop-instance")) { try { Thread.sleep(5000); } catch (InterruptedException e) { //noop. } } - } + } @Handler(id = "gf.nodeAction", @@ -370,74 +369,71 @@ public static void instanceAction(HandlerContext handlerCtx) { @HandlerInput(name = "nodeInstanceMap", type = Map.class)}) public static void nodeAction(HandlerContext handlerCtx) { String action = (String) handlerCtx.getInputValue("action"); - Map nodeInstanceMap = (Map) handlerCtx.getInputValue("nodeInstanceMap"); - if (nodeInstanceMap == null){ - nodeInstanceMap=new HashMap(); + Map nodeInstanceMap = (Map) handlerCtx.getInputValue("nodeInstanceMap"); + if (nodeInstanceMap == null) { + nodeInstanceMap = new HashMap(); } - List rows = (List) handlerCtx.getInputValue("rows"); + List> rows = (List>) handlerCtx.getInputValue("rows"); String prefix = GuiUtil.getSessionValue("REST_URL") + "/nodes/"; - for (Map oneRow : rows) { + for (Map oneRow : rows) { String nodeName = (String) oneRow.get("name"); final String localhostNodeName = (String) GuiUtil.getSessionValue("localhostNodeName"); - if (nodeName.equals(localhostNodeName)){ - GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), - GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "node.error.removeLocalhost" , new String[]{localhostNodeName})); + if (nodeName.equals(localhostNodeName)) { + GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), + GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "node.error.removeLocalhost", new String[]{localhostNodeName})); return; } - List instancesList = (List)nodeInstanceMap.get(nodeName); - if ( instancesList!= null && (instancesList.size()) != 0){ - GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), - GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "nodes.instanceExistError", new String[]{ nodeName, nodeInstanceMap.get(nodeName).toString()})); + List instancesList = (List) nodeInstanceMap.get(nodeName); + if (instancesList != null && (instancesList.size()) != 0) { + GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), + GuiUtil.getMessage(CLUSTER_RESOURCE_NAME, "nodes.instanceExistError", new String[]{nodeName, nodeInstanceMap.get(nodeName).toString()})); return; } - if(action.equals("delete-node")){ - try{ + if (action.equals("delete-node")) { + try { String endpoint = prefix + "node/" + nodeName; GuiUtil.getLogger().info(endpoint); - RestUtil.restRequest(endpoint, null, "DELETE",null, false); - }catch (Exception ex){ + RestUtil.restRequest(endpoint, null, "DELETE", null, false); + } catch (Exception ex) { GuiUtil.getLogger().severe( - GuiUtil.getCommonMessage("LOG_NODE_ACTION_ERROR", new Object[]{prefix + nodeName, "DELETE" , "null"})); + GuiUtil.getCommonMessage("LOG_NODE_ACTION_ERROR", new Object[]{prefix + nodeName, "DELETE", "null"})); GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), ex.getMessage()); return; } continue; } - Map payload = null; + Map payload = null; String type = (String) oneRow.get("type"); String endpoint = ""; - if(action.equals("delete-node-uninstall")){ - try{ - if ("CONFIG".equals(type)){ + if (action.equals("delete-node-uninstall")) { + try { + if ("CONFIG".equals(type)) { endpoint = prefix + "delete-node-config"; - payload = new HashMap(); + payload = new HashMap<>(); payload.put("id", nodeName); - }else - if ("SSH".equals(type)){ - endpoint = prefix + "delete-node-ssh"; - payload = new HashMap(); + } else if ("SSH".equals(type)) { + endpoint = prefix + "delete-node-ssh"; + payload = new HashMap<>(); payload.put("id", nodeName); payload.put("uninstall", "true"); - }else - if ("DCOM".equals(type)){ - endpoint = prefix + "delete-node-dcom"; - payload = new HashMap(); + } else if ("DCOM".equals(type)) { + endpoint = prefix + "delete-node-dcom"; + payload = new HashMap<>(); payload.put("id", nodeName); payload.put("uninstall", "true"); - } + } GuiUtil.getLogger().info(endpoint); - RestUtil.restRequest(endpoint, payload, "DELETE",null, false); - }catch (Exception ex){ + RestUtil.restRequest(endpoint, payload, "DELETE", null, false); + } catch (Exception ex) { GuiUtil.getLogger().severe( - GuiUtil.getCommonMessage("LOG_NODE_ACTION_ERROR", new Object[]{endpoint,"" , payload})); + GuiUtil.getCommonMessage("LOG_NODE_ACTION_ERROR", new Object[]{endpoint, "", payload})); GuiUtil.prepareAlert("error", GuiUtil.getMessage("msg.Error"), ex.getMessage()); return; - } + } } } - } - + } @Handler(id = "gf.createClusterInstances", @@ -446,29 +442,29 @@ public static void nodeAction(HandlerContext handlerCtx) { @HandlerInput(name = "instanceRow", type = List.class, required = true)}) public static void createClusterInstances(HandlerContext handlerCtx) { String clusterName = (String) handlerCtx.getInputValue("clusterName"); - List instanceRow = (List) handlerCtx.getInputValue("instanceRow"); - Map attrsMap = new HashMap(); + List> instanceRow = (List>) handlerCtx.getInputValue("instanceRow"); + Map attrsMap = new HashMap<>(); String endpoint = GuiUtil.getSessionValue("REST_URL") + "/create-instance"; - for (Map oneInstance : instanceRow) { + for (Map oneInstance : instanceRow) { attrsMap.put("name", oneInstance.get("name")); attrsMap.put("cluster", clusterName); attrsMap.put("node", oneInstance.get("node")); - try{ + try { GuiUtil.getLogger().info(endpoint); GuiUtil.getLogger().info(attrsMap.toString()); - RestUtil.restRequest( endpoint , attrsMap, "post" ,null, false); + RestUtil.restRequest(endpoint, attrsMap, "post", null, false); //set lb weight String wt = (String) oneInstance.get("weight"); - if ( !GuiUtil.isEmpty(wt)) { - String encodedInstanceName = URLEncoder.encode((String)oneInstance.get("name"), "UTF-8"); - String ep = GuiUtil.getSessionValue("REST_URL") + "/servers/server/" + encodedInstanceName ; - Map wMap = new HashMap(); + if (!GuiUtil.isEmpty(wt)) { + String encodedInstanceName = URLEncoder.encode((String) oneInstance.get("name"), "UTF-8"); + String ep = GuiUtil.getSessionValue("REST_URL") + "/servers/server/" + encodedInstanceName; + Map wMap = new HashMap<>(); wMap.put("lbWeight", wt); - RestUtil.restRequest( ep , wMap, "post" , null, false); + RestUtil.restRequest(ep, wMap, "post", null, false); } - }catch (Exception ex){ + } catch (Exception ex) { GuiUtil.getLogger().severe( - GuiUtil.getCommonMessage("LOG_CREATE_CLUSTER_INSTANCE" , new Object[]{clusterName,endpoint, attrsMap})); + GuiUtil.getCommonMessage("LOG_CREATE_CLUSTER_INSTANCE", new Object[]{clusterName, endpoint, attrsMap})); GuiUtil.prepareException(handlerCtx, ex); } } @@ -491,78 +487,79 @@ public static void createClusterInstances(HandlerContext handlerCtx) { @HandlerOutput(name = "result", type = List.class) }) public static void getDeploymentTargets(HandlerContext handlerCtx) { - List result = new ArrayList(); + List result = new ArrayList<>(); result.add("server"); - try{ - List clusterList = (List) handlerCtx.getInputValue("clusterList"); - if (clusterList != null){ - for(String oneCluster : clusterList){ + try { + List clusterList = (List) handlerCtx.getInputValue("clusterList"); + if (clusterList != null) { + for (String oneCluster : clusterList) { result.add(oneCluster); } } - List instances = (List) handlerCtx.getInputValue("listInstanceProps"); + List> instances = (List>) handlerCtx.getInputValue("listInstanceProps"); if (instances != null) { - for (Map instance : instances) { - result.add((String)instance.get("name")); + for (Map instance : instances) { + result.add((String) instance.get("name")); //result.addAll(props.keySet()); } } - }catch(Exception ex){ - GuiUtil.getLogger().severe(ex.getLocalizedMessage());//"getDeploymentTargets failed."); - //print stacktrace ?? - } + } catch (Exception ex) { + GuiUtil.getLogger().severe(ex.getLocalizedMessage());//"getDeploymentTargets failed."); + //print stacktrace ?? + } handlerCtx.setOutputValue("result", result); - } + } // If successfully deleted the instance, null will be returned, otherwise, return the error string to be displayed to user. - private static String deleteInstance(String instanceName){ - try{ + private static String deleteInstance(String instanceName) { + try { String endpoint = GuiUtil.getSessionValue("REST_URL") + "/servers/server/" + instanceName + "/delete-instance"; GuiUtil.getLogger().info(endpoint); - RestUtil.restRequest( endpoint, null,"post", null, false ); + RestUtil.restRequest(endpoint, null, "post", null, false); return null; - }catch(Exception ex){ + } catch (Exception ex) { String endpoint = GuiUtil.getSessionValue("REST_URL") + "/servers/server/" + instanceName + "/delete-instance\n"; GuiUtil.getLogger().severe( - GuiUtil.getCommonMessage("LOG_DELETE_INSTANCE", new Object[]{endpoint, "null"})); + GuiUtil.getCommonMessage("LOG_DELETE_INSTANCE", new Object[]{endpoint, "null"})); return ex.getMessage(); } } @Handler(id = "gf.listDeploymentGroups", - output = { - @HandlerOutput(name = "deploymentgroups", type = List.class) - }) + output = { + @HandlerOutput(name = "deploymentgroups", type = List.class) + }) public static void listDeploymentGroups(HandlerContext handlerCtx) { - List deploymentGroups = TargetUtil.getDeploymentGroups(); + List deploymentGroups = TargetUtil.getDeploymentGroups(); handlerCtx.setOutputValue("deploymentgroups", deploymentGroups); } @Handler(id = "gf.listClusters", - output = { - @HandlerOutput(name = "clusters", type = List.class) - }) + output = { + @HandlerOutput(name = "clusters", type = List.class) + }) public static void listClusters(HandlerContext handlerCtx) { - List clusters = TargetUtil.getClusters(); + List clusters = TargetUtil.getClusters(); handlerCtx.setOutputValue("clusters", clusters); } @Handler(id = "gf.listConfigs", - output = { - @HandlerOutput(name = "configs", type = List.class) - }) + output = { + @HandlerOutput(name = "configs", type = List.class) + }) public static void listConfigs(HandlerContext handlerCtx) { - List configs = TargetUtil.getConfigs(); + List configs = TargetUtil.getConfigs(); + configs.sort(Comparator.naturalOrder()); handlerCtx.setOutputValue("configs", configs); } @Handler(id = "gf.listInstances", input = { - @HandlerInput(name="optionKeys", type=List.class, required=true), - @HandlerInput(name="optionValues", type=List.class, required=true) + @HandlerInput(name = "optionKeys", type = List.class, required = true), + @HandlerInput(name = "optionValues", type = List.class, required = true) }, output = { @HandlerOutput(name = "instances", type = List.class), @@ -572,34 +569,35 @@ public static void listConfigs(HandlerContext handlerCtx) { }) public static void listInstances(HandlerContext handlerCtx) { - List instances = new ArrayList(); - Map statusMap = new HashMap(); - Map uptimeMap = new HashMap(); + List instances = new ArrayList<>(); + Map statusMap = new HashMap<>(); + Map uptimeMap = new HashMap<>(); List keys = (List) handlerCtx.getInputValue("optionKeys"); - List values = (List) handlerCtx.getInputValue("optionValues"); + List values = (List) handlerCtx.getInputValue("optionValues"); Map attrs = new HashMap<>(); if (keys != null && values != null) { for (int i = 0; i < keys.size(); i++) { attrs.put(keys.get(i), values.get(i)); } } - String endpoint = GuiUtil.getSessionValue("REST_URL")+"/list-instances"; - try{ - Map responseMap = RestUtil.restRequest( endpoint , attrs, "GET" , handlerCtx, false); - Map extraPropertiesMap = (Map)((Map)responseMap.get("data")).get("extraProperties"); - if (extraPropertiesMap != null){ - List instanceList = (List)extraPropertiesMap.get("instanceList"); - if (instanceList != null){ - for(Map oneInstance : instanceList){ + String endpoint = GuiUtil.getSessionValue("REST_URL") + "/list-instances"; + try { + Map responseMap = RestUtil.restRequest(endpoint, attrs, "GET", handlerCtx, false); + Map extraPropertiesMap = + (Map) ((Map) responseMap.get("data")).get("extraProperties"); + if (extraPropertiesMap != null) { + List> instanceList = (List>) extraPropertiesMap.get("instanceList"); + if (instanceList != null) { + for (Map oneInstance : instanceList) { instances.add(oneInstance.get("name")); statusMap.put(oneInstance.get("name"), oneInstance.get("status")); uptimeMap.put(oneInstance.get("name"), oneInstance.get("uptime")); } } } - }catch (Exception ex){ + } catch (Exception ex) { GuiUtil.getLogger().severe( - GuiUtil.getCommonMessage("LOG_LIST_INSTANCES", new Object[]{endpoint, attrs})); + GuiUtil.getCommonMessage("LOG_LIST_INSTANCES", new Object[]{endpoint, attrs})); //we don't need to call GuiUtil.handleError() because thats taken care of in restRequest() when we pass in the handler. } handlerCtx.setOutputValue("instances", instances); @@ -610,25 +608,25 @@ public static void listInstances(HandlerContext handlerCtx) { @Handler(id = "gf.getClusterNameForInstance", input = { - @HandlerInput(name="instanceName", type=String.class, required=true)}, + @HandlerInput(name = "instanceName", type = String.class, required = true)}, output = { @HandlerOutput(name = "clusterName", type = String.class)}) public static void getClusterNameForInstance(HandlerContext handlerCtx) { String instanceName = (String) handlerCtx.getInputValue("instanceName"); - try{ - List clusterList = new ArrayList(RestUtil.getChildMap(GuiUtil.getSessionValue("REST_URL")+"/clusters/cluster").keySet()); - for(String oneCluster : clusterList){ - List serverRefs = new ArrayList (RestUtil.getChildMap(GuiUtil.getSessionValue("REST_URL")+ "/clusters/cluster/" + - URLEncoder.encode(oneCluster, "UTF-8") + "/server-ref").keySet()); - if (serverRefs.contains(instanceName)){ + try { + List clusterList = new ArrayList<>(RestUtil.getChildMap(GuiUtil.getSessionValue("REST_URL") + "/clusters/cluster").keySet()); + for (String oneCluster : clusterList) { + List serverRefs = new ArrayList<>(RestUtil.getChildMap(GuiUtil.getSessionValue("REST_URL") + "/clusters/cluster/" + + URLEncoder.encode(oneCluster, "UTF-8") + "/server-ref").keySet()); + if (serverRefs.contains(instanceName)) { handlerCtx.setOutputValue("clusterName", oneCluster); return; } } - }catch(Exception ex){ + } catch (Exception ex) { GuiUtil.getLogger().info(GuiUtil.getCommonMessage("LOG_GET_CLUSTERNAME_FOR_INSTANCE")); - if (GuiUtil.getLogger().isLoggable(Level.FINE)){ + if (GuiUtil.getLogger().isLoggable(Level.FINE)) { ex.printStackTrace(); } } @@ -637,17 +635,17 @@ public static void getClusterNameForInstance(HandlerContext handlerCtx) { @Handler(id = "gf.convertNodePswd", input = { - @HandlerInput(name = "pswd", type=String.class, required=true)}, + @HandlerInput(name = "pswd", type = String.class, required = true)}, output = { @HandlerOutput(name = "pswdText", type = String.class), @HandlerOutput(name = "pswdAlias", type = String.class)}) public static void convertNodePswd(HandlerContext handlerCtx) { String pswd = (String) handlerCtx.getInputValue("pswd"); - if (GuiUtil.isEmpty(pswd)){ + if (GuiUtil.isEmpty(pswd)) { return; } - if (pswd.startsWith("${ALIAS=") && pswd.endsWith("}")){ - String pswdAlias = pswd.substring(8, pswd.length()-1); + if (pswd.startsWith("${ALIAS=") && pswd.endsWith("}")) { + String pswdAlias = pswd.substring(8, pswd.length() - 1); handlerCtx.setOutputValue("pswdAlias", pswdAlias); return; } @@ -682,7 +680,7 @@ public static void presetNodeAuthSelectBox(HandlerContext handlerCtx) { private static boolean setIfSet(final HandlerContext handlerCtx, final String inputKey, final String outputKey, - final String outputValue) { + final String outputValue) { final Object value = handlerCtx.getInputValue(inputKey); if (value == null || value.toString().isEmpty()) { return false; @@ -692,19 +690,19 @@ private static boolean setIfSet(final HandlerContext handlerCtx, final String in } -//gf.convertToAlias(in="#{pageSession.pswdAlias}" out="#{requestScope.tmpv}"); + //gf.convertToAlias(in="#{pageSession.pswdAlias}" out="#{requestScope.tmpv}"); @Handler(id = "gf.convertToAlias", input = { - @HandlerInput(name="in", type=String.class, required=true)}, + @HandlerInput(name = "in", type = String.class, required = true)}, output = { @HandlerOutput(name = "out", type = String.class)}) public static void convertToAlias(HandlerContext handlerCtx) { String in = (String) handlerCtx.getInputValue("in"); String out = null; - if (! GuiUtil.isEmpty(in)){ - out = "${ALIAS="+in+"}"; + if (!GuiUtil.isEmpty(in)) { + out = "${ALIAS=" + in + "}"; } - handlerCtx.setOutputValue("out", out); + handlerCtx.setOutputValue("out", out); } @Handler(id = "gf.changeClusterStatus", @@ -714,37 +712,37 @@ public static void convertToAlias(HandlerContext handlerCtx) { @HandlerInput(name = "Enabled", type = String.class, required = true), @HandlerInput(name = "forLB", type = Boolean.class, required = true)}) public static void changeClusterStatus(HandlerContext handlerCtx) { - String Enabled = (String) handlerCtx.getInputValue("Enabled"); + String Enabled = (String) handlerCtx.getInputValue("Enabled"); String clusterName = (String) handlerCtx.getInputValue("clusterName"); - List selectedRows = (List) handlerCtx.getInputValue("selectedRows"); + List> selectedRows = (List>) handlerCtx.getInputValue("selectedRows"); boolean forLB = (Boolean) handlerCtx.getInputValue("forLB"); - for(Map oneRow : selectedRows){ - Map attrs = new HashMap(); + for (Map oneRow : selectedRows) { + Map attrs = new HashMap<>(); String name = (String) oneRow.get("name"); String endpoint = GuiUtil.getSessionValue("REST_URL") + "/clusters/cluster/" + clusterName + "/server-ref/" + name; - if(forLB){ + if (forLB) { attrs.put("lbEnabled", Enabled); RestUtil.restRequest(endpoint, attrs, "post", handlerCtx, false); - }else{ + } else { attrs.put("enabled", Enabled); RestUtil.restRequest(endpoint, attrs, "post", handlerCtx, false); } } - } - - @Handler(id="gf.getClusterForConfig", - input={ - @HandlerInput(name="configName", type=String.class, required=true) - }, - output={ - @HandlerOutput(name="cluster", type=String.class) - }) + } + + @Handler(id = "gf.getClusterForConfig", + input = { + @HandlerInput(name = "configName", type = String.class, required = true) + }, + output = { + @HandlerOutput(name = "cluster", type = String.class) + }) public static void getClusterForConfig(HandlerContext handlerCtx) { - String configName = (String)handlerCtx.getInputValue("configName"); + String configName = (String) handlerCtx.getInputValue("configName"); String clusterName = null; Domain domain = GuiUtil.getHabitat().getService(Domain.class); - for (Cluster cluster: domain.getClusters().getCluster()) { + for (Cluster cluster : domain.getClusters().getCluster()) { if (cluster.getConfigRef().equals(configName)) { clusterName = cluster.getName(); break; @@ -753,8 +751,6 @@ public static void getClusterForConfig(HandlerContext handlerCtx) { if (clusterName != null) { handlerCtx.setOutputValue("cluster", clusterName); - } else { - // } } } diff --git a/nucleus/cluster/admin/src/main/java/com/sun/enterprise/v3/admin/cluster/ListInstancesCommand.java b/nucleus/cluster/admin/src/main/java/com/sun/enterprise/v3/admin/cluster/ListInstancesCommand.java index a73c0a1a8c9..ca31b004bad 100644 --- a/nucleus/cluster/admin/src/main/java/com/sun/enterprise/v3/admin/cluster/ListInstancesCommand.java +++ b/nucleus/cluster/admin/src/main/java/com/sun/enterprise/v3/admin/cluster/ListInstancesCommand.java @@ -120,7 +120,7 @@ public class ListInstancesCommand implements AdminCommand { @Param(optional = true, primary = true, defaultValue = "domain") String whichTarget = ""; - private List infos = new LinkedList(); + private List infos = new LinkedList<>(); private List serverList; private ActionReport report; private ActionReport.MessagePart top = null; @@ -179,7 +179,7 @@ private void noStatus(List serverList) { StringBuilder sb = new StringBuilder(); boolean firstServer = true; Properties extraProps = new Properties(); - List instanceList = new ArrayList(); + List> instanceList = new ArrayList<>(); for (Server server : serverList) { boolean clustered = server.getCluster() != null; @@ -200,7 +200,7 @@ private void noStatus(List serverList) { sb.append(name); top.addProperty(name, ""); - HashMap insDetails = new HashMap(); + Map insDetails = new HashMap<>(); insDetails.put("name", name); instanceList.add(insDetails); } @@ -263,8 +263,9 @@ private void yesStatus(List serverList, int timeoutInMsec, Logger logger } Properties extraProps = new Properties(); - List instanceList = new ArrayList(); + List> instanceList = new ArrayList<>(); + infos.sort(Comparator.comparing(InstanceInfo::getName)); for (InstanceInfo ii : infos) { String name = ii.getName(); String value = (ii.isRunning()) ? InstanceState.StateType.RUNNING.getDescription() @@ -279,7 +280,7 @@ private void yesStatus(List serverList, int timeoutInMsec, Logger logger } } - HashMap insDetails = new HashMap(); + Map insDetails = new HashMap<>(); insDetails.put("name", name); insDetails.put("status", value); insDetails.put("deploymentgroup", ii.getDeploymentGroups().trim()); @@ -317,7 +318,7 @@ private List createServerList() { return getServersForNodeOrConfig(); } else if (rc.isServer()) { - List l = new LinkedList(); + List l = new LinkedList<>(); l.add((Server) rc); return l; } @@ -381,7 +382,7 @@ private List getServersForConfig() { } List rcs = domain.getReferenceContainersOf(config); - List servers = new LinkedList(); + List servers = new LinkedList<>(); for (ReferenceContainer rc : rcs) { if (rc.isServer()) {