/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.outrigger;

import com.sun.jini.config.Config;
import com.sun.jini.thread.TaskManager;
import com.sun.jini.thread.RetryTask;
import com.sun.jini.thread.WakeupManager;

import net.jini.config.Configuration;
import net.jini.config.ConfigurationException;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class provides a driver for monitoring the state of transactions
 * that have blocked progress of other operations recently.  It creates
 * tasks that monitor each transaction by intermittently querying the
 * transaction's state.  If it finds that the transaction has aborted,
 * it makes sure that the local space aborts the transaction, too, so
 * that operations will cease to be blocked by the transaction.
 *
 * @author Sun Microsystems, Inc.
 *
 * @see TxnMonitorTask
 * @see OutriggerServerImpl#monitor
 */
class TxnMonitor implements Runnable {
    /**
     * Each <code>ToMonitor</code> object represents a need to monitor
     * the given transactions, possibly under a lease.
     *
     * @see #pending
     */
    private static class ToMonitor {
	QueryWatcher	query;         // query governing interest in txns
	Collection	txns;	       // the transactions to monitor

	ToMonitor(QueryWatcher query, Collection txns) {
	    this.query = query;
	    this.txns = txns;
	}
    }

    /**
     * This list is used to contain requests to monitor interfering
     * transactions.  We use a list like this so that the
     * <code>getMatch</code> request that detected the conflict
     * doesn't have to wait for all the setup before returning -- it
     * just puts the data on this list and the <code>TxnMonitor</code>
     * pulls it off using its own thread.
     *
     * @see #ToMonitor
     * @see OutriggerServerImpl#getMatch 
     */
    private LinkedList pending = new LinkedList();

    /** wakeup manager for <code>TxnMonitorTask</code>s */
    private final WakeupManager wakeupMgr = 
	new WakeupManager(new WakeupManager.ThreadDesc(null, true));

    /**
     * The manager for <code>TxnMonitorTask</code> objects.
     */
    private TaskManager taskManager;

    /**
     * The space we belong to.  Needed for aborts.
     */
    private OutriggerServerImpl	space;

    /**
     * The thread running us.
     */
    private Thread ourThread;

    /** Logger for logging transaction related information */
    private static final Logger logger = 
	Logger.getLogger(OutriggerServerImpl.txnLoggerName);

    /**
     * Create a new TxnMonitor.
     */
    TxnMonitor(OutriggerServerImpl space, Configuration config)
	throws ConfigurationException 
    {
	if (space == null)
	    throw new NullPointerException("space must be non-null");
	this.space = space;

	taskManager = (TaskManager)Config.getNonNullEntry(config,
	    OutriggerServerImpl.COMPONENT_NAME, "txnMonitorTaskManager", 
	    TaskManager.class, new TaskManager());

        ourThread = new Thread(this, "TxnMonitor");
        ourThread.start();
    }

    public void destroy() {
        taskManager.terminate();
	wakeupMgr.stop();	
	wakeupMgr.cancelAll();	

	ourThread.interrupt();

        try {
	    ourThread.join();
	} catch(InterruptedException ie) {
	    // ignore
	}
    }

    /**
     * Return the space we're part of.
     */
    OutriggerServerImpl space() {
	return space;
    }

    /**
     * Add a set of <code>transactions</code> to be monitored under the
     * given query.
     */
    synchronized void add(QueryWatcher query, Collection transactions) {
	if (logger.isLoggable(Level.FINEST)) {
	    final StringBuffer buf = new StringBuffer();
	    buf.append("Setting up monitor for ");
	    buf.append(query);
	    buf.append(" toMonitor:");
	    boolean notFirst = false;
	    for (Iterator i=transactions.iterator(); i.hasNext();) {
		if (notFirst) {
		    buf.append(",");
		    notFirst = true;
		}
		buf.append(i.next());
	    }
	    logger.log(Level.FINEST, buf.toString());
	}

	pending.add(new ToMonitor(query, transactions));
	notifyAll();
    }

    /**
     * Add a set of <code>transactions</code> to be monitored under no
     * lease.
     */
    void add(Collection transactions) {
	add(null, transactions);
    }

    /**
     * Take pending monitor requests off the queue, creating the
     * required <code>TxnMonitorTask</code> objects and scheduling them.
     */
    public void run() {
	try {
	    ToMonitor tm;
	    for (;;)  {
		synchronized (this) {
		
		    // Sleep if nothing is pending.
		    while (pending.isEmpty())
			wait();

		    tm = (ToMonitor)pending.removeFirst();
		}

		logger.log(Level.FINER, "creating monitor tasks for {0}",
			   tm.query);

		Iterator it = tm.txns.iterator();
		while (it.hasNext()) {
		    Txn txn = (Txn) it.next();
		    TxnMonitorTask task = taskFor(txn);
		    task.add(tm.query);
		}
		if (Thread.currentThread().interrupted()) {
		    return;
		}
	    }
	} catch (InterruptedException e) {
	    return;
	}
    }

    /**
     * Return the monitor task for this transaction, creating it if
     * necessary.
     */
    private TxnMonitorTask taskFor(Txn txn) {
	TxnMonitorTask task = txn.monitorTask();
	if (task == null) {
	    logger.log(Level.FINER, "creating TxnMonitorTask for {0}", 
			   txn);

	    task = new TxnMonitorTask(txn, this, taskManager, wakeupMgr);
	    txn.monitorTask(task);
	    taskManager.add(task);  // add it after we've set it in the txn
	}
	return task;
    }
}
