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