diff options
author | Shireesh Anjal <shireesh@gluster.com> | 2011-08-04 17:31:49 +0530 |
---|---|---|
committer | Shireesh Anjal <shireesh@gluster.com> | 2011-08-04 17:33:08 +0530 |
commit | 5cc54f2bb36fc4455d52eb24315c0e00e7eea448 (patch) | |
tree | 3f672a27f0f1f04b76365977bc2a68961db1d828 | |
parent | baf1e9dd11693a28a351e17816fc31954f6fab01 (diff) |
Removed redundant class GenericResponse
8 files changed, 55 insertions, 175 deletions
diff --git a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/response/GenericResponse.java b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/response/GenericResponse.java deleted file mode 100644 index 6c8e87b5..00000000 --- a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/response/GenericResponse.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * GenericServerResponse.java - * - * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> - * This file is part of Gluster Management Console. - * - * Gluster Management Console is free software; you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * Gluster Management Console 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 Affero General Public License - * for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - */ -package com.gluster.storage.management.core.response; - -import javax.xml.bind.annotation.XmlRootElement; - -import com.gluster.storage.management.core.model.Status; - -@XmlRootElement(name = "response") -public class GenericResponse<T> extends AbstractResponse { - private T data; - private Status status; - - @Override - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - - public GenericResponse(Status status, T data) { - setStatus(status); - this.data = data; - } - - public GenericResponse() { - } - - @Override - public T getData() { - return data; - } - - public void setData(T data) { - this.data = data; - } - -} diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java index bfc5106d..bdc9ce58 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java @@ -140,18 +140,18 @@ public class VolumeOptionsDefaults { GlusterServer onlineServer = clusterService.getOnlineServer(clusterName); try { - options = runDefaultVolumeOptions(onlineServer.getName(), command); + options = getVolumeOptionsInfo(onlineServer.getName(), command); return options.getOptions(); } catch (ConnectionException e) { onlineServer = clusterService.getNewOnlineServer(clusterName); - options = runDefaultVolumeOptions(onlineServer.getName(), command); + options = getVolumeOptionsInfo(onlineServer.getName(), command); return options.getOptions(); } catch (Exception e) { throw new GlusterRuntimeException("Fetching volume options default failed! [" + e.getMessage() + "]"); } } - private VolumeOptionInfoListResponse runDefaultVolumeOptions(String serverName, String command) { + private VolumeOptionInfoListResponse getVolumeOptionsInfo(String serverName, String command) { return (VolumeOptionInfoListResponse) serverUtil.executeOnServer(true, serverName, command, VolumeOptionInfoListResponse.class); } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java index 5762c89f..82644c4d 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java @@ -678,12 +678,13 @@ public class VolumesResource extends AbstractResource { String serverName = brickInfo[0]; String brickDirectory = brickInfo[1]; - // String mountPoint = brickDirectory.substring(0, - // brickDirectory.lastIndexOf("/")); - Object output = serverUtil.executeScriptOnServer(true, serverName, VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + try { + String output = serverUtil.executeScriptOnServer(true, serverName, VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + brickDirectory + " " + (deleteFlag ? "-d" : ""), String.class); - if (output instanceof Status) { - errors += "[" + brickDirectory + "] => " + output + CoreConstants.NEWLINE; + } catch(Exception e) { + logger.error("Error while cleaning brick [" + serverName + ":" + brickDirectory + "] of volume [" + + volumeName + "] : " + e.getMessage(), e); + errors += "[" + brickDirectory + "] => " + e.getMessage() + CoreConstants.NEWLINE; } } if(!errors.trim().isEmpty()) { @@ -696,13 +697,9 @@ public class VolumesResource extends AbstractResource { String brickDirectory = brick.getBrickDirectory(); // String mountPoint = brickDirectory.substring(0, brickDirectory.lastIndexOf("/")); - Object output = serverUtil.executeScriptOnServer(true, brick.getServerName(), + serverUtil.executeScriptOnServer(true, brick.getServerName(), VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + brickDirectory + " " + (deleteFlag ? "-d" : ""), String.class); - if (output instanceof Status) { - throw new GlusterRuntimeException("Error in post-delete operation of volume [" + volumeName + "]: " - + output); - } } } @@ -825,22 +822,15 @@ public class VolumesResource extends AbstractResource { String logFilePath = logDir + CoreConstants.FILE_SEPARATOR + logFileName; // Usage: get_volume_disk_log.py <volumeName> <diskName> <lineCount> - Object responseObj = serverUtil.executeScriptOnServer(true, brick.getServerName(), VOLUME_BRICK_LOG_SCRIPT + LogMessageListResponse response = serverUtil.executeScriptOnServer(true, brick.getServerName(), VOLUME_BRICK_LOG_SCRIPT + " " + logFilePath + " " + lineCount, LogMessageListResponse.class); - LogMessageListResponse response = null; - if (responseObj instanceof LogMessageListResponse) { - response = (LogMessageListResponse) responseObj; - // populate disk and trim other fields - List<VolumeLogMessage> logMessages = response.getLogMessages(); - for (VolumeLogMessage logMessage : logMessages) { - logMessage.setBrickDirectory(brick.getBrickDirectory()); - } - return logMessages; - } else { - Status status = (Status) responseObj; - throw new GlusterRuntimeException(status.toString()); + // populate disk and trim other fields + List<VolumeLogMessage> logMessages = response.getLogMessages(); + for (VolumeLogMessage logMessage : logMessages) { + logMessage.setBrickDirectory(brick.getBrickDirectory()); } + return logMessages; } @GET diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java index ba1cba21..e15ed62e 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java @@ -120,13 +120,9 @@ public class InitializeDiskTask extends Task { private void startInitializeDisk(String serverName) { String fsTypeCommand = (getFsType().equals(GlusterConstants.FSTYPE_DEFAULT)) ? "" : " -t " + getFsType(); - Object output = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_SCRIPT + String output = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_SCRIPT + fsTypeCommand + " " + getDiskName(), String.class); - if(output instanceof Status) { - // Status object will be returned only in case of failure - throw new GlusterRuntimeException(((Status)output).toString()); - } - TaskStatus taskStatus = new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, (String)output)); + TaskStatus taskStatus = new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output)); taskStatus.setPercentageSupported((getFsType().equals(GlusterConstants.FSTYPE_XFS)) ? false : true); getTaskInfo().setStatus(taskStatus); } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java index 134359fc..1be71c0d 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java @@ -75,12 +75,8 @@ public class RebalanceVolumeTask extends Task { private void startRebalance(String serverName) { String command = "gluster volume rebalance " + getTaskInfo().getReference() + " " + getLayout() + " start"; - Object output = serverUtil.executeOnServer(true, serverName, command, String.class); - if(output instanceof Status) { - // Status object will be returned only in case of failure - throw new GlusterRuntimeException(((Status)output).toString()); - } - getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, (String)output))); + String output = serverUtil.executeOnServer(true, serverName, command, String.class); + getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output))); } @Override diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java index 13a58729..4953785c 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java @@ -155,14 +155,8 @@ public abstract class AbstractStatsFactory implements StatsFactory { argsStr += " " + arg; } } - Object output = serverUtil.executeScriptOnServer(true, serverName, getStatsScriptName() + argsStr + " " + return serverUtil.executeScriptOnServer(true, serverName, getStatsScriptName() + argsStr + " " + period, ServerStats.class); - //String cpuUsageData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <xport> <meta> <start>1310468100</start> <step>300</step> <end>1310471700</end> <rows>13</rows> <columns>3</columns> <legend> <entry>user</entry> <entry>system</entry> <entry>total</entry> </legend> </meta> <data> <row><t>1310468100</t><v>2.23802952e-1</v><v>4.3747778209e-01</v><v>6.6128073384e-01</v></row> <row><t>1310468400</t><v>2.3387347338e-01</v><v>4.4642717442e-01</v><v>6.8030064780e-01</v></row> <row><t>1310468700</t><v>5.5043873220e+00</v><v>6.2462376636e+00</v><v>1.1750624986e+01</v></row> <row><t>1310469000</t><v>2.4350593653e+01</v><v>2.6214585217e+01</v><v>5.0565178869e+01</v></row> <row><t>1310469300</t><v>4.0786489953e+01</v><v>4.6784713828e+01</v><v>8.7571203781e+01</v></row> <row><t>1310469600</t><v>4.1459955508e+01</v><v>5.2546309044e+01</v><v>9.4006264551e+01</v></row> <row><t>1310469900</t><v>4.2312286165e+01</v><v>5.2390588332e+01</v><v>9.4702874497e+01</v></row> <row><t>1310470200</t><v>4.2603794982e+01</v><v>5.1598861493e+01</v><v>9.4202656475e+01</v></row> <row><t>1310470500</t><v>3.8238751290e+01</v><v>4.5312089966e+01</v><v>8.3550841256e+01</v></row> <row><t>1310470800</t><v>1.7949961224e+01</v><v>2.1282058418e+01</v><v>3.9232019642e+01</v></row> <row><t>1310471100</t><v>1.2330371421e-01</v><v>4.6347832868e-01</v><v>5.8678204289e-01</v></row> <row><t>1310471400</t><v>1.6313260492e-01</v><v>5.4088119561e-01</v><v>7.0401380052e-01</v></row> <row><t>1310471700</t><v>NaN</v><v>NaN</v><v>NaN</v></row> </data> </xport>"; - //Object output = unmarshal(ServerStats.class, cpuUsageData, false); - if(output instanceof Status) { - throw new GlusterRuntimeException(((Status)output).toString()); - } - return (ServerStats) output; } public abstract String getStatsScriptName(); diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java index 62ea6839..598dae13 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java @@ -411,24 +411,15 @@ public class GlusterUtil { } private void addBrickToVolume(Volume volume, String serverName, String brickDir, BRICK_STATUS status) { - //If brick directory has standard path, find and assign device name otherwise null -// String stdBrickDirPattern = "^/export/.*/.*"; // e.g: /export/sdb/test - String deviceName = null; -// if (Pattern.matches(stdBrickDirPattern, brickDir) ) { -// deviceName = brickDir.split("/")[2].trim(); -// } volume.addBrick(new Brick(serverName, status, brickDir)); } // Do not throw exception, Gracefully handle as Offline brick. private BRICK_STATUS getBrickStatus(String serverName, String volumeName, String brick){ try { - Object output = serverUtil.executeScriptOnServer(true, serverName, BRICK_STATUS_SCRIPT + " " + volumeName + " " + brick, String.class); - if(output instanceof Status) { - // script failed. throw exception. - throw new GlusterRuntimeException(((Status)output).toString()); - } - if (((String)output).equals(CoreConstants.ONLINE)) { + String output = serverUtil.executeScriptOnServer(true, serverName, BRICK_STATUS_SCRIPT + " " + volumeName + + " " + brick, String.class); + if (output.equals(CoreConstants.ONLINE)) { return BRICK_STATUS.ONLINE; } else { return BRICK_STATUS.OFFLINE; @@ -638,18 +629,18 @@ public class GlusterUtil { } public TaskStatus getInitializingDeviceStatus(String serverName, String diskName) { - Object response = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_STATUS_SCRIPT + " " - + diskName, InitDiskStatusResponse.class); - + InitDiskStatusResponse initDiskStatusResponse; TaskStatus taskStatus = new TaskStatus(); - if (response instanceof Status) { + + try { + initDiskStatusResponse = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_STATUS_SCRIPT + " " + + diskName, InitDiskStatusResponse.class); + } catch(RuntimeException e) { taskStatus.setCode(Status.STATUS_CODE_FAILURE); - taskStatus.setMessage(((Status) response).getMessage()); - throw new GlusterRuntimeException(((Status) response).getMessage()); + taskStatus.setMessage(e.getMessage()); + throw e; } - InitDiskStatusResponse initDiskStatusResponse = (InitDiskStatusResponse) response; - if (initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.COMPLETED) { taskStatus.setCode(Status.STATUS_CODE_SUCCESS); } else if (initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.IN_PROGRESS) { diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java index 84514893..73292197 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java @@ -105,13 +105,9 @@ public class ServerUtil { return ((Server) response).getName(); } - private Object fetchServerDetails(String serverName) { + private Server fetchServerDetails(String serverName) { // fetch standard server details like cpu, disk, memory details - Object response = executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class); - if (response instanceof Status) { - throw new GlusterRuntimeException(((Status) response).getMessage()); - } - return response; + return executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class); } /** @@ -128,9 +124,10 @@ public class ServerUtil { * @return Object of the expected class from remote execution of the command. In case the remote execution fails * ungracefully, an object of class {@link Status} will be returned. */ - public Object executeScriptOnServer(boolean runInForeground, String serverName, String scriptWithArgs, - @SuppressWarnings("rawtypes") Class expectedClass) { - return executeOnServer(runInForeground, serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, expectedClass); + public <T> T executeScriptOnServer(boolean runInForeground, String serverName, String scriptWithArgs, + Class<T> expectedClass) { + return executeOnServer(runInForeground, serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, + expectedClass); } /** @@ -144,33 +141,15 @@ public class ServerUtil { * @return Object of the expected class from remote execution of the command. In case the remote execution fails * ungracefully, an object of class {@link Status} will be returned. */ - @SuppressWarnings("rawtypes") - public Object executeOnServer(boolean runInForeground, String serverName, String commandWithArgs, - Class expectedClass) { - try { - String output = executeOnServer(serverName, commandWithArgs); - if(expectedClass == String.class) { - return output; - } - - // In case the script execution exits ungracefully, the agent would return a GenericResponse. - // hence pass last argument as true to try GenericResponse unmarshalling in such cases. - Object response = unmarshal(expectedClass, output, expectedClass != GenericResponse.class); - if (expectedClass != GenericResponse.class && response instanceof GenericResponse) { - // expected class was not GenericResponse, but that's what we got. This means the - // script failed ungracefully. Extract and return the status object from the response - return ((GenericResponse) response).getStatus(); - } - return response; - } catch (RuntimeException e) { - // Except for connection exception, wrap any other exception in the a object and return it. - if (e instanceof ConnectionException) { - throw e; - } else { - // error during unmarshalling. return status with error from exception. - return new Status(e); - } + @SuppressWarnings("unchecked") + public <T> T executeOnServer(boolean runInForeground, String serverName, String commandWithArgs, + Class<T> expectedClass) { + String output = executeOnServer(serverName, commandWithArgs); + if (expectedClass == String.class) { + return (T) output; } + + return unmarshal(expectedClass, output); } private String executeOnServer(String serverName, String commandWithArgs) { @@ -231,27 +210,20 @@ public class ServerUtil { * Class whose object is expected * @param input * Input string - * @param tryGenericResponseOnFailure - * If true, and if the unmarshalling fails for given class, another unmarshalling will be attempted with - * class {@link GenericResponse}. If this also fails, a status object with exception message is created - * and returned. - * @return Object of given expected class, or a status object in case first unmarshalling fails. + * @return Object of given expected class */ - @SuppressWarnings("rawtypes") - public Object unmarshal(Class expectedClass, String input, boolean tryGenericResponseOnFailure) { + @SuppressWarnings("unchecked") + public <T> T unmarshal(Class<T> expectedClass, String input) { try { // create JAXB context and instantiate marshaller JAXBContext context = JAXBContext.newInstance(expectedClass); Unmarshaller um = context.createUnmarshaller(); - return um.unmarshal(new ByteArrayInputStream(input.getBytes())); + return (T)um.unmarshal(new ByteArrayInputStream(input.getBytes())); } catch (JAXBException e) { - if (tryGenericResponseOnFailure) { - // unmarshalling failed. try to unmarshal a GenericResponse object - return unmarshal(GenericResponse.class, input, false); - - } - return new Status(Status.STATUS_CODE_FAILURE, "Error during unmarshalling string [" + input - + "] for class [" + expectedClass.getName() + ": [" + e.getMessage() + "]"); + String errMsg = "Error during unmarshalling string [" + input + "] for class [" + expectedClass.getName() + + ": [" + e.getMessage() + "]"; + logger.error(errMsg, e); + throw new GlusterRuntimeException(errMsg, e); } } @@ -263,7 +235,7 @@ public class ServerUtil { * @return Status object containing the disk name, or error message in case the remote script fails. */ public Status getDiskForDir(String serverName, String brickDir) { - return (Status) executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class); + return executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class); } public static void main(String[] args) { @@ -282,7 +254,6 @@ public class ServerUtil { // System.out.println(row.getUsageData().get(2)); // } // } catch (JAXBException e) { -// // TODO Auto-generated catch block // e.printStackTrace(); // } } |