001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.server.jmx;
023:
024: import java.util.Arrays;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.jms.IllegalStateException;
029:
030: import org.jboss.mq.DurableSubscriptionID;
031: import org.jboss.mq.MessageStatistics;
032: import org.jboss.mq.SpyTopic;
033: import org.jboss.mq.Subscription;
034: import org.jboss.mq.server.BasicQueue;
035: import org.jboss.mq.server.JMSDestinationManager;
036: import org.jboss.mq.server.JMSTopic;
037: import org.jboss.mq.server.MessageCounter;
038:
039: /**
040: * This class is a message queue which is stored (hashed by Destination) on the
041: * JMS provider
042: *
043: * @jmx:mbean extends="org.jboss.mq.server.jmx.DestinationMBean"
044: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
045: * @author <a href="hiram.chirino@jboss.org">Hiram Chirino</a>
046: * @author <a href="pra@tim.se">Peter Antman</a>
047: * @version $Revision: 57198 $
048: */
049: public class Topic extends DestinationMBeanSupport implements
050: TopicMBean {
051: protected JMSTopic destination;
052:
053: /**
054: * @jmx:managed-attribute
055: */
056: public String getTopicName() {
057: return destinationName;
058: }
059:
060: public void startService() throws Exception {
061: if (destinationName == null || destinationName.length() == 0) {
062: throw new IllegalStateException("TopicName was not set");
063: }
064:
065: JMSDestinationManager jmsServer = (JMSDestinationManager) server
066: .getAttribute(jbossMQService, "Interceptor");
067:
068: spyDest = new SpyTopic(destinationName);
069: destination = new JMSTopic(spyDest, null, jmsServer, parameters);
070:
071: jmsServer.addDestination(destination);
072:
073: if (jndiName == null) {
074: setJNDIName("topic/" + destinationName);
075: } else {
076: // in config phase, we only stored the name, and didn't actually bind it
077: setJNDIName(jndiName);
078: }
079: super .startService();
080: }
081:
082: public void stopService() throws Exception {
083: super .stopService();
084: destination = null;
085: }
086:
087: /**
088: * @see DestinationMBean#removeAllMessages()
089: */
090: public void removeAllMessages() throws Exception {
091: if (destination == null)
092: return;
093: destination.removeAllMessages();
094: }
095:
096: /**
097: * @jmx:managed-attribute
098: */
099: public int getAllMessageCount() {
100: if (destination == null)
101: return 0;
102: return destination.getAllMessageCount();
103: }
104:
105: /**
106: * @jmx:managed-attribute
107: */
108: public int getDurableMessageCount() {
109: if (destination == null)
110: return 0;
111: return destination.getDurableMessageCount();
112: }
113:
114: /**
115: * @jmx:managed-attribute
116: */
117: public int getNonDurableMessageCount() {
118: if (destination == null)
119: return 0;
120: return destination.getNonDurableMessageCount();
121: }
122:
123: /**
124: * @jmx:managed-attribute
125: */
126: public int getAllSubscriptionsCount() {
127: if (destination == null)
128: return 0;
129: return destination.getAllSubscriptionsCount();
130: }
131:
132: /**
133: * @jmx:managed-attribute
134: */
135: public int getDurableSubscriptionsCount() {
136: if (destination == null)
137: return 0;
138: return destination.getDurableSubscriptionsCount();
139: }
140:
141: /**
142: * @jmx:managed-attribute
143: */
144: public int getNonDurableSubscriptionsCount() {
145: if (destination == null)
146: return 0;
147: return destination.getNonDurableSubscriptionsCount();
148: }
149:
150: /**
151: * @jmx:managed-operation
152: */
153: public List listAllSubscriptions() {
154: if (destination == null)
155: return null;
156: return destination.getAllSubscriptions();
157: }
158:
159: /**
160: * @jmx:managed-operation
161: */
162: public List listDurableSubscriptions() {
163: if (destination == null)
164: return null;
165: return destination.getDurableSubscriptions();
166: }
167:
168: /**
169: * @jmx:managed-operation
170: */
171: public List listNonDurableSubscriptions() {
172: if (destination == null)
173: return null;
174: return destination.getNonDurableSubscriptions();
175: }
176:
177: public List listMessages(String id) throws Exception {
178: if (destination == null)
179: return null;
180: BasicQueue queue = findBasicQueue(id);
181: return Arrays.asList(queue.browse(null));
182: }
183:
184: public List listMessages(String id, String selector)
185: throws Exception {
186: if (destination == null)
187: return null;
188: BasicQueue queue = findBasicQueue(id);
189: return Arrays.asList(queue.browse(selector));
190: }
191:
192: public List listNonDurableMessages(String id, String sub)
193: throws Exception {
194: if (destination == null)
195: return null;
196: BasicQueue queue = findNonDurableBasicQueue(id, sub);
197: return Arrays.asList(queue.browse(null));
198: }
199:
200: public List listNonDurableMessages(String id, String sub,
201: String selector) throws Exception {
202: if (destination == null)
203: return null;
204: BasicQueue queue = findNonDurableBasicQueue(id, sub);
205: return Arrays.asList(queue.browse(selector));
206: }
207:
208: public List listDurableMessages(String id, String name)
209: throws Exception {
210: if (destination == null)
211: return null;
212: BasicQueue queue = findDurableBasicQueue(id, name);
213: return Arrays.asList(queue.browse(null));
214: }
215:
216: public List listDurableMessages(String id, String name,
217: String selector) throws Exception {
218: if (destination == null)
219: return null;
220: BasicQueue queue = findDurableBasicQueue(id, name);
221: return Arrays.asList(queue.browse(selector));
222: }
223:
224: public long getNonDurableMessageCount(String id, String sub)
225: throws Exception {
226: if (destination == null)
227: return 0;
228: BasicQueue queue = findNonDurableBasicQueue(id, sub);
229: return queue.getQueueDepth();
230: }
231:
232: public long getDurableMessageCount(String id, String name)
233: throws Exception {
234: if (destination == null)
235: return 0;
236: BasicQueue queue = findDurableBasicQueue(id, name);
237: return queue.getQueueDepth();
238: }
239:
240: public List listNonDurableScheduledMessages(String id, String sub)
241: throws Exception {
242: if (destination == null)
243: return null;
244: BasicQueue queue = findNonDurableBasicQueue(id, sub);
245: return queue.browseScheduled(null);
246: }
247:
248: public List listNonDurableScheduledMessages(String id, String sub,
249: String selector) throws Exception {
250: if (destination == null)
251: return null;
252: BasicQueue queue = findNonDurableBasicQueue(id, sub);
253: return queue.browseScheduled(selector);
254: }
255:
256: public List listDurableScheduledMessages(String id, String name)
257: throws Exception {
258: if (destination == null)
259: return null;
260: BasicQueue queue = findDurableBasicQueue(id, name);
261: return queue.browseScheduled(null);
262: }
263:
264: public List listDurableScheduledMessages(String id, String name,
265: String selector) throws Exception {
266: if (destination == null)
267: return null;
268: BasicQueue queue = findDurableBasicQueue(id, name);
269: return queue.browseScheduled(selector);
270: }
271:
272: public long getNonDurableScheduledMessageCount(String id, String sub)
273: throws Exception {
274: if (destination == null)
275: return 0;
276: BasicQueue queue = findNonDurableBasicQueue(id, sub);
277: return queue.getScheduledMessageCount();
278: }
279:
280: public long getDurableScheduledMessageCount(String id, String name)
281: throws Exception {
282: if (destination == null)
283: return 0;
284: BasicQueue queue = findDurableBasicQueue(id, name);
285: return queue.getScheduledMessageCount();
286: }
287:
288: public List listNonDurableInProcessMessages(String id, String sub)
289: throws Exception {
290: if (destination == null)
291: return null;
292: BasicQueue queue = findNonDurableBasicQueue(id, sub);
293: return queue.browseInProcess(null);
294: }
295:
296: public List listNonDurableInProcessMessages(String id, String sub,
297: String selector) throws Exception {
298: if (destination == null)
299: return null;
300: BasicQueue queue = findNonDurableBasicQueue(id, sub);
301: return queue.browseInProcess(selector);
302: }
303:
304: public List listDurableInProcessMessages(String id, String name)
305: throws Exception {
306: if (destination == null)
307: return null;
308: BasicQueue queue = findDurableBasicQueue(id, name);
309: return queue.browseInProcess(null);
310: }
311:
312: public List listDurableInProcessMessages(String id, String name,
313: String selector) throws Exception {
314: if (destination == null)
315: return null;
316: BasicQueue queue = findDurableBasicQueue(id, name);
317: return queue.browseInProcess(selector);
318: }
319:
320: public long getNonDurableInProcessMessageCount(String id, String sub)
321: throws Exception {
322: if (destination == null)
323: return 0;
324: BasicQueue queue = findNonDurableBasicQueue(id, sub);
325: return queue.getInProcessMessageCount();
326: }
327:
328: public long getDurableInProcessMessageCount(String id, String name)
329: throws Exception {
330: if (destination == null)
331: return 0;
332: BasicQueue queue = findDurableBasicQueue(id, name);
333: return queue.getInProcessMessageCount();
334: }
335:
336: public MessageCounter[] getMessageCounter() {
337: if (destination == null)
338: return null;
339: return destination.getMessageCounter();
340: }
341:
342: public MessageStatistics[] getMessageStatistics() throws Exception {
343: if (destination == null)
344: return null;
345: return MessageCounter.getMessageStatistics(destination
346: .getMessageCounter());
347: }
348:
349: protected BasicQueue findBasicQueue(String id) throws Exception {
350: if (destination == null)
351: return null;
352: List queues = destination.getAllQueues();
353: if (id == null)
354: throw new IllegalArgumentException("Null subscription id: "
355: + help(queues));
356: for (Iterator i = queues.iterator(); i.hasNext();) {
357: BasicQueue q = (BasicQueue) i.next();
358: if (q.getDescription().equals(id))
359: return q;
360: }
361: throw new IllegalArgumentException("Invalid subscription id: "
362: + help(queues));
363: }
364:
365: protected BasicQueue findNonDurableBasicQueue(String id, String sub)
366: throws Exception {
367: if (destination == null)
368: return null;
369: List subscriptions = destination.getNonDurableSubscriptions();
370: if (id == null)
371: throw new IllegalArgumentException(
372: "Null subscription id, enter client id plus the subscription id: "
373: + help(subscriptions));
374: for (Iterator i = subscriptions.iterator(); i.hasNext();) {
375: Subscription s = (Subscription) i.next();
376: String clientId = s.connectionToken.getClientID();
377:
378: if (sub == null || sub.trim().length() == 0
379: || Integer.toString(s.subscriptionId).equals(sub)) {
380: if (clientId != null && clientId.equals(id))
381: return destination.getQueue(s);
382: }
383: }
384: throw new IllegalArgumentException(
385: "Invalid subscription id, enter client id plus the subscription id: "
386: + help(subscriptions));
387: }
388:
389: protected BasicQueue findDurableBasicQueue(String id, String name)
390: throws Exception {
391: if (destination == null)
392: return null;
393: List subscriptions = destination.getDurableSubscriptions();
394: if (id == null || name == null)
395: throw new IllegalArgumentException(
396: "Null durable subscription enter client id and subscription name: "
397: + help(subscriptions));
398: DurableSubscriptionID durID = new DurableSubscriptionID(id,
399: name, null);
400: BasicQueue result = destination.getDurableSubscription(durID);
401: if (result != null)
402: return result;
403: throw new IllegalArgumentException(
404: "Invalid durable subscription enter client id and subscription name: "
405: + help(subscriptions));
406: }
407:
408: protected String help(List queues) {
409: return "id must be one from the following list " + queues;
410: }
411: }
|