001/*
002 * #%L
003 * Netarchivesuite - monitor
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.monitor.tools;
025
026import java.lang.management.ManagementFactory;
027
028import dk.netarkivet.common.tools.SimpleCmdlineTool;
029import dk.netarkivet.common.tools.ToolRunnerBase;
030import dk.netarkivet.monitor.jmx.HostForwarding;
031import dk.netarkivet.monitor.logging.SingleLogRecord;
032
033/**
034 * This tool will simply reregister all MBeans that matches the given query from the JMX hosts read in settings, using
035 * its own platformmbeanserver. It will then wait forever.
036 * <p>
037 * It can then be connected to with any JMX client, e.g. jconsole. Start this tool with -Dcom.sun.management.jmxremote
038 */
039public class JMXProxy extends ToolRunnerBase {
040
041    /**
042     * Run the tool as described in class comment.
043     *
044     * @param args Should take one arg, the query.
045     */
046    public static void main(String[] args) {
047        new JMXProxy().runTheTool(args);
048    }
049
050    /**
051     * Factory method. Creates and returns the actual workhorse that does the job.
052     *
053     * @return An implementation of the SimpleCmdlineTool interface.
054     */
055    protected SimpleCmdlineTool makeMyTool() {
056        return new SimpleCmdlineTool() {
057            /**
058             * Check (command line) arguments. There should be one, the query
059             *
060             * @param args The command line arguments passed directly from a public static void main(String[] args)
061             * method.
062             * @return True, if parameters are size 1. False if not.
063             */
064            public boolean checkArgs(String... args) {
065                if (args.length != 1) {
066                    System.err.println("This tool takes one argument");
067                    return false;
068                }
069                return true;
070            }
071
072            /**
073             * Does nothing.
074             *
075             * @param args Not used.
076             */
077            public void setUp(String... args) {
078            }
079
080            /**
081             * Does nothing.
082             */
083            public void tearDown() {
084            }
085
086            /**
087             * Run the tool. Simply forward all logging mbeans to the local mbean server. Then waits.
088             *
089             * @param args Not used.
090             */
091            public void run(String... args) {
092                String query = args[0];
093                HostForwarding.getInstance(SingleLogRecord.class, ManagementFactory.getPlatformMBeanServer(), query);
094                synchronized (this) {
095                    try {
096                        wait();
097                    } catch (InterruptedException e) {
098                        // end the tool nicely on wakeup.
099                    }
100                }
101            }
102
103            /**
104             * Describes the parameters that this tool accepts. One argument is needed: The query, e.g
105             * dk.netarkivet.common.logging:*
106             *
107             * @return The parameter description in a String object.
108             */
109            public String listParameters() {
110                return "query";
111            }
112        };
113    }
114}