⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplelifecycle--rosonsandy.htm

📁 这是最详细的对Tomcat4以上版本的解析。
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://rosonsandy.blogdriver.com/rosonsandy/1046606.html -->
<HTML><HEAD><TITLE>SimpleLifecycle--rosonsandy</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GBK">
<META http-equiv=Pragma content=no-cache>
<META http-equiv=Cache-Control content=no-cache>
<META http-equiv=Expires content=0>
<META 
content="jdomSimpleLifecycleloadFromMlet  博客 博客动力 blog blogdriver blogger 中国" 
name=description>
<META 
content="rosonsandy jdomSimpleLifecycleloadFromMlet 博客 博客动力 blog blogdriver blogger 中国" 
name=keywords><LINK href="SimpleLifecycle--rosonsandy.files/diary.css" 
type=text/css rel=stylesheet>
<SCRIPT language=JavaScript 
src="SimpleLifecycle--rosonsandy.files/UBB.js"></SCRIPT>

<SCRIPT src="SimpleLifecycle--rosonsandy.files/blog.js" 
type=text/javascript></SCRIPT>

<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY>
<DIV id=container>
<DIV id=header>
<H1 class=title><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/index.html">rosonsandy</A></H1></DIV>
<DIV id=category><A title=上一篇 
href="http://rosonsandy.blogdriver.com/rosonsandy/1045976.html">jdom</A>- -| <A 
href="http://rosonsandy.blogdriver.com/rosonsandy/index.html">回首页</A> | <A 
href="http://rosonsandy.blogdriver.com/rosonsandy/catalog_2005.html">2005年索引</A> 
| - -<A title=下一篇 
href="http://rosonsandy.blogdriver.com/rosonsandy/1047527.html">loadFromMlet</A></DIV>
<DIV class=entity>
<H2 class=diaryTitle>SimpleLifecycle- -</H2>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

<P>有如下几个源程序: <BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#a">Lifecycle.java</A> 
<BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#b">LifecycleEvent.java</A> 
<BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#c">LifecycleException.java</A> 
<BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#d">LifecycleListener.java</A> 
<BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#e">LifecycleSupport.java</A> 
<BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#f">SimpleServer.java 
</A><BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#g">SimpleServerLifecycleListener.java</A> 
<BR><A 
href="http://rosonsandy.blogdriver.com/rosonsandy/1046606.html#h">TestLifecycle.java</A> 
<BR>
<P><IMG src="SimpleLifecycle--rosonsandy.files/simpleLifecycle.jpg"> </P>
<P><A name=#a></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>Lifecycle.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public interface Lifecycle {
	
	public static final String START_EVENT="start";
	
	public static final String STOP_EVENT="stop";
	
	public void addLifecycleListener(LifecycleListener listener);
	
	public LifecycleListener[] findLifecycleListeners();
	
	public void removeLifecycleListener(LifecycleListener listener);
	
	public void start() throws LifecycleException;
	
	public void stop()throws LifecycleException;
}
</PRE></DIV>
<P><A name=#b></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>LifecycleEvent.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */
import java.util.EventObject;

public final class LifecycleEvent extends EventObject {
	private Object data;
	private Lifecycle lifecycle;
	private String type;
	
	public LifecycleEvent(Lifecycle lifecycle,String type)
	{
		this(lifecycle,type,null);
	}

	public LifecycleEvent(Lifecycle lifecycle, String type,Object data) {
		super(lifecycle);
		
		this.data = data;
		this.lifecycle = lifecycle;
		this.type = type;
	}

	public Object getData() {
		return data;
	}

	public Lifecycle getLifecycle() {
		return lifecycle;
	}

	public String getType() {
		return type;
	}
}
</PRE></DIV>
<P><A name=#c></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>LifecycleException.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public final class LifecycleException extends Exception {
	protected String message;
	protected Throwable throwable;
	
	public LifecycleException()
    {
        this(null,null);
    }
    
    public LifecycleException(String message)
    {
        this(message,null);
    }
    
    public LifecycleException(Throwable throwable)
    {
        this(null,throwable);
    }
    
    public LifecycleException(String message,Throwable throwable)
    {
        super();
        this.message=message;
        this.throwable=throwable;
    }
    
    public String getMessage() 
    {
        return message;
    }
    
    public Throwable getThrowable() 
    {
        return throwable;
    }
    
    public String toString()
    {
        StringBuffer buf=new StringBuffer("LifecycleException: ");
        if(message!=null)
        {
            buf.append(message);
        }
        if(throwable!=null)
        {
            buf.append(": "+throwable.toString());
        }
        return buf.toString();
    }
}
</PRE></DIV>
<P><A name=#d></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>LifecycleListener.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public interface LifecycleListener {
	
	public void lifecycleEvent(LifecycleEvent event);
	
}
</PRE></DIV>
<P><A name=#e></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>LifecycleSupport.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public class LifecycleSupport {
	private Lifecycle lifecycle;
	private LifecycleListener[] listeners=new LifecycleListener[0];
	
	public LifecycleSupport(Lifecycle lifecycle)
	{
		this.lifecycle=lifecycle;
	}

	public void addLifecycleListener(LifecycleListener listener)
	{
		synchronized(listeners)
		{
			LifecycleListener[] results=new LifecycleListener[listeners.length+1];
			
			for(int i=0;i&lt;listeners.length;i++)
				results[i]=listeners[i];
			
			results[listeners.length]=listener;
			listeners=results;
		}
	}
	
	public void removeLifecycleListener(LifecycleListener listener)
	{
		synchronized(listeners)
		{
			int n=-1;
			for(int i=0;i&lt;listeners.length;i++)
			{
				if(listeners[i]==listener)
				{
					n=i;
					break;
				}
			}
			
			if(n&lt;0)
				return;
			
			LifecycleListener[] results=new LifecycleListener[listeners.length-1];
			int j=0;
			for(int i=0;i&lt;listeners.length;i++)
			{
				if(i!=n)
					results[j++]=listeners[i];
			}
			listeners=results;
		}
	}
	
	public LifecycleListener[] findLifecycleListeners()
	{
		return listeners;
	}
	
	public void fireLifecycleEvent(String type,String data)
	{
		LifecycleEvent event=new LifecycleEvent(lifecycle,type,data);
		LifecycleListener interested[]=null;
		
		synchronized(listeners)
		{
			interested=(LifecycleListener[])listeners.clone();
		}
		
		for(int i=0;i&lt;interested.length;i++)
			interested[i].lifecycleEvent(event);
	}
}
</PRE></DIV>
<P><A name=#f></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>SimpleServer.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public class SimpleServer implements Lifecycle {
	private LifecycleSupport lifecycle=new LifecycleSupport(this);
	private boolean started=false;
	

	public void addLifecycleListener(LifecycleListener listener) {
		lifecycle.addLifecycleListener(listener);
	}

	public LifecycleListener[] findLifecycleListeners() {
		return lifecycle.findLifecycleListeners();
	}

	public void removeLifecycleListener(LifecycleListener listener) {
		lifecycle.removeLifecycleListener(listener);
	}

	public void start() throws LifecycleException {
		started=true;
		lifecycle.fireLifecycleEvent(START_EVENT,null);
	}

	public void stop() throws LifecycleException {
		started=false;
		lifecycle.fireLifecycleEvent(STOP_EVENT,null);
	}
}
</PRE></DIV>
<P><A name=#g></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>SimpleServerLifecycleListener.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public class SimpleServerLifecycleListener implements LifecycleListener {

	public void lifecycleEvent(LifecycleEvent event) {
		Lifecycle lifecycle=event.getLifecycle();
		
		if(Lifecycle.START_EVENT.equals(event.getType()))
		{
			System.out.println("Server is starting");
		}
		
		if(Lifecycle.STOP_EVENT.equals(event.getType()))
		{
			System.out.println("Server is stopping");
		}
	}
}
</PRE></DIV>
<P><A name=#h></A></P>
<DIV 
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 5px; FONT: 11px/110% Andale Mono, Courier New, Courier, monospace; OVERFLOW: visible; WIDTH: 550px; PADDING-TOP: 5px; WHITE-SPACE: pre; BACKGROUND-COLOR: #efefef"><PRE style="COLOR: #006600; FONT-FAMILY: Andale Mono, Courier New, Courier, monospace"><B>TestLifecycle.java</B>

/*
 * @author roson
 *
 * 2005-11-7 
 */

public class TestLifecycle {

	public static void main(String[] args) throws LifecycleException {
		SimpleServer server=new SimpleServer();
		server.addLifecycleListener(new SimpleServerLifecycleListener());
		server.start();	
		server.stop();
	}
}
</PRE></DIV>
<P></P>
<P class=diaryFoot>- 作者: <A 
onclick="window.open('http://publishblog.blogdriver.com/blog/postMessage.b?receiver=517836','发送短消息','width=520, height=455')" 
href="javascript:void(0);">rosonsandy</A> 2005年11月7日, 星期一 20:35 </P></DIV>
<DIV class=operation><A name=trackback>
<H3>Trackback</H3></A>
<P class=trackback>你可以使用这个链接引用该篇日志 
http://publishblog.blogdriver.com/blog/tb.b?diaryID=1046606 </P></DIV>
<DIV class=operation><A name=comment>
<H3>回复</H3></A></DIV>
<DIV class=operation>
<TABLE class=comment cellSpacing=0 cellPadding=0 width=700 border=0>
  <FORM id=replyForm method=post><INPUT type=hidden value=356891 name=blogID> 
  <INPUT type=hidden value=1046606 name=diaryID> <INPUT type=hidden 
  value=rosonsandy name=blogDomino>
  <SCRIPT>
if(getCookie('userID') == null){        
document.write('<tr><td width="70">发布人:</td>');
document.write('<td width="150"> <input name="remark.authorNameFUI" type="text" size="20" class="inputStyle" maxlength="20"></td>');
document.write('<td width="70">邮箱:</td>');
document.write('<td width="435"> <input name="remark.authorEmail" type="text" size="20" class="inputStyle" maxlength="40"></td>');
document.write('</tr><tr><td>主 页:</td>');
document.write('<td colspan="3"> <input name="remark.authorURL" type="text" class="inputStyle" value="HTTP://" size="63" maxlength="40"></td></tr>');
}else{
document.write('<input type="hidden" name="remark.authorNameFUI" value="Blogdriver">');
}
</SCRIPT>
   
  <TBODY>
  <TR align=left>
    <TD colSpan=4>评论内容:<BR><TEXTAREA class=textStyle id=remark name=remark.remarkFUI rows=8 cols=60>          </TEXTAREA> 
    </TD></TR>
  <TR align=left>
    <TD colSpan=4>              <INPUT onclick=reply() type=button value=提交>   
<INPUT type=reset value=重置> </TD></TR></FORM></TBODY></TABLE></DIV></DIV>
<SCRIPT src="" type=text/javascript></SCRIPT>

<DIV id=footer><A href="http://www.blogdriver.com/jsp/reg/register.jsp"><IMG 
class=logo src="SimpleLifecycle--rosonsandy.files/logo3.gif" border=0></A><A 
href="http://www.blogdriver.com/">2003-2004 BLOGDRIVER.COM All rights 
reserved</A></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -