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