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: DestinationBinder.java,v 1.2 2005/11/12 10:49:48 tanderson Exp $
44 */
45 package org.exolab.jms.messagemgr;
46
47 import java.util.Iterator;
48 import java.util.List;
49 import javax.jms.JMSException;
50 import javax.naming.Context;
51 import javax.naming.NamingException;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55
56 import org.exolab.jms.client.JmsDestination;
57 import org.exolab.jms.server.NameService;
58 import org.exolab.jms.service.Service;
59 import org.exolab.jms.service.ServiceException;
60
61
62 /***
63 * Binds persistent destinations in JNDI.
64 *
65 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
66 * @version $Revision: 1.2 $ $Date: 2005/11/12 10:49:48 $
67 */
68 public class DestinationBinder extends Service
69 implements DestinationEventListener {
70
71 /***
72 * The destination manager.
73 */
74 private final DestinationManager _destinations;
75
76 /***
77 * The name service.
78 */
79 private final NameService _names;
80
81 /***
82 * The logger.
83 */
84 private static final Log _log = LogFactory.getLog(DestinationBinder.class);
85
86
87 /***
88 * Construct a new <code>DestinationBinder</code>.
89 *
90 * @param destinations the destination manager
91 * @param names the name service
92 */
93 public DestinationBinder(DestinationManager destinations,
94 NameService names) {
95 if (destinations == null) {
96 throw new IllegalArgumentException(
97 "Argument 'destinations' is null");
98 }
99 if (names == null) {
100 throw new IllegalArgumentException("Argument 'names' is null");
101 }
102 _destinations = destinations;
103 _names = names;
104 }
105
106 /***
107 * Invoked when a destination is created.
108 *
109 * @param destination the destination that was added
110 */
111 public void destinationAdded(JmsDestination destination)
112 throws JMSException {
113 if (destination.getPersistent()) {
114 try {
115 Context context = _names.getInitialContext();
116 ContextHelper.rebind(context, destination.getName(),
117 destination);
118 } catch (NamingException exception) {
119 final String msg = "Failed to add destination "
120 + destination.getName() + " to JNDI context";
121 _log.error(msg, exception);
122 throw new JMSException(msg + ": " + exception.getMessage());
123 }
124 }
125 }
126
127 /***
128 * Invoked when a destination is removed.
129 *
130 * @param destination the destination that was removed
131 */
132 public void destinationRemoved(JmsDestination destination) {
133 if (destination.getPersistent()) {
134 try {
135 Context context = _names.getInitialContext();
136 context.unbind(destination.getName());
137 } catch (NamingException error) {
138 _log.error("Failed to remove destination "
139 + destination.getName() + " from JNDI", error);
140 }
141 }
142 }
143
144 /***
145 * Invoked when a message cache is created.
146 *
147 * @param destination the destination that messages are being cached for
148 * @param cache the corresponding cache
149 */
150 public void cacheAdded(JmsDestination destination, DestinationCache cache) {
151
152 }
153
154 /***
155 * Invoked when a message cache is removed.
156 *
157 * @param destination the destination that messages are no longer being
158 * cached for
159 * @param cache the corresponding cache
160 */
161 public void cacheRemoved(JmsDestination destination,
162 DestinationCache cache) {
163
164 }
165
166 /***
167 * Start the service.
168 *
169 * @throws ServiceException if the service fails to start
170 */
171 protected void doStart() throws ServiceException {
172
173 Context context;
174 try {
175 context = _names.getInitialContext();
176 } catch (NamingException exception) {
177 throw new ServiceException("Failed to get initial JNDI context",
178 exception);
179 }
180
181 List destinations;
182 try {
183 destinations = _destinations.getDestinations();
184 } catch (JMSException exception) {
185 throw new ServiceException("Failed to get destinations",
186 exception);
187 }
188
189 for (Iterator i = destinations.iterator(); i.hasNext();) {
190 JmsDestination destination = (JmsDestination) i.next();
191 if (destination.getPersistent()) {
192 try {
193 ContextHelper.rebind(context, destination.getName(),
194 destination);
195 } catch (NamingException exception) {
196 throw new ServiceException("Failed to add destination "
197 + destination.getName()
198 + " to JNDI", exception);
199 }
200 }
201 }
202
203
204 _destinations.addDestinationEventListener(this);
205 }
206
207 /***
208 * Stop the service.
209 *
210 * @throws ServiceException if the service fails to stop
211 */
212 protected void doStop() throws ServiceException {
213 _destinations.removeDestinationEventListener(this);
214 }
215
216 }