001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Stephane Lacoin at Nuxeo (aka matic) 016 */ 017package org.nuxeo.connect.tools.report.management; 018 019import java.io.IOException; 020import java.io.OutputStream; 021import java.io.OutputStreamWriter; 022import java.util.Arrays; 023import java.util.HashMap; 024import java.util.Map; 025 026import javax.management.JMException; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.jolokia.backend.BackendManager; 031import org.jolokia.config.ConfigKey; 032import org.jolokia.config.Configuration; 033import org.jolokia.request.JmxRequest; 034import org.jolokia.request.JmxRequestFactory; 035import org.jolokia.util.LogHandler; 036import org.jolokia.util.RequestType; 037import org.json.simple.JSONObject; 038import org.nuxeo.runtime.model.DefaultComponent; 039 040/** 041 * 042 * 043 * @since 8.3 044 */ 045public class MXComponent extends DefaultComponent { 046 047 static MXComponent instance; 048 049 public MXComponent() { 050 instance = this; 051 } 052 053 final Configuration config = new Configuration(ConfigKey.AGENT_ID, "false", ConfigKey.IGNORE_ERRORS, "true"); 054 055 final BackendManager manager = new BackendManager(config, new LogHandler() { 056 057 final Log log = LogFactory.getLog(MXComponent.class); 058 059 @Override 060 public void info(String message) { 061 log.info(message); 062 } 063 064 @Override 065 public void error(String message, Throwable t) { 066 log.error(message, t); 067 } 068 069 @Override 070 public void debug(String message) { 071 log.debug(message); 072 } 073 }); 074 075 class RequestBuilder { 076 077 RequestBuilder(RequestType oftype) { 078 pRequestMap.put("type", oftype.getName()); 079 } 080 081 @SuppressWarnings("unchecked") 082 final Map<String, Object> pRequestMap = new JSONObject(); 083 084 RequestBuilder withMbean(String value) { 085 pRequestMap.put("mbean", value); 086 return this; 087 } 088 089 RequestBuilder withOperation(String value, Object... arguments) { 090 pRequestMap.put("operation", value); 091 pRequestMap.put("arguments", Arrays.asList(arguments)); 092 return this; 093 } 094 095 final Map<String, String> pParams = new HashMap<>(); 096 097 void run(OutputStream sink) { 098 try { 099 JmxRequest request = JmxRequestFactory.createPostRequest(pRequestMap, config.getProcessingParameters(pParams)); 100 JSONObject json = manager.handleRequest(request); 101 OutputStreamWriter writer = new OutputStreamWriter(sink); 102 json.writeJSONString(writer); 103 writer.flush(); 104 } catch (JMException | IOException cause) { 105 throw new AssertionError("Cannot invoke jolokia", cause); 106 } 107 } 108 109 } 110 111 RequestBuilder list() { 112 return new RequestBuilder(RequestType.LIST); 113 } 114 115 RequestBuilder search(String pattern) { 116 return new RequestBuilder(RequestType.SEARCH) 117 .withMbean(pattern); 118 } 119 120 RequestBuilder read(String pattern) { 121 return new RequestBuilder(RequestType.READ) 122 .withMbean(pattern); 123 } 124 125 RequestBuilder exec(String pattern, String operation, Object... arguments) { 126 return new RequestBuilder(RequestType.EXEC) 127 .withMbean(pattern) 128 .withOperation(operation, arguments); 129 } 130 131}