001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.tck;
018
019 import javax.jbi.JBIException;
020 import javax.jbi.component.ComponentContext;
021 import javax.jbi.messaging.InOnly;
022 import javax.jbi.messaging.MessagingException;
023 import javax.jbi.messaging.NormalizedMessage;
024 import javax.jbi.servicedesc.ServiceEndpoint;
025 import javax.xml.namespace.QName;
026
027 import org.apache.servicemix.components.util.ComponentSupport;
028 import org.apache.servicemix.jbi.api.EndpointResolver;
029 import org.apache.servicemix.jbi.jaxp.StringSource;
030 import org.apache.servicemix.jbi.resolver.NullEndpointFilter;
031
032 /**
033 * @version $Revision: 690190 $
034 */
035 public class SenderComponent extends ComponentSupport implements Sender {
036
037 public static final QName SERVICE = new QName("http://servicemix.org/example/", "sender");
038 public static final String ENDPOINT = "sender";
039
040 private EndpointResolver resolver;
041 private String message = "<s12:Envelope xmlns:s12='http://www.w3.org/2003/05/soap-envelope'>"
042 + " <s12:Body>"
043 + " <foo>Hello!</foo>"
044 + " </s12:Body>"
045 + "</s12:Envelope>";
046
047 public SenderComponent() {
048 super(SERVICE, ENDPOINT);
049 }
050
051 public EndpointResolver getResolver() {
052 return resolver;
053 }
054
055 public void setResolver(EndpointResolver resolver) {
056 this.resolver = resolver;
057 }
058
059 public void sendMessages(int messageCount) throws JBIException {
060 sendMessages(messageCount, false);
061 }
062
063 public void sendMessages(int messageCount, boolean sync) throws JBIException {
064 ComponentContext context = getContext();
065
066 for (int i = 0; i < messageCount; i++) {
067 InOnly exchange = context.getDeliveryChannel().createExchangeFactory().createInOnlyExchange();
068 NormalizedMessage msg = exchange.createMessage();
069
070 ServiceEndpoint destination = null;
071 if (resolver != null) {
072 destination = resolver.resolveEndpoint(getContext(), exchange, NullEndpointFilter.getInstance());
073 }
074 if (destination != null) {
075 // lets explicitly specify the destination - otherwise
076 // we'll let the container choose for us
077 exchange.setEndpoint(destination);
078 }
079
080 exchange.setInMessage(msg);
081 // lets set the XML as a byte[], String or DOM etc
082 msg.setContent(new StringSource(this.message));
083 if (sync) {
084 boolean result = context.getDeliveryChannel().sendSync(exchange, 1000);
085 if (!result) {
086 throw new MessagingException("Message delivery using sendSync has timed out");
087 }
088 } else {
089 context.getDeliveryChannel().send(exchange);
090 }
091 }
092 }
093
094 public String getMessage() {
095 return message;
096 }
097
098 public void setMessage(String message) {
099 this.message = message;
100 }
101
102 }