1 43 package org.exolab.jms.server; 44 45 import java.sql.Connection ; 46 import java.util.Enumeration ; 47 import java.util.Vector ; 48 import javax.jms.JMSException ; 49 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 import org.exolab.jms.authentication.AuthenticationMgr; 54 import org.exolab.jms.authentication.User; 55 import org.exolab.jms.client.JmsDestination; 56 import org.exolab.jms.client.JmsQueue; 57 import org.exolab.jms.client.JmsTopic; 58 import org.exolab.jms.config.Configuration; 59 import org.exolab.jms.config.ConfigurationManager; 60 import org.exolab.jms.config.Connector; 61 import org.exolab.jms.config.types.SchemeType; 62 import org.exolab.jms.messagemgr.ConsumerEndpoint; 63 import org.exolab.jms.messagemgr.ConsumerManager; 64 import org.exolab.jms.messagemgr.DestinationCache; 65 import org.exolab.jms.messagemgr.DestinationManager; 66 import org.exolab.jms.persistence.DatabaseService; 67 import org.exolab.jms.persistence.SQLHelper; 68 import org.exolab.jms.service.ServiceManager; 69 70 71 78 public class AdminConnection { 79 80 83 private static final Log _log = LogFactory.getLog(AdminConnection.class); 84 85 88 protected AdminConnection() { 89 } 90 91 94 public void close() { 95 } 96 97 104 public int getDurableConsumerMessageCount(String topic, String name) { 105 int count = -1; 106 Connection connection = null; 107 108 try { 109 DestinationManager dmgr = DestinationManager.instance(); 111 ConsumerManager cmgr = ConsumerManager.instance(); 112 JmsDestination dest = dmgr.getDestination(topic); 113 ConsumerEndpoint endpoint = null; 114 if ((dest != null) 115 && ((name != null) 116 || (name.length() > 0))) { 117 118 endpoint = cmgr.getConsumerEndpoint(name); 119 if ((endpoint != null) 120 && (endpoint.getDestination().equals(dest))) { 121 count = endpoint.getMessageCount(); 124 } else { 125 if (dmgr.isPersistent(topic)) { 129 connection = DatabaseService.getConnection(); 130 count = DatabaseService.getAdapter(). 131 getDurableConsumerMessageCount(connection, topic, 132 name); 133 134 connection.commit(); 135 } 136 } 137 } 138 } catch (Exception exception) { 139 _log.error("Failed to get message count for topic=" + topic, 140 exception); 141 SQLHelper.rollback(connection); 142 } finally { 143 SQLHelper.close(connection); 144 } 145 146 return count; 147 } 148 149 157 public int getQueueMessageCount(String queue) { 158 int count = -1; 159 Connection connection = null; 160 161 try { 162 DestinationManager mgr = DestinationManager.instance(); 164 JmsDestination dest = mgr.getDestination(queue); 165 DestinationCache cache = null; 166 if (dest != null) { 167 cache = mgr.getDestinationCache(dest); 168 count = cache.getMessageCount(); 171 } 172 } catch (Exception exception) { 173 _log.error("Failed to get message count for queue=" + queue, 174 exception); 175 SQLHelper.rollback(connection); 176 } finally { 177 SQLHelper.close(connection); 178 } 179 180 return count; 181 } 182 183 190 public boolean addDurableConsumer(String topic, String name) { 191 boolean result = false; 192 try { 193 ConsumerManager.instance().createDurableConsumer( 194 new JmsTopic(topic), name); 195 result = true; 196 } catch (JMSException exception) { 197 _log.error("Failed to add durable consumer=" + name 198 + " for topic=" + topic, exception); 199 } 200 201 return result; 202 } 203 204 211 public boolean removeDurableConsumer(String name) { 212 boolean result = false; 213 try { 214 ConsumerManager.instance().removeDurableConsumer(name); 215 result = true; 216 } catch (JMSException exception) { 217 _log.error("Failed to remove durable consumer=" + name, exception); 218 } 219 220 return result; 221 } 222 223 229 public boolean durableConsumerExists(String name) { 230 return ConsumerManager.instance().durableConsumerExists(name); 231 } 232 233 240 public Vector getDurableConsumers(String topic) { 241 Enumeration iter = null; 242 Vector result = new Vector (); 243 Connection connection = null; 244 245 try { 246 connection = DatabaseService.getConnection(); 247 248 iter = DatabaseService.getAdapter().getDurableConsumers(connection, 249 topic); 250 while (iter.hasMoreElements()) { 252 result.addElement(iter.nextElement()); 253 } 254 connection.commit(); 255 } catch (Exception exception) { 256 _log.error("Failed on get durable consumers for topic=" + topic, 257 exception); 258 SQLHelper.rollback(connection); 259 } finally { 260 SQLHelper.close(connection); 261 } 262 263 return result; 264 } 265 266 272 public boolean unregisterConsumer(String name) { 273 boolean success = false; 274 275 try { 276 ConsumerManager.instance().deleteDurableConsumerEndpoint(name); 277 success = true; 278 } catch (JMSException exception) { 279 } 281 282 return success; 283 } 284 285 292 public boolean isConnected(String name) { 293 return ConsumerManager.instance().isDurableConsumerActive(name); 294 } 295 296 301 public Vector getAllDestinations() { 302 Enumeration iter = null; 303 Vector result = new Vector (); 304 Connection connection = null; 305 306 try { 307 connection = DatabaseService.getConnection(); 308 309 iter = DatabaseService.getAdapter().getAllDestinations(connection); 310 while (iter.hasMoreElements()) { 312 result.addElement(iter.nextElement()); 313 } 314 connection.commit(); 315 } catch (Exception exception) { 316 _log.error("Failed to get all destinations", exception); 317 SQLHelper.rollback(connection); 318 } finally { 319 SQLHelper.close(connection); 320 } 321 322 return result; 323 } 324 325 332 public boolean addDestination(String name, Boolean queue) { 333 334 boolean success = false; 335 336 JmsDestination destination = (queue.booleanValue()) 338 ? (JmsDestination) new JmsQueue(name) 339 : (JmsDestination) new JmsTopic(name); 340 destination.setPersistent(true); 341 342 try { 344 success = DestinationManager.instance(). 345 createAdministeredDestination(destination); 346 } catch (JMSException exception) { 347 _log.error("Failed to add destination=" + name, exception); 348 } 349 350 return success; 351 } 352 353 361 public boolean removeDestination(String name) { 362 363 boolean success = false; 364 JmsDestination dest = 365 DestinationManager.instance().getDestination(name); 366 367 if (dest != null) { 370 try { 371 DestinationManager.instance().deleteAdministeredDestination( 372 dest); 373 success = true; 374 } catch (JMSException exception) { 375 _log.error("Failed to remove destination=" + name, exception); 376 } 377 } 378 379 return success; 380 } 381 382 388 public boolean destinationExists(String name) { 389 390 boolean exists = false; 391 DestinationManager mgr = DestinationManager.instance(); 392 JmsDestination dest = mgr.getDestination(name); 393 394 if (dest != null) { 395 exists = mgr.destinationExists(dest); 396 } 397 398 return exists; 399 } 400 401 406 public void stopServer() { 407 boolean isEmbedded = false; 408 Configuration config = ConfigurationManager.getConfig(); 409 Connector[] connectors = config.getConnectors().getConnector(); 410 for (int i = 0; i < connectors.length; ++i) { 411 if (connectors[i].getScheme().equals(SchemeType.EMBEDDED)) { 412 isEmbedded = true; 413 break; 414 } 415 } 416 417 final boolean exit = !isEmbedded; 418 419 Runnable r = new Runnable () { 420 421 public void run() { 422 try { 423 Thread.sleep(1000); 426 } catch (InterruptedException ignore) { 427 } 428 _log.info("Stopping all services"); 429 ServiceManager.instance().removeAll(); 430 if (exit) { 431 _log.info("Server shutdown scheduled for 5 secs"); 432 try { 433 Thread.sleep(5000); 434 } catch (InterruptedException ignore) { 435 } 436 System.exit(0); 437 } 438 } 439 }; 440 Thread t = new Thread (r); 441 t.start(); 442 } 443 444 449 public int purgeMessages() { 450 return DatabaseService.getAdapter().purgeMessages(); 451 } 452 453 461 public boolean addUser(String username, String password) { 462 return AuthenticationMgr.instance().addUser( 463 new User(username, password)); 464 } 465 466 474 public boolean changePassword(String username, String password) { 475 return AuthenticationMgr.instance().updateUser( 476 new User(username, password)); 477 } 478 479 486 public boolean removeUser(String username) { 487 return AuthenticationMgr.instance().removeUser( 488 new User(username, null)); 489 } 490 491 496 public Vector getAllUsers() { 497 Enumeration iter = null; 498 Vector result = new Vector (); 499 Connection connection = null; 500 501 try { 502 connection = DatabaseService.getConnection(); 503 504 iter = DatabaseService.getAdapter().getAllUsers(connection); 505 while (iter.hasMoreElements()) { 507 result.addElement(iter.nextElement()); 508 } 509 connection.commit(); 510 } catch (Exception exception) { 511 _log.error("Failed on get all users", exception); 512 SQLHelper.rollback(connection); 513 } finally { 514 SQLHelper.close(connection); 515 } 516 517 return result; 518 } 519 520 } 521 | Popular Tags |