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 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: JmsAdminConnectionImpl.java,v 1.4 2006/05/24 07:59:19 tanderson Exp $
44 */
45 package org.exolab.jms.administration.net;
46
47 import org.exolab.jms.administration.AdminConnection;
48 import org.exolab.jms.administration.JmsAdminServerIfc;
49 import org.exolab.jms.client.net.SharedORB;
50 import org.exolab.jms.net.orb.ORB;
51 import org.exolab.jms.net.proxy.Proxy;
52 import org.exolab.jms.net.registry.Registry;
53 import org.exolab.jms.server.net.RemoteJmsAdminConnectionIfc;
54 import org.exolab.jms.server.net.RemoteJmsAdminServerIfc;
55
56 import javax.jms.JMSException;
57 import java.rmi.NotBoundException;
58 import java.rmi.RemoteException;
59 import java.util.HashMap;
60 import java.util.Map;
61 import java.util.Vector;
62
63
64 /***
65 * This class is repsonsible for an admin connection to the server
66 *
67 * @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
68 * @version $Revision: 1.4 $ $Date: 2006/05/24 07:59:19 $
69 * @see org.exolab.jms.administration.AdminConnectionFactory
70 */
71 public class JmsAdminConnectionImpl
72 implements JmsAdminServerIfc, AdminConnection {
73
74 /***
75 * The admin connection.
76 */
77 private RemoteJmsAdminConnectionIfc _connection;
78
79
80 /***
81 * Construct a new <code>JmsAdminConnectionImpl</code>
82 *
83 * @param url the server URI
84 * @param username the client's username
85 * @param password the client's password
86 */
87 public JmsAdminConnectionImpl(String url, String username, String password)
88 throws JMSException {
89 Map properties = new HashMap();
90 properties.put(ORB.PROVIDER_URI, url);
91 if (username != null) {
92 properties.put(ORB.SECURITY_PRINCIPAL, username);
93 }
94 if (password != null) {
95 properties.put(ORB.SECURITY_CREDENTIALS, password);
96 }
97
98 Registry registry;
99 try {
100 ORB orb = SharedORB.getInstance();
101 registry = orb.getRegistry(properties);
102 } catch (RemoteException exception) {
103 JMSException error = new JMSException(
104 "Failed to get registry service for URL: " + url);
105 error.setLinkedException(exception);
106 throw error;
107 }
108
109 RemoteJmsAdminServerIfc admin = null;
110 try {
111 admin = (RemoteJmsAdminServerIfc) registry.lookup("admin");
112 _connection = admin.createConnection(username, password);
113 } catch (NotBoundException exception) {
114 throw new JMSException("Administration server is not bound in the registry for "
115 + "URL: " + url);
116 } catch (RemoteException exception) {
117 JMSException error = new JMSException("Failed to lookup OpenJMS administration server at URL: "
118 + url);
119 error.setLinkedException(exception);
120 throw error;
121 } finally {
122 if (admin instanceof Proxy) {
123 ((Proxy) admin).disposeProxy();
124 }
125 if (registry instanceof Proxy) {
126 ((Proxy) registry).disposeProxy();
127 }
128 }
129 }
130
131
132 public boolean addDurableConsumer(String topic, String name)
133 throws JMSException {
134 boolean result = false;
135 try {
136 result = _connection.addDurableConsumer(topic, name);
137 } catch (Exception exception) {
138 raise(exception);
139 }
140 return result;
141 }
142
143
144 public boolean removeDurableConsumer(String name) throws JMSException {
145 boolean result = false;
146 try {
147 result = _connection.removeDurableConsumer(name);
148 } catch (Exception exception) {
149 raise(exception);
150 }
151 return result;
152 }
153
154
155 public boolean durableConsumerExists(String name) throws JMSException {
156 boolean result = false;
157 try {
158 result = _connection.durableConsumerExists(name);
159 } catch (Exception exception) {
160 raise(exception);
161 }
162 return result;
163 }
164
165
166 public Vector getDurableConsumers(String topic) throws JMSException {
167 Vector result = null;
168 try {
169 result = _connection.getDurableConsumers(topic);
170 } catch (Exception exception) {
171 raise(exception);
172 }
173 return result;
174 }
175
176
177 public boolean unregisterConsumer(String name) throws JMSException {
178 boolean result = false;
179 try {
180 result = _connection.unregisterConsumer(name);
181 } catch (Exception exception) {
182 raise(exception);
183 }
184 return result;
185 }
186
187
188 public boolean isConnected(String name) throws JMSException {
189 boolean result = false;
190 try {
191 result = _connection.isConnected(name);
192 } catch (Exception exception) {
193 raise(exception);
194 }
195 return result;
196 }
197
198
199 public boolean addDestination(String destination, Boolean queue)
200 throws JMSException {
201 boolean result = false;
202 try {
203 result = _connection.addDestination(destination, queue);
204 } catch (Exception exception) {
205 raise(exception);
206 }
207 return result;
208 }
209
210
211 public boolean removeDestination(String name) throws JMSException {
212 boolean result = false;
213 try {
214 result = _connection.removeDestination(name);
215 } catch (Exception exception) {
216 raise(exception);
217 }
218 return result;
219 }
220
221
222 public boolean destinationExists(String name) throws JMSException {
223 boolean result = false;
224 try {
225 result = _connection.destinationExists(name);
226 } catch (Exception exception) {
227 raise(exception);
228 }
229 return result;
230 }
231
232
233 public Vector getAllDestinations() throws JMSException {
234 Vector result = null;
235 try {
236 result = _connection.getAllDestinations();
237 } catch (Exception exception) {
238 raise(exception);
239 }
240 return result;
241 }
242
243
244 public int getDurableConsumerMessageCount(String topic, String name)
245 throws JMSException {
246 int result = 0;
247 try {
248 result = _connection.getDurableConsumerMessageCount(topic, name);
249 } catch (Exception exception) {
250 raise(exception);
251 }
252 return result;
253 }
254
255
256 public int getQueueMessageCount(String queue) throws JMSException {
257 int result = 0;
258 try {
259 result = _connection.getQueueMessageCount(queue);
260 } catch (Exception exception) {
261 raise(exception);
262 }
263 return result;
264 }
265
266
267 public int purgeMessages() throws JMSException {
268 int result = 0;
269 try {
270 result = _connection.purgeMessages();
271 } catch (Exception exception) {
272 raise(exception);
273 }
274 return result;
275 }
276
277
278 public void stopServer() throws JMSException {
279 try {
280 _connection.stopServer();
281 } catch (Exception exception) {
282 raise(exception);
283 }
284 }
285
286
287 public void close() {
288 if (_connection instanceof Proxy) {
289 ((Proxy) _connection).disposeProxy();
290 _connection = null;
291 }
292 }
293
294
295 public boolean addUser(String username, String password)
296 throws JMSException {
297 boolean result = false;
298 try {
299 result = _connection.addUser(username, password);
300 } catch (Exception exception) {
301 raise(exception);
302 }
303 return result;
304 }
305
306
307 public Vector getAllUsers() throws JMSException {
308 Vector result = null;
309 try {
310 result = _connection.getAllUsers();
311 } catch (Exception exception) {
312 raise(exception);
313 }
314 return result;
315 }
316
317
318 public boolean removeUser(String username)
319 throws JMSException {
320 boolean result = false;
321 try {
322 result = _connection.removeUser(username);
323 } catch (Exception exception) {
324 raise(exception);
325 }
326 return result;
327 }
328
329
330 public boolean changePassword(String username, String password)
331 throws JMSException {
332 boolean result = false;
333 try {
334 result = _connection.changePassword(username, password);
335 } catch (Exception exception) {
336 raise(exception);
337 }
338 return result;
339 }
340
341 private void raise(Exception exception) throws JMSException {
342 if (exception instanceof JMSException) {
343 throw (JMSException) exception;
344 } else {
345 JMSException error = new JMSException(exception.getMessage());
346 error.setLinkedException(exception);
347 throw error;
348 }
349 }
350
351 }