【Java开源代码栏目提醒】:本文主要为网学会员提供AuditBeanContainer.java,希望对需要AuditBeanContainer.java网友有所帮助,学习一下!
package ejava.beansch7;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.beans.Beans;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
/**
* This class implemented the container which contains the AuditBean.
* This class also implemented the serialization mechanism to test the
* Audit bean
*/
public class AuditBeanContainer extends JPanel
{
private AuditBean auditBean;
public AuditBeanContainer()
{
try{
ClassLoader useClassLoader = null;
// Instantiate Bean
auditBean = (AuditBean)Beans.instantiate(
useClassLoader,"ejava.beansch7.AuditBean");
// Add PropertyChangeListener to the Bean
AuditPropertyChangeAdapter auditPropertyChangeAdapter =
new AuditPropertyChangeAdapter();
auditBean.addPropertyChangeListener(
auditPropertyChangeAdapter);
// Add VetoPropertyChange Listener
AuditVetoPropertyChangeAdapter auditVetoPropertyChangeAdapter
= new AuditVetoPropertyChangeAdapter();
auditBean.addVetoableChangeListener(
auditVetoPropertyChangeAdapter);
}
catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.printStackTrace();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public AuditBean getBean()
{
return auditBean;
}
public void setBean(AuditBean newBean)
{
auditBean = newBean;
}
public static void main(String[] args)
{
AuditBeanContainer auditBeanContainer =
new AuditBeanContainer();
//Name of the file to which audit goes.
String auditFileName = "NewAuditFile.txt";
auditBeanContainer.auditBean.setFile(auditFileName);
System.out.println("Audit File Name :" + auditFileName);
//Notified the audit event event to the registered listeners
auditBeanContainer.auditBean.auditMessage("Starting Auditing");
//Name the Serialized file
String serializedFileName = "AuditBean.ser";
try{
FileOutputStream fileOutputStream
= new FileOutputStream(serializedFileName);
ObjectOutputStream objectOutputStream
= new ObjectOutputStream(fileOutputStream);
AuditBean beanToBeSerialized = auditBeanContainer.getBean();
//Writes the AuditBean object into the file
objectOutputStream.writeObject(beanToBeSerialized);
objectOutputStream.flush();
objectOutputStream.close();
FileInputStream fileInputStream =
new FileInputStream(serializedFileName);
ObjectInputStream objectInputStream =
new ObjectInputStream(fileInputStream);
//Reads the serialized AuditBean object from the file
AuditBean auditBean =
(AuditBean)objectInputStream.readObject();
String auditingFileName = auditBean.getFileName();
System.out.println("File to which Audit goes :" +
auditingFileName);
}
catch (IOException ioException) {
System.out.println(" Error :"+ioException);
ioException.printStackTrace();
}
catch (ClassNotFoundException classNotFoundException) {
System.out.println(" Error :"+classNotFoundException);
classNotFoundException.printStackTrace();
}
}
}
上一篇:
AuditBeanBeanInfo.java
下一篇:
多年来只想说一句,我不怪你