001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jms.support.destination; 018 019import javax.jms.Destination; 020import javax.jms.JMSException; 021import javax.jms.Session; 022 023import org.springframework.beans.BeansException; 024import org.springframework.beans.factory.BeanFactory; 025import org.springframework.beans.factory.BeanFactoryAware; 026import org.springframework.util.Assert; 027 028/** 029 * {@link DestinationResolver} implementation based on a Spring {@link BeanFactory}. 030 * 031 * <p>Will lookup Spring managed beans identified by bean name, 032 * expecting them to be of type {@code javax.jms.Destination}. 033 * 034 * @author Juergen Hoeller 035 * @since 2.5 036 * @see org.springframework.beans.factory.BeanFactory 037 */ 038public class BeanFactoryDestinationResolver implements DestinationResolver, BeanFactoryAware { 039 040 private BeanFactory beanFactory; 041 042 043 /** 044 * Create a new instance of the {@link BeanFactoryDestinationResolver} class. 045 * <p>The BeanFactory to access must be set via {@code setBeanFactory}. 046 * @see #setBeanFactory 047 */ 048 public BeanFactoryDestinationResolver() { 049 } 050 051 /** 052 * Create a new instance of the {@link BeanFactoryDestinationResolver} class. 053 * <p>Use of this constructor is redundant if this object is being created 054 * by a Spring IoC container, as the supplied {@link BeanFactory} will be 055 * replaced by the {@link BeanFactory} that creates it (c.f. the 056 * {@link BeanFactoryAware} contract). So only use this constructor if you 057 * are using this class outside the context of a Spring IoC container. 058 * @param beanFactory the bean factory to be used to lookup {@link javax.jms.Destination Destinatiosn} 059 */ 060 public BeanFactoryDestinationResolver(BeanFactory beanFactory) { 061 Assert.notNull(beanFactory, "BeanFactory is required"); 062 this.beanFactory = beanFactory; 063 } 064 065 066 @Override 067 public void setBeanFactory(BeanFactory beanFactory) { 068 this.beanFactory = beanFactory; 069 } 070 071 072 @Override 073 public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) 074 throws JMSException { 075 076 Assert.state(this.beanFactory != null, "BeanFactory is required"); 077 try { 078 return this.beanFactory.getBean(destinationName, Destination.class); 079 } 080 catch (BeansException ex) { 081 throw new DestinationResolutionException( 082 "Failed to look up Destination bean with name '" + destinationName + "'", ex); 083 } 084 } 085 086}