KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > cluster > HAManagementService


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.management.j2ee.cluster;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.management.Attribute JavaDoc;
30 import javax.management.AttributeList JavaDoc;
31 import javax.management.AttributeNotFoundException JavaDoc;
32 import javax.management.InstanceAlreadyExistsException JavaDoc;
33 import javax.management.InstanceNotFoundException JavaDoc;
34 import javax.management.IntrospectionException JavaDoc;
35 import javax.management.InvalidAttributeValueException JavaDoc;
36 import javax.management.ListenerNotFoundException JavaDoc;
37 import javax.management.MBeanException JavaDoc;
38 import javax.management.MBeanInfo JavaDoc;
39 import javax.management.MBeanRegistrationException JavaDoc;
40 import javax.management.MBeanServer JavaDoc;
41 import javax.management.NotCompliantMBeanException JavaDoc;
42 import javax.management.NotificationFilter JavaDoc;
43 import javax.management.ObjectInstance JavaDoc;
44 import javax.management.ObjectName JavaDoc;
45 import javax.management.QueryExp JavaDoc;
46 import javax.management.ReflectionException JavaDoc;
47
48 import org.jboss.ha.framework.interfaces.HAPartition;
49 import org.jboss.management.j2ee.J2EEDomain;
50 import org.jboss.mx.util.ObjectNameFactory;
51 import org.jboss.system.ServiceMBeanSupport;
52 import org.jboss.system.server.ServerConfigUtil;
53
54 /**
55  * This class enables a client to get a management view (JSR-77) on a
56  * JBoss Cluster. It contains the entire logic to map all the {@link
57  * org.jboss.management.j2ee.J2EEManagedObject Managed Object (MO) of
58  * all the nodes in the cluster to one MO. The same applies to State
59  * Management and Performance Statistics.
60  *
61  * @author Andreas Schaefer
62  * @version $Revision: 37459 $ <p>
63  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
64  */

65 public class HAManagementService
66         extends ServiceMBeanSupport
67         implements HAManagementServiceMBean
68 {
69
70    // Constants -----------------------------------------------------
71

72    private final static String JavaDoc SERVICE_NAME = "HAManagementService";
73    /**
74     * The default object name.
75     */

76    ObjectName JavaDoc OBJECT_NAME = ObjectNameFactory.create("jboss:service=HAManagement");
77    
78    // Attributes ----------------------------------------------------
79

80    private ObjectName JavaDoc mHAManagementName;
81    private ObjectName JavaDoc mClusterPartitionName;
82    private String JavaDoc mBackgroundPartition = ServerConfigUtil.getDefaultPartitionName();
83    private HAPartition mPartition;
84    
85    // Static --------------------------------------------------------
86

87    // Constructors --------------------------------------------------
88

89    /**
90     * Creates a undefined clustering management service. Before this
91     * MBean can be started the HAPartition must be up and running.
92     * <p/>
93     * ATTENTION: The current implemenation is a brute force one because
94     * it is retrieving the data on every hit. Later on this has to be
95     * resolved with a caching to avoid unecessary round-trips.
96     */

97    public HAManagementService()
98    {
99    }
100    
101    // Public --------------------------------------------------------
102

103    /**
104     **/

105    public Object JavaDoc _getAttribute(ObjectName JavaDoc pName, String JavaDoc pAttribute)
106    {
107       Object JavaDoc lReturn = null;
108       try
109       {
110          lReturn = server.getAttribute(pName, pAttribute);
111       }
112       catch (Exception JavaDoc e)
113       {
114          // Ignore them
115
}
116       return lReturn;
117    }
118
119    /**
120     * @jmx:managed-attribute
121     */

122    public Object JavaDoc getAttribute(ObjectName JavaDoc pName, String JavaDoc pAttribute)
123            throws
124            MBeanException JavaDoc,
125            AttributeNotFoundException JavaDoc,
126            InstanceNotFoundException JavaDoc,
127            ReflectionException JavaDoc,
128            RemoteException JavaDoc
129    {
130       // First we try to get the attribute from the local node
131
// and if not found the look for an instance out in the
132
// node. This does not apply for JVMs.
133
//AS ToDo: JVMs are not supported yet
134
Object JavaDoc lReturn = null;
135       try
136       {
137          lReturn = server.getAttribute(pName, pAttribute);
138       }
139       catch (InstanceNotFoundException JavaDoc infe)
140       {
141          // Ignore and search on the cluster
142
Object JavaDoc[] lArguments = new Object JavaDoc[]{
143             pName,
144             pAttribute
145          };
146          List JavaDoc lValues = null;
147          try
148          {
149             lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
150                     "_getAttribute",
151                     lArguments,
152                new Class JavaDoc[]{ObjectName JavaDoc.class, String JavaDoc.class},
153                false
154             );
155          }
156          catch (Exception JavaDoc e)
157          {
158             throw new RemoteException JavaDoc("Could not get management attributes on the cluster", e);
159          }
160          Iterator JavaDoc i = lValues.iterator();
161          while (i.hasNext())
162          {
163             Object JavaDoc lValue = i.next();
164             if (lValue != null)
165             {
166                lReturn = lValue;
167                break;
168             }
169          }
170          if (lReturn == null)
171          {
172             // Throw the instance not found exception because there is none
173
throw infe;
174          }
175       }
176       return lReturn;
177    }
178
179    /**
180     **/

181    public AttributeList JavaDoc _getAttributes(ObjectName JavaDoc pName, String JavaDoc[] pAttributes)
182    {
183       AttributeList JavaDoc lReturn = null;
184       try
185       {
186          lReturn = server.getAttributes(pName, pAttributes);
187       }
188       catch (Exception JavaDoc e)
189       {
190          // Ignore them
191
}
192       return lReturn;
193    }
194
195    /**
196     * @jmx:managed-attribute
197     */

198    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc pName, String JavaDoc[] pAttributes)
199            throws
200            InstanceNotFoundException JavaDoc,
201            ReflectionException JavaDoc,
202            RemoteException JavaDoc
203    {
204       // First we try to get the attribute from the local node
205
// and if not found the look for an instance out in the
206
// node. This does not apply for JVMs.
207
//AS ToDo: JVMs are not supported yet
208
//AS ToDo: Now only the first list is taken, shall we add a merge capabilities ?
209
AttributeList JavaDoc lReturn = null;
210       try
211       {
212          lReturn = server.getAttributes(pName, pAttributes);
213       }
214       catch (InstanceNotFoundException JavaDoc infe)
215       {
216          // Ignore and search on the cluster
217
Object JavaDoc[] lArguments = new Object JavaDoc[]{
218             pName,
219             pAttributes
220          };
221          List JavaDoc lValues = null;
222          try
223          {
224             lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
225                     "_getAttributes",
226                     lArguments,
227                new Class JavaDoc[]{ObjectName JavaDoc.class, String JavaDoc[].class},
228                false
229             );
230          }
231          catch (Exception JavaDoc e)
232          {
233             throw new RemoteException JavaDoc("Could not get management attributes on the cluster", e);
234          }
235          Iterator JavaDoc i = lValues.iterator();
236          while (i.hasNext())
237          {
238             Object JavaDoc lValue = i.next();
239             if (lValue != null)
240             {
241                lReturn = (AttributeList JavaDoc) lValue;
242                break;
243             }
244          }
245          if (lReturn == null)
246          {
247             // Throw the instance not found exception because there is none
248
throw infe;
249          }
250       }
251       return lReturn;
252    }
253
254    /**
255     * @jmx:managed-attribute
256     */

257    public String JavaDoc getDefaultDomain()
258            throws RemoteException JavaDoc
259    {
260       return J2EEDomain.getDomainName();
261    }
262
263    /**
264     * @jmx:managed-attribute
265     */

266    public Integer JavaDoc getMBeanCount()
267            throws RemoteException JavaDoc
268    {
269       try
270       {
271          return new Integer JavaDoc(queryNames(new ObjectName JavaDoc("*:*"),
272                  null).size());
273       }
274       catch (Exception JavaDoc e)
275       {
276       }
277       return new Integer JavaDoc(0);
278    }
279
280    /**
281     * @jmx:managed-attribute
282     */

283    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc pName)
284            throws
285            IntrospectionException JavaDoc,
286            InstanceNotFoundException JavaDoc,
287            ReflectionException JavaDoc,
288            RemoteException JavaDoc
289    {
290       return server.getMBeanInfo(pName);
291    }
292
293    /**
294     * @jmx:managed-attribute
295     */

296    public javax.management.j2ee.ListenerRegistration JavaDoc getListenerRegistry()
297            throws RemoteException JavaDoc
298    {
299       return null;
300 /*
301       return new ListenerRegistration(
302          (ManagementHome) mContext.getEJBObject().getEJBHome(),
303          new String[] {}
304       );
305 */

306    }
307
308    /**
309     **/

310    public Object JavaDoc _invoke(ObjectName JavaDoc pName, String JavaDoc pOperationName, Object JavaDoc[] pParams, String JavaDoc[] pSignature)
311    {
312       Object JavaDoc lReturn = null;
313       try
314       {
315          log.info("_invoke(), name: " + pName + ", operation: " + pOperationName +
316                  ", params: " + pParams + ", signature: " + pSignature);
317          lReturn = server.invoke(pName,
318                  pOperationName,
319                  pParams,
320                  pSignature);
321       }
322       catch (Exception JavaDoc e)
323       {
324          // Return the exception instead
325
lReturn = e;
326       }
327       return lReturn;
328    }
329
330    /**
331     * @jmx:managed-attribute
332     */

333    public Object JavaDoc invoke(ObjectName JavaDoc pName, String JavaDoc pOperationName, Object JavaDoc[] pParams, String JavaDoc[] pSignature)
334            throws
335            InstanceNotFoundException JavaDoc,
336            MBeanException JavaDoc,
337            ReflectionException JavaDoc,
338            RemoteException JavaDoc
339    {
340       Object JavaDoc lReturn = null;
341       InstanceNotFoundException JavaDoc lException = null;
342       log.info("invoke(), name: " + pName + ", operation: " + pOperationName +
343               ", params: " + pParams + ", signature: " + pSignature);
344       try
345       {
346          lReturn = server.invoke(pName, pOperationName, pParams, pSignature);
347       }
348       catch (InstanceNotFoundException JavaDoc infe)
349       {
350          lException = infe;
351       }
352       Object JavaDoc[] lArguments = new Object JavaDoc[]{
353          pName,
354          pOperationName,
355          pParams,
356          pSignature
357       };
358       List JavaDoc lValues = null;
359       try
360       {
361          log.info("call _invoke()");
362          lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
363                  "_invoke",
364                  lArguments,
365             new Class JavaDoc[]{ObjectName JavaDoc.class, String JavaDoc.class, Object JavaDoc[].class, String JavaDoc[].class},
366             true
367          );
368       }
369       catch (Exception JavaDoc e)
370       {
371          throw new RemoteException JavaDoc("Could not get management attributes on the cluster", e);
372       }
373       Iterator JavaDoc i = lValues.iterator();
374       while (i.hasNext())
375       {
376          Object JavaDoc lValue = i.next();
377          if (lValue instanceof Throwable JavaDoc)
378          {
379             log.debug("invoke a method on the cluster caused an exception: " + lValue);
380             if (lValue instanceof InstanceNotFoundException JavaDoc)
381             {
382                // Go ahead when INFE is found
383
continue;
384             }
385          }
386          // A non-INFE is found therefore ignore all the other INFE
387
lException = null;
388          if (lValue != null)
389          {
390             lReturn = lValue;
391             break;
392          }
393       }
394       if (lException != null)
395       {
396          // Only if all calls throws an INFE it will throw this exception
397
throw lException;
398       }
399       return lReturn;
400    }
401
402    /**
403     * @jmx:managed-attribute
404     */

405    public boolean isRegistered(ObjectName JavaDoc pName)
406            throws RemoteException JavaDoc
407    {
408       return server.isRegistered(pName);
409    }
410
411    /**
412     * @jmx:managed-attribute
413     */

414    public Set JavaDoc queryNames(ObjectName JavaDoc pName, QueryExp JavaDoc pQuery)
415            throws RemoteException JavaDoc
416    {
417       return server.queryNames(pName, pQuery);
418    }
419
420    /**
421     **/

422    public Object JavaDoc _setAttribute(ObjectName JavaDoc pName, Attribute JavaDoc pAttribute)
423    {
424       Object JavaDoc lReturn = null;
425       try
426       {
427          log.info("_setAttribute(), name: " + pName + ", attribute: " + pAttribute);
428          server.setAttribute(pName, pAttribute);
429       }
430       catch (Exception JavaDoc e)
431       {
432          // Return the exception instead
433
lReturn = e;
434       }
435       return lReturn;
436    }
437
438    /**
439     * @jmx:managed-attribute
440     */

441    public void setAttribute(ObjectName JavaDoc pName, Attribute JavaDoc pAttribute)
442            throws
443            AttributeNotFoundException JavaDoc,
444            InstanceNotFoundException JavaDoc,
445            InvalidAttributeValueException JavaDoc,
446            MBeanException JavaDoc,
447            ReflectionException JavaDoc,
448            RemoteException JavaDoc
449    {
450       InstanceNotFoundException JavaDoc lException = null;
451       try
452       {
453          server.setAttribute(pName, pAttribute);
454       }
455       catch (InstanceNotFoundException JavaDoc infe)
456       {
457          lException = infe; // Remember that instance was not found
458
}
459       Object JavaDoc[] lArguments = new Object JavaDoc[]{
460          pName,
461          pAttribute
462       };
463       List JavaDoc lValues = null;
464       try
465       {
466          log.info("call _setAttribute()");
467          lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
468                  "_setAttribute",
469                  lArguments,
470             new Class JavaDoc[]{ObjectName JavaDoc.class, Attribute JavaDoc.class},
471             true
472          );
473       }
474       catch (Exception JavaDoc e)
475       {
476          throw new RemoteException JavaDoc("Could not set management attributes on the cluster", e);
477       }
478       Iterator JavaDoc i = lValues.iterator();
479       while (i.hasNext())
480       {
481          Object JavaDoc lValue = i.next();
482          // If one value is null (because the method does not return a value) then
483
// everything is fine
484
if (lValue instanceof Throwable JavaDoc)
485          {
486             log.debug("invoke a method on the cluster caused an exception: " + lValue);
487             if (lValue instanceof InstanceNotFoundException JavaDoc)
488             {
489                if (lException == null)
490                {
491                   lException = (InstanceNotFoundException JavaDoc) lValue; // Remember this exception
492
}
493             }
494             else
495             {
496                // Only Throwables are returned
497
if (lValue instanceof AttributeNotFoundException JavaDoc)
498                {
499                   throw (AttributeNotFoundException JavaDoc) lValue;
500                }
501                if (lValue instanceof InvalidAttributeValueException JavaDoc)
502                {
503                   throw (InvalidAttributeValueException JavaDoc) lValue;
504                }
505                if (lValue instanceof MBeanException JavaDoc)
506                {
507                   throw (MBeanException JavaDoc) lValue;
508                }
509                if (lValue instanceof ReflectionException JavaDoc)
510                {
511                   throw (ReflectionException JavaDoc) lValue;
512                }
513                throw new RemoteException JavaDoc(lValue.toString());
514             }
515          }
516       }
517       if (lException != null)
518       {
519          throw lException; // Now this exception can be thrown because it has low priority
520
}
521    }
522
523    /**
524     **/

525    public Object JavaDoc _setAttributes(ObjectName JavaDoc pName, AttributeList JavaDoc pAttributes)
526    {
527       Object JavaDoc lReturn = null;
528       try
529       {
530          log.info("_setAttributes(), name: " + pName + ", attribute: " + pAttributes);
531          server.setAttributes(pName, pAttributes);
532       }
533       catch (Exception JavaDoc e)
534       {
535          // Return the exception instead
536
lReturn = e;
537       }
538       return lReturn;
539    }
540
541    /**
542     * @jmx:managed-attribute
543     */

544    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc pName, AttributeList JavaDoc pAttributes)
545            throws
546            InstanceNotFoundException JavaDoc,
547            ReflectionException JavaDoc,
548            RemoteException JavaDoc
549    {
550       Object JavaDoc lReturn = null;
551       InstanceNotFoundException JavaDoc lException = null;
552       try
553       {
554          lReturn = server.setAttributes(pName, pAttributes);
555       }
556       catch (InstanceNotFoundException JavaDoc infe)
557       {
558          lException = infe;
559       }
560       Object JavaDoc[] lArguments = new Object JavaDoc[]{
561          pName,
562          pAttributes
563       };
564       List JavaDoc lValues = null;
565       try
566       {
567          log.info("call _setAttributes()");
568          lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
569                  "_setAttributes",
570                  lArguments,
571             new Class JavaDoc[]{ObjectName JavaDoc.class, AttributeList JavaDoc.class},
572             true
573          );
574       }
575       catch (Exception JavaDoc e)
576       {
577          throw new RemoteException JavaDoc("Could not set management attributes on the cluster", e);
578       }
579       Iterator JavaDoc i = lValues.iterator();
580       while (i.hasNext())
581       {
582          Object JavaDoc lValue = i.next();
583          // If one value is null (because the method does not return a value) then
584
// everything is fine
585
if (lValue instanceof Throwable JavaDoc)
586          {
587             log.debug("set Attributes on the cluster caused an exception: " + lValue);
588             if (lValue instanceof InstanceNotFoundException JavaDoc)
589             {
590                if (lException == null)
591                {
592                   lException = (InstanceNotFoundException JavaDoc) lValue; // Remember this exception
593
}
594             }
595             else
596             {
597                if (lValue instanceof ReflectionException JavaDoc)
598                {
599                   throw (ReflectionException JavaDoc) lValue;
600                }
601                throw new RemoteException JavaDoc(lValue.toString());
602             }
603          }
604       }
605       if (lException != null)
606       {
607          // If no other exception is thrown then the INFE will be thrown here
608
throw lException;
609       }
610       return (AttributeList JavaDoc) lReturn;
611    }
612
613    /**
614     **/

615    public Object JavaDoc _createMBean(String JavaDoc pClass,
616                               ObjectName JavaDoc pName,
617                               Object JavaDoc[] pParameters,
618                               String JavaDoc[] pSignature)
619    {
620       Object JavaDoc lReturn = null;
621       try
622       {
623          log.info("_createMBean(), name: " + pName);
624          lReturn = server.createMBean(pClass, pName, pParameters, pSignature);
625       }
626       catch (Exception JavaDoc e)
627       {
628          // Return the exception instead
629
lReturn = e;
630       }
631       return lReturn;
632    }
633
634    /**
635     * @jmx:managed-attribute
636     */

637    public ObjectInstance JavaDoc createMBean(String JavaDoc pClass,
638                                      ObjectName JavaDoc pName,
639                                      Object JavaDoc[] pParameters,
640                                      String JavaDoc[] pSignature)
641            throws
642            InstanceAlreadyExistsException JavaDoc,
643            MBeanException JavaDoc,
644            MBeanRegistrationException JavaDoc,
645            NotCompliantMBeanException JavaDoc,
646            ReflectionException JavaDoc,
647            RemoteException JavaDoc
648    {
649       List JavaDoc lValues = null;
650       Object JavaDoc[] lArguments = new Object JavaDoc[]{
651          pClass,
652          pName,
653          pParameters,
654          pSignature
655       };
656       try
657       {
658          log.info("call _createMBean()");
659          lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
660                  "_createMBean",
661                  lArguments,
662             new Class JavaDoc[]{String JavaDoc.class, ObjectName JavaDoc.class, Object JavaDoc[].class, String JavaDoc[].class},
663             false
664          );
665       }
666       catch (Exception JavaDoc e)
667       {
668          //AS ToDo: must be checked later how to go ahead here
669
throw new RemoteException JavaDoc("Could not create a MBean on the cluster", e);
670       }
671       Iterator JavaDoc i = lValues.iterator();
672       ObjectInstance JavaDoc lInstance = null;
673       Throwable JavaDoc lException = null;
674       while (i.hasNext())
675       {
676          Object JavaDoc lValue = i.next();
677          if (lValue instanceof ObjectInstance JavaDoc)
678          {
679             if (lInstance == null)
680             {
681                lInstance = (ObjectInstance JavaDoc) lValue;
682             }
683          }
684          else if (lValue instanceof Throwable JavaDoc)
685          {
686             if (lException == null)
687             {
688                lException = (Throwable JavaDoc) lValue;
689             }
690          }
691       }
692       if (lException != null)
693       {
694          if (lInstance != null)
695          {
696             // Remove all existing MBeans
697
try
698             {
699                unregisterMBean(lInstance.getObjectName());
700             }
701             catch (Exception JavaDoc e)
702             {
703                // Ignore any exception
704
}
705          }
706          if (lException instanceof InstanceAlreadyExistsException JavaDoc)
707          {
708             throw