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 java.util.ArrayList;
020 import java.util.List;
021
022 import javax.jbi.JBIException;
023 import javax.jbi.component.ComponentContext;
024 import javax.jbi.component.ComponentLifeCycle;
025 import javax.jbi.messaging.DeliveryChannel;
026 import javax.jbi.messaging.InOnly;
027 import javax.jbi.messaging.MessagingException;
028 import javax.jbi.messaging.NormalizedMessage;
029 import javax.jbi.servicedesc.ServiceEndpoint;
030 import javax.xml.namespace.QName;
031
032 import org.apache.commons.logging.Log;
033 import org.apache.commons.logging.LogFactory;
034 import org.apache.servicemix.components.util.PojoSupport;
035 import org.apache.servicemix.jbi.jaxp.StringSource;
036
037 /**
038 * @version $Revision: 564607 $
039 */
040 public class SenderPojo extends PojoSupport implements ComponentLifeCycle, Sender {
041
042 public static final QName SERVICE = new QName("http://servicemix.org/example/", "sender");
043
044 public static final String ENDPOINT = "sender";
045
046 private static final Log LOG = LogFactory.getLog(SenderPojo.class);
047
048 protected QName interfaceName;
049 protected int numMessages = 10;
050 protected ComponentContext context;
051 protected List messages = new ArrayList();
052 protected boolean done;
053
054 public SenderPojo() {
055 this(ReceiverPojo.SERVICE);
056 }
057
058 public SenderPojo(QName interfaceName) {
059 this.interfaceName = interfaceName;
060 }
061
062 public void init(ComponentContext ctx) throws JBIException {
063 this.context = ctx;
064 ctx.activateEndpoint(SERVICE, ENDPOINT);
065 }
066
067 public int messagesReceived() {
068 return messages.size();
069 }
070
071 public void sendMesssages() throws MessagingException {
072 sendMessages(numMessages);
073 }
074
075 public void sendMessages(int messageCount) throws MessagingException {
076 sendMessages(messageCount, true);
077 }
078
079 public void sendMessages(int messageCount, boolean sync) throws MessagingException {
080 LOG.info("Looking for services for interface: " + interfaceName);
081
082 ServiceEndpoint[] endpoints = context.getEndpointsForService(interfaceName);
083 if (endpoints.length > 0) {
084 ServiceEndpoint endpoint = endpoints[0];
085 LOG.info("Sending to endpoint: " + endpoint);
086
087 for (int i = 0; i < messageCount; i++) {
088 InOnly exchange = context.getDeliveryChannel().createExchangeFactory().createInOnlyExchange();
089 NormalizedMessage message = exchange.createMessage();
090 exchange.setEndpoint(endpoint);
091 exchange.setInMessage(message);
092 // lets set the XML as a byte[], String or DOM etc
093 String xml = "<s12:Envelope xmlns:s12='http://www.w3.org/2003/05/soap-envelope'>"
094 + " <s12:Body><foo>Hello!</foo> </s12:Body>"
095 + "</s12:Envelope>";
096 message.setContent(new StringSource(xml));
097 LOG.info("sending message: " + i);
098 DeliveryChannel deliveryChannel = context.getDeliveryChannel();
099 LOG.info("sync send on deliverychannel: " + deliveryChannel);
100 if (sync) {
101 deliveryChannel.sendSync(exchange);
102 } else {
103 deliveryChannel.send(exchange);
104 }
105 }
106 } else {
107 LOG.warn("No endpoints available for interface: " + interfaceName);
108 }
109 }
110
111 // Properties
112 // -------------------------------------------------------------------------
113 public QName getInterfaceName() {
114 return interfaceName;
115 }
116
117 public void setInterfaceName(QName interfaceName) {
118 this.interfaceName = interfaceName;
119 }
120
121 public int getNumMessages() {
122 return numMessages;
123 }
124
125 public void setNumMessages(int numMessages) {
126 this.numMessages = numMessages;
127 }
128 }