From d2a69e96416b2d8380743e443cebc03c87c6bf3c Mon Sep 17 00:00:00 2001 From: slaws Date: Sun, 22 Mar 2009 14:34:30 +0000 Subject: Add some exception handling to the customer registry git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@757191 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/java/payment/PaymentImpl.java | 11 +++- .../customer/CustomerNotFoundException.java | 58 ++++++++++++++++++++++ .../java/scatours/customer/CustomerRegistry.java | 2 +- .../scatours/customer/CustomerRegistryImpl.java | 17 +++++-- .../src/test/java/payment/PaymentTestCase.java | 1 + 5 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerNotFoundException.java (limited to 'sandbox') diff --git a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/payment/PaymentImpl.java b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/payment/PaymentImpl.java index cd2a096632..d7bac59fcf 100644 --- a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/payment/PaymentImpl.java +++ b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/payment/PaymentImpl.java @@ -27,6 +27,7 @@ import org.osoa.sca.annotations.Service; import payment.creditcard.CreditCardDetailsType; import payment.creditcard.CreditCardPayment; import scatours.customer.Customer; +import scatours.customer.CustomerNotFoundException; import scatours.customer.CustomerRegistry; import scatours.emailgateway.EmailGateway; @@ -50,7 +51,15 @@ public class PaymentImpl implements Payment { protected float transactionFeeRate = 0.01f; public String makePaymentMember(String customerId, float amount) { - Customer customer = customerRegistry.getCustomer(customerId); + Customer customer = null; + + try { + customer = customerRegistry.getCustomer(customerId); + } catch (CustomerNotFoundException ex) { + return "Payment failed due to " + ex.getMessage(); + } catch (Throwable t) { + return "Payment failed due to system error " + t.getMessage(); + } CreditCardDetailsType ccDetails = customer.getCreditCard(); diff --git a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerNotFoundException.java b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerNotFoundException.java new file mode 100644 index 0000000000..ebfb104650 --- /dev/null +++ b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerNotFoundException.java @@ -0,0 +1,58 @@ +/* + * 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 scatours.customer; + + +public class CustomerNotFoundException extends Exception { + private static final long serialVersionUID = -129752837478357452L; + + /** + * + */ + public CustomerNotFoundException() { + + } + + /** + * @param message + */ + public CustomerNotFoundException(String message) { + super(message); + + } + + /** + * @param cause + */ + public CustomerNotFoundException(Throwable cause) { + super(cause); + + } + + /** + * @param message + * @param cause + */ + public CustomerNotFoundException(String message, Throwable cause) { + super(message, cause); + + } + +} diff --git a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistry.java b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistry.java index 0a23f6a81a..c7c9f11a6c 100644 --- a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistry.java +++ b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistry.java @@ -31,7 +31,7 @@ public interface CustomerRegistry { boolean updateCustomer(Customer customer); - Customer getCustomer(String id); + Customer getCustomer(String id) throws CustomerNotFoundException; Collection getAllCustomers(); diff --git a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistryImpl.java b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistryImpl.java index 8a55e43fc5..615f0ef1b9 100644 --- a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistryImpl.java +++ b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/main/java/scatours/customer/CustomerRegistryImpl.java @@ -85,15 +85,24 @@ public class CustomerRegistryImpl implements CustomerRegistry { return customers.values(); } - public Customer getCustomer(String id) { - return customers.get(id); + public Customer getCustomer(String id) throws CustomerNotFoundException { + Customer customer = customers.get(id); + + if (customer == null){ + throw new CustomerNotFoundException("Customer " + id + " not found"); + } + + return customer; } public boolean updateCustomer(Customer customer) { - Customer current = getCustomer(customer.getId()); - if (current == null) { + Customer current = null; + try { + current = getCustomer(customer.getId()); + } catch (Exception ex) { return false; } + current.setEmail(customer.getEmail()); current.setName(customer.getName()); current.setCreditCard(customer.getCreditCard()); diff --git a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/test/java/payment/PaymentTestCase.java b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/test/java/payment/PaymentTestCase.java index 9acf17e5e6..d88764cfac 100644 --- a/sandbox/travelsample/shared-contributions/payment-java-contribution/src/test/java/payment/PaymentTestCase.java +++ b/sandbox/travelsample/shared-contributions/payment-java-contribution/src/test/java/payment/PaymentTestCase.java @@ -47,6 +47,7 @@ public class PaymentTestCase { SCAClient client = (SCAClient) node; Payment payment = client.getService(Payment.class, "PaymentComponent"); System.out.println(payment.makePaymentMember("c-0", 100.00f)); + System.out.println(payment.makePaymentMember("c-1", 100.00f)); } @Test -- cgit v1.2.3