first commit
authorgumartinm <gu.martinm@gmail.com>
Sun, 29 Jan 2012 16:00:44 +0000 (17:00 +0100)
committergumartinm <gu.martinm@gmail.com>
Sun, 29 Jan 2012 16:00:44 +0000 (17:00 +0100)
12 files changed:
Daemon/Makefile [new file with mode: 0644]
Daemon/README.txt [new file with mode: 0644]
Daemon/javafork [new file with mode: 0755]
Daemon/javafork.c [new file with mode: 0644]
Daemon/javafork.h [new file with mode: 0644]
JavaExample/javafork-example/pom.xml [new file with mode: 0644]
JavaExample/javafork-example/src/main/java/de/fork/java/LauncherProcesses.java [new file with mode: 0644]
JavaExample/javafork-example/src/main/java/de/fork/java/RemoteForkMain.java [new file with mode: 0644]
JavaExample/javafork-example/src/main/java/de/fork/java/TCPForkDaemon.java [new file with mode: 0644]
JavaExample/javafork-example/src/main/java/de/fork/java/XmlForkParser.java [new file with mode: 0644]
JavaExample/pom.xml [new file with mode: 0644]
README [new file with mode: 0644]

diff --git a/Daemon/Makefile b/Daemon/Makefile
new file mode 100644 (file)
index 0000000..d785678
--- /dev/null
@@ -0,0 +1,8 @@
+all: javafork
+
+javafork: javafork.c javafork.h
+        gcc -Wall -g -o javafork javafork.c -lpthread -D_GNU_SOURCE
+
+clean:
+       rm -f  javafork
+
diff --git a/Daemon/README.txt b/Daemon/README.txt
new file mode 100644 (file)
index 0000000..604f093
--- /dev/null
@@ -0,0 +1,3 @@
+Launch:
+
+javafork IPADDRESS TCP_PORT MAX_LISTEN
diff --git a/Daemon/javafork b/Daemon/javafork
new file mode 100755 (executable)
index 0000000..135062e
Binary files /dev/null and b/Daemon/javafork differ
diff --git a/Daemon/javafork.c b/Daemon/javafork.c
new file mode 100644 (file)
index 0000000..9fe2210
--- /dev/null
@@ -0,0 +1,692 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <syslog.h>
+#include <signal.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <strings.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+#include <sys/poll.h>
+#include <string.h>
+#include <signal.h>
+#include <ctype.h>
+#include <errno.h>
+#include <endian.h>
+#include "javafork.h"
+
+
+pid_t daemonPID;    /*Stores the daemon server PID*/
+int sockfd = -1;    /*Stores the daemon server TCP socket.*/
+
+
+
+static int restartableClose(int fd) 
+{
+    return TEMP_FAILURE_RETRY(close(fd));
+}
+
+
+
+static int closeSafely(int fd) 
+{
+    /*If we always initialize file descriptor variables with value -1*/
+    /*this method should work like a charm*/
+    return (fd == -1) ? 0 : restartableClose(fd);
+}
+
+
+
+int main (int argc, char *argv[]) 
+{
+       int c;                      /*Getopt parameter*/
+       /*Default values*/
+       char *avalue = IPADDRESS;   /*Address: numeric value or hostname*/
+       int pvalue = PORT;          /*TCP port*/
+       int qvalue = QUEUE;         /*TCP listen queue*/
+       
+       
+       /*This process is intended to be used as a daemon, it sould be launched by the INIT process, because of that*/
+       /*we are not forking it (INIT needs it)*/
+       if (daemonize(argv[0], LOG_SYSLOG, LOG_PID) < 0)
+               return 1;
+
+       /*Changing session.*/   
+       setsid();
+       
+       if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) {
+               syslog (LOG_ERR, "signal SIGPIPE desactivation failed: %m");
+               return 1;
+       }
+       
+       opterr = 0;
+       while ((c = getopt (argc, argv, "a:p:q:")) != -1) {
+               switch (c) {
+               case 'a':
+                       avalue = optarg;
+                       break;
+               case 'p':
+                       pvalue = atoi(optarg);
+                       if ((pvalue > 65535) || (pvalue <= 0)) {
+                               syslog (LOG_ERR, "Port value %d out of range", pvalue);
+                               return 1;
+                       }
+                       break;
+               case 'q':
+                       qvalue = atoi(optarg);
+                       break;
+               case '?':
+                       if ((optopt == 'a') || (optopt == 'p') || (optopt == 'q'))
+                               syslog (LOG_ERR, "Option -%c requires an argument.", optopt);
+                       else if (isprint (optopt))
+                               syslog (LOG_ERR, "Invalid option '-%c'.", optopt);
+                       else
+                               syslog (LOG_ERR, "Unknown option character '\\x%x'.", optopt);
+                       return 1;
+               default:
+                       abort ();
+               }
+       }
+
+       /*This program does not admit options*/
+       if (optind < argc) {
+               syslog (LOG_ERR,"This program does not admit options just argument elements with their values.");
+               return 1;
+       }
+       
+       /*INIT process sending SIGINT? Should I catch that signal?*/
+       daemonPID = getpid();
+       if (signal(SIGINT, sigint_handler) == SIG_ERR) {
+               syslog (LOG_ERR, "SIGTERM signal handler failed: %m");
+               return 1;
+       }
+       
+       if (main_daemon (avalue, pvalue, qvalue) < 0)
+               return 1;
+       
+       return 0;
+}
+
+
+
+int main_daemon (char *address, int port, int queue)
+{
+       struct protoent *protocol;          /*Network protocol*/
+       struct sockaddr_in addr_server;     /*struct with the server socket address*/
+       struct sockaddr_in  addr_client;    /*struct with the client socket address*/
+       int sockclient = -1;                /*File descriptor for the accepted socket*/
+       pthread_t idThread;                 /*Thread identifier number*/
+       socklen_t clilen;
+       int optval;
+       int returnValue = 0;                /*The return value from this function, OK by default*/
+       
+       
+       /*Retrieve protocol number from /etc/protocols file */
+       protocol=getprotobyname("tcp");
+       if (protocol == NULL) {
+               syslog(LOG_ERR, "cannot map \"tcp\" to protocol number: %m");
+               goto err;
+       }
+       
+       bzero((char*) &addr_server, sizeof(addr_server));
+       addr_server.sin_family = AF_INET;
+       if (inet_pton(AF_INET, address, &addr_server.sin_addr.s_addr) <= 0) {
+               syslog (LOG_ERR, "error inet_pton: %m");
+               goto err;
+       }
+       
+       addr_server.sin_port = htons(port);
+       
+       if ((sockfd = socket(AF_INET, SOCK_STREAM, protocol->p_proto)) < 0) {
+               syslog (LOG_ERR, "socket creation failed: %m");
+               goto err; 
+       }
+
+
+       /*We want to avoid issues while trying to bind a socket in TIME_WAIT state*/
+       optval = 1;
+       if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
+               syslog (LOG_ERR, "setsockopt failed: %m");
+               goto err;
+       }
+       
+       if (bind(sockfd, (struct sockaddr *) &addr_server, sizeof(addr_server)) < 0) {
+               syslog (LOG_ERR, "socket bind failed: %m");
+               goto err;
+       }
+       
+       if (listen (sockfd, queue) < 0 ) {
+               syslog (LOG_ERR, "socket listen failed: %m");
+               goto err;
+       }       
+       
+       while(1) {
+               clilen =  sizeof(addr_client);
+               if ((sockclient = TEMP_FAILURE_RETRY(accept (sockfd, (struct sockaddr *) &addr_client, &clilen))) < 0) {
+                       syslog (LOG_ERR, "socket accept failed: %m");
+                       goto err;
+               }
+
+               if (pthread_create (&idThread, NULL, serverThread, (void *)sockclient) != 0 ) {
+                       syslog (LOG_ERR, "thread creation failed: %m");
+               }
+       }
+
+end:
+    closeSafely (sockfd);
+    return returnValue;
+err:
+       /*When there is an error.*/
+       returnValue = -1;       
+       goto end;
+}
+
+
+
+int daemonize(const char *pname, int facility, int option)
+{
+       int fd = -1;    /*Temporaly store for the /dev/tty and /dev/null file descriptors*/
+       
+       if ((fd = TEMP_FAILURE_RETRY(open( "/dev/tty", O_RDWR, 0))) == -1) {
+        /*We already have no tty control*/
+        closeSafely(fd);
+        return 0;
+       }
+       
+       /*Sending messages to log*/
+       openlog(pname, option, facility);
+
+       /*To get a controlling tty*/
+       if (ioctl(fd, TIOCNOTTY, (caddr_t)0) <0 ) {
+               syslog (LOG_ERR, "Getting tty failed: %m");
+               return -1;
+       }
+
+       if (closeSafely(fd) < 0) {
+               syslog (LOG_ERR, "Closing tty failed: %m");
+               return -1;
+       }
+       
+       if ((fd = TEMP_FAILURE_RETRY(open( "/dev/null", O_RDWR, 0))) == -1) {
+               closeSafely(fd);
+               return -1;
+       }
+
+       if (TEMP_FAILURE_RETRY(dup2(fd,0)) < 0 || 
+        TEMP_FAILURE_RETRY(dup2(fd,1)) < 0 ||
+        TEMP_FAILURE_RETRY(dup2(fd,2)) < 0) {
+           closeSafely(fd);
+        return -1;
+    }
+
+    closeSafely(fd);   
+
+    return 0;
+}
+
+
+
+void *serverThread (void * arg)
+{
+       int socket = -1;                    /*Open socket by the Java client*/
+       long timeout, utimeout;             /*Timeout for reading data from client: secs and usecs respectively*/
+       int len;                            /*Control parameter used while receiving data from the client*/
+       char buffer[1025];                  /*This buffer is intended to store the data received from the client*/
+       char *command = NULL;               /*The command sent by the client, to be executed by this process*/  
+       uint32_t *commandLength = NULL;     /*Store the command length*/
+       
+       socket = (int) arg;
+       
+       pthread_detach(pthread_self());
+
+
+    if (required_sock_options (socket) < 0)
+        goto err;
+       
+       
+            /****************************************************************************************/
+            /*   Just over 1 TCP connection                                                         */
+            /*   COMMAND_LENGTH: Java integer 4 bytes, BIG-ENDIAN (the same as network order)       */
+            /*   COMMAND: locale character set encoding                                             */
+            /*   RESULTS: locale character set encoding                                             */
+            /*                                                                                      */                                                                                         
+            /*   JAVA CLIENT: ------------ COMMAND_LENGTH -------> :SERVER                          */
+            /*   JAVA CLIENT: -------------- COMMAND ------------> :SERVER                          */
+            /*   JAVA CLIENT: <-------------- RESULTS ------------ :SERVER                          */
+            /*   JAVA CLIENT: <---------- CLOSE CONNECTION ------- :SERVER                          */
+            /*                                                                                      */
+            /****************************************************************************************/
+
+    /*Wait max 2 seconds for data coming from client, otherwise exits with error.*/
+    timeout = 2;
+    utimeout = 0;
+
+
+    /*1. COMMAND LENGTH*/
+    /*First of all we receive the command size as a Java integer (4 bytes primitive type)*/    
+    if ((commandLength = (uint32_t *) malloc(sizeof(uint32_t))) == NULL) {
+        syslog (LOG_ERR, "commandLength malloc failed: %m");
+        goto err;
+    }
+
+    bzero(buffer, sizeof(buffer));
+    len = sizeof(uint32_t);
+
+    if (receive_from_socket (socket, buffer, len, timeout, utimeout) < 0)
+        goto err;
+
+    /*Retrieve integer (4 bytes) from buffer*/
+    memcpy (commandLength, buffer, sizeof(uint32_t));
+    /*Java sends the primitive integer using big-endian order (it is the same as network order)*/
+    *commandLength = be32toh (*commandLength);
+
+
+    /*2. COMMAND*/
+    /*Reserving commandLength + 1 because of the string end character*/
+    if ((command = (char *) malloc(*commandLength + 1)) == NULL) {
+        syslog (LOG_ERR, "command malloc failed: %m");
+        goto err;
+    }
+
+    bzero(command, ((*commandLength) + 1));
+    len = *commandLength;
+    /*Wait max 2 seconds for data coming from client, otherwise exits with error.*/
+    if (receive_from_socket (socket, command, len, timeout, utimeout) < 0)
+        goto err;
+
+
+    /*3. RESULTS*/     
+    pre_fork_system(socket, command);
+
+
+    /*4. CLOSE CONNECTION AND FINISH*/
+
+err:
+    free(command);
+    closeSafely(socket);
+    free(commandLength);
+
+    pthread_exit(0);
+}
+
+
+
+int required_sock_options (int socket)
+{
+    int optval, flags;
+
+    /*We want non blocking sockets.*/
+    /*See the discussion of spurious readiness notifications under the BUGS section of select(2) */
+    if ((flags = TEMP_FAILURE_RETRY(fcntl(socket,F_GETFL,0))) < 0) {
+        syslog (LOG_ERR, "read socket status flags failed: %m");
+        return -1;
+    }
+
+    if (TEMP_FAILURE_RETRY(fcntl(socket, F_SETFL, O_NONBLOCK|flags)) < 0){
+        syslog (LOG_ERR, "set socket status flags failed: %m");
+        return -1;
+    }
+
+    /*Portable programs should not rely on inheritance or noninheritance of file status flags and */
+    /*always explicitly set all required flags*/
+    optval = 1;
+    if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
+        syslog (LOG_ERR, "setsockopt SO_REUSEADDR failed: %m");
+        return -1;
+    }
+
+    /*Enable keepalive for this socket*/
+    optval = 1;
+    if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) < 0) {
+        syslog (LOG_ERR, "setsockopt SO_KEEPALIVE failed: %m");
+        return -1;
+    }
+
+    /*TODO: keepalive is not enough to find out if the connection is broken                     */
+    /*      apparently it just works while the handshake phase. See:                            */
+    /*      http://stackoverflow.com/questions/4345415/socket-detect-connection-is-lost         */
+    /*      I have to implement an echo/ping messages protocol (application layer)              */
+
+    return 0;
+}
+
+
+
+int readable_timeout (int fd, long timeout, long utimeout)
+{
+    struct timeval ptime;   /*Timeout, secs and usecs*/
+    fd_set fd_read;         /*Values for select function.*/
+
+    ptime.tv_sec = timeout;
+    ptime.tv_usec = utimeout;
+    FD_ZERO(&fd_read);
+    FD_SET(fd, &fd_read);
+  
+    return TEMP_FAILURE_RETRY(select(fd+1, &fd_read, NULL, NULL, &ptime)); 
+}
+
+
+
+int receive_from_socket (int socket, char *data, int len, long timeout, long utimeout)
+{
+    int nData, iPos;   /*Control variables.*/
+    int ret;            /*Store return value from select.*/
+
+    nData = iPos = 0;
+    do {
+        ret = readable_timeout(socket, timeout, utimeout);
+
+        if (ret == 0) {
+            syslog(LOG_INFO, "receiving timeout error");
+            return -1;
+        } else if (ret == -1) {
+            syslog(LOG_ERR, "receiving error: %m");
+            return -1;
+        }
+
+        nData = TEMP_FAILURE_RETRY(recv(socket, &data[iPos], len, 0));
+
+        if (nData < 0) {
+            if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
+                syslog (LOG_ERR, "read TCP socket failed: %m");
+                return -1;
+            } else {
+                /*see spurious readiness man select(2) BUGS section*/
+                nData = 0;
+                syslog (LOG_INFO, "read TCP socket spurious readiness");
+            }
+        } else if (nData == 0) {
+            /*if nData is 0, client closed connection but we wanted to receive more data, this is an error */
+            syslog (LOG_ERR, "expected more data from client");
+            return -1;
+        }
+
+        len -= nData;
+        iPos += nData;
+    } while (len > 0);
+
+    return 0;
+}
+
+
+int pre_fork_system(int socket, char *command)
+{
+    /*Required variables in order to share memory between processes*/
+    key_t keyvalue;
+    int idreturnstatus = -1;
+    /*Store the return status from the process launched using system or execve*/
+    /*Using shared memory between the child and parent process*/
+    int *returnstatus = NULL;
+       
+    /*Required variables in order to share the semaphore between processes*/
+    key_t keysemaphore;
+    int idsemaphore = -1;
+    sem_t *semaphore = NULL;    /*Used as a barrier: the child process just can start after sending the XML init code*/
+
+    int returnValue = -1;       /*Return value from this function can be caught by upper layers, NOK by default*/
+               
+       
+       
+    /*Allocate shared memory because we can not use named semaphores*/
+    /*We are using this semaphore as a barrier, because we just want to start the child process when the parent process has sent*/
+    /*the XML header (see: for_system function)*/
+       
+    keysemaphore=ftok("/bin/ls", SHAREMEMSEM);  /*the /bin/ls must exist otherwise this does not work... */
+    if (keysemaphore == -1) {
+        syslog (LOG_ERR, "ftok failed: %m");
+        goto end;
+    }
+
+    /*Attach shared memory*/
+    if ((idsemaphore = shmget(keysemaphore,sizeof(sem_t), 0660 | IPC_CREAT)) < 0) {
+        syslog (LOG_ERR, "semaphore initialization failed: %m");
+        goto end_release_sem;
+    }
+
+    if ((semaphore = (sem_t *)shmat(idsemaphore, (void *)0, 0)) < 0) {
+        goto end_release_sem;
+    }
+
+    if (sem_init(semaphore, 1, 1) < 0) {
+        syslog (LOG_ERR, "semaphore initialization failed: %m");
+        goto end_destroy_sem;
+    }
+
+    if (TEMP_FAILURE_RETRY(sem_wait(semaphore)) < 0) {
+        syslog (LOG_ERR, "semaphore wait failed: %m");
+        goto end_destroy_sem;
+    }
+       
+       
+       
+    /*Allocate shared memory for the return status code from the process which is going to be launched by the system function.*/
+    /*We want to share the returnstatus variable between this process and the child that is going to be created in the fork_system method.*/
+    /*The goal is to store in this variable the return status code received from the process launched with the system method by the child process,*/
+    /*then the parent process can retrieve that return status code and send it by TCP to the Java client.*/
+    /*There are not concurrency issues because the parent process will just try to read this variable when the child process is dead, taking*/
+    /*in that moment its last value and sending it to the Java client.*/
+
+
+    keyvalue=ftok("/bin/ls", SHAREMEMKEY);  /*the /bin/ls must exist otherwise this does not work... */
+    if (keyvalue == -1) {
+        syslog (LOG_ERR, "ftok failed: %m");
+        goto end_destroy_sem;
+    }
+
+    /*Attach shared memory*/
+    if ((idreturnstatus=shmget(keyvalue,sizeof(int), 0660 | IPC_CREAT)) < 0) {
+        syslog (LOG_ERR, "shmget failed: %m");
+        goto end_release_mem;
+    }
+
+    returnstatus = (int *)shmat(idreturnstatus, (void *)0, 0);
+    if ((*returnstatus)== -1) {
+        syslog (LOG_ERR, "shmat failed: %m");
+        goto end_release_mem;
+    } 
+
+    /*After allocating and attaching shared memory we reach this code if everything went OK.*/
+
+    returnValue = fork_system(socket, command, semaphore, returnstatus);
+
+
+end_release_mem:
+    if (returnstatus != NULL) {
+        /*detach memory*/
+        if (shmdt ((int *)returnstatus) < 0)
+            syslog (LOG_ERR, "returnstatus shared variable shmdt failed: %m");
+    }
+
+    /*Mark the segment to be destroyed.*/
+    if (shmctl (idreturnstatus, IPC_RMID, (struct shmid_ds *)NULL) < 0 )
+        syslog (LOG_ERR, "returnstatus shared variable shmctl failed: %m");
+end_destroy_sem:
+    if (sem_destroy(semaphore) <0)
+         syslog (LOG_ERR, "semaphore destroy failed: %m");
+end_release_sem:
+    /*after sem_destroy-> input/output parameter NULL?*/
+    if (semaphore != NULL) {
+        /*detach memory*/
+        if (shmdt ((sem_t *)semaphore) < 0)
+            syslog (LOG_ERR, "semaphore shmdt failed: %m");
+    }
+
+    /*Mark the segment to be destroyed.*/
+    if (shmctl (idsemaphore, IPC_RMID, (struct shmid_ds *)NULL) < 0 )
+        syslog (LOG_ERR, "semaphore shmctl failed: %m");
+end:
+    return returnValue;
+}
+
+
+
+int fork_system(int socket, char *command, sem_t *semaphore, int *returnstatus) 
+{
+    int pid;
+    int out[2], err[2];     /*Store pipes file descriptors. Write ends attached to the stdout and stderr streams.*/
+    char buf[2000];         /*Read data buffer.*/
+    char string[100];
+    /*We are going to use a poll in order to find out if there are data coming from the*/
+    /*pipes attached to the stdout and stderr streams.*/
+    struct pollfd polls[2];
+    int n;
+    int childreturnstatus;
+    int returnValue = 0;    /*return value from this function can be caught by upper layers, OK by default*/
+
+
+    /*Value by default*/
+    (*returnstatus) = 0;
+
+       
+    out[0] = out[1] = err[0] = err[1]  = -1;
+
+
+    /*Creating the pipes, they will be attached to the stderr and stdout streams*/     
+    if (pipe(out) < 0 || pipe(err) < 0) {
+        syslog (LOG_ERR, "pipe failed: %m");
+        goto err;
+    }
+
+    if ((pid=fork()) == -1) {
+        syslog (LOG_ERR, "fork failed: %m");
+        goto err;
+    }
+
+    if (pid == 0) {
+        /*Child process*/
+        /*It has to launch another one using system or execve*/
+        if ((TEMP_FAILURE_RETRY(dup2(out[1],1)) < 0) || (TEMP_FAILURE_RETRY(dup2(err[1],2)) < 0)) {    
+            syslog (LOG_ERR, "child dup2 failed: %m");
+            /*Going to zombie state, hopefully waitpid will catch it*/ 
+            exit(-1);
+        }
+
+        if (TEMP_FAILURE_RETRY(sem_wait(semaphore)) < 0) {
+            syslog (LOG_ERR, "child semaphore wait failed: %m");
+            /*Going to zombie state, hopefully waitpid will catch it*/
+            exit(-1);
+        }
+
+        *returnstatus=system(command);
+        if (WIFEXITED(returnstatus) == 1)
+            (*returnstatus) = WEXITSTATUS(*returnstatus);
+        else
+            (*returnstatus) = -1;
+        /*Going to zombie state, hopefully waitpid will catch it*/
+        exit(0);
+    }
+    else {
+        /*Parent process*/
+        /*It sends data to the Java client using a TCP connection.*/
+        polls[0].fd=out[0];
+        polls[1].fd=err[0];
+        polls[0].events=POLLIN;
+        polls[1].events=POLLIN;
+        sprintf(string,"<?xml version=\"1.0\"?>");
+        send(socket,string,strlen(string),0);
+        sprintf(string,"<streams>");
+        send(socket,string,strlen(string),0);
+
+        /*Releasing barrier, the child process can keep running*/
+        if (sem_post(semaphore) < 0 ) {
+            syslog (LOG_ERR, "parent error releasing barrier: %m");
+            /*TODO: May I kill a child process if it is already dead? I mean,   */
+            /*      what could happen if the child process is dead?             */
+            /*      Should I implement a SIGCHILD handler?                      */
+            /*TODO: Should I have a SIGTERM handler in the child process?       */
+            kill(pid, SIGTERM);
+            goto err;
+        }
+
+        while(1) {
+            if(poll(polls,2,100)) {
+                if(polls[0].revents&&POLLIN) {
+                    bzero(buf,2000);
+                    n=read(out[0],buf,1990);
+                    sprintf(string,"<out><![CDATA[");
+                    send(socket,string,strlen(string),0);
+                    send(socket,buf,n,0);
+                    sprintf(string,"]]></out>");
+                    send(socket,string,strlen(string),0);
+                }
+                if(polls[1].revents&&POLLIN) {
+                    bzero(buf,2000);
+                    n=read(err[0],buf,1990);
+                    sprintf(string,"<error><![CDATA[");
+                    send(socket,string,strlen(string),0);
+                    send(socket,buf,n,0);
+                    sprintf(string,"]]></error>");
+                    send(socket,string,strlen(string),0);
+                }
+
+                if(!polls[0].revents&&POLLIN && !polls[1].revents&&POLLIN) {
+                    syslog (LOG_ERR, "parent error polling pipes: %m");
+                    /*TODO: May I kill a child process if it is already dead? I mean,   */
+                    /*      what could happen if the child process is dead?             */
+                    /*      Should I implement a SIGCHILD handler?                      */
+                    /*TODO: Should I have a SIGTERM handler in the child process?       */
+                    kill(pid, SIGTERM);
+                    /*I want to send an error status to the remote calling process                              */
+                    /*TODO: Before giving this value I should make sure the child process is dead               */
+                    /*      otherwise I could finish having in *returnstatus the value from the child process   */
+                    (*returnstatus) = -1;
+                    break;
+                }
+            }   
+            else {
+                /*When timeout*/
+                if(waitpid(pid, &childreturnstatus, WNOHANG)) {
+                    /*Child is dead, we can finish the connection*/
+                    /*First of all, we check the exit status of our child process*/
+                    /*In case of error send an error status to the remote calling process*/
+                    if (WIFEXITED(childreturnstatus) != 1)
+                        (*returnstatus) = -1;
+                    break;
+                }
+                /*The child process is not dead, keep polling more data from stdout or stderr streams*/
+            }
+        }
+    }
+    /*Reaching this code when child finished or if error while polling pipes*/
+    sprintf(string,"<ret><![CDATA[%d]]></ret>", (*returnstatus));
+    send(socket,string,strlen(string),0);
+    sprintf(string,"</streams>");
+    send(socket,string,strlen(string),0);
+
+    /*Stuff just done by the Parent process. The child process ends with exit*/
+
+end:
+    closeSafely (out[0]);
+    closeSafely (out[1]);
+    closeSafely (err[0]);
+    closeSafely (err[1]);
+    return returnValue;
+err:
+    returnValue = -1;
+       goto end;
+}
+
+
+void sigint_handler(int sig)
+{
+    if (daemonPID != getpid()) {
+        //Do nothing
+        return;
+    }
+
+    if (signal (SIGINT, SIG_IGN) == SIG_ERR)
+        syslog (LOG_ERR, "signal SIGINT desactivation failed: %m");
+
+    closeSafely (sockfd);
+    /*TODO: kill child processes and release allocate memory*/
+    exit (0);
+}
diff --git a/Daemon/javafork.h b/Daemon/javafork.h
new file mode 100644 (file)
index 0000000..8abacc1
--- /dev/null
@@ -0,0 +1,77 @@
+/*System V IPC keys*/
+#define SHAREMEMKEY 1
+#define SHAREMEMSEM 2
+
+/*Non-argument default values*/
+#define PORT 5193
+#define IPADDRESS "127.0.0.1"
+#define QUEUE 6
+
+
+
+/****************************************************************************************
+* This method is used by pthread_create                                                 *
+*                                                                                                                                                                          *
+* INPUT PARAMETER: socket file descriptor                                                                                              *
+* RETURNS: void                                                                                                                                                        *
+****************************************************************************************/
+void *serverThread (void *arg);
+
+
+
+/****************************************************************************************
+* This method is used by pthread_create                                                 *
+*                                                                                       *
+* INPUT PARAMETER: socket file descriptor                                               *
+* INPUT PARAMETER: 
+* INPUT PARAMETER:
+* RETURNS: void                                                                          *
+****************************************************************************************/
+int daemonize(const char *pname, int facility, int option);
+
+
+
+/****************************************************************************************
+* This method is used by pthread_create                                                 *
+*                                                                                       *
+* INPUT PARAMETER: socket file descriptor                                               *
+* RETURNS: int                                                                         *
+****************************************************************************************/
+int main_daemon (char *address, int port, int queue);
+
+
+
+/****************************************************************************************
+* This method is used by pthread_create                                                 *
+*                                                                                       *
+* INPUT PARAMETER: socket file descriptor                                               *
+* RETURNS: void                                                                          *
+****************************************************************************************/
+int fork_system(int socket, char *command, sem_t *semaphore, int *returnst);
+
+
+/****************************************************************************************
+* This method is used by pthread_create                                                 *
+*                                                                                       *
+* INPUT PARAMETER: socket file descriptor                                               *
+* RETURNS: void                                                                          *
+****************************************************************************************/
+int pre_fork_system(int socket, char *command);
+
+
+
+
+
+/****************************************************************************************
+* This method is used by pthread_create                                                 *
+*                                                                                       *
+* INPUT PARAMETER: socket file descriptor                                               *
+* RETURNS: void                                                                          *
+****************************************************************************************/
+void sigint_handler();
+
+
+
+int required_sock_options (int socket);
+int receive_from_socket (int socket, char *data, int len, long timeout, long utimeout);
+int readable_timeout (int fd, long timeout, long utimeout);
diff --git a/JavaExample/javafork-example/pom.xml b/JavaExample/javafork-example/pom.xml
new file mode 100644 (file)
index 0000000..f24711c
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+       xmlns="http://maven.apache.org/POM/4.0.0" 
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+       
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <artifactId>javafork</artifactId>
+               <groupId>de.fork.java</groupId>
+               <version>2.0-SNAPSHOT</version>
+       </parent>
+
+       <artifactId>javafork-example</artifactId>
+       <name>javafork-example</name>
+       <url>http://gumartinm.name</url>
+
+       <dependencies>
+               
+       </dependencies>
+</project>
diff --git a/JavaExample/javafork-example/src/main/java/de/fork/java/LauncherProcesses.java b/JavaExample/javafork-example/src/main/java/de/fork/java/LauncherProcesses.java
new file mode 100644 (file)
index 0000000..dfb7d14
--- /dev/null
@@ -0,0 +1,285 @@
+package de.fork.java;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.net.UnknownHostException;
+import javax.xml.parsers.ParserConfigurationException;
+import org.apache.log4j.Logger;
+import org.xml.sax.SAXException;
+
+/**
+ * 
+ */
+public class LauncherProcesses {
+       // Exit process status
+       private static final int STATUS_ERR = -1;
+       private static final int DEFAULT_PORT = 5193;
+       private static final String DEFAULT_HOST = "127.0.0.1";
+       
+       /**
+        * Run a process.
+        * 
+        * @param command system command to be executed.
+        * 
+        * @return return code.
+        */
+       public static int exec(final String command) throws IOException, InterruptedException {
+
+               return exec(command, null, null);
+       }
+
+       /**
+        * Run a process.
+        * 
+        * @param command system command to execute.
+        * @param standarOutPut if not null, the standard output is redirected to this parameter.
+        *            
+        * @return return code.            
+        */
+       public static int exec(final String command, final PrintStream standarOutPut) throws IOException, InterruptedException {
+
+               return exec(command, standarOutPut, null);
+       }
+
+       
+       /**
+        * Run a process.
+        * 
+        * @param command system command to be executed.
+        * @param standarOutPut if not null, the standard output is redirected to this parameter.
+        * @param errorOutPut if not null, the error output is redirected to this parameter.
+        * 
+        * @return return code from the executed system command.     
+        */
+       public static int exec(final String command, final PrintStream standarOutPut, final PrintStream errorOutPut) throws IOException, InterruptedException {
+
+               return exec(command, standarOutPut, errorOutPut, DEFAULT_HOST, DEFAULT_PORT);
+       }
+
+       /**
+        * Run a process.
+        * 
+        * @param command system command to be executed.
+        * @param aLogger send the information to log.
+        */
+       public static int exec(final String command, final Logger aLogger) throws IOException, InterruptedException {
+
+               //calling private method to handle logger input/ouput in a common method
+               return execHandlingLogger(command, aLogger, DEFAULT_HOST, DEFAULT_PORT);
+       }
+       
+       
+       /**
+        * Run process.
+        * 
+        * @param commandAndArguments String array containing system command and its 
+        * arguments to be executed.<br>
+        * <b>For example:</b> 
+        * <pre>
+        * commandAndArguments[0]="ls";
+        * commandAndArguments[1]="-lr";
+        * </pre>
+        * @param aLogger
+        * 
+        * @return return code from the executed system command.
+        * 
+        * @throws IOException
+        * @throws InterruptedException
+        */
+       public static int exec(final String[] commandAndArguments, final Logger aLogger) throws IOException, InterruptedException {
+               String wholeCommand="";
+               
+               for(String argument : commandAndArguments) {
+                       wholeCommand = wholeCommand + " " + argument;
+               }
+               
+               //calling private method to handle logger input/ouput in a common method
+               return execHandlingLogger(wholeCommand, aLogger, DEFAULT_HOST, DEFAULT_PORT);
+       }
+       
+       
+       /**
+        * Run process using a remote process runner.
+        * 
+        * @param command system command to be executed.
+        * @param standarOutPut the stdout stream from that command as a <code>PrintStream</code>
+        * @param errorOutPut the stderr stream from that command as a <code>PrintStream</code>
+        * @param host the specified host.
+        * @param port the where the remote process runner accepts connections.
+        * 
+        * <p> The host name can either be a machine name, such as
+     * "<code>java.sun.com</code>", or a textual representation of its
+     * IP address. If a literal IP address is supplied, only the
+     * validity of the address format is checked.
+     * </p>
+     * <p> For <code>host</code> specified in literal IPv6 address,
+     * either the form defined in RFC 2732 or the literal IPv6 address
+     * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
+     * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
+     * scoped addresses.
+        * </p>
+        * 
+        * @return the executed command's return code.
+        * 
+        * @throws UnknownHostException
+        * @throws IOException
+        */
+       public static int exec(final String command, final PrintStream standarOutPut, 
+                       final PrintStream errorOutPut, final String host, final int port) 
+                                                                                       throws IOException, InterruptedException {
+               int exitStatus = LauncherProcesses.STATUS_ERR;
+               XmlForkParser forkParser = null;
+               TCPForkDaemon process = null;
+               
+               try {
+                       forkParser = new XmlForkParser();
+                       process = new TCPForkDaemon(forkParser, host, port);
+                       exitStatus = process.exec(command);
+               } catch (ParserConfigurationException e) {
+                       // This is not a crazy thing, we are trying to insert this new method without
+                       // breaking the old methods which did not throw SAXException or ParserConfigurationException
+                       // Do not blame me.
+                       throw new IOException(e);
+               } catch (SAXException e) {
+                       // This is not a crazy thing, we are trying to insert this new method without
+                       // breaking the old methods which did not throw SAXException or ParserConfigurationException
+                       // Do not blame me.
+                       throw new IOException(e);
+               }       
+               
+
+               
+               if ((standarOutPut != null) && (process.getStdout() != null)){
+                       standarOutPut.println(process.getStdout());
+               }
+
+               if ((errorOutPut != null) && (process.getStderr() != null)){
+                       errorOutPut.println(process.getStderr());
+               }
+
+               return exitStatus;
+       }
+       
+       
+       /**
+        * Run process.
+        * 
+        * @param command system command to be executed.
+        * @param aLogger
+        * @param host the specified host.
+        * @param port the TCP port where the daemon accepts connections.
+        * 
+        * @return the executed command's return code.
+        * 
+        * @throws IOException
+        * @throws InterruptedException
+        */
+       private static int execHandlingLogger(final String command, final Logger aLogger, 
+                               final String host, int port) throws IOException, InterruptedException {
+               int exitStatus = LauncherProcesses.STATUS_ERR;
+               XmlForkParser forkParser = null;
+               TCPForkDaemon process = null;
+               
+               try {
+                       forkParser = new XmlForkParser();
+                       process = new TCPForkDaemon(forkParser, host, port);
+                       exitStatus = process.exec(command);
+               } catch (ParserConfigurationException e) {
+                       // This is not a crazy thing, we are trying to insert this new method without
+                       // breaking the old methods which did not throw SAXException or ParserConfigurationException
+                       // Do not blame me.
+                       throw new IOException(e);
+               } catch (SAXException e) {
+                       // This is not a crazy thing, we are trying to insert this new method without
+                       // breaking the old methods which did not throw SAXException or ParserConfigurationException
+                       // Do not blame me.
+                       throw new IOException(e);
+               }
+               
+
+               
+               if (process.getStdout() != null) {
+                       aLogger.info(process.getStdout());
+               }
+               if (process.getStderr() != null) {
+                       aLogger.error(process.getStderr());
+               }
+
+               return exitStatus;
+       }
+       
+       
+       /**
+        * Run process
+        * 
+        * @param command command and its arguments to be executed.<br>
+        * <b>For example:</b> 
+        * <pre>
+        * commandAndArguments[0]="ls";
+        * commandAndArguments[1]="-lr";
+        * </pre>
+        * @param aLogger send information to log
+        * 
+        * @return the executed command's return code.
+        * 
+        * @throws IOException
+        * @throws InterruptedException
+        */
+       public static InputStream execStream (final String [] command, final Logger aLogger) 
+                                                                                                       throws IOException, InterruptedException {
+               int exitStatus = LauncherProcesses.STATUS_ERR;
+               InputStream stdInput = null;
+               XmlForkParser forkParser = null;
+               TCPForkDaemon process = null;
+               String wholeCommand="";
+               
+               for(String argument : command) {
+                       wholeCommand = wholeCommand + " " + argument;
+               }               
+               
+               try {
+                       forkParser = new XmlForkParser();
+                       process = new TCPForkDaemon(forkParser, DEFAULT_HOST, DEFAULT_PORT);
+                       exitStatus = process.exec(wholeCommand);
+               } catch (ParserConfigurationException e) {
+                       throw new IOException(e);
+               } catch (SAXException e) {
+                       throw new IOException(e);
+               }
+
+               
+               if(exitStatus == 0) {
+                       stdInput = new ByteArrayInputStream(process.getStdout().getBytes("UTF-8"));
+               }
+               else {
+                       aLogger.error(process.getStderr());
+               }
+               
+
+               return stdInput;
+       }
+       
+       /**
+        * <p>The <em>command</em> is lunched from <em>location</em>
+        * <li>#>cd <em>location</em></li>
+        * <li>#location> <em>command</em></li></p>
+        * 
+        * @param command the command to be executed by the daemon.
+        * @param location
+        * 
+        * @return the executed command's return code. <br>
+        * Usually <code>0</code> if execution is OK, otherwise <code>!=0</code> 
+        * 
+        * @throws IOException
+        * @throws InterruptedException
+        */
+       public static int execInLocation (final String command, final String location) throws IOException, InterruptedException {
+               int exitStatus = LauncherProcesses.STATUS_ERR;
+               final String wholeCommand = "cd " + location + " && " + command;
+               
+               exitStatus =  exec(wholeCommand, null, null, DEFAULT_HOST, DEFAULT_PORT);
+               return exitStatus;
+       }
+}
diff --git a/JavaExample/javafork-example/src/main/java/de/fork/java/RemoteForkMain.java b/JavaExample/javafork-example/src/main/java/de/fork/java/RemoteForkMain.java
new file mode 100644 (file)
index 0000000..5b9c57d
--- /dev/null
@@ -0,0 +1,36 @@
+package de.fork.java;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.PrintStream;
+import javax.xml.parsers.ParserConfigurationException;
+import org.xml.sax.SAXException;
+
+
+public class RemoteForkMain  {
+
+       /**
+        * @param args
+        * @throws InterruptedException 
+        * @throws IOException 
+        * @throws SAXException 
+        * @throws ParserConfigurationException 
+        * @throws FileNotFoundException 
+        */
+       public static void main(String[] args) throws IOException, InterruptedException {
+               
+               final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               final PrintStream stdout = new PrintStream(baos);
+               final String command = "ls -lah ~/Desktop; ls -lah * bbbb aaa";
+               final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
+               final PrintStream stderr = new PrintStream(baos2);
+               int result;
+               
+               result = LauncherProcesses.exec(command,stdout, stderr, "127.0.0.1", 5193);
+               System.out.println(result);
+               System.out.println("Stdout: " +  baos.toString());
+               System.out.println("Stderr: " +  baos2.toString());
+       }
+
+}
\ No newline at end of file
diff --git a/JavaExample/javafork-example/src/main/java/de/fork/java/TCPForkDaemon.java b/JavaExample/javafork-example/src/main/java/de/fork/java/TCPForkDaemon.java
new file mode 100644 (file)
index 0000000..c9da0cb
--- /dev/null
@@ -0,0 +1,184 @@
+package de.fork.java;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>
+ * With this class we can run processes using the intended daemon which is 
+ * waiting for TCP connections in a specified port.
+ * </p>
+ * <p>
+ * Receiving the results from the daemon where we can find three kinds of 
+ * different fields: stderror, stdout and the return value of the command which was
+ * run by the remote daemon. Each field is related to the stderr, stdout and 
+ * return code respectively.
+ * </p>
+ * <p>
+ * This class has to retrieve the results from the remote daemon and it offers two 
+ * methods wich can be used to retrieve the stderr and stdout in a right way 
+ * without having to know about the coding used by the daemon to send us the results. 
+ * The user does not have to know about how the daemon sends the data, he or she 
+ * will work directly with the strings related to each stream using these methods:
+ * {@link TCPForkDaemon#getStdout()} and {@link TCPForkDaemon#getStderr()}.
+ * The return code from the command executed by the daemon can be retrieved as the 
+ * return parameter from the method {@link TCPForkDaemon#exec(String, String, int)}
+ * </p>
+ * <p>
+ * Instances of this class are mutable. To use them concurrently, clients must surround each
+ * method invocation (or invocation sequence) with external synchronization of the clients choosing.
+ * </p>
+ */
+public class TCPForkDaemon {
+       private final XmlForkParser parser;
+       private final String host;
+       private final int port;
+       
+       
+       /**
+        * Default constructor for this {@link TCPForkDaemon} implementation.
+        * 
+     * <p> The host name can either be a machine name, such as
+     * "<code>java.sun.com</code>", or a textual representation of its
+     * IP address. If a literal IP address is supplied, only the
+     * validity of the address format is checked.
+     * </p>
+     * <p> For <code>host</code> specified in literal IPv6 address,
+     * either the form defined in RFC 2732 or the literal IPv6 address
+     * format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
+     * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
+     * scoped addresses.
+        * </p>
+        * @param parser instance implemeting {@link XmlForkParser} which knows about what 
+        * codification uses the daemon to send us the results of the command sent to
+        * by the remote daemon by the {@link TCPForkDaemon.#exec(String)} method.
+        * @param host the specified host.
+        * @param port the TCP port where the daemon accepts connections.
+        * 
+        */
+       public TCPForkDaemon (final XmlForkParser parser, final String host, final int port) {
+               this.parser = parser;
+               this.host = host;
+               this.port = port;
+       }
+       
+       
+       /**
+        * <p>
+        * This method sends commands to a remote daemon using a TCP socket.
+        * We create a new TCP socket every time we send commands.
+        * </p>
+        * <p>
+        * It uses a TCP connection in order to send commands and receive
+        * the results related to that command from the remote daemon. The command's 
+        * result code which was run by the remote daemon can be retrieved from the
+        * return parameter of this method.
+        * </p>
+        * @param  command the command to be executed by the daemon.
+        * @return the executed command's return code.
+        * @throws IOException 
+        * @throws UnknownHostException
+        * @throws SAXException 
+        * @throws SecurityException if a security manager exists
+        */
+       public int exec(final String command) throws UnknownHostException, IOException, SAXException {
+               PrintWriter out = null;
+               Socket socket = null;
+               
+       /******************************************************************************************/
+       /*          Just over 1 TCP connection                                                    */
+       /*          COMMAND_LENGTH: Java integer 4 bytes, BIG-ENDIAN (the same as network order)  */
+       /*          COMMAND: remote locale character set encoding                                 */
+       /*          RESULTS: remote locale character set encoding                                 */
+       /*                                                                                        */
+       /*              JAVA CLIENT: ------------ COMMAND_LENGTH -------> :SERVER                 */
+       /*              JAVA CLIENT: -------------- COMMAND ------------> :SERVER                 */
+       /*              JAVA CLIENT: <-------------- RESULTS ------------ :SERVER                 */
+       /*              JAVA CLIENT: <---------- CLOSE CONNECTION ------- :SERVER                 */
+       /*                                                                                        */
+       /******************************************************************************************/
+
+
+               
+               socket = new Socket(InetAddress.getByName(host), port);
+               try {
+                       //By default in UNIX systems the keepalive message is sent after 20hours 
+                       //with Java we can not use the TCP_KEEPCNT, TCP_KEEPIDLE and TCP_KEEPINTVL options by session.
+                       //It is up to the server administrator and client user to configure them.
+                       //I guess it is because Solaris does not implement those options...
+                       //see: Net.c openjdk 6 and net_util_md.c openjdk 7
+                       //So in Java applications the only way to find out if the connection is broken (one router down)
+                       //is sending ping messages or something similar from the application layer. Java is a toy language...
+                       //Anyway I think the keepalive messages just work during the handshake phase, just after sending some
+                       //data over the link the keepalive does not work.
+                       // See: http://stackoverflow.com/questions/4345415/socket-detect-connection-is-lost
+                       socket.setKeepAlive(true);
+                       
+                       //It must be used the remote locale character set encoding
+                       byte [] commandEncoded = command.getBytes("UTF-8"); 
+                       
+                       DataOutputStream sendData = new DataOutputStream(socket.getOutputStream());
+                                               
+                       // 1. COMMAND_LENGTH
+                       sendData.writeInt(commandEncoded.length);
+                               
+                       // 2. COMMAND
+                       sendData.write(commandEncoded);
+                       
+                       // 3. RESULTS
+                       // TODO: When the network infrastructure (between client and server) fails in this point 
+                       // (broken router for example) Could we stay here until TCP keepalive is sent?
+                       // (20 hours by default in Linux)
+                       // Impossible to use a timeout, because we do not know how much time is going to long the command :/
+                       // the only way to fix this issue in Java is sending ping messages (Could we fix it using custom settings in the OS
+                       // of the client and server machines? for example in Linux see /proc/sys/net/ipv4/)
+                       InputSource inputSource = new InputSource(socket.getInputStream());
+                       //Must be used the remote locale character set encoding?
+                       inputSource.setEncoding("UTF-8");
+                       parser.setStream(socket.getInputStream());
+                       
+                       // 4. SERVER CLOSED CONNECTION
+               }
+               finally {
+                       if (out != null) {
+                               out.close();
+                       }
+                       socket.close();
+               }
+               
+               //If everything went alright we should be able to retrieve the return 
+               //status of the remotely executed command.
+               return parser.getReturnValue();
+       }
+
+       
+       /**
+        * Retrieve the standard output. <br>
+        * When there is nothing from the standard output this method returns null.
+        * 
+        * @see {@link TCPForkDaemon#getStderr()}
+        * @return the stdout stream
+        */
+       public String getStdout() {
+               return parser.getStdout();
+       }
+       
+       
+       /**
+        * Retrieve the stderr stream as a {@link String} from the command which 
+        * was run by the remote daemon 
+        * 
+        * @see {@link TCPForkDaemon#getStdout()}
+        * @return the stderr stream
+        */
+       public String getStderr() {
+               return parser.getStderr();
+       }
+}
diff --git a/JavaExample/javafork-example/src/main/java/de/fork/java/XmlForkParser.java b/JavaExample/javafork-example/src/main/java/de/fork/java/XmlForkParser.java
new file mode 100644 (file)
index 0000000..255cb0f
--- /dev/null
@@ -0,0 +1,195 @@
+package de.fork.java;
+
+import java.io.IOException;
+import java.io.InputStream;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import org.apache.log4j.Logger;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.ext.DefaultHandler2;
+
+/**
+ * <p>
+ * Class intended to parse the XML stream received from the daemon which is
+ * waiting to run commands. These commands are sent by the method 
+ * {@link de.fork.java.TCPForkDaemon#exec(String, String, int)} 
+ * </p>
+ * <p>
+ * After processing one command the daemon sends a XML stream with the stderr,
+ * stdout and return status of that command. With this class we extract those values
+ * and we can retrieve them using the methods {@link #getStderr() }, {@link #getStdout()}
+ * and {@link #getReturnValue()}
+ * </p>
+ * <p>
+ * <pre>
+ * <b>Example, stream received from daemon:</b>
+ * {@code
+ * <?xml version="1.0"?><salida><error><![CDATA[ls: no se puede acceder a bbb: No existe el fichero o el directorio
+ * ls: no se puede acceder a aaa: No existe el fichero o el directorio
+ * ls: no se puede acceder a dddd: No existe el fichero o el directorio
+ * ]]></error><ret><![CDATA[2]]></ret></salida>
+ * }
+ * </pre>
+ * </p>
+ * <p>
+ * Instances of this class are mutable. To use them concurrently, clients must surround each
+ * method invocation (or invocation sequence) with external synchronization of the clients choosing.
+ * </p>
+ */
+public class XmlForkParser extends DefaultHandler2 {
+       private static final Logger logger = Logger.getLogger(XmlForkParser.class);
+    private StringBuffer accumulator = new StringBuffer();
+       private String stderr = new String();
+       private String stdout = new String();
+       private String returnCode = new String();
+       final SAXParserFactory spf = SAXParserFactory.newInstance();
+       private final SAXParser saxParser;
+       
+       public XmlForkParser() throws ParserConfigurationException, SAXException {
+               saxParser = spf.newSAXParser();
+       }
+       
+       public void setStream(InputStream stream) throws SAXException, IOException {
+               saxParser.parse(stream, this);
+       }
+
+       /**
+        * <p>
+        * The daemon sends a XML stream, we parse that stream and the results are
+        * stored in the instace fields {@link #stderr}, {@link #stdout} and {@link returnCode}
+        * </p>
+        * <p>
+        * Later we can retrieve the results with {@link #getStderr()}, {@link #getStdout()} and
+        * {@link #getReturnValue()}
+        * </p>
+        */
+       @Override
+       public void endElement (final String uri, final String localName, final String qName) {
+               if (qName.equals("error")) {
+                       // After </error>, we've got the stderror
+                       stderr = stderr + accumulator.toString();
+               } else if (qName.equals("out")) {
+                       // After </out>, we've got the stdout
+                       stdout = stdout + accumulator.toString();
+               } else if (qName.equals("ret")) {
+                       returnCode = returnCode + accumulator.toString();
+               }
+       }
+       
+       /**
+        * <p>
+        * This method removes the <code>\n<code> characters at the end of the stdout 
+        * or stderr stream.
+        * </p>
+        * 
+     * @throws SAXException If any SAX errors occur during processing.
+        */
+       @Override
+       public void endDocument () throws SAXException
+       {
+               if (stderr.length() != 0) {
+                       String lastStderr = stderr.replaceFirst("\\\n$", "");
+                       stderr = lastStderr;
+               }
+               else {
+                       //if there is nothing from the stderr stream
+                       stderr = null;
+               }
+               if (stdout.length() != 0) {
+                       String lastStdout = stdout.replaceFirst("\\\n$", "");
+                       stdout = lastStdout;
+               }
+               else {
+                       //if there is nothing from the stdout stream
+                       stdout = null;
+               }
+       }
+       
+       /**
+        * Retrieve the standard error.
+        * When there is nothing from the standard error this method returns null.
+        * 
+        * <pre>
+        * <b>Example, stream received from daemon:</b>
+        * {@code
+        * <?xml version="1.0"?><salida><error><![CDATA[ls: no se puede acceder a bbb: No existe el fichero o el directorio
+        * ls: no se puede acceder a aaa: No existe el fichero o el directorio
+        * ls: no se puede acceder a dddd: No existe el fichero o el directorio
+        * ]]></error><ret><![CDATA[2]]></ret></salida>
+        * }
+        * </pre>
+        * </p>
+        * <p>
+        * <pre>
+        * <b>From that example with this method we are going to obtain this return parameter:</b>
+        * {@code
+        * ls: no se puede acceder a bbb: No existe el fichero o el directorio
+        * ls: no se puede acceder a aaa: No existe el fichero o el directorio
+        * ls: no se puede acceder a dddd: No existe el fichero o el directorio
+        * }
+        * </pre>
+        * 
+        * @return stderr
+        */
+       public String getStderr() {
+               return stderr;
+               
+       }
+       
+       
+       /**
+        * Retrieve the standard output.
+        * When there is nothing from the standard output this method returns null.
+        * 
+        * @see {@link XmlForkParser#getStderr()}
+        * @return stdout
+        */
+       public String getStdout() {
+               return stdout;
+       }
+       
+       
+       /**
+        * Retrieve the return code from the executed command.
+        * 
+        * @return return status, usually <code>0<code> means the command went OK.
+        */
+       public int getReturnValue() {
+               return new Integer(returnCode).intValue();
+       }
+       
+       
+       @Override
+       public void startElement (final String uri, final String localName, 
+                                                                       final String qName, final Attributes attributes) {
+               accumulator.setLength(0);
+       }
+       
+       
+       @Override
+       public void characters(final char[] buffer, final int start, final int length) {
+           accumulator.append(buffer, start, length);
+       }
+       
+       
+       @Override
+       public void warning(final SAXParseException exception) {
+               logger.error("WARNING line:" + exception.getLineNumber(), exception);
+       }
+       
+       
+       @Override
+       public void error(final SAXParseException exception) {
+               logger.error("ERROR line:" + exception.getLineNumber(), exception);
+       }
+       
+       
+       @Override
+       public void fatalError(final SAXParseException exception) throws SAXException {
+               logger.error("FATAL ERROR line:" + exception.getLineNumber(), exception);
+               throw (exception);
+       }
+}
diff --git a/JavaExample/pom.xml b/JavaExample/pom.xml
new file mode 100644 (file)
index 0000000..aaf5867
--- /dev/null
@@ -0,0 +1,663 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" 
+        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>de.fork.java</groupId>
+       <artifactId>javafork</artifactId>
+       <version>2.0-SNAPSHOT</version>
+       <name>javafork</name>
+       <url>http://www.gumartinm.name</url>
+       <description>Java fork with Linux daemon</description>
+       <organization>
+               <name>MyOrganization</name>
+               <url>http://www.gumartinm.name</url>
+       </organization>
+       <packaging>pom</packaging>
+       <issueManagement>
+               <system>trac</system>
+               <url>http://gumartinm.name/trac</url>
+       </issueManagement>
+       <ciManagement>
+               <system>jenkins</system>
+               <url>http://gumartinm.name//jenkins/</url>
+       </ciManagement>
+       <scm>
+               <developerConnection>scm:svn:http://gumartinm.name</developerConnection>
+               <url>http://gumartinm.name</url>
+       </scm>
+       <dependencies>
+               <dependency>
+                       <groupId>com.sun.jdmk</groupId>
+                       <artifactId>jmxtools</artifactId>
+                       <version>1.2.1</version>
+               </dependency>
+               <dependency>
+                       <groupId>javax.activation</groupId>
+                       <artifactId>activation</artifactId>
+                       <version>1.1</version>
+               </dependency>
+               <dependency>
+                       <groupId>log4j</groupId>
+                       <artifactId>log4j</artifactId>
+                       <version>1.2.15</version>
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>com.sun.jdmk</groupId>
+                                       <artifactId>jmxtools</artifactId>
+                               </exclusion>
+                               <exclusion>
+                                       <groupId>com.sun.jmx</groupId>
+                                       <artifactId>jmxri</artifactId>
+                               </exclusion>
+                               <exclusion>
+                                       <groupId>javax.mail</groupId>
+                                       <artifactId>mail</artifactId>
+                               </exclusion>
+                               <exclusion>
+                                       <groupId>javax.jms</groupId>
+                                       <artifactId>jms</artifactId>
+                               </exclusion>
+                       </exclusions>
+               </dependency>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <version>4.4</version>
+                       <scope>test</scope>
+               </dependency>
+       </dependencies>
+       <dependencyManagement>
+               <dependencies>
+                       <dependency>
+                               <groupId>c3p0</groupId>
+                               <artifactId>c3p0</artifactId>
+                               <version>0.9.1.2</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>cglib</groupId>
+                               <artifactId>cglib-nodep</artifactId>
+                               <version>2.1_3</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>commons-collections</groupId>
+                               <artifactId>commons-collections</artifactId>
+                               <version>3.2.1</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>commons-configuration</groupId>
+                               <artifactId>commons-configuration</artifactId>
+                               <version>1.6</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>commons-dbcp</groupId>
+                               <artifactId>commons-dbcp</artifactId>
+                               <version>1.2.2</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>commons-io</groupId>
+                               <artifactId>commons-io</artifactId>
+                               <version>1.4</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>commons-lang</groupId>
+                               <artifactId>commons-lang</artifactId>
+                               <version>2.4</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>commons-logging</groupId>
+                               <artifactId>commons-logging</artifactId>
+                               <version>1.1.1</version>
+                       </dependency>
+                        <dependency>
+                               <groupId>commons-net</groupId>
+                                <artifactId>commons-net</artifactId>
+                               <version>2.0</version>
+                        </dependency>
+                       <dependency>
+                               <groupId>commons-pool</groupId>
+                               <artifactId>commons-pool</artifactId>
+                               <version>1.3</version>
+                       </dependency> 
+                       <dependency>
+                               <groupId>com.h2database</groupId>
+                               <artifactId>h2</artifactId>
+                               <version>1.2.130</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>dom4j</groupId>
+                               <artifactId>dom4j</artifactId>
+                               <version>1.6.1</version>
+                               <exclusions>
+                                       <exclusion>
+                                               <groupId>xml-apis</groupId>
+                                               <artifactId>xml-apis</artifactId>
+                                       </exclusion>
+                               </exclusions>
+                       </dependency>
+                       <dependency>
+                               <groupId>hsqldb</groupId>
+                               <artifactId>hsqldb</artifactId>
+                               <version>1.8.0.7</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>javatar</groupId>
+                               <artifactId>javatar</artifactId>
+                               <version>2.5</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>jpos</groupId>
+                               <artifactId>jpos</artifactId>
+                               <version>1.12.2</version>
+                               <scope>provided</scope>
+                       </dependency>
+                       <dependency>
+                               <groupId>jpos</groupId>
+                               <artifactId>jpos-controls</artifactId>
+                               <version>1.12.2</version>
+                               <scope>provided</scope>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.python</groupId>
+                               <artifactId>jython</artifactId>
+                               <version>2.5.2b2</version>
+                       </dependency>           
+                       <dependency>
+                               <groupId>urbanophile</groupId>
+                               <artifactId>java-getopt</artifactId>
+                               <version>1.0.13</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>mysql</groupId>
+                               <artifactId>mysql-connector-java</artifactId>
+                               <version>5.1.6</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.apache.ibatis</groupId>
+                               <artifactId>ibatis-sqlmap</artifactId>
+                               <version>2.3.4.726</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.apache.mina</groupId>
+                               <artifactId>mina-core</artifactId>
+                               <version>2.0.0-M6</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.aspectj</groupId>
+                               <artifactId>aspectjrt</artifactId>
+                               <version>1.6.5</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.aspectj</groupId>
+                               <artifactId>aspectjweaver</artifactId>
+                               <version>1.6.5</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.dbunit</groupId>
+                               <artifactId>dbunit</artifactId>
+                               <version>2.4.4</version>
+                               <scope>test</scope>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-continuation</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-http</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-io</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-security</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-server</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-servlet</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-webapp</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-util</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.eclipse.jetty</groupId>
+                               <artifactId>jetty-xml</artifactId>
+                               <version>7.0.0.v20091005</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.slf4j</groupId>
+                               <artifactId>slf4j-api</artifactId>
+                               <version>1.5.2</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.slf4j</groupId>
+                               <artifactId>slf4j-log4j12</artifactId>
+                               <version>1.5.2</version>
+                               <exclusions>
+                                       <exclusion>
+                                               <groupId>log4j</groupId>
+                                               <artifactId>log4j</artifactId>
+                                       </exclusion>
+                               </exclusions>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-aop</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-beans</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-core</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-context</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-jdbc</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-tx</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-web</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-webmvc</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                                       <artifactId>spring-context-support</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-test</artifactId>
+                               <version>${spring.version}</version>
+                               <scope>test</scope>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework.batch</groupId>
+                               <artifactId>spring-batch-test</artifactId>
+                               <version>2.0.4.RELEASE</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework.batch</groupId>
+                               <artifactId>spring-batch-core</artifactId>
+                               <version>2.0.4.RELEASE</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework.batch</groupId>
+                               <artifactId>spring-batch-infrastructure</artifactId>
+                               <version>2.0.4.RELEASE</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.springframework</groupId>
+                               <artifactId>spring-orm</artifactId>
+                               <version>${spring.version}</version>
+                       </dependency>
+                       <dependency>
+                           <groupId>net.sf.ehcache</groupId>
+                           <artifactId>ehcache</artifactId>
+                           <version>1.6.2</version>
+                       </dependency>
+                       <dependency>
+                           <groupId>org.springmodules</groupId>
+                           <artifactId>spring-modules-cache</artifactId>
+                           <version>0.9</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>p6spy</groupId>
+                               <artifactId>p6spy</artifactId>
+                               <version>1.3</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>javax.transaction</groupId>
+                               <artifactId>jta</artifactId>
+                               <version>1.0.1B</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>javax.servlet</groupId>
+                               <artifactId>servlet-api</artifactId>
+                               <version>2.5</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>com.caucho</groupId>
+                               <artifactId>hessian</artifactId>
+                               <version>3.1.6</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.codehaus.jettison</groupId>
+                               <artifactId>jettison</artifactId>
+                               <version>1.0</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>com.thoughtworks.xstream</groupId>
+                               <artifactId>xstream</artifactId>
+                               <version>1.3</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.ini4j</groupId>
+                               <artifactId>ini4j</artifactId>
+                               <version>0.5.1</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.easymock</groupId>
+                               <artifactId>easymock</artifactId>
+                               <version>2.4</version>
+                               <scope>test</scope>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.easymock</groupId>
+                               <artifactId>easymockclassextension</artifactId>
+                               <version>2.4</version>
+                               <scope>test</scope>
+                       </dependency>
+
+                       <dependency>
+                               <groupId>org.apache.commons</groupId>
+                               <artifactId>commons-compress</artifactId>
+                               <version>1.0</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.apache.commons</groupId>
+                               <artifactId>commons-math</artifactId>
+                               <version>2.0</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>dtgjpos_forms</groupId>
+                               <artifactId>dtgjpos_forms</artifactId>
+                               <version>1.4.12</version>
+                               <scope>provided</scope>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.codehaus.castor</groupId>
+                               <artifactId>castor-xml</artifactId>
+                               <version>1.3.1</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.apache.xmlbeans</groupId>
+                               <artifactId>xmlbeans</artifactId>
+                               <version>2.5.0</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.apache.velocity.tools</groupId>
+                               <artifactId>velocity-tools-generic</artifactId>
+                               <version>1.4</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.apache.velocity</groupId>
+                               <artifactId>velocity</artifactId>
+                               <version>1.6.2</version>
+                       </dependency>
+                       <dependency>
+                               <groupId>org.tmatesoft.svnkit</groupId>
+                               <artifactId>svnkit</artifactId>
+                               <version>1.3.1</version>
+                       </dependency>
+               </dependencies>
+       </dependencyManagement>
+       <modules>
+               <module>javafork-example</module>
+       </modules>
+       <build>
+               <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                               <version>2.0.2</version>
+                               <configuration>
+                                       <source>1.6</source>
+                                       <target>1.6</target>
+                                       <encoding>${project.build.sourceEncoding}</encoding>
+                               </configuration>
+                       </plugin>
+                       <!-- Usually you will not need this plugin
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-resources-plugin</artifactId>
+                               <version>2.2</version>
+                               <configuration>
+                                       <encoding>${project.build.sourceEncoding}</encoding>
+                               </configuration>
+                       </plugin>
+                       -->
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-javadoc-plugin</artifactId>
+                               <version>2.6</version>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-surefire-plugin</artifactId>
+                               <version>2.4.2</version>
+                               <configuration>
+                                       <testFailureIgnore>true</testFailureIgnore>
+                               </configuration>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.codehaus.mojo</groupId>
+                               <artifactId>jdepend-maven-plugin</artifactId>
+                               <version>2.0-beta-2</version>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-jar-plugin</artifactId>
+                               <version>2.3.1</version>
+                               <configuration>
+                                       <archive>
+                                               <manifestEntries>
+                                                       <Specification-Title>${project.description}</Specification-Title>
+                                                       <Specification-Version>${project.version}</Specification-Version>
+                                                       <Specification-Vendor>${project.organization.name}</Specification-Vendor>
+                                                       <Implementation-Title>${project.description}</Implementation-Title>
+                                                       <Implementation-Version>${project.version}</Implementation-Version>
+                                                       <Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
+                                                       <Implementation-Build>${BUILD_TAG}</Implementation-Build>
+                                                       <Implementation-Build-id>${BUILD_ID}</Implementation-Build-id>
+                                                       <Implementation-Build-number>${BUILD_NUMBER}</Implementation-Build-number>
+                                                       <scm-committed-revision>${prefix.committedRevision}</scm-committed-revision>
+                                                       <scm-repository>${prefix.repository}</scm-repository>
+                                                       <scm-path>${prefix.path}</scm-path>                                                           
+                                               </manifestEntries>
+                                       </archive>
+                               </configuration>
+                       </plugin>
+                       <plugin>
+                               <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
+                               <artifactId>maven-svn-revision-number-plugin</artifactId>
+                               <version>1.6</version>
+                               <executions>
+                                       <execution>
+                                               <goals>
+                                                       <goal>revision</goal>
+                                               </goals>
+                                       </execution>
+                               </executions>
+                               <configuration>
+                                       <entries>
+                                               <entry>
+                                                       <prefix>prefix</prefix>
+                                               </entry>
+                                       </entries>
+                               </configuration>
+                       </plugin>
+               </plugins>
+       </build>
+       <!--We need this for the javadoc and reporting maven plugin -->
+       <distributionManagement>
+               <repository>
+                       <id>releases</id>
+                       <name>releases</name>
+                       <url>http://noserver/artifactory/custom-annotations-libs-releases-local</url>
+               </repository>
+               <snapshotRepository>
+                       <id>snapshots-releases</id>
+                       <name>snapshots-releases</name>
+                       <url>http://noserver/artifactory/custom-annotations-libs-snapshots-local</url>
+               </snapshotRepository>
+               <site>
+                       <id>noserver</id>
+                       <url>file:///mnt/sdb1/data/downloads/jenkins/</url>
+               </site>
+
+       </distributionManagement>
+       <reporting>
+               <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-project-info-reports-plugin</artifactId>
+                               <version>2.1.2</version>
+                               <reportSets>
+                                       <reportSet>
+                                               <reports>
+                                                       <report>index</report>
+                                                       <report>dependencies</report>
+                                                       <report>cim</report>
+                                                       <report>issue-tracking</report>
+                                                       <report>scm</report>
+                                                       <report>summary</report>
+                                                       <report>project-team</report>
+                                               </reports>
+                                       </reportSet>
+                               </reportSets>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-javadoc-plugin</artifactId>
+                               <version>2.6.1</version>
+                               <reportSets>
+                                       <reportSet>
+                                               <id>html</id>
+                                               <configuration>
+                                                       <doctitle>MYPROJECT API for ${project.name} ${project.version}</doctitle>
+                                                       <windowtitle>MYPROJECT API for ${project.name} ${project.version}</windowtitle>
+                                               </configuration>
+                                               <reports>
+                                                       <report>javadoc</report>
+                                                       <report>aggregate</report>
+                                               </reports>
+                                       </reportSet>
+                               </reportSets>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.codehaus.mojo</groupId>
+                               <artifactId>taglist-maven-plugin</artifactId>
+                               <version>2.3</version>
+                               <configuration>
+                                       <tags>
+                                               <tag>TODO</tag>
+                                               <tag>@todo</tag>
+                                               <tag>FIXME</tag>
+                                               <tag>XXX</tag>
+                                       </tags>
+                               </configuration>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-surefire-report-plugin</artifactId>
+                               <version>2.4.3</version>
+                               <reportSets>
+                                       <reportSet>
+                                               <id>integration-tests</id>
+                                               <reports>
+                                                       <report>report-only</report>
+                                               </reports>
+                                               <configuration>
+                                                       <outputName>failsafe-report</outputName>
+                                                       <reportsDirectories>
+                                                               <reportsDirectory>target/failsafe-reports</reportsDirectory>
+                                                       </reportsDirectories>
+                                               </configuration>
+                                       </reportSet>
+                                       <reportSet>
+                                               <id>junit-tests</id>
+                                               <reports>
+                                                       <report>report-only</report>
+                                               </reports>
+                                               <configuration>
+                                                       <outputName>surefire-report</outputName>
+                                                       <reportsDirectories>
+                                                               <reportsDirectory>target/surefire-reports</reportsDirectory>
+                                                       </reportsDirectories>
+                                               </configuration>
+                                       </reportSet>
+                               </reportSets>
+                       </plugin>
+               </plugins>
+       </reporting>
+       <properties>
+               <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+               <spring.version>2.5.6</spring.version>
+       </properties>
+       <repositories>
+               <!--
+               <repository>
+                       <id>central-myproject-repo</id>
+                       <name>Internal repository</name>
+                       <url>http://noserver/artifactory/my-repo</url>
+                       <snapshots>
+                               <enabled>false</enabled>
+                       </snapshots>
+               </repository>
+               <repository>
+                       <id>snapshots-releases</id>
+                       <name>snapshots-releases</name>
+                       <url>http://noserver/artifactory/custom-annotations-libs-snapshots-local</url>
+                       <snapshots>
+                               <enabled>true</enabled>
+                       </snapshots>
+               </repository>
+               -->
+       </repositories>
+       <pluginRepositories>
+               <!--
+               <pluginRepository>
+                       <id>central-myproject-plugins-repo</id>
+                       <name>Plugins internal repository</name>
+                       <url>http://noserver/artifactory/plugins-repo</url>
+                       <snapshots>
+                               <enabled>true</enabled>
+                       </snapshots>
+                       <releases>
+                               <enabled>true</enabled>
+                       </releases>
+               </pluginRepository>
+               -->
+       </pluginRepositories>
+</project>
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..bf09334
--- /dev/null
+++ b/README
@@ -0,0 +1,9 @@
+Fork process from Java using a remote daemon.
+
+It is useful in environments where there are not enough memory
+and you could finish having OutOfMemory exceptions while 
+trying to run system processes.
+
+
+If you want to know more about memory overcommit in Linux see:
+http://developers.sun.com/solaris/articles/subprocess/subprocess.html