View Javadoc

1   
2   /* $Id: OpenMBeanInvocationManager.java 4012 2005-12-14 18:08:08Z dbernstein $
3    *
4    * (Created on Dec 12, 2005
5    *
6    * Copyright (C) 2005 Internet Archive.
7    *  
8    * This file is part of the Heritrix Cluster Controller (crawler.archive.org).
9    *  
10   * HCC is free software; you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser Public License as published by
12   * the Free Software Foundation; either version 2.1 of the License, or
13   * any later version.
14   * 
15   * Heritrix is distributed in the hope that it will be useful, 
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU Lesser Public License
21   * along with Heritrix; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   */
24  package org.archive.hcc.util.jmx;
25  
26  import java.lang.reflect.InvocationTargetException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import javax.management.MBeanException;
31  import javax.management.ReflectionException;
32  import javax.management.openmbean.OpenMBeanOperationInfo;
33  
34  public class OpenMBeanInvocationManager {
35      private List<MBeanOperation> operations = new ArrayList<MBeanOperation>();
36  
37      private OpenMBeanOperationInfo[] info = new OpenMBeanOperationInfo[0];
38  
39      public Object invoke(String actionName, Object[] params, String[] signature)
40              throws MBeanException,
41              ReflectionException {
42          // get the operation
43          MBeanOperation operation = getOperation(actionName, params, signature);
44  
45          if (operation != null) {
46              MBeanInvocation invocation = operation.createInvocation(
47                      params,
48                      signature);
49              return invocation.invoke();
50          }
51  
52          throw new ReflectionException(new InvocationTargetException(
53                  null,
54                  "no operation found with name and " + "signature: actionName="
55                          + actionName + "; params=" + params + "; signature"));
56  
57      }
58  
59      protected MBeanOperation getOperation(
60              String actionName,
61              Object[] params,
62              String[] signature) throws ReflectionException {
63  
64          for (MBeanOperation operation : operations) {
65              if (operation.matches(actionName, signature)) {
66                  return operation;
67              }
68  
69          }
70  
71          throw new ReflectionException(new InvocationTargetException(
72                  null,
73                  "The method with the specified signature was not found:"
74                          + actionName + "; signature=" + signature));
75  
76      }
77  
78      public void addMBeanOperation(MBeanOperation operation) {
79          this.operations.add(operation);
80          setInfo();
81      }
82  
83      public OpenMBeanOperationInfo[] getInfo() {
84          return this.info;
85      }
86  
87      private void setInfo() {
88          OpenMBeanOperationInfo[] newInfo = new OpenMBeanOperationInfo[this.operations
89                  .size()];
90          for (int i = 0; i < newInfo.length; i++) {
91              newInfo[i] = this.operations.get(i).getInfo();
92          }
93  
94          this.info = newInfo;
95      }
96  }