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.jaxp.StringSource;
029: import org.apache.servicemix.jbi.resolver.EndpointResolver;
030: import org.apache.servicemix.jbi.resolver.NullEndpointFilter;
031:
032: /**
033: * @version $Revision: 564607 $
034: */
035: public class SenderComponent extends ComponentSupport implements Sender {
036:
037: public static final QName SERVICE = new QName(
038: "http://servicemix.org/example/", "sender");
039: public static final String ENDPOINT = "sender";
040:
041: private EndpointResolver resolver;
042: private String message = "<s12:Envelope xmlns:s12='http://www.w3.org/2003/05/soap-envelope'>"
043: + " <s12:Body>"
044: + " <foo>Hello!</foo>"
045: + " </s12:Body>" + "</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)
064: throws JBIException {
065: ComponentContext context = getContext();
066:
067: for (int i = 0; i < messageCount; i++) {
068: InOnly exchange = context.getDeliveryChannel()
069: .createExchangeFactory().createInOnlyExchange();
070: NormalizedMessage msg = exchange.createMessage();
071:
072: ServiceEndpoint destination = null;
073: if (resolver != null) {
074: destination = resolver.resolveEndpoint(getContext(),
075: exchange, NullEndpointFilter.getInstance());
076: }
077: if (destination != null) {
078: // lets explicitly specify the destination - otherwise
079: // we'll let the container choose for us
080: exchange.setEndpoint(destination);
081: }
082:
083: exchange.setInMessage(msg);
084: // lets set the XML as a byte[], String or DOM etc
085: msg.setContent(new StringSource(this .message));
086: if (sync) {
087: boolean result = context.getDeliveryChannel().sendSync(
088: exchange, 1000);
089: if (!result) {
090: throw new MessagingException(
091: "Message delivery using sendSync has timed out");
092: }
093: } else {
094: context.getDeliveryChannel().send(exchange);
095: }
096: }
097: }
098:
099: public String getMessage() {
100: return message;
101: }
102:
103: public void setMessage(String message) {
104: this.message = message;
105: }
106:
107: }
|