You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sirona.apache.org by ol...@apache.org on 2014/09/08 04:50:34 UTC
svn commit: r1623259 - in /incubator/sirona/trunk:
core/src/main/java/org/apache/sirona/util/
server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/
server/reporting/reporting-ui/src/main/webapp/js/app/controllers/
server/rep...
Author: olamy
Date: Mon Sep 8 02:50:33 2014
New Revision: 1623259
URL: http://svn.apache.org/r1623259
Log:
jmx operation invocation
Added:
incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java (with props)
Modified:
incubator/sirona/trunk/core/src/main/java/org/apache/sirona/util/ClassLoaders.java
incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXServices.java
incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/controllers/controllers-jmx.js
incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/services/services.js
incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/partials/jmx.html
Modified: incubator/sirona/trunk/core/src/main/java/org/apache/sirona/util/ClassLoaders.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/core/src/main/java/org/apache/sirona/util/ClassLoaders.java?rev=1623259&r1=1623258&r2=1623259&view=diff
==============================================================================
--- incubator/sirona/trunk/core/src/main/java/org/apache/sirona/util/ClassLoaders.java (original)
+++ incubator/sirona/trunk/core/src/main/java/org/apache/sirona/util/ClassLoaders.java Mon Sep 8 02:50:33 2014
@@ -16,7 +16,7 @@
*/
package org.apache.sirona.util;
-public final class ClassLoaders {
+public class ClassLoaders {
public static ClassLoader current() {
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
if (tccl != null) {
Added: incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java?rev=1623259&view=auto
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java (added)
+++ incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java Mon Sep 8 02:50:33 2014
@@ -0,0 +1,70 @@
+package org.apache.sirona.reporting.web.jmx;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ */
+public class JMXInvocationRequest
+ implements Serializable
+{
+
+ private String mbeanEncodedName;
+
+ private String operationName;
+
+ private List<String> parameters;
+
+ public JMXInvocationRequest()
+ {
+ // no op
+ }
+
+ public JMXInvocationRequest( String mbeanEncodedName, String operationName, List<String> parameters )
+ {
+ this.mbeanEncodedName = mbeanEncodedName;
+ this.operationName = operationName;
+ this.parameters = parameters;
+ }
+
+ public String getMbeanEncodedName()
+ {
+ return mbeanEncodedName;
+ }
+
+ public void setMbeanEncodedName( String mbeanEncodedName )
+ {
+ this.mbeanEncodedName = mbeanEncodedName;
+ }
+
+ public String getOperationName()
+ {
+ return operationName;
+ }
+
+ public void setOperationName( String operationName )
+ {
+ this.operationName = operationName;
+ }
+
+ public List<String> getParameters()
+ {
+ return parameters;
+ }
+
+ public void setParameters( List<String> parameters )
+ {
+ this.parameters = parameters;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "JMXInvocationRequest{" +
+ "mbeanEncodedName='" + mbeanEncodedName + '\'' +
+ ", operationName='" + operationName + '\'' +
+ ", parameters=" + parameters +
+ '}';
+ }
+}
Propchange: incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXInvocationRequest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified: incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXServices.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXServices.java?rev=1623259&r1=1623258&r2=1623259&view=diff
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXServices.java (original)
+++ incubator/sirona/trunk/server/reporting/reporting-api/src/main/java/org/apache/sirona/reporting/web/jmx/JMXServices.java Mon Sep 8 02:50:33 2014
@@ -18,6 +18,7 @@ package org.apache.sirona.reporting.web.
import org.apache.commons.codec.binary.Base64;
import org.apache.sirona.SironaException;
+import org.apache.sirona.configuration.Configuration;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
@@ -32,7 +33,9 @@ import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
+import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@@ -40,12 +43,16 @@ import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Array;
+import java.lang.reflect.Field;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.apache.sirona.util.ClassLoaders;
/**
* @since 0.3
@@ -57,6 +64,35 @@ public class JMXServices
private static final String EMPTY_STRING = "";
+ private static final boolean METHOD_INVOCATION_ALLOWED =
+ Configuration.is( Configuration.CONFIG_PROPERTY_PREFIX + "jmx.method.allowed", true );
+
+ private static final Map<String, Class<?>> WRAPPERS = new HashMap<String, Class<?>>();
+
+ static
+ {
+ for ( final Class<?> c : Arrays.<Class<?>>asList( Byte.class, //
+ Short.class, //
+ Integer.class, //
+ Long.class, //
+ Float.class, //
+ Double.class, //
+ Character.class, //
+ Boolean.class ) )
+ {
+ try
+ {
+ final Field f = c.getField( "TYPE" );
+ Class<?> p = (Class<?>) f.get( null );
+ WRAPPERS.put( p.getName(), c );
+ }
+ catch ( Exception e )
+ {
+ throw new AssertionError( e );
+ }
+ }
+ }
+
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
public JMXNode root()
@@ -75,15 +111,6 @@ public class JMXServices
{
final ObjectName name = new ObjectName( new String( Base64.decodeBase64( encodedName ) ) );
-/*
- .set("objectname", name.toString())
- .set("objectnameHash", Base64.encodeBase64URLSafeString(name.toString().getBytes()))
- .set("classname", info.getClassName())
- .set("description", value(info.getDescription()))
- .set("attributes", attributes(name, info))
- .set("operations", operations(info))
- */
-
final MBeanInfo info = server.getMBeanInfo( name );
MBeanInformations mBeanInformations = new MBeanInformations( name.toString(), //
@@ -101,6 +128,149 @@ public class JMXServices
}
+ @POST
+ @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+ public String invoke( JMXInvocationRequest request )
+ {
+
+ if ( !METHOD_INVOCATION_ALLOWED )
+ {
+ throw new SironaException( "Method invocation not allowed" );
+ }
+
+ try
+ {
+ final ObjectName name =
+ new ObjectName( new String( Base64.decodeBase64( request.getMbeanEncodedName() ) ) );
+ final MBeanInfo info = server.getMBeanInfo( name );
+ for ( final MBeanOperationInfo op : info.getOperations() )
+ {
+ if ( op.getName().equals( request.getOperationName() ) )
+ {
+ final MBeanParameterInfo[] signature = op.getSignature();
+ final String[] sign = new String[signature.length];
+ for ( int i = 0; i < sign.length; i++ )
+ {
+ sign[i] = signature[i].getType();
+ }
+ final Object result = server.invoke( name, request.getOperationName(),
+ convertParams( signature, request.getParameters() ), sign );
+ return "<div>Method was invoked and returned:</div>" + value( result );
+ }
+ }
+ }
+ catch ( final Exception e )
+ {
+ return "<div class=\"alert alert-error\">\n" +
+ "\n" + e.getMessage() + "\n" +
+ "</div>";
+ }
+
+ return "<div class=\"alert alert-error\">Operation" + request.getOperationName() + " not found.</div>";
+
+
+ }
+
+ private Object[] convertParams( final MBeanParameterInfo[] signature, final List<String> params )
+ {
+ if ( params == null )
+ {
+ return null;
+ }
+
+ final Object[] convertedParams = new Object[signature.length];
+ for ( int i = 0; i < signature.length; i++ )
+ {
+ if ( i < params.size() )
+ {
+ convertedParams[i] = convert( signature[i].getType(), params.get( i ) );
+ }
+ else
+ {
+ convertedParams[i] = null;
+ }
+ }
+ return convertedParams;
+ }
+
+ public static Object convert( final String type, final String value )
+ {
+ try
+ {
+ if ( WRAPPERS.containsKey( type ) )
+ {
+ if ( type.equals( Character.TYPE.getName() ) )
+ {
+ return value.charAt( 0 );
+ }
+ return tryStringConstructor( WRAPPERS.get( type ).getName(), value );
+ }
+
+ if ( type.equals( Character.class.getName() ) )
+ {
+ return value.charAt( 0 );
+ }
+
+ if ( Number.class.isAssignableFrom( ClassLoaders.current().loadClass( type ) ) )
+ {
+ return toNumber( value );
+ }
+
+ if ( value == null || value.equals( "null" ) )
+ {
+ return null;
+ }
+
+ return tryStringConstructor( type, value );
+ }
+ catch ( final Exception e )
+ {
+ throw new SironaException( e );
+ }
+ }
+
+
+ private static Number toNumber( final String value )
+ throws NumberFormatException
+ {
+ // first the user can force the conversion
+ final char lastChar = Character.toLowerCase( value.charAt( value.length() - 1 ) );
+ if ( lastChar == 'd' )
+ {
+ return Double.valueOf( value.substring( 0, value.length() - 1 ) );
+ }
+ if ( lastChar == 'l' )
+ {
+ return Long.valueOf( value.substring( 0, value.length() - 1 ) );
+ }
+ if ( lastChar == 'f' )
+ {
+ return Float.valueOf( value.substring( 0, value.length() - 1 ) );
+ }
+
+ // try all conversions in cascade until it works
+ for ( final Class<?> clazz : new Class<?>[]{ Integer.class, Long.class, Double.class } )
+ {
+ try
+ {
+ return Number.class.cast( clazz.getMethod( "valueOf" ).invoke( null, value ) );
+ }
+ catch ( final Exception e )
+ {
+ // no-op
+ }
+ }
+
+ throw new SironaException( value + " is not a number" );
+ }
+
+ private static Object tryStringConstructor( String type, final String value )
+ throws Exception
+ {
+ return ClassLoaders.current().loadClass( type ).getConstructor( String.class ).newInstance( value );
+ }
+
+
private JMXNode buildJmxTree()
throws IOException
{
Modified: incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/controllers/controllers-jmx.js
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/controllers/controllers-jmx.js?rev=1623259&r1=1623258&r2=1623259&view=diff
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/controllers/controllers-jmx.js (original)
+++ incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/controllers/controllers-jmx.js Mon Sep 8 02:50:33 2014
@@ -45,8 +45,19 @@ define(['jquery','angular','bootstrap','
}
};
- $scope.invoke=function(name){
- console.log("name:"+name);
+ $scope.invoke=function(name,base64){
+ console.log("name:"+name+","+base64);
+
+ var parameters = [];
+
+ jQuery("#"+name + " input").each(function( index ) {
+ parameters.push(jQuery( this ).val());
+ });
+
+ var request={ mbeanEncodedName: base64, operationName: name, parameters: parameters };
+
+ jmx.invoke({ mbeanEncodedName: base64, operationName: name, parameters: parameters });
+
};
}]);
Modified: incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/services/services.js
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/services/services.js?rev=1623259&r1=1623258&r2=1623259&view=diff
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/services/services.js (original)
+++ incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/js/app/services/services.js Mon Sep 8 02:50:33 2014
@@ -113,7 +113,8 @@ define(['angular','angular-resource'], f
{},
{
query: {method:'GET', params:{mbean:'@mbean'},isArray:false},
- all: {method:'GET', params:{},isArray:true}
+ all: {method:'GET', params:{},isArray:true},
+ invoke: {method:'POST', isArray:false}
});
}
]);
Modified: incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/partials/jmx.html
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/partials/jmx.html?rev=1623259&r1=1623258&r2=1623259&view=diff
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/partials/jmx.html (original)
+++ incubator/sirona/trunk/server/reporting/reporting-ui/src/main/webapp/partials/jmx.html Mon Sep 8 02:50:33 2014
@@ -56,7 +56,7 @@
<form class="form-inline" id="{{operation.name}}">
{{operation.name}}
<input ng-repeat="param in operation.parameters" type="text" class="input-small" placeholder="{{param.name}}:{{param.type}}">
- <button type="submit" class="btn btn-primary" ng-click="invoke(operation.name)">Invoke</button>
+ <button type="submit" class="btn btn-primary" ng-click="invoke(operation.name,mbean.objectNameHash)">Invoke</button>
</form>
</li>
</ul>