KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > dq > DQMessageDispatcher


1 /*
2  * $Id: DQMessageDispatcher.java 3982 2006-11-22 14:28:01Z lajos $
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.providers.dq;
12
13 import org.mule.impl.MuleMessage;
14 import org.mule.providers.AbstractMessageDispatcher;
15 import org.mule.umo.UMOEvent;
16 import org.mule.umo.UMOException;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.endpoint.UMOEndpointURI;
19 import org.mule.umo.endpoint.UMOImmutableEndpoint;
20
21 import com.ibm.as400.access.AS400;
22 import com.ibm.as400.access.DataQueue;
23 import com.ibm.as400.access.DataQueueEntry;
24 import com.ibm.as400.access.Record;
25 import com.ibm.as400.access.RecordFormat;
26
27 /**
28  * <code>DQMessageDispatcher</code> TODO document
29  */

30 public class DQMessageDispatcher extends AbstractMessageDispatcher
31 {
32     private final DQConnector connector;
33
34     /**
35      * Constructor
36      *
37      * @param endpoint The endpoint for this adapter
38      */

39     public DQMessageDispatcher(UMOImmutableEndpoint endpoint)
40     {
41         super(endpoint);
42         this.connector = (DQConnector)endpoint.getConnector();
43     }
44
45     protected void doDispatch(UMOEvent event) throws Exception JavaDoc
46     {
47         try
48         {
49             DQMessage msg = (DQMessage)event.getMessage().getPayload();
50             AS400 system = connector.getSystem();
51
52             RecordFormat format = getRecordFormat(event.getEndpoint().getEndpointURI());
53             Record rec = DQMessageUtils.getRecord(msg, format);
54
55             DataQueue dq = new DataQueue(system, event.getEndpoint().getEndpointURI().getAddress());
56             dq.write(rec.getContents());
57
58         }
59         catch (Exception JavaDoc e)
60         {
61             getConnector().handleException(e);
62         }
63         if (logger.isDebugEnabled())
64         {
65             logger.debug("doDispatch(UMOEvent) - end");
66         }
67     }
68
69     protected RecordFormat getRecordFormat(UMOEndpointURI endpointUri) throws Exception JavaDoc
70     {
71         String JavaDoc recordDescriptor = (String JavaDoc)endpointUri.getParams().get(DQConnector.RECORD_DESCRIPTOR_PROPERTY);
72         if (recordDescriptor == null)
73         {
74             if (connector.getFormat() == null)
75             {
76                 throw new IllegalArgumentException JavaDoc("Property " + DQConnector.RECORD_DESCRIPTOR_PROPERTY
77                                 + " must be set on the endpoint");
78             }
79             else
80             {
81                 if (logger.isDebugEnabled())
82                 {
83                     logger.debug("Defaulting to connector format: " + connector.getRecordFormat());
84                 }
85                 return connector.getFormat();
86             }
87         }
88         if (logger.isDebugEnabled())
89         {
90             logger.debug("Using endpoint-specific format: " + connector.getRecordFormat());
91         }
92         return DQMessageUtils.getRecordFormat(recordDescriptor, connector.getSystem());
93     }
94
95     protected UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
96     {
97         doDispatch(event);
98         return null;
99     }
100
101     public Object JavaDoc getDelegateSession() throws UMOException
102     {
103         return null;
104     }
105
106     /**
107      * Make a specific request to the underlying transport
108      *
109      * @param endpoint the endpoint to use when connecting to the resource
110      * @param timeout the maximum time the operation should block before returning.
111      * The call should return immediately if there is data available. If
112      * no data becomes available before the timeout elapses, null will be
113      * returned
114      * @return the result of the request wrapped in a UMOMessage object. Null will be
115      * returned if no data was avaialable
116      * @throws Exception if the call to the underlying protocal cuases an exception
117      */

118     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
119     {
120         DataQueue dq = new DataQueue(connector.getSystem(), endpoint.getEndpointURI().getAddress());
121         DataQueueEntry entry = dq.read((int)timeout);
122         if (entry != null)
123         {
124             RecordFormat format = getRecordFormat(endpoint.getEndpointURI());
125             DQMessage message = DQMessageUtils.getDQMessage(entry.getData(), format);
126             message.setSenderInformation(entry.getSenderInformation());
127             return new MuleMessage(connector.getMessageAdapter(message));
128         }
129         return null;
130     }
131
132     protected void doDispose()
133     {
134         // template method
135
}
136
137     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
138     {
139         // template method
140
}
141
142     protected void doDisconnect() throws Exception JavaDoc
143     {
144         // template method
145
}
146
147 }
148
Popular Tags