001: /*
002: * HMain.java
003: *
004: * Created on March 23, 2006, 10:38 AM
005: *
006: * To change this template, choose Tools | Options and locate the template under
007: * the Source Creation and Management node. Right-click the template and choose
008: * Open. You can then make changes to the template in the Source Editor.
009: */
010:
011: package simpleorm.simplehtml;
012:
013: import java.io.InputStreamReader;
014: import java.io.LineNumberReader;
015: import java.io.OutputStream;
016: import java.net.BindException;
017: import java.net.InetAddress;
018: import java.net.ServerSocket;
019: import java.net.Socket;
020: import org.mortbay.http.HttpContext;
021: import org.mortbay.http.HttpServer;
022: import org.mortbay.http.handler.DumpHandler;
023: import org.mortbay.http.handler.NotFoundHandler;
024: import org.mortbay.http.handler.ResourceHandler;
025: import org.mortbay.jetty.servlet.ServletHandler;
026: import org.mortbay.util.InetAddrPort;
027: import org.mortbay.util.MultiException;
028:
029: /**
030: *
031: * @author aberglas
032: */
033: public class HJettyMain {
034:
035: public void doMain(String contextDocRoot) throws Exception {
036: shutdownExisting();
037:
038: for (int i = 0; i < 3; i++) {
039: try {
040: startup(contextDocRoot);
041: } catch (MultiException ex) {
042: System.err.println("Failed to start " + ex);
043: if (ex.getException(0) instanceof BindException) {
044: System.err
045: .println("Sleeping, hoping that old instance will shut down... ");
046: Thread.sleep(2000);
047: continue;
048: } else
049: throw ex;
050: }
051: new Monitor(getPort() - 1);
052: return;
053: }
054: throw new Exception("Failed to start in multiple attempts");
055: }
056:
057: /** Can be overridden */
058: protected int getPort() {
059: return 8040;
060: }
061:
062: /**
063: * Each servlet and requestlet needs to be listed here.<p>
064: *
065: * servlets.addServlet("Master", "driver/*", HMasterServlet.class.getName());<br>
066: * servlets.addServlet("Scratch", "scratch/*", HScratchServlet.class.getName());
067: */
068: protected void addServlets(ServletHandler servlets) {
069: }
070:
071: /** Should be overriden, default is top level. */
072: public String getContext() {
073: return "";
074: }
075:
076: void startup(String contextDocRoot) throws Exception {
077: // From HttpServer main()
078: int port = getPort();
079: // http://jetty.mortbay.org/jetty/tut/Server.html#nonwebapps
080:
081: HttpServer server = new HttpServer();
082:
083: InetAddrPort address = new InetAddrPort(port + ""); // [<addr>:]<port>
084: server.addListener(address);
085:
086: // Default is no virtual host
087: String host = null;
088: HttpContext context = server.getContext(host, "/"
089: + getContext());
090: context.setResourceBase(contextDocRoot);
091:
092: //context.add(new SecurityHandler());
093: ServletHandler servlets = new ServletHandler();
094: context.addHandler(servlets);
095:
096: addServlets(servlets);
097:
098: ResourceHandler rhandler = new ResourceHandler();
099: rhandler.setDirAllowed(true);
100: context.addHandler(rhandler);
101:
102: context.addHandler(new DumpHandler());
103: context.addHandler(new NotFoundHandler());
104:
105: server.start();
106: }
107:
108: /**
109: *Shutdown any existing Jetty on the port.
110: *Taken from org.mortbay.stop.HMain
111: */
112: public void shutdownExisting() throws Exception {
113: Socket s;
114: int mport = getPort() - 1;
115: try {
116: s = new Socket(InetAddress.getByName("127.0.0.1"), mport);
117: } catch (java.net.ConnectException ce) {
118: System.err.println("Shutdown failed to connect port "
119: + mport + ": " + ce);
120: return; // Probably just no previous server running.
121: }
122: OutputStream out = s.getOutputStream();
123: out.write(("" + "stop\r\n").getBytes());
124: out.flush();
125: s.shutdownOutput();
126: s.close();
127: Thread.sleep(3000); // ##
128: }
129:
130: /**
131: * Waits for a shutdown message and then crudely exits the process.
132: * Taken from org.mortbay.start.
133: */
134: static class Monitor extends Thread {
135: ServerSocket _socket;
136: int _port;
137:
138: Monitor(int mport) throws Exception {
139: _port = mport;
140: setDaemon(true); // ie. do not prevent Java from exiting.
141: _socket = new ServerSocket(_port, 1, InetAddress
142: .getByName("127.0.0.1"));
143: this .start();
144: }
145:
146: public void run() {
147: System.err.println("Started Monitor");
148: while (true) {
149: Socket socket = null;
150: try {
151: socket = _socket.accept();
152:
153: LineNumberReader lin = new LineNumberReader(
154: new InputStreamReader(socket
155: .getInputStream()));
156: String cmd = lin.readLine();
157: System.err.println("Monitor command:- " + cmd);
158: if ("stop".equals(cmd)) {
159: try {
160: socket.close();
161: } catch (Exception e) {
162: e.printStackTrace();
163: }
164: try {
165: _socket.close();
166: } catch (Exception e) {
167: e.printStackTrace();
168: }
169: System.exit(0);
170: } else if ("status".equals(cmd)) {
171: socket.getOutputStream().write(
172: "OK\r\n".getBytes());
173: socket.getOutputStream().flush();
174: }
175: } catch (Exception e) {
176: System.err.println(e.toString());
177: } finally {
178: if (socket != null)
179: try {
180: socket.close();
181: } catch (Exception e) {
182: }
183: socket = null;
184: }
185: }
186: }
187:
188: }
189:
190: }
|