fixed shutdown

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@288 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-05-26 02:47:51 +00:00
parent e17c559c84
commit 3ef3440a59
2 changed files with 37 additions and 17 deletions

View File

@ -21,6 +21,8 @@ import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -68,6 +70,7 @@ public class Server extends HandlerWrapper implements Attributes
private AttributesMap _attributes = new AttributesMap(); private AttributesMap _attributes = new AttributesMap();
private List<Object> _dependentBeans=new ArrayList<Object>(); private List<Object> _dependentBeans=new ArrayList<Object>();
private int _graceful=0; private int _graceful=0;
private boolean _stopAtShutdown;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public Server() public Server()
@ -107,16 +110,15 @@ public class Server extends HandlerWrapper implements Attributes
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public boolean getStopAtShutdown() public boolean getStopAtShutdown()
{ {
return hookThread.contains(this); return _stopAtShutdown;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public void setStopAtShutdown(boolean stop) public void setStopAtShutdown(boolean stop)
{ {
_stopAtShutdown=stop;
if (stop) if (stop)
hookThread.add(this); hookThread.add(this);
else
hookThread.remove(this);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -128,7 +130,6 @@ public class Server extends HandlerWrapper implements Attributes
return _connectors; return _connectors;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public void addConnector(Connector connector) public void addConnector(Connector connector)
{ {
@ -184,6 +185,9 @@ public class Server extends HandlerWrapper implements Attributes
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
if (getStopAtShutdown())
hookThread.add(this);
Log.info("jetty-"+_version); Log.info("jetty-"+_version);
HttpGenerator.setServerVersion(_version); HttpGenerator.setServerVersion(_version);
MultiException mex=new MultiException(); MultiException mex=new MultiException();
@ -301,6 +305,7 @@ public class Server extends HandlerWrapper implements Attributes
} }
mex.ifExceptionThrow(); mex.ifExceptionThrow();
hookThread.remove(this);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -519,8 +524,8 @@ public class Server extends HandlerWrapper implements Attributes
*/ */
private static class ShutdownHookThread extends Thread private static class ShutdownHookThread extends Thread
{ {
private boolean hooked = false; private boolean _hooked = false;
private ArrayList servers = new ArrayList(); private Set<Server> _servers = new CopyOnWriteArraySet<Server>();
/** /**
* Hooks this thread for shutdown. * Hooks this thread for shutdown.
@ -529,7 +534,7 @@ public class Server extends HandlerWrapper implements Attributes
*/ */
private void createShutdownHook() private void createShutdownHook()
{ {
if (!hooked) if (!_hooked)
{ {
try try
{ {
@ -537,7 +542,7 @@ public class Server extends HandlerWrapper implements Attributes
{ java.lang.Thread.class}); { java.lang.Thread.class});
shutdownHook.invoke(Runtime.getRuntime(), new Object[] shutdownHook.invoke(Runtime.getRuntime(), new Object[]
{ this}); { this});
this.hooked = true; _hooked = true;
} }
catch (Exception e) catch (Exception e)
{ {
@ -550,11 +555,11 @@ public class Server extends HandlerWrapper implements Attributes
/** /**
* Add Server to servers list. * Add Server to servers list.
*/ */
public boolean add(Server server) public void add(Server server)
{ {
_servers.add(server);
if (server.getStopAtShutdown()) if (server.getStopAtShutdown())
createShutdownHook(); createShutdownHook();
return this.servers.add(server);
} }
/** /**
@ -562,7 +567,12 @@ public class Server extends HandlerWrapper implements Attributes
*/ */
public boolean contains(Server server) public boolean contains(Server server)
{ {
return this.servers.contains(server); return _servers.contains(server);
}
public Iterable<Server> getServers()
{
return _servers;
} }
/** /**
@ -570,7 +580,7 @@ public class Server extends HandlerWrapper implements Attributes
*/ */
public void clear() public void clear()
{ {
this.servers.clear(); _servers.clear();
} }
/** /**
@ -579,7 +589,7 @@ public class Server extends HandlerWrapper implements Attributes
public boolean remove(Server server) public boolean remove(Server server)
{ {
createShutdownHook(); createShutdownHook();
return this.servers.remove(server); return _servers.remove(server);
} }
/** /**
@ -589,11 +599,9 @@ public class Server extends HandlerWrapper implements Attributes
{ {
setName("Shutdown"); setName("Shutdown");
Log.info("Shutdown hook executing"); Log.info("Shutdown hook executing");
Iterator it = servers.iterator(); for (Server svr : _servers)
while (it.hasNext())
{ {
Server svr = (Server) it.next(); if (svr == null || !svr.getStopAtShutdown() || !svr.isRunning())
if (svr == null || !svr.getStopAtShutdown())
continue; continue;
try try
{ {

View File

@ -18,6 +18,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.text.DateFormat; import java.text.DateFormat;
@ -57,6 +58,17 @@ public abstract class Resource implements Serializable
return __defaultUseCaches; return __defaultUseCaches;
} }
/* ------------------------------------------------------------ */
/** Construct a resource from a uri.
* @param uri A URI.
* @return A Resource object.
*/
public static Resource newResource(URI uri)
throws IOException
{
return newResource(uri.toURL());
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Construct a resource from a url. /** Construct a resource from a url.
* @param url A URL. * @param url A URL.