Coverage Report - org.mule.impl.ImmutableMuleEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
ImmutableMuleEndpoint
65%
171/264
43%
74/174
3.116
 
 1  
 /*
 2  
  * $Id: ImmutableMuleEndpoint.java 11728 2008-05-13 07:31:11Z dirk.olmes $
 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 CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.impl;
 12  
 
 13  
 import org.mule.MuleException;
 14  
 import org.mule.MuleManager;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.impl.endpoint.MuleEndpoint;
 17  
 import org.mule.impl.endpoint.MuleEndpointURI;
 18  
 import org.mule.providers.AbstractConnector;
 19  
 import org.mule.providers.service.TransportFactory;
 20  
 import org.mule.providers.service.TransportFactoryException;
 21  
 import org.mule.umo.UMOEvent;
 22  
 import org.mule.umo.UMOException;
 23  
 import org.mule.umo.UMOFilter;
 24  
 import org.mule.umo.UMOMessage;
 25  
 import org.mule.umo.UMOTransactionConfig;
 26  
 import org.mule.umo.endpoint.UMOEndpoint;
 27  
 import org.mule.umo.endpoint.UMOEndpointURI;
 28  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 29  
 import org.mule.umo.lifecycle.InitialisationException;
 30  
 import org.mule.umo.provider.DispatchException;
 31  
 import org.mule.umo.provider.UMOConnector;
 32  
 import org.mule.umo.security.UMOEndpointSecurityFilter;
 33  
 import org.mule.umo.transformer.UMOTransformer;
 34  
 import org.mule.util.ClassUtils;
 35  
 import org.mule.util.MuleObjectHelper;
 36  
 import org.mule.util.ObjectNameHelper;
 37  
 import org.mule.util.StringUtils;
 38  
 
 39  
 import java.net.URI;
 40  
 import java.util.Collections;
 41  
 import java.util.Map;
 42  
 import java.util.regex.Matcher;
 43  
 import java.util.regex.Pattern;
 44  
 
 45  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 46  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 47  
 import org.apache.commons.logging.Log;
 48  
 import org.apache.commons.logging.LogFactory;
 49  
 
 50  
 /**
 51  
  * <code>ImmutableMuleEndpoint</code> describes a Provider in the Mule Server. A
 52  
  * endpoint is a grouping of an endpoint, an endpointUri and a transformer.
 53  
  */
 54  
 public class ImmutableMuleEndpoint implements UMOImmutableEndpoint
 55  
 {
 56  
     /**
 57  
      * logger used by this class
 58  
      */
 59  4
     protected static final Log logger = LogFactory.getLog(ImmutableMuleEndpoint.class);
 60  
 
 61  
     /**
 62  
      * The endpoint used to communicate with the external system
 63  
      */
 64  868
     protected UMOConnector connector = null;
 65  
 
 66  
     /**
 67  
      * The endpointUri on which to send or receive information
 68  
      */
 69  868
     protected UMOEndpointURI endpointUri = null;
 70  
 
 71  
     /**
 72  
      * The transformer used to transform the incoming or outgoing data
 73  
      */
 74  868
     protected UMOTransformer transformer = null;
 75  
 
 76  
     /**
 77  
      * The transformer used to transform the incoming or outgoing data
 78  
      */
 79  868
     protected UMOTransformer responseTransformer = null;
 80  
 
 81  
     /**
 82  
      * The name for the endpoint
 83  
      */
 84  868
     protected String name = null;
 85  
 
 86  
     /**
 87  
      * Determines whether the endpoint is a receiver or sender or both
 88  
      */
 89  868
     protected String type = ENDPOINT_TYPE_SENDER_AND_RECEIVER;
 90  
 
 91  
     /**
 92  
      * Any additional properties for the endpoint
 93  
      */
 94  868
     protected Map properties = null;
 95  
 
 96  
     /**
 97  
      * The transaction configuration for this endpoint
 98  
      */
 99  868
     protected UMOTransactionConfig transactionConfig = null;
 100  
 
 101  
     /**
 102  
      * event filter for this endpoint
 103  
      */
 104  868
     protected UMOFilter filter = null;
 105  
 
 106  
     /**
 107  
      * determines whether unaccepted filtered events should be removed from the
 108  
      * source. If they are not removed its up to the Message receiver to handle
 109  
      * recieving the same message again
 110  
      */
 111  868
     protected boolean deleteUnacceptedMessages = false;
 112  
 
 113  
     /**
 114  
      * has this endpoint been initialised
 115  
      */
 116  868
     protected AtomicBoolean initialised = new AtomicBoolean(false);
 117  
 
 118  
     /**
 119  
      * The security filter to apply to this endpoint
 120  
      */
 121  868
     protected UMOEndpointSecurityFilter securityFilter = null;
 122  
 
 123  
     /**
 124  
      * whether events received by this endpoint should execute in a single thread
 125  
      */
 126  868
     protected Boolean synchronous = null;
 127  
 
 128  
     /**
 129  
      * Determines whether a synchronous call should block to obtain a response from a
 130  
      * remote server (if the transport supports it). For example for Jms endpoints,
 131  
      * setting remote sync will cause a temporary destination to be set up as a
 132  
      * replyTo destination and will send the message a wait for a response on the
 133  
      * replyTo destination. If the JMSReplyTo is already set on the message that
 134  
      * destination will be used instead.
 135  
      */
 136  868
     protected Boolean remoteSync = null;
 137  
 
 138  
     /**
 139  
      * How long to block when performing a remote synchronisation to a remote host.
 140  
      * This property is optional and will be set to the default Synchonous Event time
 141  
      * out value if not set
 142  
      */
 143  868
     protected Integer remoteSyncTimeout = null;
 144  
 
 145  
     /**
 146  
      * Determines whether the endpoint should deal with requests as streams
 147  
      */
 148  868
     protected boolean streaming = false;
 149  
 
 150  
     /**
 151  
      * The state that the endpoint is initialised in such as started or stopped
 152  
      */
 153  868
     protected String initialState = INITIAL_STATE_STARTED;
 154  
 
 155  868
     protected String endpointEncoding = null;
 156  
 
 157  
     /**
 158  
      * determines if a new connector should be created for this endpoint
 159  
      */
 160  868
     protected int createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
 161  
 
 162  
     /**
 163  
      * Default constructor.
 164  
      */
 165  
     private ImmutableMuleEndpoint()
 166  
     {
 167  42
         super();
 168  42
     }
 169  
 
 170  
     public ImmutableMuleEndpoint(String name,
 171  
                                  UMOEndpointURI endpointUri,
 172  
                                  UMOConnector connector,
 173  
                                  UMOTransformer transformer,
 174  
                                  String type,
 175  
                                  int createConnector,
 176  
                                  String endpointEncoding,
 177  
                                  Map props)
 178  826
     {
 179  826
         this.name = name;
 180  826
         this.connector = connector;
 181  826
         this.createConnector = createConnector;
 182  826
         this.endpointEncoding = endpointEncoding;
 183  826
         this.type = type;
 184  
 
 185  826
         if (endpointUri != null)
 186  
         {
 187  14
             this.endpointUri = new MuleEndpointURI(endpointUri);
 188  
         }
 189  
 
 190  826
         if (transformer != null)
 191  
         {
 192  0
             transformer.setEndpoint(this);
 193  0
             this.transformer = transformer;
 194  
         }
 195  
 
 196  826
         this.properties = new ConcurrentHashMap();
 197  
 
 198  826
         if (props != null)
 199  
         {
 200  12
             this.properties.putAll(props);
 201  
         }
 202  
 
 203  826
         if (endpointUri != null)
 204  
         {
 205  14
             properties.putAll(endpointUri.getParams());
 206  
         }
 207  
 
 208  
         // seal the properties if we are immutable to avoid
 209  
         // write-through aliasing problems with the exposed Map
 210  826
         if (!(this instanceof MuleEndpoint))
 211  
         {
 212  0
             this.properties = Collections.unmodifiableMap(this.properties);
 213  
         }
 214  
 
 215  
         // Create a default transaction config
 216  826
         transactionConfig = new MuleTransactionConfig();
 217  826
     }
 218  
 
 219  
     public ImmutableMuleEndpoint(UMOImmutableEndpoint source)
 220  
     {
 221  2
         this();
 222  2
         this.initFromDescriptor(source);
 223  2
     }
 224  
 
 225  
     public ImmutableMuleEndpoint(String endpointName, boolean receiver) throws UMOException
 226  
     {
 227  40
         this();
 228  40
         String type = (receiver ? UMOEndpoint.ENDPOINT_TYPE_RECEIVER : UMOEndpoint.ENDPOINT_TYPE_SENDER);
 229  40
         UMOEndpoint p = getOrCreateEndpointForUri(new MuleEndpointURI(endpointName), type);
 230  40
         this.initFromDescriptor(p);
 231  40
     }
 232  
 
 233  
     protected void initFromDescriptor(UMOImmutableEndpoint source)
 234  
     {
 235  42
         if (this.name == null)
 236  
         {
 237  42
             this.name = source.getName();
 238  
         }
 239  
 
 240  42
         if (this.endpointUri == null && source.getEndpointURI() != null)
 241  
         {
 242  42
             this.endpointUri = new MuleEndpointURI(source.getEndpointURI());
 243  
         }
 244  
 
 245  42
         if (this.endpointEncoding == null)
 246  
         {
 247  42
             this.endpointEncoding = source.getEncoding();
 248  
         }
 249  
 
 250  42
         if (this.connector == null)
 251  
         {
 252  42
             this.connector = source.getConnector();
 253  
         }
 254  
 
 255  42
         if (this.transformer == null)
 256  
         {
 257  42
             this.transformer = source.getTransformer();
 258  
         }
 259  
 
 260  42
         if (this.transformer != null)
 261  
         {
 262  0
             this.transformer.setEndpoint(this);
 263  
         }
 264  
 
 265  42
         if (this.responseTransformer == null)
 266  
         {
 267  42
             this.responseTransformer = source.getResponseTransformer();
 268  
         }
 269  
 
 270  42
         if (responseTransformer != null)
 271  
         {
 272  0
             this.responseTransformer.setEndpoint(this);
 273  
         }
 274  
 
 275  42
         this.properties = new ConcurrentHashMap();
 276  
 
 277  42
         if (source.getProperties() != null)
 278  
         {
 279  42
             this.properties.putAll(source.getProperties());
 280  
         }
 281  
 
 282  42
         if (endpointUri != null && endpointUri.getParams() != null)
 283  
         {
 284  42
             this.properties.putAll(endpointUri.getParams());
 285  
         }
 286  
 
 287  
         // seal the properties if we are immutable to avoid
 288  
         // write-through aliasing problems with the exposed Map
 289  42
         if (!(this instanceof MuleEndpoint))
 290  
         {
 291  2
             this.properties = Collections.unmodifiableMap(this.properties);
 292  
         }
 293  
 
 294  42
         this.type = source.getType();
 295  42
         this.transactionConfig = source.getTransactionConfig();
 296  42
         this.deleteUnacceptedMessages = source.isDeleteUnacceptedMessages();
 297  42
         this.initialState = source.getInitialState();
 298  
 
 299  42
         remoteSyncTimeout = new Integer(source.getRemoteSyncTimeout());
 300  42
         remoteSync = Boolean.valueOf(source.isRemoteSync());
 301  
 
 302  42
         filter = source.getFilter();
 303  42
         securityFilter = source.getSecurityFilter();
 304  42
     }
 305  
 
 306  
     /*
 307  
      * (non-Javadoc)
 308  
      *
 309  
      * @see org.mule.umo.endpoint.UMOEndpoint#getEndpointURI()
 310  
      */
 311  
     public UMOEndpointURI getEndpointURI()
 312  
     {
 313  2622
         return endpointUri;
 314  
     }
 315  
 
 316  
     public String getEncoding()
 317  
     {
 318  92
         return endpointEncoding;
 319  
     }
 320  
 
 321  
     /*
 322  
      * (non-Javadoc)
 323  
      *
 324  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#getType()
 325  
      */
 326  
     public String getType()
 327  
     {
 328  64
         return type;
 329  
     }
 330  
 
 331  
     /*
 332  
      * (non-Javadoc)
 333  
      *
 334  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#getConnectorName()
 335  
      */
 336  
     public UMOConnector getConnector()
 337  
     {
 338  176
         return connector;
 339  
     }
 340  
 
 341  
     /*
 342  
      * (non-Javadoc)
 343  
      *
 344  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#getName()
 345  
      */
 346  
     public String getName()
 347  
     {
 348  552
         return name;
 349  
     }
 350  
 
 351  
     /*
 352  
      * (non-Javadoc)
 353  
      *
 354  
      * @see org.mule.umo.endpoint.UMOEndpoint#getTransformer()
 355  
      */
 356  
     public UMOTransformer getTransformer()
 357  
     {
 358  926
         return transformer;
 359  
     }
 360  
 
 361  
     /*
 362  
      * (non-Javadoc)
 363  
      *
 364  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#getParams()
 365  
      */
 366  
     public Map getProperties()
 367  
     {
 368  1174
         return properties;
 369  
     }
 370  
 
 371  
     /*
 372  
      * (non-Javadoc)
 373  
      *
 374  
      * @see java.lang.Object#clone()
 375  
      */
 376  
     // TODO this is the 'old' implementation of the clone() method which returns
 377  
     // a MUTABLE endpoint! We need to clarify what endpoint mutability and
 378  
     // cloning actually means
 379  
     public Object clone()
 380  
     {
 381  0
         UMOEndpoint clone = new MuleEndpoint(name, endpointUri, connector, transformer, type,
 382  
             createConnector, endpointEncoding, properties);
 383  
 
 384  0
         clone.setTransactionConfig(transactionConfig);
 385  0
         clone.setFilter(filter);
 386  0
         clone.setSecurityFilter(securityFilter);
 387  
 
 388  0
         if (remoteSync != null)
 389  
         {
 390  0
             clone.setRemoteSync(isRemoteSync());
 391  
         }
 392  0
         if (remoteSyncTimeout != null)
 393  
         {
 394  0
             clone.setRemoteSyncTimeout(getRemoteSyncTimeout());
 395  
         }
 396  
 
 397  0
         if (synchronous != null)
 398  
         {
 399  0
             clone.setSynchronous(synchronous.booleanValue());
 400  
         }
 401  
 
 402  0
         clone.setDeleteUnacceptedMessages(deleteUnacceptedMessages);
 403  
 
 404  0
         clone.setInitialState(initialState);
 405  0
         if (initialised.get())
 406  
         {
 407  
             try
 408  
             {
 409  0
                 clone.initialise();
 410  
             }
 411  0
             catch (InitialisationException e)
 412  
             {
 413  
                 // this really should never happen as the endpoint is already
 414  
                 // initialised
 415  0
                 logger.error(e.getMessage(), e);
 416  0
             }
 417  
         }
 418  
 
 419  0
         return clone;
 420  
     }
 421  
 
 422  
     /*
 423  
      * (non-Javadoc)
 424  
      *
 425  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#isReadOnly()
 426  
      */
 427  
     public boolean isReadOnly()
 428  
     {
 429  0
         return true;
 430  
     }
 431  
 
 432  
     public String toString()
 433  
     {
 434  
         // Use the interface to retrieve the string and set
 435  
         // the endpoint uri to a default value
 436  56
         String sanitizedEndPointUri = null;
 437  56
         URI uri = null;
 438  56
         if (endpointUri != null)
 439  
         {
 440  56
             sanitizedEndPointUri = endpointUri.toString();
 441  56
             uri = endpointUri.getUri();
 442  
         }
 443  
         //
 444  
         // The following will further sanitize the endpointuri by removing
 445  
         // the embedded password. This will only remove the password if the
 446  
         // uri contains all the necessary information to successfully rebuild the url
 447  56
         if (uri != null && (uri.getRawUserInfo() != null) && (uri.getScheme() != null) 
 448  
             && (uri.getHost() != null) && (uri.getRawPath() != null)) 
 449  
         {
 450  
             // build a pattern up that matches what we need tp strip out the password
 451  0
             Pattern sanitizerPattern = Pattern.compile("(.*):.*");
 452  0
             Matcher sanitizerMatcher = sanitizerPattern.matcher(uri.getRawUserInfo());
 453  0
             if (sanitizerMatcher.matches()) 
 454  
             {
 455  0
                sanitizedEndPointUri = new StringBuffer(uri.getScheme())
 456  
                    .append("://").append(sanitizerMatcher.group(1))
 457  
                    .append(":<password>").append("@")
 458  
                    .append(uri.getHost()).append(uri.getRawPath()).toString();
 459  
             }
 460  0
             if ( uri.getRawQuery() != null)
 461  
             {
 462  0
                sanitizedEndPointUri = sanitizedEndPointUri + "?" + uri.getRawQuery();
 463  
             }
 464  
 
 465  
         }
 466  
 
 467  56
         return ClassUtils.getClassName(this.getClass()) + "{endpointUri=" + sanitizedEndPointUri
 468  
                + ", connector=" + connector +  ", transformer=" + transformer + ", name='" + name + "'" 
 469  
                + ", type='" + type + "'" + ", properties=" + properties + ", transactionConfig=" 
 470  
                + transactionConfig + ", filter=" + filter + ", deleteUnacceptedMessages=" 
 471  
                + deleteUnacceptedMessages + ", initialised=" + initialised + ", securityFilter=" 
 472  
                + securityFilter + ", synchronous=" + synchronous + ", initialState=" + initialState 
 473  
                + ", createConnector=" + createConnector + ", remoteSync=" + remoteSync 
 474  
                + ", remoteSyncTimeout=" + remoteSyncTimeout + ", endpointEncoding=" + endpointEncoding + "}";
 475  
     }
 476  
 
 477  
     /*
 478  
      * (non-Javadoc)
 479  
      *
 480  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#getProtocol()
 481  
      */
 482  
     public String getProtocol()
 483  
     {
 484  0
         return connector.getProtocol();
 485  
     }
 486  
 
 487  
     /*
 488  
      * (non-Javadoc)
 489  
      *
 490  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#canReceive()
 491  
      */
 492  
     public boolean canReceive()
 493  
     {
 494  8
         return (getType().equals(ENDPOINT_TYPE_RECEIVER) || getType().equals(
 495  
             ENDPOINT_TYPE_SENDER_AND_RECEIVER));
 496  
     }
 497  
 
 498  
     /*
 499  
      * (non-Javadoc)
 500  
      *
 501  
      * @see org.mule.umo.endpoint.UMOImmutableEndpoint#canSend()
 502  
      */
 503  
     public boolean canSend()
 504  
     {
 505  10
         return (getType().equals(ENDPOINT_TYPE_SENDER) || getType().equals(ENDPOINT_TYPE_SENDER_AND_RECEIVER));
 506  
     }
 507  
 
 508  
     /*
 509  
      * (non-Javadoc)
 510  
      *
 511  
      * @see org.mule.umo.endpoint.UMOEndpoint#getTransactionConfig()
 512  
      */
 513  
     public UMOTransactionConfig getTransactionConfig()
 514  
     {
 515  148
         return transactionConfig;
 516  
     }
 517  
 
 518  
     public boolean equals(Object o)
 519  
     {
 520  88
         if (this == o)
 521  
         {
 522  82
             return true;
 523  
         }
 524  6
         if (!(o instanceof ImmutableMuleEndpoint))
 525  
         {
 526  0
             return false;
 527  
         }
 528  
 
 529  6
         final ImmutableMuleEndpoint immutableMuleProviderDescriptor = (ImmutableMuleEndpoint) o;
 530  
 
 531  6
         if (!connector.getName().equals(immutableMuleProviderDescriptor.connector.getName()))
 532  
         {
 533  0
             return false;
 534  
         }
 535  6
         if (endpointUri != null && immutableMuleProviderDescriptor.endpointUri != null
 536  
                         ? !endpointUri.getAddress().equals(
 537  
                             immutableMuleProviderDescriptor.endpointUri.getAddress())
 538  
                         : immutableMuleProviderDescriptor.endpointUri != null)
 539  
         {
 540  0
             return false;
 541  
         }
 542  6
         if (!name.equals(immutableMuleProviderDescriptor.name))
 543  
         {
 544  0
             return false;
 545  
         }
 546  
         // MULE-1551
 547  
 //        if (transformer != null
 548  
 //                        ? !transformer.equals(immutableMuleProviderDescriptor.transformer)
 549  
 //                        : immutableMuleProviderDescriptor.transformer != null)
 550  
 //        {
 551  
 //            return false;
 552  
 //        }
 553  6
         if (!type.equals(immutableMuleProviderDescriptor.type))
 554  
         {
 555  0
             return false;
 556  
         }
 557  
 
 558  6
         return true;
 559  
     }
 560  
 
 561  
     public int hashCode()
 562  
     {
 563  24
         int result = appendHash(0, connector);
 564  24
         result = appendHash(result, endpointUri);
 565  
         // MULE-1551
 566  
 //        result = appendHash(result, transformer);
 567  24
         result = appendHash(result, name);
 568  24
         result = appendHash(result, type);
 569  
 //        if (logger.isDebugEnabled())
 570  
 //        {
 571  
 //            logger.debug("hashCode: " + result);
 572  
 //        }
 573  24
         return result;
 574  
     }
 575  
 
 576  
     private int appendHash(int hash, Object component)
 577  
     {
 578  96
         int delta = component != null ? component.hashCode() : 0;
 579  
 //        if (logger.isDebugEnabled())
 580  
 //        {
 581  
 //            logger.debug(component + ": " + delta);
 582  
 //        }
 583  96
         return 29 * hash + delta;
 584  
     }
 585  
 
 586  
     public UMOFilter getFilter()
 587  
     {
 588  102
         return filter;
 589  
     }
 590  
 
 591  
     public static UMOEndpoint createEndpointFromUri(UMOEndpointURI uri, String type) throws UMOException
 592  
     {
 593  74
         return TransportFactory.createEndpoint(uri, type);
 594  
     }
 595  
 
 596  
     public static UMOEndpoint getEndpointFromUri(String uri)
 597  
     {
 598  10
         UMOEndpoint endpoint = null;
 599  10
         if (uri != null)
 600  
         {
 601  10
             String endpointString = MuleManager.getInstance().lookupEndpointIdentifier(uri, uri);
 602  10
             endpoint = MuleManager.getInstance().lookupEndpoint(endpointString);
 603  
         }
 604  10
         return endpoint;
 605  
     }
 606  
 
 607  
     public static UMOEndpoint getEndpointFromUri(UMOEndpointURI uri) throws UMOException
 608  
     {
 609  40
         String endpointName = uri.getEndpointName();
 610  40
         if (endpointName != null)
 611  
         {
 612  0
             String endpointString = MuleManager.getInstance().lookupEndpointIdentifier(endpointName,
 613  
                 endpointName);
 614  0
             UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointString);
 615  0
             if (endpoint != null)
 616  
             {
 617  0
                 if (StringUtils.isNotEmpty(uri.getAddress()))
 618  
                 {
 619  0
                     endpoint.setEndpointURI(uri);
 620  
                 }
 621  
             }
 622  0
             return endpoint;
 623  
         }
 624  
 
 625  40
         return null;
 626  
     }
 627  
 
 628  
     public static UMOEndpoint getOrCreateEndpointForUri(String uriIdentifier, String type)
 629  
         throws UMOException
 630  
     {
 631  10
         UMOEndpoint endpoint = getEndpointFromUri(uriIdentifier);
 632  10
         if (endpoint == null)
 633  
         {
 634  10
             endpoint = createEndpointFromUri(new MuleEndpointURI(uriIdentifier), type);
 635  
         }
 636  
         else
 637  
         {
 638  0
             if (endpoint.getType().equals(UMOEndpoint.ENDPOINT_TYPE_SENDER_AND_RECEIVER))
 639  
             {
 640  0
                 endpoint.setType(type);
 641  
             }
 642  0
             else if (!endpoint.getType().equals(type))
 643  
             {
 644  0
                 throw new IllegalArgumentException("Endpoint matching: " + uriIdentifier
 645  
                                                    + " is not of type: " + type + ". It is of type: "
 646  
                                                    + endpoint.getType());
 647  
 
 648  
             }
 649  
         }
 650  8
         return endpoint;
 651  
     }
 652  
 
 653  
     public static UMOEndpoint getOrCreateEndpointForUri(UMOEndpointURI uri, String type) throws UMOException
 654  
     {
 655  40
         UMOEndpoint endpoint = getEndpointFromUri(uri);
 656  40
         if (endpoint == null)
 657  
         {
 658  40
             endpoint = createEndpointFromUri(uri, type);
 659  
         }
 660  40
         return endpoint;
 661  
     }
 662  
 
 663  
     public boolean isDeleteUnacceptedMessages()
 664  
     {
 665  42
         return deleteUnacceptedMessages;
 666  
     }
 667  
 
 668  
     public void initialise() throws InitialisationException
 669  
     {
 670  446
         if (initialised.get())
 671  
         {
 672  44
             logger.debug("Already initialised: " + toString());
 673  44
             return;
 674  
         }
 675  402
         if (connector == null)
 676  
         {
 677  0
             if (endpointUri.getConnectorName() != null)
 678  
             {
 679  0
                 connector = MuleManager.getInstance().lookupConnector(endpointUri.getConnectorName());
 680  0
                 if (connector == null)
 681  
                 {
 682  0
                     throw new IllegalArgumentException("Connector not found: "
 683  
                                                        + endpointUri.getConnectorName());
 684  
                 }
 685  
             }
 686  
             else
 687  
             {
 688  
                 try
 689  
                 {
 690  0
                     connector = TransportFactory.getOrCreateConnectorByProtocol(this);
 691  0
                     if (connector == null)
 692  
                     {
 693  0
                         throw new InitialisationException(
 694  
                             CoreMessages.connectorWithProtocolNotRegistered(endpointUri.getScheme()), this);
 695  
                     }
 696  
                 }
 697  0
                 catch (TransportFactoryException e)
 698  
                 {
 699  0
                     throw new InitialisationException(
 700  
                         CoreMessages.failedToCreateConnectorFromUri(endpointUri), e, this);
 701  0
                 }
 702  
             }
 703  
 
 704  0
             if (endpointUri.getEndpointName() != null && name == null)
 705  
             {
 706  0
                 name = endpointUri.getEndpointName();
 707  
             }
 708  
         }
 709  402
         name = ObjectNameHelper.getEndpointName(this);
 710  
 
 711  402
         String sync = endpointUri.getParams().getProperty("synchronous", null);
 712  402
         if (sync != null)
 713  
         {
 714  0
             synchronous = Boolean.valueOf(sync);
 715  
         }
 716  402
         if (properties != null && endpointUri.getParams() != null)
 717  
         {
 718  402
             properties.putAll(endpointUri.getParams());
 719  
         }
 720  
 
 721  402
         if (endpointUri.getTransformers() != null)
 722  
         {
 723  
             try
 724  
             {
 725  0
                 transformer = MuleObjectHelper.getTransformer(endpointUri.getTransformers(), ",");
 726  
             }
 727  0
             catch (MuleException e)
 728  
             {
 729  0
                 throw new InitialisationException(e, this);
 730  0
             }
 731  
         }
 732  
 
 733  402
         if (transformer == null)
 734  
         {
 735  402
             if (connector instanceof AbstractConnector)
 736  
             {
 737  402
                 if (UMOEndpoint.ENDPOINT_TYPE_SENDER.equals(type))
 738  
                 {
 739  380
                     transformer = ((AbstractConnector) connector).getDefaultOutboundTransformer();
 740  
                 }
 741  22
                 else if (UMOEndpoint.ENDPOINT_TYPE_SENDER_AND_RECEIVER.equals(type))
 742  
                 {
 743  0
                     transformer = ((AbstractConnector) connector).getDefaultOutboundTransformer();
 744  0
                     responseTransformer = ((AbstractConnector) connector).getDefaultInboundTransformer();
 745  
                 }
 746  
                 else
 747  
                 {
 748  22
                     transformer = ((AbstractConnector) connector).getDefaultInboundTransformer();
 749  
                 }
 750  
             }
 751  
         }
 752  402
         if (transformer != null)
 753  
         {
 754  0
             transformer.setEndpoint(this);
 755  
         }
 756  
 
 757  402
         if (endpointUri.getResponseTransformers() != null)
 758  
         {
 759  
             try
 760  
             {
 761  0
                 responseTransformer =
 762  
                         MuleObjectHelper.getTransformer(endpointUri.getResponseTransformers(), ",");
 763  
             }
 764  0
             catch (MuleException e)
 765  
             {
 766  0
                 throw new InitialisationException(e, this);
 767  0
             }
 768  
         }
 769  
         // Only set default transport response transformer if inbound endpoint (MULE-2868)
 770  402
         if (responseTransformer == null  && ENDPOINT_TYPE_RECEIVER.equals(type))
 771  
         {
 772  22
             if (connector instanceof AbstractConnector)
 773  
             {
 774  22
                 responseTransformer = ((AbstractConnector) connector).getDefaultResponseTransformer();
 775  
             }
 776  
         }
 777  402
         if (responseTransformer != null)
 778  
         {
 779  0
             responseTransformer.setEndpoint(this);
 780  
         }
 781  
 
 782  402
         if (securityFilter != null)
 783  
         {
 784  0
             securityFilter.setEndpoint(this);
 785  0
             securityFilter.initialise();
 786  
         }
 787  
 
 788  
         // Allow remote sync values to be set as params on the endpoint URI
 789  402
         String rs = (String) endpointUri.getParams().remove("remoteSync");
 790  402
         if (rs != null)
 791  
         {
 792  0
             remoteSync = Boolean.valueOf(rs);
 793  
         }
 794  
 
 795  402
         String rsTimeout = (String) endpointUri.getParams().remove("remoteSyncTimeout");
 796  402
         if (rsTimeout != null)
 797  
         {
 798  0
             remoteSyncTimeout = Integer.valueOf(rsTimeout);
 799  
         }
 800  
 
 801  402
         initialised.set(true);
 802  402
     }
 803  
 
 804  
     /**
 805  
      * Returns an UMOEndpointSecurityFilter for this endpoint. If one is not set,
 806  
      * there will be no authentication on events sent via this endpoint
 807  
      *
 808  
      * @return UMOEndpointSecurityFilter responsible for authenticating message flow
 809  
      *         via this endpoint.
 810  
      * @see org.mule.umo.security.UMOEndpointSecurityFilter
 811  
      */
 812  
     public UMOEndpointSecurityFilter getSecurityFilter()
 813  
     {
 814  52
         return securityFilter;
 815  
     }
 816  
 
 817  
     /**
 818  
      * Determines if requests originating from this endpoint should be synchronous
 819  
      * i.e. execute in a single thread and possibly return an result. This property
 820  
      * is only used when the endpoint is of type 'receiver'
 821  
      *
 822  
      * @return whether requests on this endpoint should execute in a single thread.
 823  
      *         This property is only used when the endpoint is of type 'receiver'
 824  
      */
 825  
     public boolean isSynchronous()
 826  
     {
 827  0
         if (synchronous == null)
 828  
         {
 829  0
             return MuleManager.getConfiguration().isSynchronous();
 830  
         }
 831  0
         return synchronous.booleanValue();
 832  
     }
 833  
 
 834  
     public boolean isSynchronousSet()
 835  
     {
 836  0
         return (synchronous != null);
 837  
     }
 838  
 
 839  
     public int getCreateConnector()
 840  
     {
 841  0
         return createConnector;
 842  
     }
 843  
 
 844  
     /**
 845  
      * For certain providers that support the notion of a backchannel such as sockets
 846  
      * (outputStream) or Jms (ReplyTo) Mule can automatically wait for a response
 847  
      * from a backchannel when dispatching over these protocols. This is different
 848  
      * for synchronous as synchronous behavior only applies to in
 849  
      *
 850  
      * @return
 851  
      */
 852  
     public boolean isRemoteSync()
 853  
     {
 854  98
         if (remoteSync == null)
 855  
         {
 856  74
             if (connector == null || connector.isRemoteSyncEnabled())
 857  
             {
 858  0
                 remoteSync = Boolean.valueOf(MuleManager.getConfiguration().isRemoteSync());
 859  
             }
 860  
             else
 861  
             {
 862  74
                 remoteSync = Boolean.FALSE;
 863  
             }
 864  
         }
 865  98
         return remoteSync.booleanValue();
 866  
     }
 867  
 
 868  
     /**
 869  
      * The timeout value for remoteSync invocations
 870  
      *
 871  
      * @return the timeout in milliseconds
 872  
      */
 873  
     public int getRemoteSyncTimeout()
 874  
     {
 875  256
         if (remoteSyncTimeout == null)
 876  
         {
 877  254
             remoteSyncTimeout = new Integer(MuleManager.getConfiguration().getSynchronousEventTimeout());
 878  
         }
 879  256
         return remoteSyncTimeout.intValue();
 880  
     }
 881  
 
 882  
     /**
 883  
      * Sets the state the endpoint will be loaded in. The States are 'stopped' and
 884  
      * 'started' (default)
 885  
      *
 886  
      * @return the endpoint starting state
 887  
      */
 888  
     public String getInitialState()
 889  
     {
 890  54
         return initialState;
 891  
     }
 892  
 
 893  
     public UMOTransformer getResponseTransformer()
 894  
     {
 895  48
         return responseTransformer;
 896  
     }
 897  
 
 898  
     /**
 899  
      * Determines whether the endpoint should deal with requests as streams
 900  
      *
 901  
      * @return true if the request should be streamed
 902  
      */
 903  
     public boolean isStreaming()
 904  
     {
 905  56
         return streaming;
 906  
     }
 907  
 
 908  
     public Object getProperty(Object key)
 909  
     {
 910  0
         Object value = properties.get(key);
 911  0
         if (value == null)
 912  
         {
 913  0
             value = endpointUri.getParams().get(key);
 914  
         }
 915  0
         return value;
 916  
     }
 917  
 
 918  
 
 919  
     // TODO the following methods should most likely be lifecycle-enabled
 920  
 
 921  
     public void dispatch(UMOEvent event) throws DispatchException
 922  
     {
 923  0
         if (connector != null)
 924  
         {
 925  0
             connector.dispatch(this, event);
 926  
         }
 927  
         else
 928  
         {
 929  
             //TODO: Either remove because this should never happen or i18n the message
 930  0
             throw new IllegalStateException("The connector on the endpoint: " + toString() + "is null. Please contact dev@mule.codehaus.org");
 931  
         }
 932  0
     }
 933  
 
 934  
     public UMOMessage receive(long timeout) throws Exception
 935  
     {
 936  0
         if (connector != null)
 937  
         {
 938  0
             return connector.receive(this, timeout);
 939  
         }
 940  
         else
 941  
         {
 942  
             //TODO: Either remove because this should never happen or i18n the message
 943  0
             throw new IllegalStateException("The connector on the endpoint: " + toString() + "is null. Please contact dev@mule.codehaus.org");
 944  
         }
 945  
     }
 946  
 
 947  
     public UMOMessage send(UMOEvent event) throws DispatchException
 948  
     {
 949  10
         if (connector != null)
 950  
         {
 951  10
             return connector.send(this, event);
 952  
         }
 953  
         else
 954  
         {
 955  
             //TODO: Either remove because this should never happen or i18n the message
 956  0
             throw new IllegalStateException("The connector on the endpoint: " + toString() + "is null. Please contact dev@mule.codehaus.org");
 957  
         }
 958  
     }
 959  
 
 960  
 }