Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mobicents/slee/container/management/jmx/ServiceUsageMBeanImpl.java


1   /***************************************************
2    *                                                 *
3    *  Mobicents: The Open Source VoIP Platform       *
4    *                                                 *
5    *  Distributable under LGPL license.              *
6    *  See terms of license at gnu.org.               *
7    *                                                 *
8    ***************************************************/
9   
10  package org.mobicents.slee.container.management.jmx;
11  
12  import java.io.Serializable;
13  import java.net.URLEncoder;
14  import java.util.HashMap;
15  import java.util.Iterator;
16  import java.util.Map;
17  import java.util.Set;
18  
19  import javax.management.InstanceAlreadyExistsException;
20  import javax.management.MalformedObjectNameException;
21  import javax.management.NotCompliantMBeanException;
22  import javax.management.ObjectName;
23  import javax.management.StandardMBean;
24  import javax.slee.InvalidArgumentException;
25  import javax.slee.SbbID;
26  import javax.slee.ServiceID;
27  import javax.slee.UnrecognizedSbbException;
28  import javax.slee.UnrecognizedServiceException;
29  import javax.slee.management.ManagementException;
30  import javax.slee.management.ServiceUsageMBean;
31  import javax.slee.management.UsageParameterSetNameAlreadyExistsException;
32  import javax.slee.usage.UnrecognizedUsageParameterSetNameException;
33  import javax.transaction.SystemException;
34  
35  import org.jboss.logging.Logger;
36  import org.mobicents.slee.container.SleeContainer;
37  import org.mobicents.slee.container.SleeContainerUtils;
38  import org.mobicents.slee.container.management.InstalledUsageParameterSet;
39  import org.mobicents.slee.container.management.SbbDescriptorImpl;
40  import org.mobicents.slee.container.service.Service;
41  import org.mobicents.slee.container.service.ServiceComponent;
42  import org.mobicents.slee.runtime.transaction.SleeTransactionManager;
43  
44  import sun.misc.HexDumpEncoder;
45  
46  /**
47   * 
48   * See SLEE 1.0 #14.9.
49   * 
50   * The ServiceUsageMBean interface defines the management interface used to
51   * interact with SBB usage parameter sets for SBBs in a Service. It defines the
52   * methods to create, lookup, and remove SBB usage parameter sets from SBBs in
53   * the Service.
54   * 
55   * @author M. Ranganathan
56   * @author Ivelin Ivanov
57   */
58  public class ServiceUsageMBeanImpl extends StandardMBean implements
59          ServiceUsageMBean, Serializable {
60  
61      // This is the service ID for this service usage mbean.
62      private ServiceID serviceID;
63  
64     
65      private static Logger logger = Logger
66              .getLogger(ServiceUsageMBeanImpl.class);
67  
68      // Note that this is just to save a lookup
69      // It is not serialized.
70      private transient ServiceComponent service;
71  
72     
73      public ServiceUsageMBeanImpl() throws NotCompliantMBeanException {
74          super(ServiceUsageMBean.class);
75      }
76  
77      public ServiceUsageMBeanImpl(ServiceID serviceID)
78              throws NotCompliantMBeanException, MalformedObjectNameException,
79              NullPointerException {
80          this();
81          this.serviceID = serviceID;
82         
83  
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see javax.slee.management.ServiceUsageMBean#getService()
90       */
91      public ServiceID getService() throws ManagementException {
92  
93          return this.serviceID;
94      }
95  
96      /*
97       * (non-Javadoc)
98       * 
99       * @see javax.slee.management.ServiceUsageMBean#createUsageParameterSet(javax.slee.SbbID,
100      *      java.lang.String)
101      */
102     public void createUsageParameterSet(SbbID sbbId,
103             String usageParameterSetName) throws NullPointerException,
104             UnrecognizedSbbException, InvalidArgumentException,
105             UsageParameterSetNameAlreadyExistsException, ManagementException {
106         
107         if (usageParameterSetName == null)
108             throw new NullPointerException("Sbb usage param set is null");
109         if ( sbbId == null ) 
110             throw new NullPointerException ("Sbb ID is null!");
111         if (usageParameterSetName.length() == 0)
112             throw new InvalidArgumentException(
113                     "The lenght of the Usage Parameter Set Name is zero!");
114         if (!isValidUsageParameterName(usageParameterSetName))
115             throw new InvalidArgumentException(
116                     "The lenght of the Usage Parameter Set Name is zero!");
117         
118         SleeTransactionManager txmgr = SleeContainer.getTransactionManager();
119         boolean rb = true;
120         try {
121 
122             txmgr.begin();
123             
124             checkSbbUsageParams(sbbId);
125           
126             service.installUsageParameter(sbbId, usageParameterSetName);
127             rb = false;
128         } catch (  UnrecognizedSbbException ex ) {
129             throw ex;
130         } catch (InvalidArgumentException ex ) {
131             throw ex;
132         } catch (InstanceAlreadyExistsException ex ) {
133             throw new UsageParameterSetNameAlreadyExistsException ("Duplicate usage parameter set name " + usageParameterSetName);
134         } catch ( NullPointerException ex) {
135             throw ex;
136         } catch  ( UsageParameterSetNameAlreadyExistsException ex ) {
137             throw ex;
138         } catch (Exception e) {
139             throw new ManagementException("Unexpected exception!", e);
140         } finally {
141             try {
142                 if (rb)
143                     txmgr.setRollbackOnly();
144                 txmgr.commit();
145             } catch (Exception ex) {
146                 throw new RuntimeException("Unexpected exception ", ex);
147             }
148         }
149 
150     }
151 
152     private boolean isValidUsageParameterName(String str) {
153         for (int i = 0; i < str.length(); i++) {
154             char c = str.charAt(i);
155             if (!(Character.isDigit(c) || Character.isLetter(c) || (c <= '\u007e' && c >= '\u0020'))) {
156                 return false;
157             }
158         }
159         return true;
160     }
161 
162     /*
163      * (non-Javadoc)
164      * 
165      * @see javax.slee.management.ServiceUsageMBean#removeUsageParameterSet(javax.slee.SbbID,
166      *      java.lang.String)
167      */
168     public void removeUsageParameterSet(SbbID sbbId, String name)
169             throws NullPointerException, UnrecognizedSbbException,
170             InvalidArgumentException,
171             UnrecognizedUsageParameterSetNameException, ManagementException {
172         if (name == null)
173             throw new NullPointerException("Sbb usage param set is null");
174         if ( sbbId == null ) 
175             throw new NullPointerException ("Sbb ID is null!");
176         SleeTransactionManager txmgr = SleeContainer.getTransactionManager();
177         boolean rb = true;
178         try {
179             txmgr.begin();
180             SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
181             if (!sleeContainer.isInstalled(sbbId))
182                 throw new UnrecognizedSbbException("Sbb not installed " + sbbId);
183 
184             //checkSbbUsageParams(sbbId);
185             this.service = sleeContainer.getServiceComponent( this.serviceID);
186 
187             service.removeUsageParameter(sbbId, name);
188         } catch (UnrecognizedUsageParameterSetNameException ex) {
189             throw ex;
190         } catch (InvalidArgumentException ex ) {
191             throw ex;
192         } catch ( UnrecognizedSbbException ex){
193             throw ex;
194         } catch (Exception ex) {
195             throw new ManagementException(
196                     "Could not uninstall usage parameter", ex);
197         } finally {
198             try {
199                 if (rb)
200                     txmgr.setRollbackOnly();
201                 txmgr.commit();
202             } catch (SystemException ex) {
203                 logger.fatal("Unexpected exception !", ex);
204                 throw new RuntimeException("Unexpected exception !", ex);
205             }
206 
207         }
208 
209     }
210 
211     /**
212      * This method returns a list containing the names of the named SBB usage
213      * parameter sets that belong to the SBB specified by the sbbID argument and
214      * the Service represented by the ServiceUsageMBean object.
215      * 
216      * @see javax.slee.management.ServiceUsageMBean#getUsageParameterSets(javax.slee.SbbID)
217      */
218     public String[] getUsageParameterSets(SbbID sbbId)
219             throws NullPointerException, UnrecognizedSbbException,
220             InvalidArgumentException, ManagementException {
221 
222         if (sbbId == null)
223             throw new NullPointerException("null sbb id specificed");
224        
225         SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
226 
227         SleeTransactionManager txmgr = SleeContainer.getTransactionManager();
228         String[] usageParameterSets = null;
229         boolean rb = true;
230         try {
231             txmgr.begin();
232             checkSbbUsageParams(sbbId);
233 
234            
235             usageParameterSets = service.getNamedUsageParameterSets(sbbId);
236             if ( usageParameterSets == null ) throw new InvalidArgumentException ("No usage parameters found ");
237            
238             rb = false;
239         } catch (UnrecognizedSbbException ex) {
240             throw ex;
241         } catch (InvalidArgumentException ex) {
242             throw ex;
243        
244         } catch (Exception ex) {
245             logger.error("unexpected exception ", ex);
246             throw new ManagementException("Something bad happened ! ", ex);
247         } finally {
248             try {
249                 if (rb)
250                     txmgr.setRollbackOnly();
251                 txmgr.commit();
252             } catch (SystemException ex) {
253                 logger.error("Txmgr failed", ex);
254                 throw new RuntimeException("Txmgr failed", ex);
255             }
256         }
257         return usageParameterSets;
258     }
259 
260     /*
261      * (non-Javadoc)
262      * 
263      * @see javax.slee.management.ServiceUsageMBean#getSbbUsageMBean(javax.slee.SbbID)
264      */
265     public ObjectName getSbbUsageMBean(SbbID sbbId)
266             throws NullPointerException, UnrecognizedSbbException,
267             InvalidArgumentException, ManagementException {
268         if ( sbbId == null ) 
269             throw new NullPointerException ("Sbb ID is null!");
270         SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
271         SleeTransactionManager txMgr = SleeContainer.getTransactionManager();
272         boolean rb = true;
273         
274         try {
275             txMgr.begin();
276            
277            checkSbbUsageParams(sbbId);
278            ObjectName on = (ObjectName) service.getUsageParameterObjectName(sbbId);
279            if ( on == null ) 
280                throw new InvalidArgumentException ("No usage mbean for this sbb ");
281           rb = false;
282           return on;
283         } catch (UnrecognizedSbbException ex) {
284             throw ex;
285         } catch (InvalidArgumentException ex) {
286             throw ex;
287        
288         } catch (Exception ex) {
289             logger.error("Unexpected exception ", ex);
290             throw new ManagementException ("cannot getUsageMBean " + sbbId);
291         } finally {
292             try {
293                 if (rb)
294                     txMgr.setRollbackOnly();
295                 txMgr.commit();
296             } catch (Exception e) {
297                 throw new RuntimeException("Unexpected error with tx manager",
298                         e);
299             }
300 
301         }
302         
303     }
304 
305  
306 
307     /*
308      * (non-Javadoc)
309      * 
310      * @see javax.slee.management.ServiceUsageMBean#getSbbUsageMBean(javax.slee.SbbID,
311      *      java.lang.String)
312      */
313     public ObjectName getSbbUsageMBean(SbbID sbbId, String name)
314             throws NullPointerException, UnrecognizedSbbException,
315             InvalidArgumentException,
316             UnrecognizedUsageParameterSetNameException, ManagementException {
317         if ( sbbId == null ) 
318             throw new NullPointerException ("Sbb ID is null!");
319         if (name == null)
320             throw new NullPointerException("Sbb usage param set is null");
321         SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
322         SleeTransactionManager txMgr = SleeContainer.getTransactionManager();
323         boolean rb = true;
324         
325       
326         try {
327             txMgr.begin();
328            
329            checkSbbUsageParams(sbbId);
330            ObjectName on = (ObjectName) service.getUsageParameterObjectName(sbbId,name);
331          
332         
333           rb = false;
334           return on;
335         } catch (UnrecognizedSbbException ex) {
336             throw ex;
337            
338         } catch (UnrecognizedUsageParameterSetNameException ex) {
339             throw ex;
340         
341         
342         } catch ( InvalidArgumentException ex ) {
343             throw ex;
344             
345         } catch (Exception ex) {
346             logger.error("unexpected exception getting usage mbean name = " + name 
347                     + " sbbid = " + sbbId, ex);
348             throw new ManagementException ("cannot getUsageMBean " + name,ex);
349         } finally {
350             try {
351                 if (rb)
352                     txMgr.setRollbackOnly();
353                 txMgr.commit();
354             } catch (Exception e) {
355                 throw new RuntimeException("Unexpected error with tx manager",
356                         e);
357             }
358 
359         }
360     }
361 
362     /**
363      * Resets the usage parameters of only the SBB specified by the sbbID
364      * argument argument (within the Service represented by the
365      * ServiceUsageMBean object).
366      * 
367      * @see javax.slee.management.ServiceUsageMBean#resetAllUsageParameters(javax.slee.SbbID)
368      */
369     public void resetAllUsageParameters(SbbID sbbId)
370             throws NullPointerException, UnrecognizedSbbException,
371             InvalidArgumentException, ManagementException {
372 
373         if (logger.isDebugEnabled()) {
374             logger.debug("resetAllUsageParameters: " + sbbId);
375         }
376         if (sbbId == null)
377             throw new NullPointerException("Sbb ID is null");
378         SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
379         SleeTransactionManager txMgr = SleeContainer.getTransactionManager();
380         boolean rb = true;
381         try {
382             txMgr.begin();
383             checkSbbUsageParams(sbbId);
384             Service svc = sleeContainer.getService(this.serviceID);
385             InstalledUsageParameterSet usageParam = svc.getDefaultUsageParameterSet(sbbId);
386             // note that the usage parameter may not yet be instantiated so this may be null.
387            
388             if ( usageParam != null ) usageParam.reset();
389 
390             String[] paramNames = service.getNamedUsageParameterSets(sbbId);
391             
392             
393             
394 
395             for (int i = 0; paramNames != null && i < paramNames.length; i++) {
396                 String name = paramNames[i];
397                 
398                 InstalledUsageParameterSet ups = svc
399                         .getNamedUsageParameter(sbbId, name);
400                 if ( ups != null )   ups.reset();
401             }
402            
403             rb = false;
404         } catch ( InvalidArgumentException ex ) {
405             throw ex;
406         } catch ( UnrecognizedSbbException ex ) {
407             throw ex;
408         } catch (Exception ex) {
409             String s = "unexpected exception in resetAllUsageParameters";
410             logger.error(s, ex);
411             throw new ManagementException(s);
412 
413         } finally {
414             try {
415                 if (rb)
416                     txMgr.setRollbackOnly();
417                 txMgr.commit();
418             } catch (Exception e) {
419                 throw new RuntimeException("Unexpected error with tx manager",
420                         e);
421             }
422            
423         }
424     }
425 
426     /**
427      * Resets the usage parameters of all SBBs within the Service represented by
428      * the ServiceUsageMBean object. The SLEE sets counter-type usage parameters
429      * to zero and removes all samples from sample-type usage parameters.
430      * 
431      * @see javax.slee.management.ServiceUsageMBean#resetAllUsageParameters()
432      */
433     public void resetAllUsageParameters() throws ManagementException {
434 
435         SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
436         SleeTransactionManager txMgr = SleeContainer.getTransactionManager();
437         boolean rb = true;
438 
439         try {
440             txMgr.begin();
441             Service service = sleeContainer.getService(this.serviceID);
442             for (Iterator it = service.getAllUsageParameters(); it.hasNext();) {
443                 InstalledUsageParameterSet ups = (InstalledUsageParameterSet) it
444                         .next();
445                 ups.reset();
446             }
447             rb = false;
448         } catch (Exception ex) {
449             throw new ManagementException("Could not reset!", ex);
450         } finally {
451             try {
452                 if (rb)
453                     txMgr.setRollbackOnly();
454                 txMgr.commit();
455             } catch (Exception ex) {
456                 throw new RuntimeException("unexpected system exception ", ex);
457             }
458         }
459 
460     }
461 
462     /*
463      * (non-Javadoc)
464      * 
465      * @see javax.slee.management.ServiceUsageMBean#close()
466      */
467     public void close() throws ManagementException {
468         // TODO Auto-generated method stub
469 
470     }
471 
472     
473 
474     private void checkSbbUsageParams(SbbID sbbId)
475             throws UnrecognizedSbbException, InvalidArgumentException,
476             UnrecognizedSbbException , SystemException, UnrecognizedServiceException {
477         
478         
479 
480         if (sbbId == null)
481             throw new NullPointerException("SbbId is null!");
482 
483      
484         SleeContainer sleeContainer = SleeContainer.lookupFromJndi();
485         
486         this.service = sleeContainer.getServiceComponent( this.serviceID);
487 
488         if (!sleeContainer.isInstalled(sbbId))
489             throw new UnrecognizedSbbException("Sbb not installed " + sbbId);
490 
491         if ( !this.service.isComponent(sbbId))
492             throw new UnrecognizedSbbException ("This sbb is not part of this service serviceID = " +
493                     this.serviceID + " sbb id = " + sbbId  );
494        
495             
496                     
497     }
498 
499 }
500