1 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 30 31 public class SpaceMessageDispatcher extends AbstractMessageDispatcher 32 { 33 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 48 { 49 if (space == null) 50 { 51 space = connector.getSpace(endpoint); 52 } 53 } 54 55 protected void doDisconnect() throws Exception 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 68 { 69 long lease = event.getMessage().getLongProperty(SpaceConstants.SPACE_LEASE_PROPERTY, -1); 70 if (lease < 0) 71 { 72 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 82 { 83 doDispatch(event); 84 return null; 85 } 86 87 99 protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 100 { 101 String destination = endpoint.getEndpointURI().toString(); 102 103 if (logger.isInfoEnabled()) 104 { 105 logger.info("Connecting to space '" + destination + "'"); 106 } 107 108 Object 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 getDelegateSession() throws UMOException 117 { 118 return null; 119 } 120 121 protected void doDispose() 122 { 123 } 125 126 } 127 | Popular Tags |