jdk/src/windows/bin/java-rmi.c
author jrose
Thu, 13 May 2010 20:01:40 -0700
changeset 5737 8a2f4c03ab6f
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <process.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This is a primitive bootstrapping utility for executing Java CGI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * programs, specifically Java RMI's CGI HTTP forwarding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * It executes the Java interpreter with options to define
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * properties corresponding to the environment variables set by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * CGI 1.0 specification and runs the target class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The following assumptions are made:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *     - the Java interpreter can be located by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       PATH variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *     - for RMI 1.1 prebeta release, the target class can be located
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *       using the system CLASSPATH variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/* name of Java interpreter executable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define JAVA_NAME "java"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/* name of Java class to execute with interpreter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define CLASS_NAME "sun.rmi.transport.proxy.CGIHandler"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/* names of environment variables set in CGI 1.0 interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static char *var_names[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    "AUTH_TYPE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    "CONTENT_LENGTH",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    "CONTENT_TYPE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    "GATEWAY_INTERFACE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    "HTTP_ACCEPT",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    "PATH_INFO",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    "PATH_TRANSLATED",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    "QUERY_STRING",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    "REMOTE_ADDR",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    "REMOTE_HOST",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    "REMOTE_IDENT",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    "REMOTE_USER",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    "REQUEST_METHOD",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    "SCRIPT_NAME",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    "SERVER_NAME",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    "SERVER_PORT",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    "SERVER_PROTOCOL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    "SERVER_SOFTWARE"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#define NUM_VARS (sizeof(var_names) / sizeof(var_names[0]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/* file static functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static void server_error(char *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Program entry point: set up arguments and invoke Java interpreter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
main(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int     argc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    char   *argv[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    int     i;                  /* loop index variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    char  **args;               /* array to store arguments to interpreter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    int     n = 0;              /* next index to fill in argument array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /* allocate space for argument list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    args = (char **)            /* allocate space for: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        malloc((1               /* executable name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                + NUM_VARS      /* property definition for each variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                + 1             /* class name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                + 1)            /* terminating NULL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                * sizeof(*args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    if (args == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        server_error("memory allocation failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* first argument: name of java interpreter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    args[n ++] = JAVA_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /* next arguments: define CGI variables as properties to Java VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    for (i = 0; i < NUM_VARS; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        char *name = var_names[i];      /* name of variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        char *value;                    /* value of variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        char *buffer;                   /* buffer to store argument string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        value = getenv(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (value == NULL)              /* if variable undefined, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            value = "";                 /* use empty string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        buffer = (char *)               /* allocate space for: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            malloc((2                   /* "-D" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    + strlen(name)      /* variable name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    + 2                 /* "=\"" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    + strlen(value)     /* variable value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    + 2)                /* "\"" and terminating '\0' */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    * sizeof(*buffer));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (buffer == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            server_error("memory allocation failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        /* construct property definition parameter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        sprintf(buffer, "-D%s=\"%s\"", name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        args[n ++] = buffer;            /* add to argument list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /* last argument: name of class to execute */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    args[n ++] = CLASS_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    args[n ++] = NULL;          /* terminate argument list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    _execvp(JAVA_NAME, args);   /* execute java interpreter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /* if exec call returns, there was an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    server_error("interpreter execution failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * Return primitive error message to server because of some failure in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * this program.  (This could be embellished to an HTML formatted error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * message.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
server_error(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    char  *message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * NOTE: CGI 1.0 spec uses "\n" (unlike "\r\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * for HTTP 1.0) for line termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    printf("Status: 500 Server Error: %s\n", message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    printf("Content-type: text/plain\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    printf("%s", message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}