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.i18n.Msg;
027import ca.uhn.fhir.rest.server.messaging.IMessage;
028import org.springframework.messaging.Message;
029import org.springframework.messaging.support.ChannelInterceptor;
030
031/**
032 * Adapt a Spring Messaging (JMS) Queue to {@link IChannelProducer}
033 *
034 * @param <T> the type of payload this message producer is expecting to send
035 */
036public class SpringMessagingProducerAdapter<T> implements IChannelProducer<T> {
037        private final Class<? extends IMessage<T>> myMessageType;
038        private final ISpringMessagingChannelProducer mySpringMessagingChannelProducer;
039
040        public SpringMessagingProducerAdapter(
041                        Class<? extends IMessage<T>> theMessageType,
042                        ISpringMessagingChannelProducer theSpringMessagingChannelProducer) {
043                myMessageType = theMessageType;
044                mySpringMessagingChannelProducer = theSpringMessagingChannelProducer;
045        }
046
047        @Override
048        public String getChannelName() {
049                return "unknown legacy channel name";
050        }
051
052        @Override
053        public ISendResult send(IMessage<T> theMessage) {
054                if (!myMessageType.isAssignableFrom(theMessage.getClass())) {
055                        throw new ConfigurationException(Msg.code(2665) + "Expecting message of type " + myMessageType
056                                        + ". But received message of type: " + theMessage.getClass());
057                }
058
059                if (!Message.class.isAssignableFrom(theMessage.getClass())) {
060                        throw new ConfigurationException(Msg.code(2664) + "Expecting message of type " + Message.class
061                                        + ". But received message of type: " + theMessage.getClass());
062                }
063                Message<?> message = (Message<?>) theMessage;
064
065                return new SpringMessagingSendResult(mySpringMessagingChannelProducer.send(message));
066        }
067
068        public void addInterceptor(ChannelInterceptor theInterceptor) {
069                mySpringMessagingChannelProducer.addInterceptor(theInterceptor);
070        }
071
072        public ISpringMessagingChannelProducer getSpringMessagingProducer() {
073                return mySpringMessagingChannelProducer;
074        }
075
076        public Class<? extends IMessage<T>> getMessageType() {
077                return myMessageType;
078        }
079}