OGAgent.java revision 460
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
407N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
1237N/A/*
1190N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1185N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.management;
0N/A
388N/Aimport java.io.FileInputStream;
1185N/Aimport java.io.IOException;
388N/Aimport java.io.InputStream;
388N/Aimport java.net.MalformedURLException;
388N/Aimport java.util.ArrayList;
388N/Aimport java.util.Date;
388N/Aimport java.util.HashMap;
388N/Aimport java.util.Properties;
388N/Aimport java.util.logging.Level;
508N/Aimport java.util.logging.Logger;
1327N/Aimport javax.management.JMException;
1185N/Aimport javax.management.MBeanServer;
388N/Aimport javax.management.MBeanServerFactory;
388N/Aimport javax.management.NotificationFilter;
1185N/Aimport javax.management.ObjectName;
1436N/Aimport javax.management.remote.JMXConnectorServer;
819N/Aimport javax.management.remote.JMXServiceURL;
350N/Aimport javax.management.remote.jmxmp.JMXMPConnectorServer;
1185N/Aimport javax.management.remote.rmi.RMIConnectorServer;
0N/Aimport javax.management.timer.Timer;
1385N/Aimport org.opensolaris.opengrok.Info;
615N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
1195N/A
1185N/A/**
1185N/A * OG Agent main class.
388N/A * Class for starting the basic components:
0N/A * Monitor and JMX and HTTP Connectors.
1111N/A * @author Jan S Berg
1185N/A */
1111N/Apublic class OGAgent {
456N/A
1327N/A private int connectorport = 9292;
456N/A private final static Logger log = Logger.getLogger("org.opensolaris.opengrok");
456N/A private static String cfgfile = null;
456N/A private MBeanServer server = null;
1185N/A private static OGAgent oga = null;
1111N/A
1185N/A @SuppressWarnings("PMD.SystemPrintln")
1185N/A public static void main(final String args[]) {
1185N/A
1185N/A for (int i = 0; i < args.length; i++) {
1185N/A if (args[i].equals("-config")) {
1185N/A if (++i < args.length) {
1185N/A cfgfile = args[i];
1111N/A } else {
1111N/A System.err.println("-config, argument missing: config file");
1237N/A System.exit(1);
1318N/A }
1190N/A }
615N/A }
1190N/A
1185N/A oga = new OGAgent();
1185N/A try {
1185N/A oga.runOGA();
819N/A } catch (MalformedURLException e) {
0N/A log.log(Level.SEVERE, "Could not create connector server: " + e, e);
1185N/A System.exit(1);
0N/A } catch (IOException e) {
1185N/A log.log(Level.SEVERE, "Could not start connector server: " + e, e);
0N/A System.exit(2);
0N/A } catch (Exception ex) {
0N/A Logger.getLogger(OGAgent.class.getName()).log(Level.SEVERE, null, ex);
0N/A System.exit(1);
0N/A }
1185N/A }
1185N/A
1190N/A public final void runOGA() throws MalformedURLException, IOException, JMException {
1461N/A String machinename = java.net.InetAddress.getLocalHost().getHostName();
1385N/A String javaver = System.getProperty("java.version");
1185N/A
1185N/A Properties props = new Properties(System.getProperties());
1185N/A // Load default values
1185N/A InputStream in = OGAgent.class.getResourceAsStream("oga.properties");
1385N/A if (in != null) {
1185N/A props.load(in);
1385N/A in.close();
1385N/A }
1185N/A
1385N/A if (cfgfile != null) {
1185N/A FileInputStream is = new FileInputStream(cfgfile);
1185N/A props.load(is);
1185N/A is.close();
1185N/A }
1385N/A
1385N/A createLogger(props);
1327N/A
1185N/A log.info("Starting " + Info.getFullVersion() +
1195N/A " JMX Agent, with java version " + javaver);
1185N/A //create mbeanserver
1185N/A
1185N/A String connprotocol = props.getProperty("org.opensolaris.opengrok.management.connection.protocol", "jmxmp");
460N/A connectorport = Integer.parseInt(props.getProperty("org.opensolaris.opengrok.management.connection." + connprotocol + ".port", Integer.toString(connectorport)));
1185N/A log.fine("Using protocol " + connprotocol + ", port: " + connectorport);
1185N/A
1185N/A ArrayList mbservs = MBeanServerFactory.findMBeanServer(null);
1185N/A log.fine("Finding MBeanservers, size " + mbservs.size());
1185N/A if (mbservs.isEmpty()) {
1185N/A server = MBeanServerFactory.createMBeanServer();
1185N/A } else {
1185N/A server = (MBeanServer) mbservs.get(0);
1185N/A }
1185N/A
1185N/A //instantiate and register OGAManagement
1190N/A ObjectName manager = new ObjectName("OGA:name=Management");
1185N/A server.registerMBean(Management.getInstance(props), manager);
1185N/A
1190N/A //instantiate and register Timer service and resource purger
1185N/A createIndexTimer(props);
1185N/A log.info("MBeans registered");
1185N/A
1185N/A // Create and start connector server
1190N/A log.fine("Starting JMX connector");
1190N/A HashMap<String, Object> env = new HashMap<String, Object>();
1190N/A JMXServiceURL url = new JMXServiceURL(connprotocol, machinename, connectorport);
1185N/A JMXConnectorServer connectorServer = null;
1190N/A
1185N/A if ("jmxmp".equals(connprotocol)) {
1185N/A connectorServer = new JMXMPConnectorServer(url, env, server);
1185N/A } else if ("rmi".equals(connprotocol) || "iiop".equals(connprotocol)) {
1185N/A connectorServer = new RMIConnectorServer(url, env, server);
1185N/A } else {
1185N/A throw new IOException("Unknown connector protocol");
1185N/A }
1185N/A connectorServer.start();
1185N/A
1185N/A log.info("OGA is ready and running...");
158N/A }
1237N/A
1237N/A private void createIndexTimer(Properties properties) throws IOException, JMException {
1185N/A
1185N/A //instantiate, register and start the Timer service
1390N/A ObjectName timer = new ObjectName("service:name=timer");
1185N/A server.registerMBean(new Timer(), timer);
1185N/A server.invoke(timer, "start", null, null);
1185N/A log.info("Started timer service");
1185N/A
1185N/A boolean enabled = Boolean.valueOf(properties.getProperty("org.opensolaris.opengrok.management.indexer.enabled")).booleanValue();
1185N/A int period = Integer.parseInt(properties.getProperty("org.opensolaris.opengrok.management.indexer.sleeptime"));
1390N/A log.fine("Indexer enabled: " + enabled);
1185N/A log.fine("Indexer period: " + period + " seconds");
1390N/A //instantiate and register resource purger
1185N/A ObjectName indexRunner = new ObjectName("OGA:name=AgentIndexRunner," + "source=timer");
1185N/A server.registerMBean(AgentIndexRunner.getInstance(enabled), indexRunner);
1185N/A // Add index notification to timer (read from org.opensolaris.opengrok.management.indexer.sleeptime property).
0N/A Date date = new Date(System.currentTimeMillis() + Timer.ONE_SECOND * 5);
1185N/A Long longPeriod = Long.valueOf(period * Timer.ONE_SECOND);
1390N/A Integer id = (Integer) server.invoke(timer, "addNotification",
1185N/A new Object[]{"timer.notification", // Type
1185N/A "Time to index again", // Message
1185N/A null, // user data
1185N/A date, // Start time
1185N/A longPeriod, // Period
1185N/A },
1185N/A new String[]{String.class.getName(),
1185N/A String.class.getName(),
1185N/A Object.class.getName(),
1111N/A Date.class.getName(),
1390N/A "long",
1185N/A });
1185N/A
1185N/A // Add indexer as listener to index notifications
1185N/A NotificationFilter filter = new TimerFilter(id);
1390N/A server.addNotificationListener(timer, indexRunner, filter, null);
1185N/A }
1185N/A
350N/A private void createLogger(Properties props) {
350N/A String OGAlogpath = props.getProperty("org.opensolaris.opengrok.management.logging.path");
350N/A
928N/A Level loglevel = null;
350N/A try {
1185N/A loglevel = Level.parse(props.getProperty("org.opensolaris.opengrok.management.logging.filelevel"));
1385N/A } catch (Exception exll) {
1185N/A loglevel = Level.FINE;
1185N/A }
1185N/A Level consoleloglevel = null;
1185N/A try {
1185N/A consoleloglevel = Level.parse(props.getProperty("org.opensolaris.opengrok.management.logging.consolelevel"));
1185N/A } catch (Exception excl) {
1185N/A consoleloglevel = Level.INFO;
1185N/A }
1461N/A
1190N/A OpenGrokLogger.setupLogger(OGAlogpath, loglevel, consoleloglevel);
1185N/A }
1190N/A}
1190N/A