1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: DestinationConfigurator.java,v 1.1 2005/08/30 07:26:49 tanderson Exp $
44 */
45 package org.exolab.jms.messagemgr;
46
47 import javax.jms.JMSException;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51
52 import org.exolab.jms.client.JmsQueue;
53 import org.exolab.jms.client.JmsTopic;
54 import org.exolab.jms.config.AdministeredDestinations;
55 import org.exolab.jms.config.AdministeredQueue;
56 import org.exolab.jms.config.AdministeredTopic;
57 import org.exolab.jms.config.Configuration;
58 import org.exolab.jms.config.Subscriber;
59 import org.exolab.jms.service.Service;
60 import org.exolab.jms.service.ServiceException;
61
62
63 /***
64 * Pre-configures {@link DestinationManager} with destinations and and
65 * {@link ConsumerManager} with subscribers from an {@link Configuration}.
66 *
67 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
68 * @version $Revision: 1.1 $ $Date: 2005/08/30 07:26:49 $
69 */
70 public class DestinationConfigurator extends Service {
71
72 /***
73 * The configuration to use.
74 */
75 private final Configuration _config;
76
77 /***
78 * The destination manager.
79 */
80 private final DestinationManager _destinations;
81
82 /***
83 * The consumer manager.
84 */
85 private final ConsumerManager _consumers;
86
87 /***
88 * The logger.
89 */
90 private static final Log _log
91 = LogFactory.getLog(DestinationConfigurator.class);
92
93
94 /***
95 * Create a new <code>DestinationConfigurator</code>.
96 *
97 * @param config the configuration to use
98 * @param destinations the destination manager
99 * @param consumers the consumer manaager
100 */
101 public DestinationConfigurator(Configuration config,
102 DestinationManager destinations,
103 ConsumerManager consumers) {
104 if (config == null) {
105 throw new IllegalArgumentException("Argument 'config' is null");
106 }
107 if (destinations == null) {
108 throw new IllegalArgumentException(
109 "Argument 'destinations' is null");
110 }
111 if (consumers == null) {
112 throw new IllegalArgumentException("Argument 'consumers' is null");
113 }
114 _config = config;
115 _destinations = destinations;
116 _consumers = consumers;
117 }
118
119
120 /***
121 * Start the service.
122 *
123 * @throws ServiceException if the service fails to start
124 */
125 protected void doStart() throws ServiceException {
126 AdministeredDestinations destinations
127 = _config.getAdministeredDestinations();
128 if (destinations != null) {
129 configureTopics(destinations.getAdministeredTopic());
130 configureQueues(destinations.getAdministeredQueue());
131 }
132
133 }
134
135 /***
136 * Configure topics.
137 *
138 * @param topics the topics to configure
139 */
140 protected void configureTopics(AdministeredTopic[] topics) {
141 for (int i = 0; i < topics.length; ++i) {
142 final AdministeredTopic topic = topics[i];
143 final String name = topics[i].getName();
144
145 if (_destinations.getDestination(name) == null) {
146 final JmsTopic destination = new JmsTopic(name);
147 destination.setPersistent(true);
148 try {
149 _destinations.createDestination(destination);
150
151
152 int scount = topic.getSubscriberCount();
153 for (int sindex = 0; sindex < scount; sindex++) {
154 Subscriber subscriber = topic.getSubscriber(sindex);
155 _consumers.subscribe(destination,
156 subscriber.getName(), null);
157 }
158 } catch (JMSException exception) {
159 _log.error("Failed to register persistent topic " + name,
160 exception);
161 }
162 }
163 }
164 }
165
166 /***
167 * Configure queues.
168 *
169 * @param queues the queues to configure
170 */
171 protected void configureQueues(AdministeredQueue[] queues) {
172 for (int i = 0; i < queues.length; ++i) {
173 final AdministeredQueue queue = queues[i];
174 final String name = queue.getName();
175
176 if (_destinations.getDestination(name) == null) {
177 JmsQueue destination = new JmsQueue(name);
178 destination.setPersistent(true);
179 try {
180 _destinations.createDestination(destination);
181 } catch (JMSException exception) {
182 _log.error("Failed to register persistent queue " + name,
183 exception);
184 }
185 }
186 }
187 }
188
189 }