AgentIndexRunner.java revision 368
207N/A/*
207N/A * CDDL HEADER START
207N/A *
207N/A * The contents of this file are subject to the terms of the
207N/A * Common Development and Distribution License (the "License").
207N/A * You may not use this file except in compliance with the License.
207N/A *
207N/A * See LICENSE.txt included in this distribution for the specific
207N/A * language governing permissions and limitations under the License.
207N/A *
207N/A * When distributing Covered Code, include this CDDL HEADER in each
207N/A * file and include the License file at LICENSE.txt.
207N/A * If applicable, add the following below this CDDL HEADER, with the
207N/A * fields enclosed by brackets "[]" replaced with your own identifying
207N/A * information: Portions Copyright [yyyy] [name of copyright owner]
207N/A *
207N/A * CDDL HEADER END
207N/A */
207N/A
207N/A/*
207N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
207N/A * Use is subject to license terms.
207N/A */
207N/Apackage org.opensolaris.opengrok.management;
207N/A
207N/Aimport java.io.File;
207N/Aimport java.util.ArrayList;
207N/Aimport java.util.HashSet;
207N/Aimport java.util.Iterator;
207N/Aimport java.util.List;
207N/Aimport java.util.Set;
207N/Aimport java.util.logging.Level;
207N/Aimport java.util.logging.Logger;
282N/Aimport javax.management.ListenerNotFoundException;
207N/Aimport javax.management.MBeanNotificationInfo;
261N/Aimport javax.management.MBeanRegistration;
320N/Aimport javax.management.MBeanServer;
312N/Aimport javax.management.Notification;
207N/Aimport javax.management.NotificationEmitter;
207N/Aimport javax.management.NotificationFilter;
207N/Aimport javax.management.NotificationListener;
207N/Aimport javax.management.ObjectName;
207N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
207N/Aimport org.opensolaris.opengrok.index.IndexChangedListener;
207N/Aimport org.opensolaris.opengrok.index.Indexer;
207N/A
207N/A/**
207N/A * AgentIndexRunner.
207N/A * @author Jan S Berg
207N/A */
207N/Apublic final class AgentIndexRunner implements AgentIndexRunnerMBean, NotificationListener,
207N/A MBeanRegistration, Runnable, IndexChangedListener, NotificationEmitter {
207N/A
207N/A private transient static AgentIndexRunner indexerInstance = null;
207N/A private final static String NOTIFICATIONACTIONTYPE = "ogaaction";
207N/A private final static String NOTIFICATIONEXCEPTIONTYPE = "ogaexception";
207N/A private final static String NOTIFICATIONINFOSTRINGTYPE = "ogainfostring";
207N/A private final static String NOTIFICATIONINFOLONGTYPE = "ogainfolong";
207N/A private static boolean enabled = true;
207N/A private transient Thread indexThread = null;
207N/A private final static Logger log = Logger.getLogger("org.opensolaris.opengrok");
207N/A private final Management jagmgt;
207N/A private RuntimeEnvironment env = null;
207N/A private long lastIndexStart = 0;
207N/A private long lastIndexFinish = 0;
207N/A private long lastIndexUsedTime = 0;
207N/A private Exception lastException = null;
207N/A private final Set<NotificationHolder> notifListeners =
207N/A new HashSet<NotificationHolder>();
207N/A private static long sequenceNo = 0;
207N/A private StringBuilder notifications = new StringBuilder();
207N/A private final static int MAXMESSAGELENGTH = 50000;
253N/A
359N/A /**
207N/A * The only constructor is private, so other classes will only get an
359N/A * instance through the static factory method getInstance().
274N/A */
320N/A private AgentIndexRunner(boolean enabledParam) {
274N/A enabled = enabledParam;
207N/A jagmgt = Management.getInstance();
207N/A }
207N/A
207N/A /**
207N/A * Static factory method to get an instance of AgentIndexRunner.
207N/A * @param enabledParam if true, the initial instance of the purgatory will
207N/A * have purging enabled.
207N/A */
207N/A public static synchronized AgentIndexRunner getInstance(boolean enabledParam) {
207N/A if (indexerInstance == null) {
207N/A indexerInstance = new AgentIndexRunner(enabledParam);
207N/A }
207N/A return indexerInstance;
207N/A }
207N/A
207N/A public ObjectName preRegister(MBeanServer serverParam, ObjectName name) {
207N/A return name;
207N/A }
207N/A
207N/A public void postRegister(Boolean registrationDone) {
207N/A }
207N/A
207N/A public void preDeregister() {
261N/A }
459N/A
207N/A public void postDeregister() {
459N/A }
261N/A
207N/A public void run() {
207N/A
207N/A try {
207N/A //Indexer ind = new Indexer();
261N/A log.info("Running...");
207N/A lastIndexStart = System.currentTimeMillis();
459N/A lastException = null;
207N/A doNotify(NOTIFICATIONINFOLONGTYPE, "StartIndexing", Long.valueOf(lastIndexStart));
312N/A String configfile = jagmgt.getInstance().getConfigurationFile();
207N/A if (configfile == null) {
207N/A doNotify(NOTIFICATIONEXCEPTIONTYPE, "Missing Configuration file", "");
207N/A }
261N/A File cfgFile = new File(configfile);
207N/A if (cfgFile.exists()) {
207N/A env = RuntimeEnvironment.getInstance();
207N/A log.info("Running indexer with configuration " + configfile);
261N/A env.readConfiguration(cfgFile);
261N/A
261N/A Indexer index = Indexer.getInstance();
261N/A int noThreads = jagmgt.getInstance().getNumberOfThreads().intValue();
261N/A boolean update = jagmgt.getInstance().getUpdateIndexDatabase().booleanValue();
261N/A String[] sublist = jagmgt.getInstance().getSubFiles();
320N/A List<String> subFiles = new ArrayList<String>();
261N/A for (int i = 0; i < sublist.length; i++) {
261N/A subFiles.add(sublist[i]);
261N/A }
207N/A log.info("Starting index, update " + update + " noThreads " + noThreads + " subfiles " + subFiles.size());
207N/A index.doIndexerExecution(update, noThreads, subFiles, this);
261N/A log.info("Finished indexing");
207N/A lastIndexFinish = System.currentTimeMillis();
207N/A sendNotifications();
207N/A doNotify(NOTIFICATIONINFOLONGTYPE, "FinishedIndexing", Long.valueOf(lastIndexFinish));
261N/A lastIndexUsedTime = lastIndexFinish - lastIndexStart;
261N/A String publishhost = jagmgt.getInstance().getPublishServerURL();
261N/A if (publishhost != null) {
261N/A index.sendToConfigHost(env, publishhost);
261N/A doNotify(NOTIFICATIONINFOSTRINGTYPE, "Published index", publishhost);
261N/A } else {
261N/A log.warning("No publishhost given, not sending updates");
320N/A }
261N/A
261N/A
261N/A } else {
207N/A log.warning("Cannot Run indexing without proper configuration file " + configfile);
207N/A doNotify(NOTIFICATIONEXCEPTIONTYPE, "Configuration file not valid", configfile);
207N/A }
460N/A } catch (Exception e) {
338N/A log.log(Level.SEVERE,
207N/A "Exception running indexing ", e);
207N/A lastException = e;
207N/A }
207N/A }
207N/A
207N/A /**
207N/A * Disables indexer
207N/A */
460N/A public void disable() {
460N/A enabled = false;
460N/A }
460N/A
207N/A /**
359N/A * Enables the indexer
359N/A */
460N/A public void enable() {
460N/A enabled = true;
460N/A }
207N/A
207N/A /**
207N/A * Handle timer notifications to the purgatory.
296N/A * Will start the purger if it is enabled and return immediately.
296N/A */
296N/A public void handleNotification(Notification n, Object hb) {
207N/A if (n.getType().equals("timer.notification")) {
207N/A log.finer("Received timer notification");
207N/A } else {
207N/A log.warning("Received unknown notification type: " +
296N/A n.getType());
207N/A return;
207N/A }
207N/A if (!enabled) {
253N/A log.finer("Indexing is disabled, doing nothing");
253N/A return;
274N/A }
207N/A index(false); // just start the purger and return (do not delay timer)
207N/A }
207N/A
274N/A /**
274N/A * The index method starts a thread that will
274N/A * start indexing part of the opengrok agent.
274N/A * @param waitForFinished if false the command returns immediately, if true
274N/A * it will return when the indexing is done.
297N/A */
274N/A public void index(boolean waitForFinished) {
274N/A log.info("Starting indexing.");
439N/A /*
439N/A * Synchronize here to make sure that you never get more than one
439N/A * indexing thread trying to start at the same time.
439N/A */
439N/A synchronized (this) {
297N/A if (indexThread != null) {
439N/A if (indexThread.isAlive()) {
460N/A log.warning("Previous indexer is still alive, will not start another.");
439N/A return;
274N/A } else {
460N/A log.fine("Previous indexer is no longer alive, starting a new one.");
460N/A }
274N/A }
274N/A indexThread = new Thread(this);
274N/A try {
274N/A indexThread.start();
207N/A if (!waitForFinished) {
459N/A return;
207N/A }
459N/A log.fine("Waiting for indexer to finish...");
359N/A indexThread.join();
359N/A log.fine("indexer finished.");
459N/A } catch (Exception e) {
359N/A log.log(Level.SEVERE,
359N/A "Caught Exception while waiting for indexing to finish.", e);
359N/A }
359N/A return;
207N/A }
207N/A }
255N/A
207N/A public void fileAdded(String path, String analyzer) {
456N/A log.info("Added " + path + " analyzer " + analyzer);
460N/A addFileAction("A:", path);
460N/A }
460N/A
274N/A public void fileRemoved(String path) {
274N/A log.info("File removed " + path);
207N/A addFileAction("R:", path);
274N/A }
274N/A
274N/A public void fileUpdated(String path) {
456N/A log.info("File updated " + path);
274N/A addFileAction("U:", path);
274N/A }
274N/A
274N/A private void addFileAction(String type, String path) {
274N/A notifications.append('\n');
274N/A notifications.append(type);
457N/A notifications.append(path);
457N/A if (notifications.length() > MAXMESSAGELENGTH) {
457N/A sendNotifications();
207N/A }
457N/A }
207N/A
457N/A private void sendNotifications() {
457N/A if (notifications.length() > 0) {
457N/A doNotify(NOTIFICATIONACTIONTYPE, "FilesInfo", notifications.toString());
457N/A notifications.delete(0, notifications.length());
457N/A }
457N/A }
274N/A
207N/A public long lastIndexTimeFinished() {
207N/A return lastIndexFinish;
207N/A }
207N/A
207N/A public long lastIndexTimeStarted() {
207N/A return lastIndexStart;
207N/A }
207N/A
359N/A public long lastIndexTimeUsed() {
359N/A return lastIndexUsedTime;
359N/A }
207N/A
207N/A public Exception getExceptions() {
359N/A return lastException;
253N/A }
253N/A
253N/A public void addNotificationListener(NotificationListener notiflistener, NotificationFilter notfilt, Object obj) throws IllegalArgumentException {
207N/A log.info("Adds a notiflistner, with obj " + obj.toString());
207N/A if (notiflistener == null) {
207N/A throw new IllegalArgumentException("Must have legal NotificationListener");
207N/A }
207N/A synchronized (notifListeners) {
270N/A notifListeners.add(new NotificationHolder(notiflistener, notfilt, obj));
270N/A }
459N/A }
270N/A
312N/A public void removeNotificationListener(NotificationListener notiflistener) throws ListenerNotFoundException {
270N/A log.info("removes a notiflistener, no obj");
270N/A boolean removed = false;
270N/A synchronized (notifListeners) {
270N/A Iterator it = notifListeners.iterator();
359N/A while (it.hasNext()) {
270N/A NotificationHolder mnf = (NotificationHolder) it.next();
270N/A if (mnf.getNL().equals(notiflistener)) {
270N/A it.remove();
459N/A removed = true;
270N/A }
270N/A }
270N/A }
270N/A if (!removed) {
270N/A throw new ListenerNotFoundException("Didnt remove the given NotificationListener");
270N/A }
359N/A }
270N/A
270N/A public void removeNotificationListener(NotificationListener notiflistener, NotificationFilter filt, Object obj) throws ListenerNotFoundException {
270N/A log.info("removes a notiflistener obj " + obj);
270N/A boolean removed = false;
270N/A synchronized (notifListeners) {
459N/A Iterator it = notifListeners.iterator();
320N/A while (it.hasNext()) {
270N/A NotificationHolder mnf = (NotificationHolder) it.next();
270N/A if (mnf.getNL().equals(notiflistener)) {
270N/A if ((mnf.getFilter() == null) || mnf.getFilter().equals(filt)) {
270N/A if ((mnf.getFilter() == null) || mnf.getObj().equals(obj)) {
270N/A it.remove();
270N/A removed = true;
270N/A }
270N/A }
207N/A }
207N/A }
207N/A }
359N/A if (!removed) {
359N/A throw new ListenerNotFoundException("Didnt remove the given NotificationListener");
359N/A }
359N/A }
359N/A
359N/A /**
359N/A * Method that the subclass can override, but doesn't have to
207N/A * @return MBeanNotificationInfo array of notification (and types) this class can emitt.
207N/A */
207N/A public MBeanNotificationInfo[] getNotificationInfo() {
320N/A MBeanNotificationInfo[] info = new MBeanNotificationInfo[1];
207N/A String[] supptypes = {NOTIFICATIONACTIONTYPE, NOTIFICATIONINFOLONGTYPE, NOTIFICATIONINFOSTRINGTYPE};
207N/A String name = "AgentIndexRunner";
207N/A String descr = "OpenGrok Indexer Notifications";
207N/A MBeanNotificationInfo minfo = new MBeanNotificationInfo(supptypes, name,
320N/A descr);
207N/A info[0] = minfo;
359N/A return info;
460N/A }
460N/A
460N/A private void doNotify(String type, String msg, Object userdata) {
359N/A try {
359N/A log.info("start notifying " + notifListeners.size() + " listeners");
359N/A //String str = "JET Finished";
207N/A //String obj = JAGConstants.jetFinishedNType;
320N/A long ts = System.currentTimeMillis();
207N/A sequenceNo++;
207N/A Notification notif = new Notification(type, this, sequenceNo, ts, msg);
207N/A notif.setUserData(userdata);
207N/A synchronized (notifListeners) {
207N/A for (NotificationHolder nl : notifListeners) {
207N/A log.fine("having one with obj " + nl.getObj());
207N/A try {
359N/A if ((nl.getFilter() == null) ||
359N/A nl.getFilter().isNotificationEnabled(notif)) {
359N/A nl.getNL().handleNotification(notif, nl.getObj());
207N/A }
207N/A } catch (Exception exnot) {
207N/A log.log(Level.INFO, "Ex " + exnot, exnot);
207N/A }
207N/A }
207N/A }
207N/A } catch (Exception ex) {
207N/A log.log(Level.SEVERE,
207N/A "Exception during notification sending: " + ex.getMessage(),
207N/A ex);
207N/A }
207N/A }
320N/A}
207N/A