1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | 0 | 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 | 0 | { |
40 | |
|
41 | 0 | muleContext = new DefaultMuleContextFactory().createMuleContext(config); |
42 | 0 | muleContext.start(); |
43 | |
|
44 | |
|
45 | 0 | JaxWsProxyFactoryBean pf = new JaxWsProxyFactoryBean(); |
46 | 0 | pf.setServiceClass(Bookstore.class); |
47 | 0 | pf.setAddress("http://localhost:8777/services/bookstore"); |
48 | 0 | bookstore = (Bookstore) pf.create(); |
49 | |
|
50 | |
|
51 | 0 | Book book = new Book(1,"J.R.R. Tolkien","The Lord of the Rings"); |
52 | 0 | bookstore.addBook(book); |
53 | 0 | } |
54 | |
|
55 | |
public void close() |
56 | |
{ |
57 | 0 | muleContext.dispose(); |
58 | 0 | } |
59 | |
|
60 | |
public static void main(String[] args) throws Exception |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | new BookstoreClient("bookstore.xml"); |
68 | 0 | int response = 0; |
69 | |
|
70 | 0 | System.out.println("\n" + LocaleMessage.getWelcomeMessage()); |
71 | |
|
72 | 0 | while (response != 'q') |
73 | |
{ |
74 | 0 | System.out.println("\n" + LocaleMessage.getMenuOption1()); |
75 | 0 | System.out.println("\n" + LocaleMessage.getMenuOption2()); |
76 | 0 | System.out.println("\n" + LocaleMessage.getMenuOption3()); |
77 | 0 | System.out.println("\n" + LocaleMessage.getMenuOption4()); |
78 | 0 | System.out.println("\n" + LocaleMessage.getMenuOptionQuit()); |
79 | 0 | System.out.println("\n" + LocaleMessage.getMenuPromptMessage()); |
80 | 0 | response = getSelection(); |
81 | |
|
82 | 0 | if (response == '1') |
83 | |
{ |
84 | 0 | Book book = createBook(); |
85 | 0 | bookstore.addBook(book); |
86 | 0 | System.out.println("Added Book"); |
87 | 0 | } |
88 | 0 | else if (response == '2') |
89 | |
{ |
90 | 0 | Collection < Book > books = new ArrayList< Book >(); |
91 | 0 | boolean isAddAnotherBook = true; |
92 | 0 | while(isAddAnotherBook) |
93 | |
{ |
94 | 0 | Book book = createBook(); |
95 | 0 | books.add(book); |
96 | 0 | System.out.println("\n" + LocaleMessage.getAddBooksMessagePrompt()); |
97 | 0 | int result = getSelection(); |
98 | 0 | if (result != 'y') |
99 | |
{ |
100 | 0 | isAddAnotherBook = false; |
101 | 0 | bookstore.addBooks(books); |
102 | 0 | System.out.println("Added book list"); |
103 | |
} |
104 | 0 | } |
105 | 0 | } |
106 | 0 | else if (response == '3') |
107 | |
{ |
108 | 0 | Collection < Book > books = bookstore.getBooks(); |
109 | 0 | System.out.println("Request returned " + books.size() + " book/s"); |
110 | |
|
111 | 0 | for (Iterator i = books.iterator(); i.hasNext();) |
112 | |
{ |
113 | 0 | Book book = (Book) i.next(); |
114 | 0 | System.out.println("Title: " + book.getTitle()); |
115 | 0 | System.out.println("Author: " + book.getAuthor()); |
116 | 0 | System.out.println("Id: " + book.getId()); |
117 | 0 | System.out.println(); |
118 | 0 | } |
119 | 0 | } |
120 | 0 | else if (response == '4') |
121 | |
{ |
122 | 0 | System.out.println("\n" + LocaleMessage.getOrderWelcomeMessage()); |
123 | 0 | System.out.println("\n" + LocaleMessage.getBookIdPrompt()); |
124 | 0 | long bookId = new Integer(getInput()); |
125 | 0 | System.out.println("\n" + LocaleMessage.getHomeAddressPrompt()); |
126 | 0 | String homeAddress = getInput(); |
127 | 0 | System.out.println("\n" + LocaleMessage.getEmailAddressPrompt()); |
128 | 0 | String emailAddress = getInput(); |
129 | |
|
130 | |
|
131 | 0 | bookstore.orderBook(bookId, |
132 | |
homeAddress, |
133 | |
emailAddress); |
134 | |
|
135 | 0 | System.out.println("Book was ordered"); |
136 | 0 | } |
137 | 0 | else if (response == 'q') |
138 | |
{ |
139 | 0 | System.out.println(LocaleMessage.getGoodbyeMessage()); |
140 | 0 | System.exit(0); |
141 | |
} |
142 | |
else |
143 | |
{ |
144 | 0 | System.out.println(LocaleMessage.getMenuErrorMessage()); |
145 | |
} |
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
private static Book createBook() throws Exception |
150 | |
{ |
151 | 0 | String title = ""; |
152 | 0 | String author = ""; |
153 | 0 | while (title.compareTo("") == 0) |
154 | |
{ |
155 | 0 | System.out.println("\n" + LocaleMessage.getBookTitlePrompt()); |
156 | 0 | title = getInput(); |
157 | |
} |
158 | 0 | while (author.compareTo("") == 0) |
159 | |
{ |
160 | 0 | System.out.println("\n" + LocaleMessage.getAuthorNamePrompt()); |
161 | 0 | author = getInput(); |
162 | |
} |
163 | |
|
164 | 0 | return new Book(generateBookId(),title,author); |
165 | |
} |
166 | |
|
167 | |
private static int getSelection() throws IOException |
168 | |
{ |
169 | 0 | byte[] buf = new byte[16]; |
170 | 0 | System.in.read(buf); |
171 | 0 | return buf[0]; |
172 | |
} |
173 | |
|
174 | |
private static String getInput() throws IOException |
175 | |
{ |
176 | 0 | BufferedReader request = new BufferedReader(new InputStreamReader(System.in)); |
177 | 0 | return request.readLine(); |
178 | |
} |
179 | |
|
180 | |
private static long generateBookId() |
181 | |
{ |
182 | 0 | Random randomGenerator = new Random(); |
183 | 0 | return randomGenerator.nextInt(5000); |
184 | |
} |
185 | |
} |