01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.jms.support.destination;
18:
19: import javax.jms.Destination;
20: import javax.jms.JMSException;
21: import javax.jms.Session;
22:
23: import org.springframework.beans.BeansException;
24: import org.springframework.beans.factory.BeanFactory;
25: import org.springframework.beans.factory.BeanFactoryAware;
26: import org.springframework.util.Assert;
27:
28: /**
29: * {@link DestinationResolver} implementation based on a Spring {@link BeanFactory}.
30: *
31: * <p>Will lookup Spring managed beans identified by bean name,
32: * expecting them to be of type <code>javax.jms.Destination</code>.
33: *
34: * @author Juergen Hoeller
35: * @since 2.5
36: * @see org.springframework.beans.factory.BeanFactory
37: */
38: public class BeanFactoryDestinationResolver implements
39: DestinationResolver, BeanFactoryAware {
40:
41: private BeanFactory beanFactory;
42:
43: /**
44: * Create a new instance of the {@link BeanFactoryDestinationResolver} class.
45: * <p>The BeanFactory to access must be set via <code>setBeanFactory</code>.
46: * @see #setBeanFactory
47: */
48: public BeanFactoryDestinationResolver() {
49: }
50:
51: /**
52: * Create a new instance of the {@link BeanFactoryDestinationResolver} class.
53: * <p>Use of this constructor is redundant if this object is being created
54: * by a Spring IoC container, as the supplied {@link BeanFactory} will be
55: * replaced by the {@link BeanFactory} that creates it (c.f. the
56: * {@link BeanFactoryAware} contract). So only use this constructor if you
57: * are using this class outside the context of a Spring IoC container.
58: * @param beanFactory the bean factory to be used to lookup {@link javax.jms.Destination Destinatiosn}
59: */
60: public BeanFactoryDestinationResolver(BeanFactory beanFactory) {
61: Assert.notNull(beanFactory, "BeanFactory is required");
62: this .beanFactory = beanFactory;
63: }
64:
65: public void setBeanFactory(BeanFactory beanFactory) {
66: this .beanFactory = beanFactory;
67: }
68:
69: public Destination resolveDestinationName(Session session,
70: String destinationName, boolean pubSubDomain)
71: throws JMSException {
72:
73: Assert.state(this .beanFactory != null,
74: "BeanFactory is required");
75: try {
76: return (Destination) this .beanFactory.getBean(
77: destinationName, Destination.class);
78: } catch (BeansException ex) {
79: throw new DestinationResolutionException(
80: "Failed to look up Destinaton bean with name '"
81: + destinationName + "'", ex);
82: }
83: }
84:
85: }
|