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 */
017package org.apache.activemq.junit;
018
019import java.io.Serializable;
020import java.net.URI;
021import java.util.Map;
022import javax.jms.BytesMessage;
023import javax.jms.JMSException;
024import javax.jms.MapMessage;
025import javax.jms.Message;
026import javax.jms.MessageProducer;
027import javax.jms.ObjectMessage;
028import javax.jms.TextMessage;
029
030import org.apache.activemq.ActiveMQConnectionFactory;
031
032public abstract class AbstractActiveMQProducerResource extends AbstractActiveMQClientResource {
033    MessageProducer producer;
034
035    public AbstractActiveMQProducerResource(ActiveMQConnectionFactory connectionFactory) {
036        super(connectionFactory);
037    }
038
039    public AbstractActiveMQProducerResource(URI brokerURI) {
040        super(brokerURI);
041    }
042
043    public AbstractActiveMQProducerResource(EmbeddedActiveMQBroker embeddedActiveMQBroker) {
044        super(embeddedActiveMQBroker);
045    }
046
047    public AbstractActiveMQProducerResource(URI brokerURI, String userName, String password) {
048        super(brokerURI, userName, password);
049    }
050
051    public AbstractActiveMQProducerResource(String destinationName, ActiveMQConnectionFactory connectionFactory) {
052        super(destinationName, connectionFactory);
053    }
054
055    public AbstractActiveMQProducerResource(String destinationName, URI brokerURI) {
056        super(destinationName, brokerURI);
057    }
058
059    public AbstractActiveMQProducerResource(String destinationName, EmbeddedActiveMQBroker embeddedActiveMQBroker) {
060        super(destinationName, embeddedActiveMQBroker);
061    }
062
063    public AbstractActiveMQProducerResource(String destinationName, URI brokerURI, String userName, String password) {
064        super(destinationName, brokerURI, userName, password);
065    }
066
067    @Override
068    public String getDestinationName() {
069        try {
070            if (producer != null && producer.getDestination() != null) {
071                return producer.getDestination().toString();
072            }
073        } catch (JMSException e) {
074            // eat this
075        }
076
077        return null;
078    }
079
080    public void sendMessage(Message message) throws JMSException {
081        producer.send(message);
082    }
083
084    public BytesMessage sendMessage(byte[] body) throws JMSException {
085        BytesMessage message = this.createMessage(body);
086        sendMessage(message);
087        return message;
088    }
089
090    public TextMessage sendMessage(String body) throws JMSException {
091        TextMessage message = this.createMessage(body);
092        sendMessage(message);
093        return message;
094    }
095
096    public MapMessage sendMessage(Map<String, Object> body) throws JMSException {
097        MapMessage message = this.createMessage(body);
098        sendMessage(message);
099        return message;
100    }
101
102    public ObjectMessage sendMessage(Serializable body) throws JMSException {
103        ObjectMessage message = this.createMessage(body);
104        sendMessage(message);
105        return message;
106    }
107
108    public BytesMessage sendMessageWithProperties(byte[] body, Map<String, Object> properties) throws JMSException {
109        BytesMessage message = this.createMessage(body, properties);
110        sendMessage(message);
111        return message;
112    }
113
114    public TextMessage sendMessageWithProperties(String body, Map<String, Object> properties) throws JMSException {
115        TextMessage message = this.createMessage(body, properties);
116        sendMessage(message);
117        return message;
118    }
119
120    public MapMessage sendMessageWithProperties(Map<String, Object> body, Map<String, Object> properties) throws JMSException {
121        MapMessage message = this.createMessage(body, properties);
122        sendMessage(message);
123        return message;
124    }
125
126    public ObjectMessage sendMessageWithProperties(Serializable body, Map<String, Object> properties) throws JMSException {
127        ObjectMessage message = this.createMessage(body, properties);
128        sendMessage(message);
129        return message;
130    }
131
132}