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
This commit is contained in:
parent
dbc0816ae9
commit
d2a69e9641
5 changed files with 83 additions and 6 deletions
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -31,7 +31,7 @@ public interface CustomerRegistry {
|
|||
|
||||
boolean updateCustomer(Customer customer);
|
||||
|
||||
Customer getCustomer(String id);
|
||||
Customer getCustomer(String id) throws CustomerNotFoundException;
|
||||
|
||||
Collection<Customer> getAllCustomers();
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue