001/*-
002 * #%L
003 * HAPI FHIR Storage api
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.broker.jms;
021
022import ca.uhn.fhir.broker.api.IChannelProducer;
023import ca.uhn.fhir.broker.api.ISendResult;
024import ca.uhn.fhir.broker.impl.SpringMessagingSendResult;
025import ca.uhn.fhir.context.ConfigurationException;
026import ca.uhn.fhir.rest.server.messaging.IMessage;
027import org.springframework.messaging.Message;
028import org.springframework.messaging.support.ChannelInterceptor;
029
030/**
031 * Adapt a Spring Messaging (JMS) Queue to {@link IChannelProducer}
032 *
033 * @param <T> the type of payload this message producer is expecting to send
034 */
035public class SpringMessagingProducerAdapter<T> implements IChannelProducer<T> {
036        private final Class<? extends IMessage<T>> myMessageType;
037        private final ISpringMessagingChannelProducer mySpringMessagingChannelProducer;
038
039        public SpringMessagingProducerAdapter(
040                        Class<? extends IMessage<T>> theMessageType,
041                        ISpringMessagingChannelProducer theSpringMessagingChannelProducer) {
042                myMessageType = theMessageType;
043                mySpringMessagingChannelProducer = theSpringMessagingChannelProducer;
044        }
045
046        @Override
047        public String getChannelName() {
048                return "unknown legacy channel name";
049        }
050
051        @Override
052        public ISendResult send(IMessage<T> theMessage) {
053                if (!myMessageType.isAssignableFrom(theMessage.getClass())) {
054                        throw new ConfigurationException("Expecting message of type " + myMessageType
055                                        + ". But received message of type: " + theMessage.getClass());
056                }
057
058                if (!Message.class.isAssignableFrom(theMessage.getClass())) {
059                        throw new ConfigurationException("Expecting message of type " + Message.class
060                                        + ". But received message of type: " + theMessage.getClass());
061                }
062                Message<?> message = (Message<?>) theMessage;
063
064                return new SpringMessagingSendResult(mySpringMessagingChannelProducer.send(message));
065        }
066
067        public void addInterceptor(ChannelInterceptor theInterceptor) {
068                mySpringMessagingChannelProducer.addInterceptor(theInterceptor);
069        }
070
071        public ISpringMessagingChannelProducer getSpringMessagingProducer() {
072                return mySpringMessagingChannelProducer;
073        }
074
075        public Class<? extends IMessage<T>> getMessageType() {
076                return myMessageType;
077        }
078}