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