Пример #1
0
hdfsFS hdfsBuilderConnect(struct hdfsBuilder * bld) {
    PARAMETER_ASSERT(bld && !bld->nn.empty(), NULL, EINVAL);
    Hdfs::Internal::SessionConfig conf(*bld->conf);
    std::string uri;
    std::stringstream ss;
    ss.imbue(std::locale::classic());
    xmlURIPtr uriobj;
    FileSystem * fs = NULL;

    if (0 == strcasecmp(bld->nn.c_str(), "default")) {
        uri = conf.getDefaultUri();
    } else {
        /*
         * handle scheme
         */
        if (bld->nn.find("://") == bld->nn.npos) {
            uri  = "hdfs://";
        }

        uri += bld->nn;
    }

    uriobj = xmlParseURI(uri.c_str());

    try {
        if (!uriobj) {
            THROW(Hdfs::InvalidParameter, "Cannot parse connection URI");
        }

        if (uriobj->port != 0 && bld->port != 0) {
            THROW(Hdfs::InvalidParameter, "Cannot determinate port");
        }

        if (uriobj->user && !bld->userName.empty()) {
            THROW(Hdfs::InvalidParameter, "Cannot determinate user name");
        }

        ss << uriobj->scheme << "://";

        if (uriobj->user || !bld->userName.empty()) {
            ss << (uriobj->user ? uriobj->user : bld->userName.c_str())
               << '@';
        }

        if (bld->port == 0 && uriobj->port == 0) {
            ss << uriobj->server;
        } else {
            ss << uriobj->server << ":" << (uriobj->port ? uriobj->port : bld->port);
        }

        uri = ss.str();
    } catch (const std::bad_alloc & e) {
        if (uriobj) {
            xmlFreeURI(uriobj);
        }

        SetErrorMessage("Out of memory");
        errno = ENOMEM;
        return NULL;
    } catch (...) {
        if (uriobj) {
            xmlFreeURI(uriobj);
        }

        SetLastException(Hdfs::current_exception());
        handleException(Hdfs::current_exception());
        return NULL;
    }

    xmlFreeURI(uriobj);

    try {
        fs = new FileSystem(*bld->conf);

        if (!bld->token.empty()) {
            fs->connect(uri.c_str(), NULL, bld->token.c_str());
        } else {
            fs->connect(uri.c_str());
        }

        return new HdfsFileSystemInternalWrapper(fs);
    } catch (const std::bad_alloc & e) {
        SetErrorMessage("Out of memory");
        delete fs;
        errno = ENOMEM;
    } catch (...) {
        delete fs;
        SetLastException(Hdfs::current_exception());
        handleException(Hdfs::current_exception());
    }

    return NULL;
}