【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,AuditBean.java,希望对大家有所帮助!
package ejava.beansch7;
import java.io.Serializable;
import java.util.Date;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.FileInputStream;
import java.util.Vector;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeSupport;
import java.util.Hashtable;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.beans.PropertyVetoException;
/**
* AuditBean bean can audit information
* it can inform to registered objects
*
*/
public class AuditBean implements Serializable
{
private transient PrintStream streamToWrite;
private final String DEFAULT_FILE_NAME = "default.txt";
// unless user changes the file , the bean by default
// writes audit information to defult.txt file.
private File file = new File(DEFAULT_FILE_NAME);
// specific Audit Type
private int auditType = -1;
// List of Listeners to be notified of an event
private Vector auditListeners = new Vector();
// Declare and instantiate a property change object
private PropertyChangeSupport propertyChange =
new PropertyChangeSupport(this);
// Declare and instantiate a vetoable change object
private VetoableChangeSupport vetoableChange =
new VetoableChangeSupport(this);
public AuditBean()
{
// default constructor, it should be implemented for a bean
}
public void setFile(String newFileName)
{
File file = new File(newFileName);
setFile(file);
}
public void setFile(File newFile)
{
File oldFile = file;
try {
vetoableChange.fireVetoableChange("file",
oldFile, newFile);
file = newFile;
propertyChange.firePropertyChange("file",
oldFile, newFile);
fileChanged();
}
catch(PropertyVetoException propertyVetoException) {
// The file name is not needed to change in this
// exception. This code is for demonstrating the
// PropertyVetoException
file = oldFile;
}
}
public File getFile()
{
return file;
}
public String getFileName()
{
return file.getName();
}
public void setFileName(String fileName)
{
setFile(fileName);
}
public void fileChanged()
{
try {
FileOutputStream foutStream = new FileOutputStream(file);
boolean flushOut = true;
streamToWrite = new PrintStream(foutStream,flushOut);
} catch(IOException ioEx) {
}
}
//Send change event to listeners when property is changed
public void setAuditType(int type)
{
int oldAuditType = auditType;
auditType = type;
this.propertyChange.firePropertyChange("AuditType",
new Integer(oldAuditType), new Integer(type));
}
public int getAuditType()
{
return auditType;
}
/**
* Add an AuditEvent Listener to the Bean
*/
public synchronized void addAuditListener(
AuditListener auditListener)
{
auditListeners.add(auditListener);
}
/**
* Remove an AuditEvent Listener from the bean.
*/
public synchronized void removeAuditListener(
AuditListener auditListener)
{
auditListeners.remove(auditListener);
}
public void auditMessage(String message)
{
Date nonCreditAuditDate = null;
if(streamToWrite == null ){
try{
FileOutputStream foutStream = new FileOutputStream(file);
boolean flushOut = true;
streamToWrite = new PrintStream(foutStream,flushOut);
}
catch(IOException ioEx) {
}
}
Date currentTime = new Date();
String auditMessage = message + " : " + currentTime;
streamToWrite.println(auditMessage);
// Helper method to notify events to the listeners
notifyAuditEvent(auditMessage);
}
/**
* notify to all the listeners
*/
protected void notifyAuditEvent(String auditMessage)
{
Vector listeners = null;
AuditEvent auditEvent = new AuditEvent(this, auditMessage);
synchronized(this) {
listeners = (Vector) auditListeners.clone();
}
for (int i = 0; i < listeners.size(); i++) {
AuditListener auditListener =
(AuditListener)listeners.elementAt(i);
auditListener.auditEvent(auditEvent);
}
}
/**
* Add property change listener to the bean.
*/
public void addPropertyChangeListener(
PropertyChangeListener propertyChangeListener)
{
propertyChange.addPropertyChangeListener(propertyChangeListener);
}
/**
* Remove property change Listener from the bean.
*/
public void removePropertyChangeListener(
PropertyChangeListener propertyChangeListener)
{
propertyChange.removePropertyChangeListener(
propertyChangeListener);
}
/**
* Add vetoable change listener to the bean.
*/
public void addVetoableChangeListener(VetoableChang