View Javadoc

1   /*
2    * $$Id: BookstoreClient.java 12381 2008-07-17 19:18:24Z tcarlson $$
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.client;
12  
13  import java.io.BufferedReader;
14  import java.io.IOException;
15  import java.io.InputStreamReader;
16  import java.util.ArrayList;
17  import java.util.Collection;
18  import java.util.Iterator;
19  import java.util.Random;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
24  import org.mule.api.MuleContext;
25  import org.mule.api.MuleException;
26  import org.mule.context.DefaultMuleContextFactory;
27  import org.mule.example.bookstore.Book;
28  import org.mule.example.bookstore.Bookstore;
29  import org.mule.example.bookstore.LocaleMessage;
30  
31  public class BookstoreClient
32  {
33      protected static transient Log logger = LogFactory.getLog(BookstoreClient.class);
34      protected static Bookstore bookstore;
35      
36      protected MuleContext muleContext;
37      
38      public BookstoreClient(String config) throws MuleException
39      {
40          // create mule
41          muleContext = new DefaultMuleContextFactory().createMuleContext(config);
42          muleContext.start();
43          
44          // create client
45          JaxWsProxyFactoryBean pf = new JaxWsProxyFactoryBean();
46          pf.setServiceClass(Bookstore.class);
47          pf.setAddress("http://localhost:8777/services/bookstore");
48          bookstore = (Bookstore) pf.create();
49          
50          // add a book to the bookstore
51          Book book = new Book(1,"J.R.R. Tolkien","The Lord of the Rings");
52          bookstore.addBook(book);
53      }
54  
55      public void close()
56      {
57          muleContext.dispose();
58      }
59      
60      public static void main(String[] args) throws Exception
61      {
62          // This is just a simple non mule way to invoke a web service.
63          // It will place an order so you can see the emailOrderService
64          // in action.
65          // For learning how to use CXF with an outbound router, please
66          // see the mule-publisher-demo portion of the project.
67          new BookstoreClient("bookstore.xml");
68          int response = 0;
69          
70          System.out.println("\n" + LocaleMessage.getWelcomeMessage());
71          
72          while (response != 'q')
73          {
74              System.out.println("\n" + LocaleMessage.getMenuOption1());
75              System.out.println("\n" + LocaleMessage.getMenuOption2());
76              System.out.println("\n" + LocaleMessage.getMenuOption3());
77              System.out.println("\n" + LocaleMessage.getMenuOption4());
78              System.out.println("\n" + LocaleMessage.getMenuOptionQuit());
79              System.out.println("\n" + LocaleMessage.getMenuPromptMessage());
80              response = getSelection();
81              
82              if (response == '1')
83              {
84                  Book book = createBook();
85                  bookstore.addBook(book);
86                  System.out.println("Added Book");
87              }
88              else if (response == '2')
89              {
90                  Collection < Book > books = new ArrayList< Book >();
91                  boolean isAddAnotherBook = true;
92                  while(isAddAnotherBook)
93                  {
94                      Book book = createBook();
95                      books.add(book);
96                      System.out.println("\n" + LocaleMessage.getAddBooksMessagePrompt());
97                      int result = getSelection();
98                      if (result != 'y')
99                      {
100                         isAddAnotherBook = false;
101                         bookstore.addBooks(books);
102                         System.out.println("Added book list");
103                     }
104                 }
105             }
106             else if (response == '3')
107             {
108                 Collection < Book > books = bookstore.getBooks();
109                 System.out.println("Request returned " + books.size() + " book/s");
110 
111                 for (Iterator i = books.iterator(); i.hasNext();)
112                 {
113                     Book book = (Book) i.next();
114                     System.out.println("Title: " + book.getTitle());
115                     System.out.println("Author: " + book.getAuthor());
116                     System.out.println("Id: " + book.getId());
117                     System.out.println();
118                 }
119             }
120             else if (response == '4')
121             {   
122                 System.out.println("\n" + LocaleMessage.getOrderWelcomeMessage());
123                 System.out.println("\n" + LocaleMessage.getBookIdPrompt());
124                 long bookId = new Integer(getInput());
125                 System.out.println("\n" + LocaleMessage.getHomeAddressPrompt());
126                 String homeAddress = getInput();
127                 System.out.println("\n" + LocaleMessage.getEmailAddressPrompt());
128                 String emailAddress = getInput();
129                 
130                 // order book
131                 bookstore.orderBook(bookId, 
132                                   homeAddress, 
133                                   emailAddress);
134                 
135                 System.out.println("Book was ordered");
136             }
137             else if (response == 'q')
138             {
139                 System.out.println(LocaleMessage.getGoodbyeMessage());
140                 System.exit(0);
141             }
142             else
143             {
144                 System.out.println(LocaleMessage.getMenuErrorMessage());
145             }
146         }
147     }
148     
149     private static Book createBook() throws Exception
150     {
151         String title = "";
152         String author = "";
153         while (title.compareTo("") == 0)
154         {
155             System.out.println("\n" + LocaleMessage.getBookTitlePrompt());
156             title = getInput();
157         }
158         while (author.compareTo("") == 0)
159         {
160             System.out.println("\n" + LocaleMessage.getAuthorNamePrompt());
161             author = getInput();
162         }
163         
164         return new Book(generateBookId(),title,author);
165     }
166     
167     private static int getSelection() throws IOException
168     {
169         byte[] buf = new byte[16];
170         System.in.read(buf);
171         return buf[0];
172     }
173     
174     private static String getInput() throws IOException
175     {
176         BufferedReader request = new BufferedReader(new InputStreamReader(System.in));
177         return request.readLine();
178     }
179     
180     private static long generateBookId()
181     {
182         Random randomGenerator = new Random();
183         return randomGenerator.nextInt(5000);
184     }
185 }