home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
prosrc.bin
/
DataBus.java
< prev
next >
Wrap
Text File
|
1998-03-18
|
8KB
|
233 lines
/*
* @(#DataBus.java
*
* Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
*
*/
//This singleton class is the manifestation of the IB. Component classes participate in the IB by calling static member functions on
//this class in order to register their interest in IB activity. In addition, helper methods are provided on this class to implement the
//broadcast of notifications of and solicitations for new data. Bus participants must implement the DataBusEventListener interface
//in order to register for and receive IB events. You cannot create a new instance of an DataBus by calling a constructor (it's
//private); however, instances of an IB may be created as a byproduct of the joinDataBus() call.
package symantec.itools.db.beans.binding.databus;
import java.beans.*;
import java.util.Vector;
import java.awt.event.*;
public class DataBus {
//The debug instance variable
//can be used to control the printing of stub information when handleEvent is called.
private boolean debug = false;
//The list of listeners
private static Vector listeners = new Vector();
//the list if members
// a listner can be a member or not
//a member can be a listener or not
private static Vector members = null;
private static DataBus ib=null;
private DataBus(){
if (members==null)
members = new Vector();
ib=this;
}
public static void initialize(){
ib=null;
members=null;
listeners=new Vector();
}
private static DataBus getDataBus(DataBus ibx){
if (ibx==null && ib==null){
ib=new DataBus();
return ib;
}
else if (ib!=null)
return ib;
else
return ibx;
}
// This static member function uses the JavaBeans constrained property mechanism to push a (possibly
// new) DataBus object onto the dbm object. This DataBus object is used in subsequent calls to identify
// which of possible multiple DataBuses within an instance of a JVM is the target.
public static synchronized void joinDataBus(DataBusMember dbm)
{
DataBus ib=dbm.getDataBus();
try
{
dbm.setDataBus(getDataBus(ib));
}
catch(PropertyVetoException e){}
members.addElement(dbm);
}
public static synchronized void leaveDataBus(DataBusMember dbm)
{
members.removeElement(dbm);
}
// These methods register and unregister a listener from a particular instance of the IB.
public synchronized void addDataBusEventListener(DataBusEventListener listener)
{
listeners.addElement(listener);
}
public synchronized void removeDataBusEventListener(DataBusEventListener listener)
{
listeners.removeElement(listener);
}
// This method broadcasts an event to all DataBusEvent listeners in the same IB context. The caller is
// responsible for construction of an appropriate DataBusEvent.
public synchronized void announceDataItem(DataBusEvent e)
{
if (debug) {
System.err.println("Event type pressed: " + e.getEventType() +
" , for the data item: " + e.getDataItemName() +" from: " +e.toString());
}
Vector targets;
synchronized (this) {
targets = (Vector) listeners.clone();
}
try{
DataBusEvent actionEvt = new DataBusEvent(e.getEventType(),e.getDataItemName(),e.getSource());
for (int i = 0; i < targets.size(); i++) {
DataBusEventListener target = (DataBusEventListener)targets.elementAt(i);
target.handleDataBusEvent(actionEvt);
}
}
catch ( InvalidType x ) {
System.err.println("Exception: " + x.getMessage());
}
}
//Each of these methods broadcasts a request for data to all IB components. The version that returns a
//single DataItem returns the DataItem from the first positive response on the DataBus. This first version
//of findDataItem() stops polling IB members when it receives a response. Note that the polling order of
//IB members is indeterminate, but once established, remains essentially unchanged. To gather all
//responses to an inquiry, a requester should use the similar method, findAllDataItems(), which polls all
//IB members and returns all responses in an array of DataItems.
public DataItem findDataItem(String dataItemName, DataBusMember consumer)
{
DataItem di=null;
try {
//create the event with Data Request and the object that creates the event
DataBusEvent ibevent=new DataBusEvent(DataBusEvent.DATA_REQUEST,dataItemName,consumer,di);
//question the members about dataItemName. If they aren't concerned than the dataItem returned
//will be null
for (int i=0;i<members.size();i++){
DataBusEventListener ibel=(DataBusEventListener)members.elementAt(i);
ibel.requestDataItem(ibevent);
if ((di=ibevent.getDataItem())!=null)
//if a data item is reached we go out
break;
}
}
catch ( InvalidType x ) {
System.err.println("Exception: " + x.getMessage());
}
finally {
return(di);
}
}
public synchronized DataItem[] findAllDataItems(String dataItemName,DataBusMember consumer)
{
DataItem dia[]=null;
DataItem di=null;
int index=0;
try {
//create the event with Data Request and the object that creates the event
DataBusEvent ibevent=new DataBusEvent(DataBusEvent.DATA_REQUEST,dataItemName,consumer,di);
//question all the members about dataItemName. If they aren't concerned than the dataItem returned
//will be null
for (int i=0;i<members.size();i++){
DataBusEventListener ibel=(DataBusEventListener)members.elementAt(i);
ibel.requestDataItem(ibevent);
if ((di=ibevent.getDataItem())!=null){
//if a data item is reached we go out
dia[i]=di;
i++;
di=null;
}
}
}
catch ( InvalidType x ) {
System.err.println("Exception: " + x.getMessage());
}
finally {
return dia;
}
}
//The last version of findDataItem() is symmetric to the one which appears on the DataBusEventListener
//class. To use this version, the requesting IB member first constructs an DataBusEvent with the
//appropriate name, with the eventType set to DataBus.DATA_REQUEST, and with the DataItem field
//of the event set to NULL. If the query is successful, the DataItem from the first positive response will
//be attached to e.DataItem. Again, once a positive response occurs on the IB, other members are not
//queried.
public synchronized void findDataItem(DataBusEvent e)
{
DataItem di=null;
//question the members about dataItemName. If they aren't concerned than the dataItem returned
//will be null
for (int i=0;i<members.capacity();i++){
DataBusEventListener ibel=(DataBusEventListener)members.elementAt(i);
ibel.requestDataItem(e);
if ((di=e.getDataItem())!=null)
//if a data item is reached we go out
break;
}
}
// Making debug a property lets you alter the reporting of events through a println
// stub on the fly by changing the value of the property from false to true using
// a property sheet editor. When debug is true calls to announceDataItem are reported
// to the users by printing the label of the action that fired the event.
public void setDebug(boolean x) {
boolean old = debug;
debug = x;
}
public boolean getDebug() {
return debug;
}
}