Fix Exception handling in addCartridgeGroup methos
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/90dc63ea Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/90dc63ea Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/90dc63ea Branch: refs/heads/master Commit: 90dc63ea0ff829b35bce8ea05e24e7927ed45d80 Parents: a6f6251 Author: Lahiru Sandaruwan <[email protected]> Authored: Fri Jun 19 17:25:26 2015 +0530 Committer: Lahiru Sandaruwan <[email protected]> Committed: Fri Jun 19 17:26:05 2015 +0530 ---------------------------------------------------------------------- .../rest/endpoint/api/StratosApiV41.java | 22 ++- .../rest/endpoint/api/StratosApiV41Utils.java | 145 +++++++++---------- .../exception/InvalidCartridgeException.java | 70 +++++++++ 3 files changed, 151 insertions(+), 86 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/90dc63ea/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java index 89af1a4..9fd893d 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java @@ -467,7 +467,7 @@ public class StratosApiV41 extends AbstractApi { public Response addCartridgeGroup( CartridgeGroupBean cartridgeGroupBean) throws RestAPIException { try { - StratosApiV41Utils.addServiceGroup(cartridgeGroupBean); + StratosApiV41Utils.addCartridgeGroup(cartridgeGroupBean); URI url = uriInfo.getAbsolutePathBuilder().path(cartridgeGroupBean.getName()).build(); return Response.created(url).entity(new ResponseMessageBean(ResponseMessageBean.SUCCESS, @@ -476,17 +476,15 @@ public class StratosApiV41 extends AbstractApi { } catch (InvalidCartridgeGroupDefinitionException e) { return Response.status(Response.Status.BAD_REQUEST).entity(new ResponseMessageBean( ResponseMessageBean.ERROR, e.getMessage())).build(); - } catch (RestAPIException e) { - if (e.getCause().getMessage().contains("already exists")) { - return Response.status(Response.Status.CONFLICT).entity(new ResponseMessageBean( - ResponseMessageBean.ERROR, "Cartridge group not found")).build(); - } else if (e.getCause().getMessage().contains("Invalid Service Group") || e.getCause().getMessage() - .contains("Required cartridges not found")) { - return Response.status(Response.Status.BAD_REQUEST).entity(new ResponseMessageBean( - ResponseMessageBean.ERROR, e.getCause().getMessage())).build(); - } else { - throw e; - } + } catch (ServiceGroupDefinitionException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(new ResponseMessageBean( + ResponseMessageBean.ERROR, e.getMessage())).build(); + } catch (AutoscalerServiceInvalidServiceGroupExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(new ResponseMessageBean( + ResponseMessageBean.ERROR, e.getFaultMessage().getInvalidServiceGroupException().getMessage())).build(); + } catch (CloudControllerServiceCartridgeNotFoundExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(new ResponseMessageBean( + ResponseMessageBean.ERROR, e.getMessage())).build(); } } http://git-wip-us.apache.org/repos/asf/stratos/blob/90dc63ea/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java index 72ad2b1..05e824a 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java @@ -924,102 +924,98 @@ public class StratosApiV41Utils { * @throws InvalidCartridgeGroupDefinitionException * @throws RestAPIException */ - public static void addServiceGroup(CartridgeGroupBean serviceGroupDefinition) - throws InvalidCartridgeGroupDefinitionException, RestAPIException { - try { - if (serviceGroupDefinition == null) { - throw new RuntimeException("Cartridge group definition is null"); - } + public static void addCartridgeGroup(CartridgeGroupBean serviceGroupDefinition) + throws InvalidCartridgeGroupDefinitionException, ServiceGroupDefinitionException, RestAPIException, + CloudControllerServiceCartridgeNotFoundExceptionException, + AutoscalerServiceInvalidServiceGroupExceptionException { - List<String> cartridgeTypes = new ArrayList<String>(); - String[] cartridgeNames = null; - List<String> groupNames; - String[] cartridgeGroupNames; + if (serviceGroupDefinition == null) { + throw new RuntimeException("Cartridge group definition is null"); + } - if (log.isDebugEnabled()) { - log.debug("Checking cartridges in cartridge group " + serviceGroupDefinition.getName()); - } + List<String> cartridgeTypes = new ArrayList<String>(); + String[] cartridgeNames = null; + List<String> groupNames; + String[] cartridgeGroupNames; - findCartridgesInGroupBean(serviceGroupDefinition, cartridgeTypes); + if (log.isDebugEnabled()) { + log.debug("Checking cartridges in cartridge group " + serviceGroupDefinition.getName()); + } - //validate the group definition to check if cartridges duplicate in any groups defined - validateCartridgeDuplicationInGroupDefinition(serviceGroupDefinition); + findCartridgesInGroupBean(serviceGroupDefinition, cartridgeTypes); - //validate the group definition to check if groups duplicate in any groups and - //validate the group definition to check for cyclic group behaviour - validateGroupDuplicationInGroupDefinition(serviceGroupDefinition); + //validate the group definition to check if cartridges duplicate in any groups defined + validateCartridgeDuplicationInGroupDefinition(serviceGroupDefinition); - CloudControllerServiceClient ccServiceClient = getCloudControllerServiceClient(); + //validate the group definition to check if groups duplicate in any groups and + //validate the group definition to check for cyclic group behaviour + validateGroupDuplicationInGroupDefinition(serviceGroupDefinition); - cartridgeNames = new String[cartridgeTypes.size()]; - int j = 0; - for (String cartridgeType : cartridgeTypes) { - try { - if (ccServiceClient.getCartridge(cartridgeType) == null) { - // cartridge is not deployed, can't continue - log.error("Invalid cartridge found in cartridge group " + cartridgeType); - throw new RestAPIException("No Cartridge Definition found with type " + cartridgeType); - } else { - cartridgeNames[j] = cartridgeType; - j++; - } - } catch (RemoteException e) { - String message = "Could not add the cartridge group: " + serviceGroupDefinition.getName(); - log.error(message, e); - throw new RestAPIException(message, e); - } catch (CloudControllerServiceCartridgeNotFoundExceptionException e) { - String message = "Required cartridges not found"; - log.error(message, e); - throw new RestAPIException(message, e); + CloudControllerServiceClient ccServiceClient = getCloudControllerServiceClient(); + + cartridgeNames = new String[cartridgeTypes.size()]; + int j = 0; + for (String cartridgeType : cartridgeTypes) { + try { + if (ccServiceClient.getCartridge(cartridgeType) == null) { + // cartridge is not deployed, can't continue + log.error("Invalid cartridge found in cartridge group " + cartridgeType); + throw new InvalidCartridgeException(); + } else { + cartridgeNames[j] = cartridgeType; + j++; } + } catch (RemoteException e) { + String message = "Could not add the cartridge group: " + serviceGroupDefinition.getName(); + log.error(message, e); + throw new RestAPIException(message, e); } + } - // if any sub groups are specified in the group, they should be already deployed - if (serviceGroupDefinition.getGroups() != null) { - if (log.isDebugEnabled()) { - log.debug("checking subGroups in cartridge group " + serviceGroupDefinition.getName()); - } + // if any sub groups are specified in the group, they should be already deployed + if (serviceGroupDefinition.getGroups() != null) { + if (log.isDebugEnabled()) { + log.debug("checking subGroups in cartridge group " + serviceGroupDefinition.getName()); + } - List<CartridgeGroupBean> groupDefinitions = serviceGroupDefinition.getGroups(); - groupNames = new ArrayList<String>(); - cartridgeGroupNames = new String[groupDefinitions.size()]; - int i = 0; - for (CartridgeGroupBean groupList : groupDefinitions) { - groupNames.add(groupList.getName()); - cartridgeGroupNames[i] = groupList.getName(); - i++; - } + List<CartridgeGroupBean> groupDefinitions = serviceGroupDefinition.getGroups(); + groupNames = new ArrayList<String>(); + cartridgeGroupNames = new String[groupDefinitions.size()]; + int i = 0; + for (CartridgeGroupBean groupList : groupDefinitions) { + groupNames.add(groupList.getName()); + cartridgeGroupNames[i] = groupList.getName(); + i++; + } - Set<String> duplicates = findDuplicates(groupNames); - if (duplicates.size() > 0) { + Set<String> duplicates = findDuplicates(groupNames); + if (duplicates.size() > 0) { - StringBuilder duplicatesOutput = new StringBuilder(); - for (String dup : duplicates) { - duplicatesOutput.append(dup).append(" "); - } - if (log.isDebugEnabled()) { - log.debug("duplicate sub-groups defined: " + duplicatesOutput.toString()); - } - throw new RestAPIException("Invalid cartridge group definition, duplicate sub-groups defined:" + - duplicatesOutput.toString()); + StringBuilder duplicatesOutput = new StringBuilder(); + for (String dup : duplicates) { + duplicatesOutput.append(dup).append(" "); + } + if (log.isDebugEnabled()) { + log.debug("duplicate sub-groups defined: " + duplicatesOutput.toString()); } + throw new InvalidCartridgeGroupDefinitionException("Invalid cartridge group definition, duplicate " + + "sub-groups defined:" + duplicatesOutput.toString()); } + } - ServiceGroup serviceGroup = ObjectConverter.convertServiceGroupDefinitionToASStubServiceGroup( - serviceGroupDefinition); + ServiceGroup serviceGroup = ObjectConverter.convertServiceGroupDefinitionToASStubServiceGroup( + serviceGroupDefinition); - AutoscalerServiceClient asServiceClient = getAutoscalerServiceClient(); + AutoscalerServiceClient asServiceClient = getAutoscalerServiceClient(); + try { asServiceClient.addServiceGroup(serviceGroup); - // Add cartridge group elements to SM cache - done after service group has been added StratosManagerServiceClient smServiceClient = getStratosManagerServiceClient(); smServiceClient.addUsedCartridgesInCartridgeGroups(serviceGroupDefinition.getName(), cartridgeNames); - } catch (InvalidCartridgeGroupDefinitionException e) { - throw e; - } catch (Exception e) { - // TODO: InvalidServiceGroupException is not received, only AxisFault. Need to fix get the custom exception - String message = "Could not add cartridge group"; + } catch (RemoteException e) { + + String message = "Could not add the cartridge group: " + serviceGroupDefinition.getName(); log.error(message, e); throw new RestAPIException(message, e); } @@ -1580,6 +1576,7 @@ public class StratosApiV41Utils { * This method validates cartridges in groups * Deployment policy should not defined in cartridge if group has a deployment policy * If group does not have a DP, then cartridge should have one + * * @param cartridgeReferenceBeans - Cartridges in a group * @throws RestAPIException */ http://git-wip-us.apache.org/repos/asf/stratos/blob/90dc63ea/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/exception/InvalidCartridgeException.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/exception/InvalidCartridgeException.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/exception/InvalidCartridgeException.java new file mode 100644 index 0000000..d9de154 --- /dev/null +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/exception/InvalidCartridgeException.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.stratos.rest.endpoint.exception; + +import javax.ws.rs.core.Response; + +public class InvalidCartridgeException extends RestAPIException { + + private static final long serialVersionUID = 1L; + + private String message; + private Response.Status httpStatusCode; + + public InvalidCartridgeException() { + super(); + } + + public InvalidCartridgeException(String message, Throwable cause) { + super(message, cause); + this.message = message; + } + + public InvalidCartridgeException(Response.Status httpStatusCode, String message, Throwable cause) { + super(message, cause); + this.message = message; + this.httpStatusCode = httpStatusCode; + } + + public InvalidCartridgeException(String message) { + super(message); + this.message = message; + } + + public InvalidCartridgeException(Response.Status httpStatusCode, String message) { + super(message); + this.message = message; + this.httpStatusCode = httpStatusCode; + } + + public InvalidCartridgeException(Throwable cause) { + super(cause); + } + + public String getMessage() { + return message; + } + + public Response.Status getHTTPStatusCode() { + return httpStatusCode; + } + + +}
