KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > space > SpaceMessageDispatcher


1 /*
2  * $Id: SpaceMessageDispatcher.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.space;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.impl.MuleMessage;
16 import org.mule.providers.AbstractMessageDispatcher;
17 import org.mule.umo.UMOEvent;
18 import org.mule.umo.UMOException;
19 import org.mule.umo.UMOMessage;
20 import org.mule.umo.endpoint.UMOImmutableEndpoint;
21 import org.mule.umo.space.UMOSpace;
22
23 /**
24  * <code>SpaceMessageDispatcher</code> Provides generic connectivity to 'Spaces'
25  * that implement the Mule Space Api, i.e. Gigaspaces, JCache imples, Rio can be
26  * accessed as well as a mule file, Journal or VM space. The dispatcher allows Mule
27  * to dispatch events synchronously and asynchronusly to a space as well as make
28  * receive calls to the space.
29  */

30
31 public class SpaceMessageDispatcher extends AbstractMessageDispatcher
32 {
33     /**
34      * logger used by this class
35      */

36     protected transient Log logger = LogFactory.getLog(getClass());
37
38     private final SpaceConnector connector;
39     private volatile UMOSpace space;
40
41     public SpaceMessageDispatcher(UMOImmutableEndpoint endpoint)
42     {
43         super(endpoint);
44         this.connector = (SpaceConnector)endpoint.getConnector();
45     }
46
47     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
48     {
49         if (space == null)
50         {
51             space = connector.getSpace(endpoint);
52         }
53     }
54
55     protected void doDisconnect() throws Exception JavaDoc
56     {
57         try
58         {
59             space.dispose();
60         }
61         finally
62         {
63             space = null;
64         }
65     }
66
67     protected void doDispatch(UMOEvent event) throws Exception JavaDoc
68     {
69         long lease = event.getMessage().getLongProperty(SpaceConstants.SPACE_LEASE_PROPERTY, -1);
70         if (lease < 0)
71         {
72             // use the space defaults
73
space.put(event.getTransformedMessage());
74         }
75         else
76         {
77             space.put(event.getTransformedMessage(), lease);
78         }
79     }
80
81     protected UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
82     {
83         doDispatch(event);
84         return null;
85     }
86
87     /**
88      * Make a specific request to the underlying transport
89      *
90      * @param endpoint the endpoint to use when connecting to the resource
91      * @param timeout the maximum time the operation should block before returning.
92      * The call should return immediately if there is data available. If
93      * no data becomes available before the timeout elapses, null will be
94      * returned
95      * @return the result of the request wrapped in a UMOMessage object. Null will be
96      * returned if no data was avaialable
97      * @throws Exception if the call to the underlying protocal cuases an exception
98      */

99     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
100     {
101         String JavaDoc destination = endpoint.getEndpointURI().toString();
102
103         if (logger.isInfoEnabled())
104         {
105             logger.info("Connecting to space '" + destination + "'");
106         }
107
108         Object JavaDoc result = space.take(timeout);
109         if (result == null)
110         {
111             return null;
112         }
113         return new MuleMessage(connector.getMessageAdapter(result));
114     }
115
116     public Object JavaDoc getDelegateSession() throws UMOException
117     {
118         return null;
119     }
120
121     protected void doDispose()
122     {
123         // template method
124
}
125
126 }
127
Popular Tags