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