1 10 11 package org.mule.routing.response; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.mule.config.MuleConfiguration; 19 import org.mule.management.stats.RouterStatistics; 20 import org.mule.routing.AbstractRouterCollection; 21 import org.mule.umo.UMOEvent; 22 import org.mule.umo.UMOMessage; 23 import org.mule.umo.endpoint.UMOEndpoint; 24 import org.mule.umo.routing.RoutingException; 25 import org.mule.umo.routing.UMOResponseMessageRouter; 26 import org.mule.umo.routing.UMOResponseRouter; 27 import org.mule.umo.routing.UMORouter; 28 29 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 30 31 39 public class ResponseMessageRouter extends AbstractRouterCollection implements UMOResponseMessageRouter 40 { 41 44 protected static Log logger = LogFactory.getLog(ResponseMessageRouter.class); 45 46 private List endpoints = new CopyOnWriteArrayList(); 47 48 private int timeout = MuleConfiguration.DEFAULT_TIMEOUT; 49 50 public ResponseMessageRouter() 51 { 52 super(RouterStatistics.TYPE_RESPONSE); 53 } 54 55 public void route(UMOEvent event) throws RoutingException 56 { 57 UMOResponseRouter router = null; 58 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) 59 { 60 router = (UMOResponseRouter)iterator.next(); 61 router.process(event); 62 if (getStatistics().isEnabled()) 64 { 65 getStatistics().incrementRoutedMessage(event.getEndpoint()); 66 } 67 } 68 } 69 70 public UMOMessage getResponse(UMOMessage message) throws RoutingException 71 { 72 UMOMessage result = null; 73 if (routers.size() == 0) 74 { 75 logger.warn("There are no routers configured on the response router. Returning the current message"); 76 result = message; 77 } 78 else 79 { 80 UMOResponseRouter router = null; 81 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) 82 { 83 router = (UMOResponseRouter)iterator.next(); 84 result = router.getResponse(message); 85 } 86 87 if (result == null) 88 { 89 if (getStatistics().isEnabled()) 91 { 92 getStatistics().incrementNoRoutedMessage(); 93 } 94 } 95 } 96 97 return result; 106 107 } 108 109 public void addRouter(UMORouter router) 110 { 111 ((UMOResponseRouter)router).setTimeout(getTimeout()); 112 routers.add(router); 113 } 114 115 public UMOResponseRouter removeRouter(UMOResponseRouter router) 116 { 117 if (routers.remove(router)) 118 { 119 return router; 120 } 121 else 122 { 123 return null; 124 } 125 } 126 127 public void addEndpoint(UMOEndpoint endpoint) 128 { 129 if (endpoint != null) 130 { 131 endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE); 132 endpoints.add(endpoint); 133 } 134 else 135 { 136 throw new NullPointerException ("endpoint = null"); 137 } 138 } 139 140 public boolean removeEndpoint(UMOEndpoint endpoint) 141 { 142 return endpoints.remove(endpoint); 143 } 144 145 public List getEndpoints() 146 { 147 return endpoints; 148 } 149 150 public void setEndpoints(List endpoints) 151 { 152 if (endpoints != null) 153 { 154 this.endpoints.clear(); 155 this.endpoints.addAll(endpoints); 156 157 for (Iterator it = this.endpoints.iterator(); it.hasNext();) 159 { 160 ((UMOEndpoint)it.next()).setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE); 161 } 162 } 163 else 164 { 165 throw new NullPointerException ("List of endpoints = null"); 166 } 167 } 168 169 174 public UMOEndpoint getEndpoint(String name) 175 { 176 UMOEndpoint endpointDescriptor; 177 for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) 178 { 179 endpointDescriptor = (UMOEndpoint)iterator.next(); 180 if (endpointDescriptor.getName().equals(name)) 181 { 182 return endpointDescriptor; 183 } 184 } 185 return null; 186 } 187 188 public int getTimeout() 189 { 190 return timeout; 191 } 192 193 public void setTimeout(int timeout) 194 { 195 this.timeout = timeout; 196 } 197 198 } 199 | Popular Tags |