001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2007 Ola Bini <ola@ologix.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby;
028:
029: import java.io.BufferedReader;
030: import java.io.InputStreamReader;
031:
032: import java.net.InetAddress;
033: import java.net.InetSocketAddress;
034: import java.net.ServerSocket;
035: import java.net.Socket;
036:
037: import java.util.List;
038: import java.util.ArrayList;
039:
040: /**
041: * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
042: */
043: public class JRubyServer extends JRubyService {
044: private Configuration conf;
045:
046: private boolean stillStarting = true;
047:
048: private JRubyServer(String[] args) throws Exception {
049: conf = new Configuration(args[0]);
050: if (conf.isDebug()) {
051: System.err.println("Starting server with port "
052: + conf.getPort() + " and key " + conf.getKey());
053: }
054: ServerSocket server = new ServerSocket();
055: server.bind(new InetSocketAddress(InetAddress.getLocalHost(),
056: conf.getPort()));
057: while (true) {
058: Thread t1 = new Thread(new Handler(server.accept()));
059: t1.setDaemon(true);
060: t1.start();
061: }
062: }
063:
064: private class Handler implements Runnable {
065: private Socket socket;
066:
067: public Handler(Socket socket) {
068: this .socket = socket;
069: }
070:
071: public void run() {
072: try {
073: BufferedReader rr = new BufferedReader(
074: new InputStreamReader(this .socket
075: .getInputStream()));
076: String command = rr.readLine();
077: rr.close();
078: this .socket.close();
079: this .socket = null;
080: if (conf.isDebug()) {
081: System.err.println("Got command: " + command);
082: }
083: String[] cmds = command.split(" ", 3);
084: if (cmds[1].equals(conf.getKey())) {
085: if (cmds[0].equals(CMD_TERM)) {
086: if (conf.isDebug()) {
087: System.err.println("Terminating hard");
088: }
089: System.exit(0);
090: } else if (cmds[0].equals(CMD_NO_MORE)) {
091: if (conf.isDebug()) {
092: System.err
093: .println("Accepting no more START");
094: }
095: stillStarting = false;
096: } else if (cmds[0].equals(CMD_START)) {
097: if (stillStarting) {
098: if (conf.isDebug()) {
099: System.err
100: .println("Doing START on command "
101: + cmds[2]);
102: }
103: new Main().run(intoCommandArguments(cmds[2]
104: .trim()));
105: } else {
106: if (conf.isDebug()) {
107: System.err
108: .println("Not doing START anymore, invalid command");
109: }
110: }
111: } else {
112: if (conf.isDebug()) {
113: System.err.println("Unrecognized command");
114: }
115: }
116: } else {
117: if (conf.isDebug()) {
118: System.err.println("Invalid key");
119: }
120: }
121: } catch (Exception e) {
122: }
123: }
124: }
125:
126: protected static String[] intoCommandArguments(String str) {
127: List args = new ArrayList();
128: boolean inSingle = false;
129: int contentStart = -1;
130:
131: for (int i = 0, j = str.length(); i < j; i++) {
132: if (str.charAt(i) == ' ' && !inSingle && contentStart != -1) {
133: args.add(str.substring(contentStart, i));
134: contentStart = -1;
135: continue;
136: }
137: if (str.charAt(i) == ' ') {
138: continue;
139: }
140: if (str.charAt(i) == '\'' && !inSingle) {
141: inSingle = true;
142: contentStart = i + 1;
143: continue;
144: }
145: if (str.charAt(i) == '\'') {
146: inSingle = false;
147: args.add(str.substring(contentStart, i));
148: contentStart = -1;
149: continue;
150: }
151: if (contentStart == -1) {
152: contentStart = i;
153: }
154: }
155: if (contentStart != -1) {
156: args.add(str.substring(contentStart));
157: }
158: return (String[]) args.toArray(new String[0]);
159: }
160:
161: public static void main(String[] args) throws Exception {
162: new JRubyServer(args);
163: }
164: }// JRubyServer
|