1
2
3
4
5
6
7
8
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
52
53
54 public class ImmutableMuleEndpoint implements UMOImmutableEndpoint
55 {
56
57
58
59 private static final long serialVersionUID = -2431378111247771909L;
60
61
62
63
64 protected static final Log logger = LogFactory.getLog(ImmutableMuleEndpoint.class);
65
66
67
68
69 protected UMOConnector connector = null;
70
71
72
73
74 protected UMOEndpointURI endpointUri = null;
75
76
77
78
79 protected UMOTransformer transformer = null;
80
81
82
83
84 protected UMOTransformer responseTransformer = null;
85
86
87
88
89 protected String name = null;
90
91
92
93
94 protected String type = ENDPOINT_TYPE_SENDER_AND_RECEIVER;
95
96
97
98
99 protected Map properties = null;
100
101
102
103
104 protected UMOTransactionConfig transactionConfig = null;
105
106
107
108
109 protected UMOFilter filter = null;
110
111
112
113
114
115
116 protected boolean deleteUnacceptedMessages = false;
117
118
119
120
121 protected AtomicBoolean initialised = new AtomicBoolean(false);
122
123
124
125
126 protected UMOEndpointSecurityFilter securityFilter = null;
127
128
129
130
131 protected Boolean synchronous = null;
132
133
134
135
136
137
138
139
140
141 protected Boolean remoteSync = null;
142
143
144
145
146
147
148 protected Integer remoteSyncTimeout = null;
149
150
151
152
153 protected boolean streaming = false;
154
155
156
157
158 protected String initialState = INITIAL_STATE_STARTED;
159
160 protected String endpointEncoding = null;
161
162
163
164
165 protected int createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
166
167
168
169
170 private ImmutableMuleEndpoint()
171 {
172 super();
173 }
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 {
184 this.name = name;
185 this.connector = connector;
186 this.createConnector = createConnector;
187 this.endpointEncoding = endpointEncoding;
188 this.type = type;
189
190 if (endpointUri != null)
191 {
192 this.endpointUri = new MuleEndpointURI(endpointUri);
193 }
194
195 if (transformer != null)
196 {
197 transformer.setEndpoint(this);
198 this.transformer = transformer;
199 }
200
201 this.properties = new ConcurrentHashMap();
202
203 if (props != null)
204 {
205 this.properties.putAll(props);
206 }
207
208 if (endpointUri != null)
209 {
210 properties.putAll(endpointUri.getParams());
211 }
212
213
214
215 if (!(this instanceof MuleEndpoint))
216 {
217 this.properties = Collections.unmodifiableMap(this.properties);
218 }
219
220
221 transactionConfig = new MuleTransactionConfig();
222 }
223
224 public ImmutableMuleEndpoint(UMOImmutableEndpoint source)
225 {
226 this();
227 this.initFromDescriptor(source);
228 }
229
230 public ImmutableMuleEndpoint(String endpointName, boolean receiver) throws UMOException
231 {
232 this();
233 String type = (receiver ? UMOEndpoint.ENDPOINT_TYPE_RECEIVER : UMOEndpoint.ENDPOINT_TYPE_SENDER);
234 UMOEndpoint p = getOrCreateEndpointForUri(new MuleEndpointURI(endpointName), type);
235 this.initFromDescriptor(p);
236 }
237
238 protected void initFromDescriptor(UMOImmutableEndpoint source)
239 {
240 if (this.name == null)
241 {
242 this.name = source.getName();
243 }
244
245 if (this.endpointUri == null && source.getEndpointURI() != null)
246 {
247 this.endpointUri = new MuleEndpointURI(source.getEndpointURI());
248 }
249
250 if (this.endpointEncoding == null)
251 {
252 this.endpointEncoding = source.getEncoding();
253 }
254
255 if (this.connector == null)
256 {
257 this.connector = source.getConnector();
258 }
259
260 if (this.transformer == null)
261 {
262 this.transformer = source.getTransformer();
263 }
264
265 if (this.transformer != null)
266 {
267 this.transformer.setEndpoint(this);
268 }
269
270 if (this.responseTransformer == null)
271 {
272 this.responseTransformer = source.getResponseTransformer();
273 }
274
275 if (responseTransformer != null)
276 {
277 this.responseTransformer.setEndpoint(this);
278 }
279
280 this.properties = new ConcurrentHashMap();
281
282 if (source.getProperties() != null)
283 {
284 this.properties.putAll(source.getProperties());
285 }
286
287 if (endpointUri != null && endpointUri.getParams() != null)
288 {
289 this.properties.putAll(endpointUri.getParams());
290 }
291
292
293
294 if (!(this instanceof MuleEndpoint))
295 {
296 this.properties = Collections.unmodifiableMap(this.properties);
297 }
298
299 this.type = source.getType();
300 this.transactionConfig = source.getTransactionConfig();
301 this.deleteUnacceptedMessages = source.isDeleteUnacceptedMessages();
302 this.initialState = source.getInitialState();
303
304 remoteSyncTimeout = new Integer(source.getRemoteSyncTimeout());
305 remoteSync = Boolean.valueOf(source.isRemoteSync());
306
307 filter = source.getFilter();
308 securityFilter = source.getSecurityFilter();
309 }
310
311
312
313
314
315
316 public UMOEndpointURI getEndpointURI()
317 {
318 return endpointUri;
319 }
320
321 public String getEncoding()
322 {
323 return endpointEncoding;
324 }
325
326
327
328
329
330
331 public String getType()
332 {
333 return type;
334 }
335
336
337
338
339
340
341 public UMOConnector getConnector()
342 {
343 return connector;
344 }
345
346
347
348
349
350
351 public String getName()
352 {
353 return name;
354 }
355
356
357
358
359
360
361 public UMOTransformer getTransformer()
362 {
363 return transformer;
364 }
365
366
367
368
369
370
371 public Map getProperties()
372 {
373 return properties;
374 }
375
376
377
378
379
380
381
382
383
384 public Object clone()
385 {
386 UMOEndpoint clone = new MuleEndpoint(name, endpointUri, connector, transformer, type,
387 createConnector, endpointEncoding, properties);
388
389 clone.setTransactionConfig(transactionConfig);
390 clone.setFilter(filter);
391 clone.setSecurityFilter(securityFilter);
392
393 if (remoteSync != null)
394 {
395 clone.setRemoteSync(isRemoteSync());
396 }
397 if (remoteSyncTimeout != null)
398 {
399 clone.setRemoteSyncTimeout(getRemoteSyncTimeout());
400 }
401
402 if (synchronous != null)
403 {
404 clone.setSynchronous(synchronous.booleanValue());
405 }
406
407 clone.setDeleteUnacceptedMessages(deleteUnacceptedMessages);
408
409 clone.setInitialState(initialState);
410 if (initialised.get())
411 {
412 try
413 {
414 clone.initialise();
415 }
416 catch (InitialisationException e)
417 {
418
419
420 logger.error(e.getMessage(), e);
421 }
422 }
423
424 return clone;
425 }
426
427
428
429
430
431
432 public boolean isReadOnly()
433 {
434 return true;
435 }
436
437 public String toString()
438 {
439
440
441 String sanitizedEndPointUri = null;
442 URI uri = null;
443 if (endpointUri != null)
444 {
445 sanitizedEndPointUri = endpointUri.toString();
446 uri = endpointUri.getUri();
447 }
448
449
450
451
452 if (uri != null && (uri.getRawUserInfo() != null) && (uri.getScheme() != null)
453 && (uri.getHost() != null) && (uri.getRawPath() != null))
454 {
455
456 Pattern sanitizerPattern = Pattern.compile("(.*):.*");
457 Matcher sanitizerMatcher = sanitizerPattern.matcher(uri.getRawUserInfo());
458 if (sanitizerMatcher.matches())
459 {
460 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 if ( uri.getRawQuery() != null)
466 {
467 sanitizedEndPointUri = sanitizedEndPointUri + "?" + uri.getRawQuery();
468 }
469
470 }
471
472 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
484
485
486
487 public String getProtocol()
488 {
489 return connector.getProtocol();
490 }
491
492
493
494
495
496
497 public boolean canReceive()
498 {
499 return (getType().equals(ENDPOINT_TYPE_RECEIVER) || getType().equals(
500 ENDPOINT_TYPE_SENDER_AND_RECEIVER));
501 }
502
503
504
505
506
507
508 public boolean canSend()
509 {
510 return (getType().equals(ENDPOINT_TYPE_SENDER) || getType().equals(ENDPOINT_TYPE_SENDER_AND_RECEIVER));
511 }
512
513
514
515
516
517
518 public UMOTransactionConfig getTransactionConfig()
519 {
520 return transactionConfig;
521 }
522
523 public boolean equals(Object o)
524 {
525 if (this == o)
526 {
527 return true;
528 }
529 if (!(o instanceof ImmutableMuleEndpoint))
530 {
531 return false;
532 }
533
534 final ImmutableMuleEndpoint immutableMuleProviderDescriptor = (ImmutableMuleEndpoint) o;
535
536 if (!connector.getName().equals(immutableMuleProviderDescriptor.connector.getName()))
537 {
538 return false;
539 }
540 if (endpointUri != null && immutableMuleProviderDescriptor.endpointUri != null
541 ? !endpointUri.getAddress().equals(
542 immutableMuleProviderDescriptor.endpointUri.getAddress())
543 : immutableMuleProviderDescriptor.endpointUri != null)
544 {
545 return false;
546 }
547 if (!name.equals(immutableMuleProviderDescriptor.name))
548 {
549 return false;
550 }
551
552
553
554
555
556
557
558 if (!type.equals(immutableMuleProviderDescriptor.type))
559 {
560 return false;
561 }
562
563 return true;
564 }
565
566 public int hashCode()
567 {
568 int result = appendHash(0, connector);
569 result = appendHash(result, endpointUri);
570
571
572 result = appendHash(result, name);
573 result = appendHash(result, type);
574
575
576
577
578 return result;
579 }
580
581 private int appendHash(int hash, Object component)
582 {
583 int delta = component != null ? component.hashCode() : 0;
584
585
586
587
588 return 29 * hash + delta;
589 }
590
591 public UMOFilter getFilter()
592 {
593 return filter;
594 }
595
596 public static UMOEndpoint createEndpointFromUri(UMOEndpointURI uri, String type) throws UMOException
597 {
598 return TransportFactory.createEndpoint(uri, type);
599 }
600
601 public static UMOEndpoint getEndpointFromUri(String uri)
602 {
603 UMOEndpoint endpoint = null;
604 if (uri != null)
605 {
606 String endpointString = MuleManager.getInstance().lookupEndpointIdentifier(uri, uri);
607 endpoint = MuleManager.getInstance().lookupEndpoint(endpointString);
608 }
609 return endpoint;
610 }
611
612 public static UMOEndpoint getEndpointFromUri(UMOEndpointURI uri) throws UMOException
613 {
614 String endpointName = uri.getEndpointName();
615 if (endpointName != null)
616 {
617 String endpointString = MuleManager.getInstance().lookupEndpointIdentifier(endpointName,
618 endpointName);
619 UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointString);
620 if (endpoint != null)
621 {
622 if (StringUtils.isNotEmpty(uri.getAddress()))
623 {
624 endpoint.setEndpointURI(uri);
625 }
626 }
627 return endpoint;
628 }
629
630 return null;
631 }
632
633 public static UMOEndpoint getOrCreateEndpointForUri(String uriIdentifier, String type)
634 throws UMOException
635 {
636 UMOEndpoint endpoint = getEndpointFromUri(uriIdentifier);
637 if (endpoint == null)
638 {
639 endpoint = createEndpointFromUri(new MuleEndpointURI(uriIdentifier), type);
640 }
641 else
642 {
643 if (endpoint.getType().equals(UMOEndpoint.ENDPOINT_TYPE_SENDER_AND_RECEIVER))
644 {
645 endpoint.setType(type);
646 }
647 else if (!endpoint.getType().equals(type))
648 {
649 throw new IllegalArgumentException("Endpoint matching: " + uriIdentifier
650 + " is not of type: " + type + ". It is of type: "
651 + endpoint.getType());
652
653 }
654 }
655 return endpoint;
656 }
657
658 public static UMOEndpoint getOrCreateEndpointForUri(UMOEndpointURI uri, String type) throws UMOException
659 {
660 UMOEndpoint endpoint = getEndpointFromUri(uri);
661 if (endpoint == null)
662 {
663 endpoint = createEndpointFromUri(uri, type);
664 }
665 return endpoint;
666 }
667
668 public boolean isDeleteUnacceptedMessages()
669 {
670 return deleteUnacceptedMessages;
671 }
672
673 public void initialise() throws InitialisationException
674 {
675 if (initialised.get())
676 {
677 logger.debug("Already initialised: " + toString());
678 return;
679 }
680 if (connector == null)
681 {
682 if (endpointUri.getConnectorName() != null)
683 {
684 connector = MuleManager.getInstance().lookupConnector(endpointUri.getConnectorName());
685 if (connector == null)
686 {
687 throw new IllegalArgumentException("Connector not found: "
688 + endpointUri.getConnectorName());
689 }
690 }
691 else
692 {
693 try
694 {
695 connector = TransportFactory.getOrCreateConnectorByProtocol(this);
696 if (connector == null)
697 {
698 throw new InitialisationException(
699 CoreMessages.connectorWithProtocolNotRegistered(endpointUri.getScheme()), this);
700 }
701 }
702 catch (TransportFactoryException e)
703 {
704 throw new InitialisationException(
705 CoreMessages.failedToCreateConnectorFromUri(endpointUri), e, this);
706 }
707 }
708
709 if (endpointUri.getEndpointName() != null && name == null)
710 {
711 name = endpointUri.getEndpointName();
712 }
713 }
714 name = ObjectNameHelper.getEndpointName(this);
715
716 String sync = endpointUri.getParams().getProperty("synchronous", null);
717 if (sync != null)
718 {
719 synchronous = Boolean.valueOf(sync);
720 }
721 if (properties != null && endpointUri.getParams() != null)
722 {
723 properties.putAll(endpointUri.getParams());
724 }
725
726 if (endpointUri.getTransformers() != null)
727 {
728 try
729 {
730 transformer = MuleObjectHelper.getTransformer(endpointUri.getTransformers(), ",");
731 }
732 catch (MuleException e)
733 {
734 throw new InitialisationException(e, this);
735 }
736 }
737
738 if (transformer == null)
739 {
740 if (connector instanceof AbstractConnector)
741 {
742 if (UMOEndpoint.ENDPOINT_TYPE_SENDER.equals(type))
743 {
744 transformer = ((AbstractConnector) connector).getDefaultOutboundTransformer();
745 }
746 else if (UMOEndpoint.ENDPOINT_TYPE_SENDER_AND_RECEIVER.equals(type))
747 {
748 transformer = ((AbstractConnector) connector).getDefaultOutboundTransformer();
749 responseTransformer = ((AbstractConnector) connector).getDefaultInboundTransformer();
750 }
751 else
752 {
753 transformer = ((AbstractConnector) connector).getDefaultInboundTransformer();
754 }
755 }
756 }
757 if (transformer != null)
758 {
759 transformer.setEndpoint(this);
760 }
761
762 if (endpointUri.getResponseTransformers() != null)
763 {
764 try
765 {
766 responseTransformer =
767 MuleObjectHelper.getTransformer(endpointUri.getResponseTransformers(), ",");
768 }
769 catch (MuleException e)
770 {
771 throw new InitialisationException(e, this);
772 }
773 }
774 if (responseTransformer == null)
775 {
776 if (connector instanceof AbstractConnector)
777 {
778 responseTransformer = ((AbstractConnector) connector).getDefaultResponseTransformer();
779 }
780 }
781 if (responseTransformer != null)
782 {
783 responseTransformer.setEndpoint(this);
784 }
785
786 if (securityFilter != null)
787 {
788 securityFilter.setEndpoint(this);
789 securityFilter.initialise();
790 }
791
792
793 String rs = (String) endpointUri.getParams().remove("remoteSync");
794 if (rs != null)
795 {
796 remoteSync = Boolean.valueOf(rs);
797 }
798
799 String rsTimeout = (String) endpointUri.getParams().remove("remoteSyncTimeout");
800 if (rsTimeout != null)
801 {
802 remoteSyncTimeout = Integer.valueOf(rsTimeout);
803 }
804
805 initialised.set(true);
806 }
807
808
809
810
811
812
813
814
815
816 public UMOEndpointSecurityFilter getSecurityFilter()
817 {
818 return securityFilter;
819 }
820
821
822
823
824
825
826
827
828
829 public boolean isSynchronous()
830 {
831 if (synchronous == null)
832 {
833 return MuleManager.getConfiguration().isSynchronous();
834 }
835 return synchronous.booleanValue();
836 }
837
838 public boolean isSynchronousSet()
839 {
840 return (synchronous != null);
841 }
842
843 public int getCreateConnector()
844 {
845 return createConnector;
846 }
847
848
849
850
851
852
853
854
855
856 public boolean isRemoteSync()
857 {
858 if (remoteSync == null)
859 {
860 if (connector == null || connector.isRemoteSyncEnabled())
861 {
862 remoteSync = Boolean.valueOf(MuleManager.getConfiguration().isRemoteSync());
863 }
864 else
865 {
866 remoteSync = Boolean.FALSE;
867 }
868 }
869 return remoteSync.booleanValue();
870 }
871
872
873
874
875
876
877 public int getRemoteSyncTimeout()
878 {
879 if (remoteSyncTimeout == null)
880 {
881 remoteSyncTimeout = new Integer(MuleManager.getConfiguration().getSynchronousEventTimeout());
882 }
883 return remoteSyncTimeout.intValue();
884 }
885
886
887
888
889
890
891
892 public String getInitialState()
893 {
894 return initialState;
895 }
896
897 public UMOTransformer getResponseTransformer()
898 {
899 return responseTransformer;
900 }
901
902
903
904
905
906
907 public boolean isStreaming()
908 {
909 return streaming;
910 }
911
912 public Object getProperty(Object key)
913 {
914 Object value = properties.get(key);
915 if (value == null)
916 {
917 value = endpointUri.getParams().get(key);
918 }
919 return value;
920 }
921
922
923
924
925 public void dispatch(UMOEvent event) throws DispatchException
926 {
927 if (connector != null)
928 {
929 connector.dispatch(this, event);
930 }
931 else
932 {
933
934 throw new IllegalStateException("The connector on the endpoint: " + toString() + "is null. Please contact dev@mule.codehaus.org");
935 }
936 }
937
938 public UMOMessage receive(long timeout) throws Exception
939 {
940 if (connector != null)
941 {
942 return connector.receive(this, timeout);
943 }
944 else
945 {
946
947 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 if (connector != null)
954 {
955 return connector.send(this, event);
956 }
957 else
958 {
959
960 throw new IllegalStateException("The connector on the endpoint: " + toString() + "is null. Please contact dev@mule.codehaus.org");
961 }
962 }
963
964 }