View Javadoc

1   /*
2    * $$Id: BookstoreImpl.java 12221 2008-07-01 20:04:09Z dfeist $$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.example.bookstore;
12  
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  import javax.jws.WebService;
20  
21  import org.mule.DefaultMuleMessage;
22  import org.mule.RequestContext;
23  import org.mule.api.MuleException;
24  import org.mule.api.MuleMessage;
25  
26  @WebService(serviceName="BookstoreService",
27      portName="BookstorePort",
28      endpointInterface="org.mule.example.bookstore.Bookstore")
29  public class BookstoreImpl implements Bookstore
30  {
31      private Map < Long, Book > books = new HashMap < Long, Book > ();
32      
33      public long addBook(Book book)
34      {
35          System.out.println("Adding book " + book.getTitle());
36          long id = books.size() + 1;
37          book.setId(id);
38          books.put(id, book);
39          return id;
40      }
41  
42      public Collection < Long > addBooks(Collection < Book > books)
43      {
44          List < Long > ids = new ArrayList < Long > ();
45          if (books != null)
46          {
47              for (Book book : books)
48              {
49                  ids.add(addBook(book));
50              }
51          }
52          return ids;
53      }
54  
55      public Collection < Book > getBooks()
56      {
57          return books.values();
58      }
59  
60      public void orderBook(long bookId, String address, String email)
61      {
62          // In the real world we'd want this hidden behind an OrderService interface
63          try
64          {
65              Book book = books.get(bookId);
66              MuleMessage msg = new DefaultMuleMessage(new Object[] { book, address, email} );
67  
68              RequestContext.getEventContext().dispatchEvent(msg, "orderEmailService");
69              System.out.println("Dispatched message to orderService.");
70          }
71          catch (MuleException e)
72          {
73              // If this was real, we'd want better error handling
74              throw new RuntimeException(e);
75          }
76      }
77  }