1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.samples.voipservice.client; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.builders.MuleXmlConfigurationBuilder; |
15 | |
import org.mule.extras.client.MuleClient; |
16 | |
import org.mule.impl.MuleMessage; |
17 | |
import org.mule.samples.voipservice.LocaleMessage; |
18 | |
import org.mule.samples.voipservice.to.CreditCardTO; |
19 | |
import org.mule.samples.voipservice.to.CreditProfileTO; |
20 | |
import org.mule.samples.voipservice.to.CustomerTO; |
21 | |
import org.mule.samples.voipservice.to.ServiceParamTO; |
22 | |
import org.mule.umo.UMOException; |
23 | |
import org.mule.umo.UMOMessage; |
24 | |
import org.mule.util.StringMessageUtils; |
25 | |
|
26 | |
import java.io.IOException; |
27 | |
|
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
public class VoipConsumer |
32 | |
{ |
33 | |
|
34 | 0 | protected static transient Log logger = LogFactory.getLog(VoipConsumer.class); |
35 | |
|
36 | 0 | private MuleClient muleClient = null; |
37 | |
|
38 | |
public VoipConsumer() throws UMOException |
39 | 0 | { |
40 | 0 | init(); |
41 | 0 | } |
42 | |
|
43 | |
public VoipConsumer(String config) throws UMOException |
44 | 0 | { |
45 | |
|
46 | 0 | MuleXmlConfigurationBuilder builder = new MuleXmlConfigurationBuilder(); |
47 | 0 | builder.configure(config); |
48 | 0 | init(); |
49 | 0 | } |
50 | |
|
51 | |
private void init() throws UMOException |
52 | |
{ |
53 | 0 | muleClient = new MuleClient(); |
54 | 0 | } |
55 | |
|
56 | |
public void close() |
57 | |
{ |
58 | 0 | MuleManager.getInstance().dispose(); |
59 | 0 | } |
60 | |
|
61 | |
public void requestSend(String endpoint) throws Exception |
62 | |
{ |
63 | |
UMOMessage result; |
64 | 0 | CustomerTO customerTO = CustomerTO.getRandomCustomer(); |
65 | 0 | CreditCardTO creditCardTO = CreditCardTO.getRandomCreditCard(); |
66 | 0 | result = muleClient.send(endpoint, new ServiceParamTO(customerTO, creditCardTO), null); |
67 | 0 | CreditProfileTO creditProfileTO = (CreditProfileTO)((MuleMessage)result).getPayload(); |
68 | 0 | boolean valid = creditProfileTO.isValid(); |
69 | 0 | logger.info("SyncVoipConsumer.requestSend. valid = " + valid); |
70 | 0 | } |
71 | |
|
72 | |
public static void main(String[] args) |
73 | |
{ |
74 | 0 | VoipConsumer voipConsumer = null; |
75 | 0 | int response = 0; |
76 | |
|
77 | |
try |
78 | |
{ |
79 | 0 | voipConsumer = new VoipConsumer("voip-broker-sync-config.xml"); |
80 | |
|
81 | 0 | String msg = LocaleMessage.getWelcomeMessage(); |
82 | |
|
83 | 0 | System.out.println(StringMessageUtils.getBoilerPlate(msg, '*', 70)); |
84 | |
|
85 | 0 | while (response != 'q') |
86 | |
{ |
87 | 0 | System.out.println("\n" + LocaleMessage.getMenuOption1()); |
88 | 0 | System.out.println(LocaleMessage.getMenuOptionQuit()); |
89 | 0 | System.out.println("\n" + LocaleMessage.getMenuPromptMessage()); |
90 | |
|
91 | 0 | response = getSelection(); |
92 | 0 | if (response == '1') |
93 | |
{ |
94 | 0 | logger.info("Sending Request..."); |
95 | 0 | voipConsumer.requestSend("vm://VoipBrokerRequests"); |
96 | 0 | logger.info("Request Completed."); |
97 | |
} |
98 | 0 | else if (response == 'q') |
99 | |
{ |
100 | 0 | System.out.println(LocaleMessage.getGoodbyeMessage()); |
101 | 0 | System.exit(0); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 0 | System.out.println(LocaleMessage.getMenuErrorMessage()); |
106 | |
} |
107 | |
} |
108 | |
|
109 | |
} |
110 | 0 | catch (Exception e) |
111 | |
{ |
112 | 0 | System.err.println(e.getMessage()); |
113 | 0 | e.printStackTrace(System.err); |
114 | 0 | System.exit(1); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
private static int getSelection() throws IOException |
119 | |
{ |
120 | 0 | byte[] buf = new byte[16]; |
121 | 0 | System.in.read(buf); |
122 | 0 | return buf[0]; |
123 | |
} |
124 | |
|
125 | |
} |