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