public static double getProcessCpuLoad() { double cpuLoad; try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem"); AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" }); if(list.isEmpty()) { return Double.NaN; } Attribute att = (Attribute) list.get(0); Double value = (Double) att.getValue(); if(value == -1.0) { return Double.NaN; } cpuLoad = value * 100d; } catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException err) { cpuLoad = Double.NaN; } return cpuLoad; }
/** * No user can call createBean or unregisterBean of GemFire Domain */ @Test @ConnectionConfiguration(user = "super-user", password = "1234567") public void testNoAccessWithWhoever() throws Exception { MBeanServerConnection con = connectionRule.getMBeanServerConnection(); assertThatThrownBy( () -> con.createMBean("FakeClassName", new ObjectName("GemFire", "name", "foo"))) .isInstanceOf(SecurityException.class); assertThatThrownBy(() -> con.unregisterMBean(new ObjectName("GemFire", "name", "foo"))) .isInstanceOf(SecurityException.class); // user is allowed to create beans of other domains assertThatThrownBy( () -> con.createMBean("FakeClassName", new ObjectName("OtherDomain", "name", "foo"))) .isInstanceOf(ReflectionException.class); }
public static double getProcessCpuLoad(MBeanServerConnection mbs) throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, ReflectionException, IOException { ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem"); AttributeList list = mbs.getAttributes(name, new String[]{"ProcessCpuLoad"}); if (list.isEmpty()) { return 0.0; } Attribute att = (Attribute) list.get(0); Double value = (Double) att.getValue(); // usually takes a couple of seconds before we get real values if (value == -1.0) { return 0.0; } // returns a percentage value with 1 decimal point precision return ((int) (value * 1000) / 10.0); }
/** * Load a class with the specified loader, or with this object * class loader if the specified loader is null. **/ static Class<?> loadClass(String className, ClassLoader loader) throws ReflectionException { Class<?> theClass; if (className == null) { throw new RuntimeOperationsException(new IllegalArgumentException("The class name cannot be null"), "Exception occurred during object instantiation"); } ReflectUtil.checkPackageAccess(className); try { if (loader == null) loader = MBeanInstantiator.class.getClassLoader(); if (loader != null) { theClass = Class.forName(className, false, loader); } else { theClass = Class.forName(className); } } catch (ClassNotFoundException e) { throw new ReflectionException(e, "The MBean class could not be loaded"); } return theClass; }
/** * Always fails since the MBeanServerDelegate MBean has no operation. * * @param actionName The name of the action to be invoked. * @param params An array containing the parameters to be set when the * action is invoked. * @param signature An array containing the signature of the action. * * @return The object returned by the action, which represents * the result of invoking the action on the MBean specified. * * @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE> * thrown by the MBean's invoked method. * @exception ReflectionException Wraps a * <CODE>java.lang.Exception</CODE> thrown while trying to invoke * the method. */ public Object invoke(String actionName, Object params[], String signature[]) throws MBeanException, ReflectionException { // Check that operation name is not null. // if (actionName == null) { final RuntimeException r = new IllegalArgumentException("Operation name cannot be null"); throw new RuntimeOperationsException(r, "Exception occurred trying to invoke the operation on the MBean"); } throw new ReflectionException( new NoSuchMethodException(actionName), "The operation with name " + actionName + " could not be found"); }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { checkCreate(className); SecurityManager sm = System.getSecurityManager(); if (sm == null) { Object object = getMBeanServer().instantiate(className); checkClassLoader(object); return getMBeanServer().registerMBean(object, name); } else { return getMBeanServer().createMBean(className, name); } }
@Override public AttributeList setAttributes(final AttributeList attributes) { AttributeList result = new AttributeList(); for (Object attributeObject : attributes) { Attribute attribute = (Attribute) attributeObject; try { setAttribute(attribute); result.add(attribute); } catch (final InvalidAttributeValueException | AttributeNotFoundException | MBeanException | ReflectionException e) { LOG.warn("Setting attribute {} failed on {}", attribute.getName(), moduleIdentifier, e); throw new IllegalArgumentException( "Setting attribute failed - " + attribute.getName() + " on " + moduleIdentifier, e); } } return result; }
/** * Gets the class for the specified class name using the specified * class loader */ public Class<?> findClass(String className, ObjectName aLoader) throws ReflectionException, InstanceNotFoundException { if (aLoader == null) throw new RuntimeOperationsException(new IllegalArgumentException(), "Null loader passed in parameter"); // Retrieve the class loader from the repository ClassLoader loader = null; synchronized (this) { loader = getClassLoader(aLoader); } if (loader == null) { throw new InstanceNotFoundException("The loader named " + aLoader + " is not registered in the MBeanServer"); } return findClass(className,loader); }
void setAttribute(Object resource, String attribute, Object value, Object cookie) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { final M cm = setters.get(attribute); if (cm == null) { final String msg; if (getters.containsKey(attribute)) msg = "Read-only attribute: " + attribute; else msg = "No such attribute: " + attribute; throw new AttributeNotFoundException(msg); } introspector.invokeSetter(attribute, cm, resource, value, cookie); }
/** * Invokes an operation on an MBean. * * @param onameStr * The name of the MBean. * @param operation * The name of the operation to invoke. * @param parameters * An array of Strings containing the parameters to the * operation. They will be converted to the appropriate types to * call the requested operation. * @return The value returned by the requested operation. */ private Object invokeOperationInternal(String onameStr, String operation, String[] parameters) throws OperationsException, MBeanException, ReflectionException { ObjectName oname = new ObjectName(onameStr); MBeanOperationInfo methodInfo = registry.getMethodInfo(oname, operation); MBeanParameterInfo[] signature = methodInfo.getSignature(); String[] signatureTypes = new String[signature.length]; Object[] values = new Object[signature.length]; for (int i = 0; i < signature.length; i++) { MBeanParameterInfo pi = signature[i]; signatureTypes[i] = pi.getType(); values[i] = registry.convertValue(pi.getType(), parameters[i]); } return mBeanServer.invoke(oname, operation, values, signatureTypes); }
public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { try { return mbsc.getAttribute(objectName, attribute); } catch (Exception ex) { throw new MBeanException(ex); } }
@Override public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { try { return folder.getProperty(attribute).getValue(); } catch (IntrospectionException ex) { throw new MBeanException(ex); } }
@Override public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { try { folder.getProperty(attribute.getName()).setValue(attribute.getValue()); } catch (IntrospectionException ex) { throw new MBeanException(ex); } }
@Override public synchronized ObjectInstance createMBean(String className, ObjectName name, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { throw new UnsupportedOperationException("Not supported by PogamutMBeanServer yet..."); }
@Override public synchronized ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { throw new UnsupportedOperationException("Not supported by PogamutMBeanServer yet..."); }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object params[], String signature[]) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { checkCreate(className); SecurityManager sm = System.getSecurityManager(); if (sm == null) { Object object = getMBeanServer().instantiate(className, loaderName, params, signature); checkClassLoader(object); return getMBeanServer().registerMBean(object, name); } else { return getMBeanServer().createMBean(className, name, loaderName, params, signature); } }
/** * Call <code>checkWrite()</code>, then forward this method to the * wrapped object. */ public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { checkWrite(); getMBeanServer().setAttribute(name, attribute); }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public Object instantiate(String className, ObjectName loaderName, Object params[], String signature[]) throws ReflectionException, MBeanException, InstanceNotFoundException { checkCreate(className); return getMBeanServer().instantiate(className, loaderName, params, signature); }
/** * Call <code>checkRead()</code>, then forward this method to the * wrapped object. */ @Deprecated public ObjectInputStream deserialize(String className, ObjectName loaderName, byte[] data) throws InstanceNotFoundException, OperationsException, ReflectionException { checkRead(); return getMBeanServer().deserialize(className, loaderName, data); }
/** * Sets an MBean attribute's value. */ private void setAttributeInternal(String onameStr, String attributeName, String value) throws OperationsException, MBeanException, ReflectionException { ObjectName oname=new ObjectName( onameStr ); String type=registry.getType(oname, attributeName); Object valueObj=registry.convertValue(type, value ); mBeanServer.setAttribute( oname, new Attribute(attributeName, valueObj)); }
public static Object getNMSPlayer( Player player ) throws ReflectionException { try { Method method = player.getClass().getMethod( "getHandle" ); return method.invoke( player ); } catch ( NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) { throw new ReflectionException( e, "can't get EntityPlayer" ); } }
/** * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the * managed resources {@link ClassLoader} before allowing the invocation to occur. * @see javax.management.modelmbean.ModelMBean#invoke */ @Override public Object invoke(String opName, Object[] opArgs, String[] sig) throws MBeanException, ReflectionException { ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); return super.invoke(opName, opArgs, sig); } finally { Thread.currentThread().setContextClassLoader(currentClassLoader); } }
/** * Call <code>checkWrite()</code>, then forward this method to the * wrapped object. */ public Object invoke(ObjectName name, String operationName, Object params[], String signature[]) throws InstanceNotFoundException, MBeanException, ReflectionException { checkWrite(); checkMLetMethods(name, operationName); return getMBeanServer().invoke(name, operationName, params, signature); }
@Override public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { updateJmxCache(); synchronized(this) { Attribute a = attrCache.get(attribute); if (a == null) { throw new AttributeNotFoundException(attribute +" not found"); } if (LOG.isDebugEnabled()) { LOG.debug(attribute +": "+ a); } return a.getValue(); } }
/** * Call <code>checkRead()</code>, then forward this method to the * wrapped object. */ public Object getAttribute(ObjectName name, String attribute) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException { checkRead(); return getMBeanServer().getAttribute(name, attribute); }
@SuppressWarnings("IllegalCatch") @Override public Object invoke(final String actionName, final Object[] params, final String[] signature) throws MBeanException, ReflectionException { if ("validate".equals(actionName) && (params == null || params.length == 0) && (signature == null || signature.length == 0)) { try { validate(); } catch (final Exception e) { throw new MBeanException(ValidationException.createForSingleException(moduleIdentifier, e)); } return Void.TYPE; } return super.invoke(actionName, params, signature); }
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { return createMBean(className, name, loaderName, (Object[]) null, (String[]) null); }
public Object getAttribute(ObjectName name, String attribute) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException { if (name == null) { throw new RuntimeOperationsException(new IllegalArgumentException("Object name cannot be null"), "Exception occurred trying to invoke the getter on the MBean"); } if (attribute == null) { throw new RuntimeOperationsException(new IllegalArgumentException("Attribute cannot be null"), "Exception occurred trying to invoke the getter on the MBean"); } name = nonDefaultDomain(name); if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) { MBEANSERVER_LOGGER.logp(Level.FINER, DefaultMBeanServerInterceptor.class.getName(), "getAttribute", "Attribute = " + attribute + ", ObjectName = " + name); } final DynamicMBean instance = getMBean(name); checkMBeanPermission(instance, attribute, name, "getAttribute"); try { return instance.getAttribute(attribute); } catch (AttributeNotFoundException e) { throw e; } catch (Throwable t) { rethrowMaybeMBeanException(t); throw new AssertionError(); // not reached } }
/** * Call <code>checkRead()</code>, then forward this method to the * wrapped object. */ public MBeanInfo getMBeanInfo(ObjectName name) throws InstanceNotFoundException, IntrospectionException, ReflectionException { checkRead(); return getMBeanServer().getMBeanInfo(name); }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public Object instantiate(String className, Object params[], String signature[]) throws ReflectionException, MBeanException { checkCreate(className); return getMBeanServer().instantiate(className, params, signature); }
private JMXConnector connectToServer(JMXConnectorServer server) throws IOException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, ReflectionException, MBeanException { JMXServiceURL url = server.getAddress(); Map<String, Object> env = new HashMap<String, Object>(); JMXConnector connector = JMXConnectorFactory.connect(url, env); System.out.println("DEBUG: Client connected to RMI at: " + url); return connector; }
/** * * @param authentication * @return Authentication */ @Override public Authentication authenticate(Authentication authentication) { Realm realm; Set<GrantedAuthority> auths = new HashSet<>(); try { realm = getTomcatContextRealm(); if(realm instanceof NullRealm) { throw new ProviderNotFoundException("No Realms configured for Jwala to Authenticate"); } Principal principal = realm.authenticate(authentication.getName(), authentication.getCredentials().toString()); if (principal == null) { throw new BadCredentialsException("Username or Password not found."); } else { if (principal instanceof GenericPrincipal) { String[] roles = ((GenericPrincipal) principal).getRoles(); for (String role : roles) { auths.add(new SimpleGrantedAuthority(role)); } } GrantedAuthoritiesMapperImpl grantedAuthoritiesMapper = new GrantedAuthoritiesMapperImpl(); return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuthoritiesMapper.mapAuthorities(auths)); } } catch (AttributeNotFoundException | InstanceNotFoundException | MBeanException | ReflectionException e) { LOGGER.error("Error getting realms", e); throw new ProviderNotFoundException(e.getMessage()); } }
/** * Call <code>checkRead()</code>, then forward this method to the * wrapped object. */ @Deprecated public ObjectInputStream deserialize(String className, byte[] data) throws OperationsException, ReflectionException { checkRead(); return getMBeanServer().deserialize(className, data); }
/** * * @return Tomcat Realms * @throws AttributeNotFoundException * @throws InstanceNotFoundException * @throws MBeanException * @throws ReflectionException */ public Realm getTomcatContextRealm() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException { try { ObjectName name = new ObjectName("Catalina", "type", "Engine"); Engine engine = (Engine) JwalaAuthenticationProvider.getmBeanServer().getAttribute(name, "managedResource"); return engine.getRealm(); } catch (MalformedObjectNameException ex) { LOGGER.error("Invalid Realm", ex); } return null; }
public Object instantiate(String className) throws ReflectionException, MBeanException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { // there are no methods to be invoked return null; }