Example #1
0
//!
//! Creates and initialize an NC stub entry
//!
//! @param[in] endpoint_uri the endpoint URI string
//! @param[in] logfile the log file name string
//! @param[in] homedir the home directory path string
//!
//! @return a pointer to the newly created NC stub structure
//!
ncStub *ncStubCreate(char *endpoint_uri, char *logfile, char *homedir)
{
    char *uri = NULL;
    char *p = NULL;
    char *node_name = NULL;
    axutil_env_t *env = NULL;
    axis2_char_t *client_home = NULL;
    axis2_stub_t *stub = NULL;
    ncStub *st = NULL;

    if (logfile) {
        env = axutil_env_create_all(logfile, AXIS2_LOG_LEVEL_TRACE);
    } else {
        env = axutil_env_create_all(NULL, 0);
    }

    if (homedir) {
        client_home = (axis2_char_t *) homedir;
    } else {
        client_home = AXIS2_GETENV("AXIS2C_HOME");
    }

    if (client_home == NULL) {
        LOGERROR("fakeNC: ERROR: cannot get AXIS2C_HOME");
        return NULL;
    }

    if (endpoint_uri == NULL) {
        LOGERROR("fakeNC: ERROR: empty endpoint_url");
        return NULL;
    }

    uri = endpoint_uri;

    // extract node name from the endpoint
    p = strstr(uri, "://");            // find "http[s]://..."
    if (p == NULL) {
        LOGERROR("fakeNC: ncStubCreate received invalid URI %s\n", uri);
        return NULL;
    }

    node_name = strdup(p + 3);         // copy without the protocol prefix
    if (node_name == NULL) {
        LOGERROR("fakeNC: ncStubCreate is out of memory\n");
        return NULL;
    }

    if ((p = strchr(node_name, ':')) != NULL)
        *p = '\0';                     // cut off the port

    if ((p = strchr(node_name, '/')) != NULL)
        *p = '\0';                     // if there is no port

    LOGDEBUG("fakeNC: DEBUG: requested URI %s\n", uri);

    // see if we should redirect to a local broker
    if (strstr(uri, "EucalyptusBroker")) {
        uri = "http://localhost:8773/services/EucalyptusBroker";
        LOGDEBUG("fakeNC: DEBUG: redirecting request to %s\n", uri);
    }
    //! @todo what if endpoint_uri, home, or env are NULL?
    stub = axis2_stub_create_EucalyptusNC(env, client_home, (axis2_char_t *) uri);

    if (stub && (st = EUCA_ZALLOC(1, sizeof(ncStub)))) {
        st->env = env;
        st->client_home = strdup((char *)client_home);
        st->endpoint_uri = (axis2_char_t *) strdup(endpoint_uri);
        st->node_name = (axis2_char_t *) strdup(node_name);
        st->stub = stub;
        if (st->client_home == NULL || st->endpoint_uri == NULL) {
            LOGWARN("fakeNC: WARNING: out of memory");
        }
    } else {
        LOGWARN("fakeNC: WARNING: out of memory");
    }

    EUCA_FREE(node_name);
    return (st);
}
ncStub * ncStubCreate (char *endpoint_uri, char *logfile, char *homedir) 
{
    axutil_env_t * env = NULL;
    axis2_char_t * client_home;
    axis2_stub_t * stub;
    ncStub * st = NULL;

    if ( logfile ) {
      env =  axutil_env_create_all (logfile, AXIS2_LOG_LEVEL_TRACE);
    } else {
      env =  axutil_env_create_all (NULL, 0);
    }
    if ( homedir ) {
        client_home = (axis2_char_t *)homedir;
    } else {
        client_home = AXIS2_GETENV("AXIS2C_HOME");
    }

    if (client_home == NULL) {
        logprintfl (EUCAERROR, "ERROR: cannot get AXIS2C_HOME");
	return NULL;
    }
    if (endpoint_uri == NULL) {
        logprintfl (EUCAERROR, "ERROR: empty endpoint_url");
	return NULL;
    }

    char * uri = endpoint_uri;

    // extract node name from the endpoint
    char * p = strstr (uri, "://"); // find "http[s]://..."
    if (p==NULL) {
      logprintfl (EUCAERROR, "ncStubCreate received invalid URI %s\n", uri);
      return NULL;
    }
    char * node_name = strdup (p+3); // copy without the protocol prefix
    if (node_name==NULL) {
      logprintfl (EUCAERROR, "ncStubCreate is out of memory\n");
      return NULL;
    }
    if ((p = strchr (node_name, ':')) != NULL) *p = '\0'; // cut off the port
    if ((p = strchr (node_name, '/')) != NULL) *p = '\0'; // if there is no port

    logprintfl (EUCADEBUG, "DEBUG: requested URI %s\n", uri);

    // see if we should redirect to the VMware broker
    if (strstr (uri, "VMwareBroker")) {
      uri = "http://localhost:8773/services/VMwareBroker";
      logprintfl (EUCADEBUG, "DEBUG: redirecting request to %s\n", uri);
    }

    // TODO: what if endpoint_uri, home, or env are NULL?
    stub = axis2_stub_create_EucalyptusNC(env, client_home, (axis2_char_t *)uri);

    if (stub && (st = malloc (sizeof(ncStub)))) {
        st->env=env;
        st->client_home=strdup((char *)client_home);
        st->endpoint_uri=(axis2_char_t *)strdup(endpoint_uri);
	st->node_name=(axis2_char_t *)strdup(node_name);
        st->stub=stub;
	if (st->client_home == NULL || st->endpoint_uri == NULL) {
            logprintfl (EUCAWARN, "WARNING: out of memory");
	}
    } else {
        logprintfl (EUCAWARN, "WARNING: out of memory");
    } 
    
    free (node_name);
    return st;
}