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,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: OnlineConnection.java,v 1.2 2005/08/30 14:04:11 tanderson Exp $
44 *
45 * Date Author Changes
46 * $Date jimm Created
47 */
48
49
50 package org.exolab.jms.tools.admin;
51
52 import java.awt.Component;
53 import java.util.Enumeration;
54 import java.util.Vector;
55
56 import javax.swing.JOptionPane;
57
58 import org.exolab.jms.administration.AdminConnectionFactory;
59 import org.exolab.jms.administration.JmsAdminServerIfc;
60 import org.exolab.jms.config.ConfigHelper;
61 import org.exolab.jms.config.Connector;
62 import org.exolab.jms.config.SecurityConfiguration;
63 import org.exolab.jms.config.Configuration;
64 import org.exolab.jms.config.types.SchemeType;
65
66
67 /***
68 * Connects to the OpenJMSServer for all updates and requests.
69 *
70 * <P>Note: The OpenJMSServer must be active and in a running state for this
71 * type of connection to succeed.
72 *
73 * @version $Revision: 1.2 $ $Date: 2005/08/30 14:04:11 $
74 * @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
75 */
76 public class OnlineConnection extends AbstractAdminConnection {
77
78
79 private JmsAdminServerIfc _admin = null;
80
81
82 private Component _parent;
83
84 /***
85 * Connect to the Admin Server
86 *
87 * @exception OnlineConnectionException When online connection fails.
88 *
89 */
90 public OnlineConnection(Component parent, Configuration config) throws OnlineConnectionException {
91 String username = "anonymous";
92 String password = "anonymous";
93
94 try {
95 if (_instance == null) {
96 Connector connector = config.getConnectors().getConnector(0);
97 SchemeType scheme = connector.getScheme();
98 String url = ConfigHelper.getAdminURL(scheme, config);
99
100 SecurityConfiguration security =
101 config.getSecurityConfiguration();
102 if (security.getSecurityEnabled()) {
103 CreateLogonDialog.instance().displayCreateLogon();
104
105 if (CreateLogonDialog.instance().isConfirmed()) {
106 username = CreateLogonDialog.instance().getName();
107 password = CreateLogonDialog.instance().getPassword();
108 }
109 }
110
111
112 _admin = AdminConnectionFactory.create(url, username, password);
113
114 _parent = parent;
115 _instance = this;
116 } else {
117 throw new OnlineConnectionException("Already connected");
118 }
119 } catch (Exception err) {
120 throw new OnlineConnectionException("Failed to connect: " + err.toString());
121 }
122 }
123
124 /***
125 * Connect to the Admin Server, special constructor to be able to stop the server
126 *
127 * @exception OnlineConnectionException When online connection fails.
128 *
129 */
130 public OnlineConnection(String username, String password, Configuration config) throws OnlineConnectionException {
131 try {
132 if (_instance == null) {
133 Connector connector = config.getConnectors().getConnector(0);
134 SchemeType scheme = connector.getScheme();
135 String url = ConfigHelper.getAdminURL(
136 scheme, config);
137
138
139 _admin = AdminConnectionFactory.create(url, username, password);
140
141 _instance = this;
142 } else {
143 throw new org.exolab.jms.tools.admin.OnlineConnectionException("Already connected");
144 }
145 } catch (Exception err) {
146 throw new org.exolab.jms.tools.admin.OnlineConnectionException("Failed to connect: " + err.toString());
147 }
148 }
149
150 /***
151 * Display the error in a JOptionPane.
152 *
153 * @param err The Error to display.
154 * @param st The string to use as a title on the JOptionPane.
155 *
156 */
157 private void displayError(Exception err, String st) {
158 JOptionPane.showMessageDialog
159 (_parent, st + "\n" + err, st, JOptionPane.ERROR_MESSAGE);
160 }
161
162
163 public void close() {
164 _admin.close();
165 _instance = null;
166 _admin = null;
167 }
168
169
170 public boolean addDurableConsumer(String topic, String name) {
171 try {
172 return _admin.addDurableConsumer(topic, name);
173 } catch (javax.jms.JMSException error) {
174 if (error.getLinkedException() != null) {
175 error.getLinkedException().printStackTrace();
176 }
177 displayError(error, "Failed to add consumer");
178 return false;
179 } catch (Exception err) {
180 displayError(err, "Failed to add consumer " + name);
181 return false;
182 }
183 }
184
185
186 public boolean removeDurableConsumer(String name) {
187 try {
188 return _admin.removeDurableConsumer(name);
189 } catch (Exception err) {
190 displayError(err, "Failed to remove consumer " + name);
191 return false;
192 }
193 }
194
195
196 public boolean unregisterConsumer(String name) {
197 try {
198 return _admin.unregisterConsumer(name);
199 } catch (Exception err) {
200 displayError(err, "Failed to De-Activate consumer " + name);
201 return false;
202 }
203 }
204
205
206 public boolean isConnected(String name) {
207 try {
208 return _admin.isConnected(name);
209 } catch (Exception err) {
210 return false;
211 }
212 }
213
214
215 public Enumeration getAllDestinations() {
216 try {
217 Vector v = _admin.getAllDestinations();
218 Enumeration e = null;
219 if (v != null) {
220 e = v.elements();
221 }
222 return e;
223 } catch (Exception err) {
224 displayError(err, "Failed to getAllQueueTopics");
225 return null;
226 }
227 }
228
229
230 public boolean addDestination(String destination, boolean isQueue) {
231 try {
232 return _admin.addDestination(destination, new Boolean(isQueue));
233 } catch (Exception err) {
234 displayError(err, "Failed to add destination");
235 return false;
236 }
237 }
238
239
240 public int getDurableConsumerMessageCount(String topic, String name) {
241 try {
242 return _admin.getDurableConsumerMessageCount(topic, name);
243 } catch (Exception err) {
244 displayError(err, "Failed in getDurableConsumerMessageCount");
245 return -1;
246 }
247 }
248
249
250 public int getQueueMessageCount(String queue) {
251 try {
252 return _admin.getQueueMessageCount(queue);
253 } catch (Exception err) {
254 displayError(err, "Failed in getQueueMessageCount");
255 return -1;
256 }
257 }
258
259
260 public boolean durableConsumerExists(String name) {
261 try {
262 return _admin.durableConsumerExists(name);
263 } catch (Exception err) {
264 displayError(err, "Failed in durableConsumerExists");
265 }
266
267 return false;
268 }
269
270
271 public Enumeration getDurableConsumers(String topic) {
272 try {
273 Vector v = _admin.getDurableConsumers(topic);
274 Enumeration e = null;
275 if (v != null) {
276 e = v.elements();
277 }
278 return e;
279 } catch (Exception err) {
280 displayError(err, "Failed in getDurableConsumers");
281 return null;
282 }
283 }
284
285
286 public boolean removeDestination(String destination) {
287 try {
288 return _admin.removeDestination(destination);
289 } catch (Exception err) {
290 displayError(err, "Failed to destroy destination");
291 return false;
292 }
293 }
294
295
296 public int purgeMessages() {
297 int result = -1;
298 try {
299 result = _admin.purgeMessages();
300 } catch (Exception err) {
301 displayError(err, "Failed to purge messages from database");
302 }
303
304 return result;
305 }
306
307
308 public void stopServer() {
309 try {
310 if (_admin == null) {
311 JOptionPane.showMessageDialog
312 (_parent, "Must connect with online mode \nto "
313 + "shutdown server", "Shutdown Error",
314 JOptionPane.ERROR_MESSAGE);
315 } else {
316 _admin.stopServer();
317 _instance = null;
318 _admin = null;
319 }
320 } catch (Exception err) {
321 displayError(err, "Failed to Stop server");
322 }
323 }
324
325
326 public boolean addUser(String username, String password) {
327 try {
328 return _admin.addUser(username, password);
329 } catch (javax.jms.JMSException error) {
330 if (error.getLinkedException() != null) {
331 error.getLinkedException().printStackTrace();
332 }
333 displayError(error, "Failed to add user");
334 return false;
335 } catch (Exception err) {
336 displayError(err, "Failed to add user " + username);
337 return false;
338 }
339 }
340
341
342 public boolean changePassword(String username, String password) {
343 try {
344 return _admin.changePassword(username, password);
345 } catch (javax.jms.JMSException error) {
346 if (error.getLinkedException() != null) {
347 error.getLinkedException().printStackTrace();
348 }
349 displayError(error, "Failed to change password");
350 return false;
351 } catch (Exception err) {
352 displayError(err, "Failed to change password for user " + username);
353 return false;
354 }
355 }
356
357
358 public boolean removeUser(String username) {
359 try {
360 return _admin.removeUser(username);
361 } catch (javax.jms.JMSException error) {
362 if (error.getLinkedException() != null) {
363 error.getLinkedException().printStackTrace();
364 }
365 displayError(error, "Failed to remove user");
366 return false;
367 } catch (Exception err) {
368 displayError(err, "Failed to remove user " + username);
369 return false;
370 }
371 }
372
373
374 public Enumeration getAllUsers() {
375 try {
376 Vector v = _admin.getAllUsers();
377 Enumeration e = null;
378 if (v != null) {
379 e = v.elements();
380 }
381 return e;
382 } catch (Exception err) {
383 displayError(err, "Failed to getAllUsers");
384 return null;
385 }
386 }
387
388 }