src/share/classes/sun/management/DiagnosticCommandImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  67     public AttributeList setAttributes(AttributeList attributes) {
  68         return new AttributeList();
  69     }
  70 
  71     private class Wrapper {
  72 
  73         String name;
  74         String cmd;
  75         DiagnosticCommandInfo info;
  76         Permission permission;
  77 
  78         Wrapper(String name, String cmd, DiagnosticCommandInfo info)
  79                 throws InstantiationException {
  80             this.name = name;
  81             this.cmd = cmd;
  82             this.info = info;
  83             this.permission = null;
  84             Exception cause = null;
  85             if (info.getPermissionClass() != null) {
  86                 try {
  87                     Class c = Class.forName(info.getPermissionClass());
  88                     if (info.getPermissionAction() == null) {
  89                         try {
  90                             Constructor constructor = c.getConstructor(String.class);
  91                             permission = (Permission) constructor.newInstance(info.getPermissionName());
  92 
  93                         } catch (InstantiationException | IllegalAccessException
  94                                 | IllegalArgumentException | InvocationTargetException
  95                                 | NoSuchMethodException | SecurityException ex) {
  96                             cause = ex;
  97                         }
  98                     }
  99                     if (permission == null) {
 100                         try {
 101                             Constructor constructor = c.getConstructor(String.class, String.class);
 102                             permission = (Permission) constructor.newInstance(
 103                                     info.getPermissionName(),
 104                                     info.getPermissionAction());
 105                         } catch (InstantiationException | IllegalAccessException
 106                                 | IllegalArgumentException | InvocationTargetException
 107                                 | NoSuchMethodException | SecurityException ex) {
 108                             cause = ex;
 109                         }
 110                     }
 111                 } catch (ClassNotFoundException ex) { }
 112                 if (permission == null) {
 113                     InstantiationException iex =
 114                             new InstantiationException("Unable to instantiate required permission");
 115                     iex.initCause(cause);
 116                 }
 117             }
 118         }
 119 
 120         public String execute(String[] args) {
 121             if (permission != null) {


 141         }
 142     }
 143 
 144     DiagnosticCommandImpl(VMManagement jvm) {
 145         this.jvm = jvm;
 146         isSupported = jvm.isRemoteDiagnosticCommandsSupported();
 147     }
 148 
 149     private static class OperationInfoComparator implements Comparator<MBeanOperationInfo> {
 150         @Override
 151         public int compare(MBeanOperationInfo o1, MBeanOperationInfo o2) {
 152             return o1.getName().compareTo(o2.getName());
 153         }
 154     }
 155 
 156     @Override
 157     public MBeanInfo getMBeanInfo() {
 158         SortedSet<MBeanOperationInfo> operations = new TreeSet<>(new OperationInfoComparator());
 159         Map<String, Wrapper> wrappersmap;
 160         if (!isSupported) {
 161             wrappersmap = (Map<String, Wrapper>) Collections.EMPTY_MAP;
 162         } else {
 163             try {
 164                 String[] command = getDiagnosticCommands();
 165                 DiagnosticCommandInfo[] info = getDiagnosticCommandInfo(command);
 166                 MBeanParameterInfo stringArgInfo[] = new MBeanParameterInfo[]{
 167                     new MBeanParameterInfo("arguments", strArrayClassName,
 168                     "Array of Diagnostic Commands Arguments and Options")
 169                 };
 170                 wrappersmap = new HashMap<>();
 171                 for (int i = 0; i < command.length; i++) {
 172                     String name = transform(command[i]);
 173                     try {
 174                         Wrapper w = new Wrapper(name, command[i], info[i]);
 175                         wrappersmap.put(name, w);
 176                         operations.add(new MBeanOperationInfo(
 177                                 w.name,
 178                                 w.info.getDescription(),
 179                                 (w.info.getArgumentsInfo() == null
 180                                     || w.info.getArgumentsInfo().isEmpty())
 181                                     ? null : stringArgInfo,
 182                                 strClassName,
 183                                 MBeanOperationInfo.ACTION_INFO,
 184                                 commandDescriptor(w)));
 185                     } catch (InstantiationException ex) {
 186                         // If for some reasons the creation of a diagnostic command
 187                         // wrappers fails, the diagnostic command is just ignored
 188                         // and won't appear in the DynamicMBean
 189                     }
 190                 }
 191             } catch (IllegalArgumentException | UnsupportedOperationException e) {
 192                 wrappersmap = (Map<String, Wrapper>) Collections.EMPTY_MAP;
 193             }
 194         }
 195         wrappers =  Collections.unmodifiableMap(wrappersmap);
 196         HashMap<String, Object> map = new HashMap<>();
 197         map.put("immutableInfo", "false");
 198         map.put("interfaceClassName","com.sun.management.DiagnosticCommandMBean");
 199         map.put("mxbean", "false");
 200         Descriptor desc = new ImmutableDescriptor(map);
 201         return new MBeanInfo(
 202                 this.getClass().getName(),
 203                 "Diagnostic Commands",
 204                 null, // attributes
 205                 null, // constructors
 206                 operations.toArray(new MBeanOperationInfo[operations.size()]), // operations
 207                 getNotificationInfo(), // notifications
 208                 desc);
 209     }
 210 
 211     @Override
 212     public Object invoke(String actionName, Object[] params, String[] signature)


   1 /*
   2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  67     public AttributeList setAttributes(AttributeList attributes) {
  68         return new AttributeList();
  69     }
  70 
  71     private class Wrapper {
  72 
  73         String name;
  74         String cmd;
  75         DiagnosticCommandInfo info;
  76         Permission permission;
  77 
  78         Wrapper(String name, String cmd, DiagnosticCommandInfo info)
  79                 throws InstantiationException {
  80             this.name = name;
  81             this.cmd = cmd;
  82             this.info = info;
  83             this.permission = null;
  84             Exception cause = null;
  85             if (info.getPermissionClass() != null) {
  86                 try {
  87                     Class<?> c = Class.forName(info.getPermissionClass());
  88                     if (info.getPermissionAction() == null) {
  89                         try {
  90                             Constructor<?> constructor = c.getConstructor(String.class);
  91                             permission = (Permission) constructor.newInstance(info.getPermissionName());
  92 
  93                         } catch (InstantiationException | IllegalAccessException
  94                                 | IllegalArgumentException | InvocationTargetException
  95                                 | NoSuchMethodException | SecurityException ex) {
  96                             cause = ex;
  97                         }
  98                     }
  99                     if (permission == null) {
 100                         try {
 101                             Constructor<?> constructor = c.getConstructor(String.class, String.class);
 102                             permission = (Permission) constructor.newInstance(
 103                                     info.getPermissionName(),
 104                                     info.getPermissionAction());
 105                         } catch (InstantiationException | IllegalAccessException
 106                                 | IllegalArgumentException | InvocationTargetException
 107                                 | NoSuchMethodException | SecurityException ex) {
 108                             cause = ex;
 109                         }
 110                     }
 111                 } catch (ClassNotFoundException ex) { }
 112                 if (permission == null) {
 113                     InstantiationException iex =
 114                             new InstantiationException("Unable to instantiate required permission");
 115                     iex.initCause(cause);
 116                 }
 117             }
 118         }
 119 
 120         public String execute(String[] args) {
 121             if (permission != null) {


 141         }
 142     }
 143 
 144     DiagnosticCommandImpl(VMManagement jvm) {
 145         this.jvm = jvm;
 146         isSupported = jvm.isRemoteDiagnosticCommandsSupported();
 147     }
 148 
 149     private static class OperationInfoComparator implements Comparator<MBeanOperationInfo> {
 150         @Override
 151         public int compare(MBeanOperationInfo o1, MBeanOperationInfo o2) {
 152             return o1.getName().compareTo(o2.getName());
 153         }
 154     }
 155 
 156     @Override
 157     public MBeanInfo getMBeanInfo() {
 158         SortedSet<MBeanOperationInfo> operations = new TreeSet<>(new OperationInfoComparator());
 159         Map<String, Wrapper> wrappersmap;
 160         if (!isSupported) {
 161             wrappersmap = Collections.emptyMap();
 162         } else {
 163             try {
 164                 String[] command = getDiagnosticCommands();
 165                 DiagnosticCommandInfo[] info = getDiagnosticCommandInfo(command);
 166                 MBeanParameterInfo stringArgInfo[] = new MBeanParameterInfo[]{
 167                     new MBeanParameterInfo("arguments", strArrayClassName,
 168                     "Array of Diagnostic Commands Arguments and Options")
 169                 };
 170                 wrappersmap = new HashMap<>();
 171                 for (int i = 0; i < command.length; i++) {
 172                     String name = transform(command[i]);
 173                     try {
 174                         Wrapper w = new Wrapper(name, command[i], info[i]);
 175                         wrappersmap.put(name, w);
 176                         operations.add(new MBeanOperationInfo(
 177                                 w.name,
 178                                 w.info.getDescription(),
 179                                 (w.info.getArgumentsInfo() == null
 180                                     || w.info.getArgumentsInfo().isEmpty())
 181                                     ? null : stringArgInfo,
 182                                 strClassName,
 183                                 MBeanOperationInfo.ACTION_INFO,
 184                                 commandDescriptor(w)));
 185                     } catch (InstantiationException ex) {
 186                         // If for some reasons the creation of a diagnostic command
 187                         // wrappers fails, the diagnostic command is just ignored
 188                         // and won't appear in the DynamicMBean
 189                     }
 190                 }
 191             } catch (IllegalArgumentException | UnsupportedOperationException e) {
 192                 wrappersmap = Collections.emptyMap();
 193             }
 194         }
 195         wrappers =  Collections.unmodifiableMap(wrappersmap);
 196         HashMap<String, Object> map = new HashMap<>();
 197         map.put("immutableInfo", "false");
 198         map.put("interfaceClassName","com.sun.management.DiagnosticCommandMBean");
 199         map.put("mxbean", "false");
 200         Descriptor desc = new ImmutableDescriptor(map);
 201         return new MBeanInfo(
 202                 this.getClass().getName(),
 203                 "Diagnostic Commands",
 204                 null, // attributes
 205                 null, // constructors
 206                 operations.toArray(new MBeanOperationInfo[operations.size()]), // operations
 207                 getNotificationInfo(), // notifications
 208                 desc);
 209     }
 210 
 211     @Override
 212     public Object invoke(String actionName, Object[] params, String[] signature)