KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > voipservice > client > VoipConsumer


1 /*
2  * $Id: VoipConsumer.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.samples.voipservice.client;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.MuleManager;
16 import org.mule.config.builders.MuleXmlConfigurationBuilder;
17 import org.mule.extras.client.MuleClient;
18 import org.mule.impl.MuleMessage;
19 import org.mule.samples.voipservice.to.CreditCardTO;
20 import org.mule.samples.voipservice.to.CreditProfileTO;
21 import org.mule.samples.voipservice.to.CustomerTO;
22 import org.mule.samples.voipservice.to.ServiceParamTO;
23 import org.mule.umo.UMOException;
24 import org.mule.umo.UMOMessage;
25 import org.mule.util.StringMessageUtils;
26
27 import java.io.IOException JavaDoc;
28
29 /**
30  * @author Binildas Christudas
31  */

32 public class VoipConsumer
33 {
34
35     protected static transient Log logger = LogFactory.getLog(VoipConsumer.class);
36
37     private MuleClient muleClient = null;
38
39     public VoipConsumer() throws UMOException
40     {
41         init();
42     }
43
44     public VoipConsumer(String JavaDoc config) throws UMOException
45     {
46
47         MuleXmlConfigurationBuilder builder = new MuleXmlConfigurationBuilder();
48         builder.configure(config);
49         init();
50     }
51
52     private void init() throws UMOException
53     {
54         muleClient = new MuleClient();
55     }
56
57     public void close()
58     {
59         MuleManager.getInstance().dispose();
60     }
61
62     public void requestSend(String JavaDoc endpoint) throws Exception JavaDoc
63     {
64         UMOMessage result;
65         CustomerTO customerTO = CustomerTO.getRandomCustomer();
66         CreditCardTO creditCardTO = CreditCardTO.getRandomCreditCard();
67         result = muleClient.send(endpoint, new ServiceParamTO(customerTO, creditCardTO), null);
68         CreditProfileTO creditProfileTO = (CreditProfileTO)((MuleMessage)result).getPayload();
69         boolean valid = creditProfileTO.isValid();
70         logger.info("SyncVoipConsumer.requestSend. valid = " + valid);
71     }
72
73     public static void main(String JavaDoc[] args)
74     {
75         VoipConsumer voipConsumer = null;
76         int response = 0;
77
78         try
79         {
80             voipConsumer = new VoipConsumer("voip-broker-sync-config.xml");
81
82             String JavaDoc msg = "Welcome to the Mule Voip Services Provisioning Example."
83                          + " This example was published as part of a featured article on java.net"
84                          + " titled 'Service Provisioning Through ESB' (http://today.java.net/lpt/a/233).";
85
86             System.out.println(StringMessageUtils.getBoilerPlate(msg, '*', 70));
87
88             while (response != 'q')
89             {
90                 System.out.println("\n[1] make a service request");
91                 System.out.println("[q] quit");
92                 System.out.println("\nPlease make your selection: ");
93
94                 response = getSelection();
95                 if (response == '1')
96                 {
97                     logger.info("Sending Request...");
98                     voipConsumer.requestSend("vm://VoipBrokerRequests");
99                     logger.info("Request Completed.");
100                 }
101                 else if (response == 'q')
102                 {
103                     System.out.println("Bye");
104                     System.exit(0);
105                 }
106                 else
107                 {
108                     System.out.println("That response is not recognised, try again:");
109                 }
110             }
111
112         }
113         catch (Exception JavaDoc e)
114         {
115             System.err.println(e.getMessage());
116             e.printStackTrace(System.err);
117             System.exit(1);
118         }
119     }
120
121     private static int getSelection() throws IOException JavaDoc
122     {
123         byte[] buf = new byte[16];
124         System.in.read(buf);
125         return buf[0];
126     }
127
128 }
129
Popular Tags