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