All Downloads are FREE. Search and download functionalities are using the official Maven repository.

olokia-client-javascript.2.1.0.source-code.jolokia-simple.js Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/*
 * Copyright 2009-2013 Roland Huss
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * Simplified access to the Jolokia agent.
 *
 * This script will add convenience methods to Jolokia for
 * simplified access to JMX information. Before including this script, "jolokia.js"
 * must be included.
 *
 * It is recommended to compress this script before using it in production.
 *
 * @author roland
 */
"use strict";

// Uses Node, AMD or browser globals to create a module.
(function (root, factory) {
    if (typeof define === "function" && define.amd) {
        // AMD. Register as an anonymous module.
        define(["./jolokia"], factory);
    } else if (typeof module === "object" && module.exports) {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory(require("./jolokia"));
    } else {
        // Browser globals
        if (root.Jolokia) {
            factory(root.Jolokia);
        } else {
            console.error("No Jolokia definition found. Please include jolokia.js before jolokia-simple.js");
        }
    }
}(typeof self !== "undefined" ? self : this, function (Jolokia) {

    var builder = function(Jolokia) {
        /**
         * Get one or more attributes
         *
         * @param mbean objectname of MBean to query. Can be a pattern.
         * @param attribute attribute name. If an array, multiple attributes are fetched.
         *                  If null, all attributes are fetched.
         * @param path optional path within the return value. For multi-attribute fetch, the path
         *             is ignored.
         * @param opts options passed to Jolokia.request()
         * @return the value of the attribute, possibly a complex object
         */
        function getAttribute(mbean,attribute,path,opts) {
            if (arguments.length === 3 && isPlainObject(path)) {
                opts = path;
                path = null;
            } else if (arguments.length == 2 && isPlainObject(attribute)) {
                opts = attribute;
                attribute = null;
                path = null;
            }
            var req = { type: "read", mbean: mbean, attribute: attribute };
            addPath(req,path);
            return extractValue(this.request(req,prepareSuccessCallback(opts)),opts);
        }

        /**
         * Set an attribute on a MBean.
         *
         * @param mbean objectname of MBean to set
         * @param attribute the attribute to set
         * @param value the value to set
         * @param path an optional inner path which, when given, is used to determine
         *        an inner object to set the value on
         * @param opts additional options passed to Jolokia.request()
         * @return the previous value
         */
        function setAttribute(mbean,attribute,value,path,opts) {
            if (arguments.length === 4 && isPlainObject(path)) {
                opts = path;
                path = null;
            }
            var req = { type: "write", mbean: mbean, attribute: attribute, value: value };
            addPath(req,path);
            return extractValue(this.request(req,prepareSuccessCallback(opts)),opts);
        }

        /**
         * Execute a JMX operation and return the result value
         *
         * @param mbean objectname of the MBean to operate on
         * @param operation name of operation to execute. Can contain a signature in case overloaded
         *                  operations are to be called (comma separated fully qualified argument types
         *                  append to the operation name within parentheses)
         * @param arg1, arg2, ..... one or more argument required for executing the operation.
         * @param opts optional options for Jolokia.request() (must be an object)
         * @return the return value of the JMX operation.
         */
        function execute(mbean,operation) {
            var req = { type: "exec", mbean: mbean, operation: operation };
            var opts, end = arguments.length;
            if (arguments.length > 2 && isPlainObject(arguments[arguments.length-1])) {
                opts = arguments[arguments.length-1];
                end = arguments.length-1;
            }
            if (end > 2) {
                var args = [];
                for (var i = 2; i < end; i++) {
                    args[i-2] = arguments[i];
                }
                req.arguments = args;
            }
            return extractValue(this.request(req,prepareSuccessCallback(opts)),opts);
        }

        /**
         * Search for MBean based on a pattern and return a reference to the list of found
         * MBeans names (as string). If no MBean can be found, null is returned. For
         * example,
         *
         * jolokia.search("*:j2eeType=J2EEServer,*")
         *
         * searches all MBeans whose name are matching this pattern, which are according
         * to JSR77 all application servers in all available domains.
         *
         * @param mbeanPattern pattern to search for
         * @param opts optional options for Jolokia.request()
         * @return an array with ObjectNames as string
         */
        function search(mbeanPattern,opts) {
            var req = { type: "search", mbean: mbeanPattern};
            return extractValue(this.request(req,prepareSuccessCallback(opts)),opts);
        }

        /**
         * This method return the version of the agent and the Jolokia protocol
         * version as part of an object. If available, server specific information
         * like the application server's name are returned as wel.
         * A typical response looks like
         *
         * 
         *  {
         *    protocol: "4.0",
         *    agent: "0.82",
         *    info: {
         *       product: "glassfish",
         *       vendor": "Sun",
         *       extraInfo: {
         *          amxBooted: false
         *       }
         *  }
         * 
* * @param opts optional options for Jolokia.request() * @param version and other meta information as object */ function version(opts) { return extractValue(this.request({type: "version"},prepareSuccessCallback(opts)),opts); } /** * Get all MBeans as registered at the specified server. A C<$path> can be * specified in order to fetch only a subset of the information. When no path is * given, the returned value has the following format * *
         * {
         *     <domain> :
         *     {
         *       <canonical property list> :
         *       {
         *           "attr" :
         *           {
         *              <attribute name> :
         *              {
         *                 desc : <description of attribute>
         *                 type : <java type>,
         *                 rw : true/false
         *              },
         *              ....
         *           },
         *           "op" :
         *           {
         *              <operation name> :
         *              {
         *                "desc" : <description of operation>
         *                "ret" : <return java type>
         *                "args" :
         *                [
         *                   {
         *                     "desc" : <description>,
         *                     "name" : <name>,
         *                     "type" : <java type>
         *                   },
         *                   ....
         *                ]
         *              },
         *              ....
         *       },
         *       ....
         *     }
         *     ....
         *  }
         * 
* * A complete path has the format <domain>/property * list>/("attribute"|"operation")/<index>"> * (e.g. java.lang/name=Code Cache,type=MemoryPool/attribute/0). A path can be * provided partially, in which case the remaining map/array is returned. The path given must * be already properly escaped (i.e. slashes must be escaped like !/ and exclamation * marks like !!. * See also the Jolokia Reference Manual for a more detailed discussion of inner paths and escaping. * * @param path optional path for diving into the list * @param opts optional opts passed to Jolokia.request() */ function list(path,opts) { if (arguments.length == 1 && !Array.isArray(path) && isPlainObject(path)) { opts = path; path = null; } var req = { type: "list" }; addPath(req,path); return extractValue(this.request(req,prepareSuccessCallback(opts)),opts); } // ======================================================================= // Private methods: // If path is an array, the elements get escaped. If not, it is // taken directly function addPath(req,path) { if (path != null) { if (Array.isArray(path)) { req.path = path.map(Jolokia.escape).join("/"); } else {1 req.path = path; } } } function extractValue(response,opts) { if (response == null) { return null; } if (response.status == 200) { return response.value; } if (opts && opts.error) { return opts.error(response); } else { throw new Error("Jolokia-Error: " + JSON.stringify(response)); } } // Prepare callback to receive directly the value (instead of the full blown response) function prepareSuccessCallback(opts) { if (opts && opts.success) { var parm = Jolokia.assignObject({}, opts); parm.success = function(resp) { opts.success(resp.value); }; return parm; } else { return opts; } } function isPlainObject(obj) { return obj && Object.prototype.toString.call(obj) === "[object Object]"; } // Extend the Jolokia prototype with new functionality (mixin) Jolokia.assignObject(Jolokia.prototype, { "getAttribute" : getAttribute, "setAttribute" : setAttribute, "execute": execute, "search": search, "version": version, "list": list }); return Jolokia; }; return builder(Jolokia); }));




© 2015 - 2024 Weber Informatics LLC | Privacy Policy