public final class lifecycleevent extends eventobject {
//......
public lifecycleevent(lifecycle lifecycle, string type, object data) {
super(lifecycle);
this.type = type;
this.data = data;
}
//......
}
public interface lifecycle {
/**
* the lifecycleevent type for the "component after init" event.
*/
public static final string before_init_event = "before_init";
/**
* the lifecycleevent type for the "component after init" event.
*/
public static final string after_init_event = "after_init";
/**
* the lifecycleevent type for the "component start" event.
*/
public static final string start_event = "start";
//......
}
public interface lifecyclelistener {
public void lifecycleevent(lifecycleevent event);
}
public class contextconfig implements lifecyclelistener {
//......
@override
public void lifecycleevent(lifecycleevent event) {
// identify the context we are associated with
try {
context = (context) event.getlifecycle();
} catch (classcastexception e) {
log.error(sm.getstring("contextconfig.cce", event.getlifecycle()), e);
return;
}
// process the event that has occurred
if (event.gettype().equals(lifecycle.configure_start_event)) {
configurestart();
} else if (event.gettype().equals(lifecycle.before_start_event)) {
beforestart();
} else if (event.gettype().equals(lifecycle.after_start_event)) {
// restore docbase for management tools
if (originaldocbase != null) {
context.setdocbase(originaldocbase);
}
} else if (event.gettype().equals(lifecycle.configure_stop_event)) {
configurestop();
} else if (event.gettype().equals(lifecycle.after_init_event)) {
init();
} else if (event.gettype().equals(lifecycle.after_destroy_event)) {
destroy();
}
}
//......
}
public final class lifecyclesupport {
//......
//监听对象集合
private lifecyclelistener listeners[] = new lifecyclelistener[0];
private final object listenerslock = new object(); // lock object for changes to listeners
//添加监听对象
public void addlifecyclelistener(lifecyclelistener listener) {
synchronized (listenerslock) {
lifecyclelistener results[] =
new lifecyclelistener[listeners.length + 1];
for (int i = 0; i < listeners.length; i++)
results[i] = listeners[i];
results[listeners.length] = listener;
listeners = results;
}
}
//发布监听对象
public void firelifecycleevent(string type, object data) {
lifecycleevent event = new lifecycleevent(lifecycle, type, data);
lifecyclelistener interested[] = listeners;
for (int i = 0; i < interested.length; i++)
interested[i].lifecycleevent(event);
}
//移除监听对象
public void removelifecyclelistener(lifecyclelistener listener) {
synchronized (listenerslock) {
int n = -1;
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == listener) {
n = i;
break;
}
}
if (n < 0)
return;
lifecyclelistener results[] =
new lifecyclelistener[listeners.length - 1];
int j = 0;
for (int i = 0; i < listeners.length; i++) {
if (i != n)
results[j++] = listeners[i];
}
listeners = results;
}
}
}
public abstract class applicationevent extends eventobject {
//......
public applicationevent(object source) {
super(source);
this.timestamp = system.currenttimemillis();
}
//......
}
public abstract class applicationcontextevent extends applicationevent {
public applicationcontextevent(applicationcontext source) {
super(source);
}
public final applicationcontext getapplicationcontext() {
return (applicationcontext)this.getsource();
}
}
public class contextstartedevent extends applicationcontextevent {
public contextstartedevent(applicationcontext source) {
super(source);
}
}