KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > server > jmx > DestinationMBeanSupport


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.server.jmx;
23
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 import javax.management.MBeanRegistration JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.InvalidAttributeValueException JavaDoc;
34 import javax.management.InstanceNotFoundException JavaDoc;
35 import javax.management.MBeanException JavaDoc;
36 import javax.management.ReflectionException JavaDoc;
37
38 import org.jboss.system.ServiceMBeanSupport;
39 import org.jboss.naming.Util;
40 import org.jboss.logging.Logger;
41
42 import org.jboss.mq.MessageStatistics;
43 import org.jboss.mq.SpyDestination;
44 import org.jboss.mq.server.BasicQueueParameters;
45 import org.jboss.mq.server.JMSDestinationManager;
46 import org.jboss.mq.server.MessageCounter;
47 import org.jboss.mq.server.Receivers;
48 import org.w3c.dom.Element JavaDoc;
49
50 /**
51  * Super class for destination managers.
52  *
53  * @author <a HREF="pra@tim.se">Peter Antman</a>
54  * @version $Revision: 37459 $
55  */

56 abstract public class DestinationMBeanSupport extends ServiceMBeanSupport
57    implements DestinationMBean, MBeanRegistration JavaDoc
58 {
59    protected SpyDestination spyDest;
60    protected String JavaDoc destinationName;
61    protected String JavaDoc jndiName;
62    protected boolean jndiBound;
63    protected ObjectName JavaDoc jbossMQService;
64    //String securityConf;
65
protected Element JavaDoc securityConf;
66    /** The basic parameters */
67    protected BasicQueueParameters parameters = new BasicQueueParameters();
68    /** Destination JNDI name */
69    ObjectName JavaDoc expiryDestination;
70
71    /**
72     * A optional security manager. Must be set to use security conf.
73     */

74    protected ObjectName JavaDoc securityManager;
75
76    /**
77     * Get the value of JBossMQService.
78     *
79     * @return value of JBossMQService.
80     */

81    public ObjectName JavaDoc getDestinationManager()
82    {
83       return jbossMQService;
84    }
85
86    /**
87     * Set the value of JBossMQService.
88     *
89     * @param v Value to assign to JBossMQService.
90     */

91    public void setDestinationManager(ObjectName JavaDoc jbossMQService)
92    {
93       this.jbossMQService = jbossMQService;
94    }
95
96    protected SpyDestination getSpyDest()
97    {
98       return spyDest;
99    }
100
101    public void setSecurityConf(org.w3c.dom.Element JavaDoc securityConf) throws Exception JavaDoc
102    {
103       log.debug("Setting securityConf: " + securityConf);
104       this.securityConf = securityConf;
105    }
106
107    protected Element JavaDoc getSecurityConf()
108    {
109       return securityConf;
110    }
111
112    public void setSecurityManager(ObjectName JavaDoc securityManager)
113    {
114       this.securityManager = securityManager;
115    }
116
117    protected ObjectName JavaDoc getSecurityManager()
118    {
119       return securityManager;
120    }
121    
122    public void createService() throws Exception JavaDoc
123    {
124       // Copy default values from the server when null or zero
125
if (parameters.receiversImpl == null)
126          parameters.receiversImpl = (Class JavaDoc) server.getAttribute(jbossMQService, "ReceiversImpl");
127       if (parameters.recoveryRetries == 0)
128          parameters.recoveryRetries = ((Integer JavaDoc) server.getAttribute(jbossMQService, "RecoveryRetries")).intValue();
129    }
130    
131    public void startService() throws Exception JavaDoc
132    {
133
134       setupSecurityManager();
135       setupExpiryDestination();
136
137    }
138
139    protected void setupSecurityManager()
140            throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
141    {
142       if (securityManager != null)
143       {
144          // Set securityConf at manager
145
getServer().invoke(securityManager, "addDestination", new Object JavaDoc[]{spyDest.getName(), securityConf}, new String JavaDoc[]{"java.lang.String", "org.w3c.dom.Element"});
146       }
147    }
148
149    protected void setupExpiryDestination()
150            throws Exception JavaDoc
151    {
152       if (expiryDestination != null)
153       {
154          String JavaDoc jndi = (String JavaDoc)getServer().getAttribute(expiryDestination, "JNDIName");
155          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
156          try
157          {
158             parameters.expiryDestination = (SpyDestination)ctx.lookup(jndi);
159          }
160          finally
161          {
162             ctx.close();
163          }
164       }
165    }
166
167    public void stopService() throws Exception JavaDoc
168    {
169       // unbind from JNDI
170
if (jndiBound)
171       {
172          InitialContext JavaDoc ctx = getInitialContext();
173          try
174          {
175             log.info("Unbinding JNDI name: " + jndiName);
176             Util.unbind(ctx, jndiName);
177          }
178          finally
179          {
180             ctx.close();
181          }
182          jndiName = null;
183          jndiBound = false;
184       }
185
186       JMSDestinationManager jmsServer = (JMSDestinationManager) server.getAttribute(jbossMQService, "Interceptor");
187       if (jmsServer != null)
188          jmsServer.closeDestination(spyDest);
189
190       // TODO: need to remove from JMSServer
191
teardownSecurityManager();
192    }
193
194    protected void teardownSecurityManager()
195            throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
196    {
197       if (securityManager != null)
198       {
199          // Set securityConf at manager
200
getServer().invoke(securityManager, "removeDestination", new Object JavaDoc[]{spyDest.getName()}, new String JavaDoc[]{"java.lang.String"});
201       }
202    }
203
204    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
205       throws MalformedObjectNameException JavaDoc
206    {
207       destinationName = name.getKeyProperty("name");
208       if (destinationName == null || destinationName.length() == 0)
209       {
210          throw new MalformedObjectNameException JavaDoc("Property 'name' not provided");
211       }
212
213       // re-setup the logger with a more descriptive name
214
log = Logger.getLogger(getClass().getName() + "." + destinationName);
215
216       return name;
217    }
218
219    /**
220     * Sets the JNDI name for this topic
221     *
222     * @param name Name to bind this topic to in the JNDI tree
223     */

224    public synchronized void setJNDIName(String JavaDoc name) throws Exception JavaDoc
225    {
226       if (spyDest == null)
227       { // nothing to bind yet, startService will recall us
228
jndiName = name;
229          return;
230       }
231
232       if (name == null)
233       {
234          throw new InvalidAttributeValueException JavaDoc("Destination JNDI names can not be null");
235       }
236
237       InitialContext JavaDoc ic = getInitialContext();
238       try
239       {
240          if (jndiName != null && jndiBound)
241          {
242             Util.unbind(ic, jndiName); //Remove old jndi name
243
jndiName = null;
244             jndiBound = false;
245          }
246
247          Util.rebind(ic, name, spyDest);
248          jndiName = name;
249          jndiBound = true;
250       }
251       finally
252       {
253          ic.close();
254       }
255
256       log.info("Bound to JNDI name: " + jndiName);
257    }
258
259    protected InitialContext JavaDoc getInitialContext()
260            throws NamingException JavaDoc
261    {
262       InitialContext JavaDoc ic = new InitialContext JavaDoc();
263       return ic;
264    }
265
266    /**
267     * Gets the JNDI name use by this topic
268     *
269     * @return The JNDI name currently in use
270     */

271    public String JavaDoc getJNDIName()
272    {
273       return jndiName;
274    }
275
276    /**
277     * Get destination message counter array
278     *
279     * @return MessageCounter[]
280     */

281    abstract public MessageCounter[] getMessageCounter();
282
283    /**
284     * Get destination stats array
285     *
286     * @return MessageStatistics[]
287     * @throws Exception for any error
288     */

289    abstract public MessageStatistics[] getMessageStatistics() throws Exception JavaDoc;
290
291    /**
292     * List destination message counter as HTML table
293     *
294     * @return String
295     */

296    public String JavaDoc listMessageCounter()
297    {
298       MessageCounter[] counter = getMessageCounter();
299       if (counter == null)
300          return null;
301       
302       String JavaDoc ret = "<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">" +
303                    "<tr>" +
304                    "<th>Type</th>" +
305                    "<th>Name</th>" +
306                    "<th>Subscription</th>" +
307                    "<th>Durable</th>" +
308                    "<th>Count</th>" +
309                    "<th>CountDelta</th>" +
310                    "<th>Depth</th>" +
311                    "<th>DepthDelta</th>" +
312                    "<th>Last Add</th>" +
313                    "</tr>";
314       
315       for( int i=0; i<counter.length; i++ )
316       {
317          String JavaDoc data = counter[i].getCounterAsString();
318          StringTokenizer JavaDoc token = new StringTokenizer JavaDoc( data, ",");
319          String JavaDoc value;
320          
321          ret += "<tr bgcolor=\"#" + ( (i%2)==0 ? "FFFFFF" : "F0F0F0") + "\">";
322
323          ret += "<td>" + token.nextToken() + "</td>"; // type
324
ret += "<td>" + token.nextToken() + "</td>"; // name
325
ret += "<td>" + token.nextToken() + "</td>"; // subscription
326
ret += "<td>" + token.nextToken() + "</td>"; // durable
327

328          ret += "<td>" + token.nextToken() + "</td>"; // count
329

330          value = token.nextToken(); // countDelta
331

332          if( value.equalsIgnoreCase("0") )
333              value = "-";
334              
335          ret += "<td>" + value + "</td>";
336          
337          ret += "<td>" + token.nextToken() + "</td>"; // depth
338

339          value = token.nextToken(); // depthDelta
340

341          if( value.equalsIgnoreCase("0") )
342              value = "-";
343              
344          ret += "<td>" + value + "</td>";
345
346          ret += "<td>" + token.nextToken() + "</td>"; // date last add
347

348          ret += "</tr>";
349       }
350       
351       ret += "</table>";
352       
353       return ret;
354    }
355    
356
357    /**
358     * Reset destination message counter
359     */

360    public void resetMessageCounter()
361    {
362       MessageCounter[] counter = getMessageCounter();
363       if (counter == null)
364          return;
365       
366       for( int i=0; i<counter.length; i++ )
367       {
368          counter[i].resetCounter();
369       }
370    }
371    
372    /**
373     * List destination message counter history as HTML table
374     *
375     * @return String
376     */

377    public String JavaDoc listMessageCounterHistory()
378    {
379       MessageCounter[] counter = getMessageCounter();
380       if (counter == null)
381          return null;
382       
383       String JavaDoc ret = "";
384                
385       for( int i=0; i<counter.length; i++ )
386       {
387          // destination name
388
ret += ( counter[i].getDestinationTopic() ? "Topic '" : "Queue '" );
389          ret += counter[i].getDestinationName() + "'";
390          
391          if( counter[i].getDestinationSubscription() != null )
392             ret += "Subscription '" + counter[i].getDestinationSubscription() + "'";
393             
394                      
395          // table header
396
ret += "<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">" +
397                 "<tr>" +
398                 "<th>Date</th>";
399
400          for( int j = 0; j < 24; j++ )
401             ret += "<th width=\"4%\">" + j + "</th>";
402
403          ret += "<th>Total</th></tr>";
404
405          // get history data as CSV string
406
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( counter[i].getHistoryAsString(), ",\n");
407          
408          // get history day count
409
int days = Integer.parseInt( tokens.nextToken() );
410          
411          for( int j=0; j<days; j++ )
412          {
413             // next day counter row
414
ret += "<tr bgcolor=\"#" + ((j%2)==0 ? "FFFFFF" : "F0F0F0") + "\">";
415          
416             // date
417
ret += "<td>" + tokens.nextToken() + "</td>";
418              
419             // 24 hour counters
420
int total = 0;
421             
422             for( int k=0; k<24; k++ )
423             {
424                int value = Integer.parseInt( tokens.nextToken().trim() );
425             
426                if( value == -1 )
427                {
428                     ret += "<td></td>";
429                }
430                else
431                {
432                     ret += "<td>" + value + "</td>";
433                     
434                     total += value;
435                }
436             }
437
438             ret += "<td>" + total + "</td></tr>";
439          }
440
441          ret += "</table><br><br>";
442       }
443
444       return ret;
445    }
446
447
448    /**
449     * Reset destination message counter history
450     */

451    public void resetMessageCounterHistory()
452    {
453       MessageCounter[] counter = getMessageCounter();
454       if (counter == null)
455          return;
456
457       for( int i=0; i<counter.length; i++ )
458       {
459          counter[i].resetHistory();
460       }
461    }
462
463    /**
464     * Sets the destination message counter history day limit
465     * <0: unlimited, =0: disabled, > 0 maximum day count
466     *
467     * @param days maximum day count
468     */

469    public void setMessageCounterHistoryDayLimit( int days )
470    {
471       if( days < -1 )
472           days = -1;
473       
474       parameters.messageCounterHistoryDayLimit = days;
475       if (getState() != STARTED)
476          return;
477
478       MessageCounter[] counter = getMessageCounter();
479       for (int i=0; i<counter.length; ++i)
480          counter[i].setHistoryLimit(days);
481    }
482
483    /**
484     * Gets the destination message counter history day limit
485     * @return Maximum day count
486     */

487    public int getMessageCounterHistoryDayLimit()
488    {
489       return parameters.messageCounterHistoryDayLimit;
490    }
491
492    public int getMaxDepth()
493    {
494       return parameters.maxDepth;
495    }
496
497    public void setMaxDepth(int depth)
498    {
499       parameters.maxDepth = depth;
500    }
501
502    public boolean getInMemory()
503    {
504       return parameters.inMemory;
505    }
506
507    public void setInMemory(boolean mode)
508    {
509       parameters.inMemory = mode;
510    }
511
512    /**
513     * Returns the message redelivery limit; the number of redelivery attempts
514     * before a message is moved to the DLQ.
515     */

516    public int getRedeliveryLimit()
517    {
518       return parameters.redeliveryLimit;
519    }
520
521    /**
522     * Sets the redelivery limit.
523     */

524    public void setRedeliveryLimit(int limit)
525    {
526       parameters.redeliveryLimit = limit;
527    }
528
529    /**
530     * Returns the message redelivery delay, the delay in milliseconds before a
531     * rolled back or recovered message is redelivered
532     */

533    public long getRedeliveryDelay()
534    {
535       return parameters.redeliveryDelay;
536    }
537
538
539    /**
540     * Sets the Message redelivery delay in milliseconds.
541     */

542    public void setRedeliveryDelay(long rDelay)
543    {
544       parameters.redeliveryDelay = rDelay;
545    }
546
547    public Class JavaDoc getReceiversImpl()
548    {
549       return parameters.receiversImpl;
550    }
551
552    public void setReceiversImpl(Class JavaDoc clazz)
553    {
554       if (clazz != null && Receivers.class.isAssignableFrom(clazz) == false)
555          throw new IllegalArgumentException JavaDoc("Class " + clazz.getName() + " is not a Receivers implementation");
556       parameters.receiversImpl = clazz;
557    }
558    
559    public int getRecoveryRetries()
560    {
561       return parameters.recoveryRetries;
562    }
563
564    public void setRecoveryRetries(int retries)
565    {
566       parameters.recoveryRetries = retries;
567    }
568
569    public ObjectName JavaDoc getExpiryDestination()
570    {
571       return expiryDestination;
572    }
573
574    public void setExpiryDestination(ObjectName JavaDoc expiryDestination)
575    {
576       this.expiryDestination = expiryDestination;
577    }
578
579 }
580
Popular Tags