📄 stopmonitor.java
字号:
package com.ca.directory.jxplorer.broker;
import java.util.Vector;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.ca.directory.jxplorer.*;
import com.ca.commons.cbutil.*;
/**
* The Stop Monitor interacts with the Brokers to cancel unwanted
* data queries. Since these data queries may well hang on
* dud connections etc., they are set to self-destruct whenever
* they 'complete' a cancelled request. Because of this the Stop Monitor starts
* up a replacement thread immediately after cancelling a particular
* request.
*/
public class StopMonitor
{
/**
* The Stop Monitor starts with a list of
* Brokers to keep track off.
*/
Vector watchList; // A vector of Brokers to monitor
Frame parent; // a link to the 'owning' gui, to be passed to the local gui
Vector watchingComponents; // A vector of components (e.g. gui 'stop buttons') linked to the StopMonitor state
StopMonitorGUI monitor;
static int threadID = 0; // a unique ID used for thread debugging.
private static Logger log = Logger.getLogger(StopMonitor.class.getName());
// An internal use data holder - we need to keep associations
// of threads and brokers, and threads/brokers/queries
protected class QueryBroker
{
public Broker broker;
public DataQuery query;
public int id;
QueryBroker(Broker b, DataQuery q)
{
broker = b;
query = q;
id = q.id;
}
}
/**
* Creates a stop monitor to look after a list of brokers and the (single)
* thread that runs each broker.
*/
public StopMonitor(Broker[] brokerList, Frame parent)
{
this.parent = parent;
watchList = new Vector(brokerList.length);
watchingComponents = new Vector(4);
for (int i=0; i<brokerList.length; i++)
add(brokerList[i]);
}
/**
* Add a new broker to the stop monitor.
*/
public void add(Broker b)
{
watchList.add(b);
b.registerStopMonitor(this);
}
/**
* Get a list of all the queries outstanding, and which brokers they
* belong to.
* @return a vector of QueryBroker objects.
*/
protected Vector getQueryBrokers()
{
int i, noBrokers = watchList.size();
Vector doublets = new Vector(noBrokers*2); // guess initial size
// first get the queries currently being executed
for (i=0; i<noBrokers; i++)
{
Broker broker = (Broker)watchList.get(i);
DataQuery query = broker.getCurrent();
if (query != null && !query.isCancelled())
doublets.add(new QueryBroker(broker, query));
}
// now get all the queries waiting in queues.
for (i=0; i<noBrokers; i++)
{
Broker broker = (Broker)watchList.get(i);
Vector queries = broker.getRequestQueue();
for (int j=0; j<queries.size(); j++)
doublets.add(new QueryBroker(broker, (DataQuery)queries.get(j)));
}
return doublets;
}
/**
* See if any of the monitored brokers has any outstanding requests.
*/
public boolean queriesPending()
{
// now get all the queries waiting in queues.
for (int i=0; i<watchList.size(); i++)
{
if ( ((Broker)watchList.get(i)).hasRequests() )
return true;
}
return false;
}
/**
* This registers a graphics component (such as a button) that
* should be enabled when there are queries pending, and disabled
* when there aren't.
*/
public void addWatcher(Component c)
{
watchingComponents.add(c);
}
/**
* This works through all the registered 'watcher' components,
* and enables or disables them depending on whether there
* are queries waiting to be cancelled or not.
*/
public void updateWatchers()
{
boolean enableStatus = queriesPending();
for (int i=0; i<watchingComponents.size(); i++)
{
Component c = (Component)watchingComponents.get(i);
if (c.isEnabled() != enableStatus)
{
c.setEnabled(enableStatus);
c.repaint();
}
}
}
// PROG NOTE - this is intended to be run from the newly
// created StopMonitorPanel thread.
/**
* This cancels a particular query in a particular broker's queue.
* If the query is already in progress it marks the query as cancelled
* which will eventually lead to the death of the thread executing that
* query, and starts another thread to take over...
*/
protected void cancelQuery(QueryBroker pair)
{
if (pair == null) return;
DataQuery query = pair.query;
Broker broker = pair.broker;
//PROG NOTE - danger of contention here. Remember that a broker thread
// may briefly have a lock on the requestQuery queue
synchronized(this)
{
if (query.isRunning()) // badness - maybe a connection has hung or something. We have
{ // kill the query and start a new thread...
query.cancel();
new Thread(broker, "restarted thread for: " + (threadID++) + " " + broker.getClass()).start();
}
else // nice 'n simple! - dump it from the array of queries and our work is done!
{
broker.removeQuery(query);
}
}
}
/**
* This prompts the stop monitor to create a GUI showing the user the possible
* queries available to be stopped.
*/
public void show()
{
Vector QBs = getQueryBrokers();
if (QBs.size() == 0)
{
JOptionPane.showMessageDialog(parent,
CBIntText.get("There are no outstanding directory queries to cancel."),
CBIntText.get("Nothing to do."), JOptionPane.INFORMATION_MESSAGE);
return;
}
if (monitor!=null)
{
monitor.update();
if (monitor.singleDelete() == false)
monitor.setVisible(true);
}
else
{
monitor = new StopMonitorGUI(this, QBs);
monitor.setSize(300, 400);
CBUtility.center(monitor, parent);
// We need to stick the monitor in a separate thread, because the action
// of deleting a query may itself hang...
Thread buttonMonitor = new Thread(monitor, "Stop Monitor Thread");
buttonMonitor.start();
if (monitor.singleDelete() == false)
monitor.setVisible(true);
}
}
//
// START StopMonitorGUI class
//
/**
* This class is used to display a gui list of cancel-able queries to the user.
* The main StopMonitor class does the hard work.
* The gui implements a runnable thread that is notified when the user
* hits a button, and calls (from the separate thread) the main StopMonitor
* class to delete queries. The thread is ended when the gui is closed.
*/
class StopMonitorGUI extends JDialog implements Runnable
{
JList list;
DefaultListModel model;
StopMonitor myMonitor;
boolean finished = false; // whether the stop monitor has been closed by the user
boolean selected = false; // whether a selection has been made by the user
QueryBroker selectedQuery = null; // which data query has been selected.
StopMonitorGUI(StopMonitor myMonitor, Vector queryBrokers)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -