/**
 * Returns a unique identifier of a dynamic component.
 *
 * @return platform-specific id of the component
 */
KNIEXPORT KNI_RETURNTYPE_INT
KNIDECL(com_sun_midp_midletsuite_DynamicComponentStorage_createComponentId) {
    ComponentIdType componentId = UNUSED_COMPONENT_ID;
    MIDPError rc;

    rc = midp_create_component_id(&componentId);
    if (rc != ALL_OK) {
        if (rc == OUT_OF_MEMORY) {
            KNI_ThrowNew(midpOutOfMemoryError, NULL);
        } else {
            KNI_ThrowNew(midpIOException, NULL);
        }
    }

    KNI_ReturnInt(componentId);
}
/**
 * Checks if a midlet suite or dynamic component with the given name
 * created by the given vendor exists.
 * If it does, this function returns a unique identifier of the suite
 * or component. Note that the suite or component may be corrupted even
 * if it exists. If it doesn't, a new suite ID or component ID is created.
 *
 * @param type type of the component
 * @param suiteId if type == COMPONENT_DYNAMIC, contains ID of the suite this
 *                components belongs to; unused otherwise
 * @param vendor name of the vendor that created the application or component,
 *        as given in a JAD file
 * @param name name of the suite or component, as given in a JAD file
 * @param pId [out] receives the platform-specific suite ID or component ID
 *        of the application given by vendor and name, or UNUSED_SUITE_ID /
 *        UNUSED_COMPONENT_ID if suite does not exist, or out of memory
 *        error occured, or the suite / component is corrupted.
 *
 * @return  ALL_OK if suite or component found,
 *          NOT_FOUND if suite or component does not exist (so a new ID
 *          was created), other error code in case of error
 */
static MIDPError
get_suite_or_component_id(ComponentType type, SuiteIdType suiteId,
                           const pcsl_string* vendor, const pcsl_string* name,
                           jint* pId) {
    MIDPError status;
    char *pszError;
    MidletSuiteData* pData;

#if ENABLE_DYNAMIC_COMPONENTS
    if (type != COMPONENT_DYNAMIC) {
        *pId = (jint)UNUSED_SUITE_ID;
    } else {
        *pId = (jint)UNUSED_COMPONENT_ID;
    }
#else
    (void)type;
    (void)suiteId;
    *pId = (jint)UNUSED_SUITE_ID;
#endif /* ENABLE_DYNAMIC_COMPONENTS */

    /* load _suites.dat */
    status = read_suites_data(&pszError);
    storageFreeError(pszError);
    if (status != ALL_OK) {
        return status;
    }

    pData = g_pSuitesData;

    /* try to find a suite */
    while (pData != NULL) {
        if (pcsl_string_equals(&pData->varSuiteData.suiteName, name) &&
                pcsl_string_equals(&pData->varSuiteData.suiteVendor, vendor)
#if ENABLE_DYNAMIC_COMPONENTS
                    && (type == pData->type) && (type != COMPONENT_DYNAMIC ||
                        (type == COMPONENT_DYNAMIC && suiteId == pData->suiteId))
#endif
        ) {
#if ENABLE_DYNAMIC_COMPONENTS
            if (type != COMPONENT_DYNAMIC) {
                *pId = (jint)pData->suiteId;
            } else {
                *pId = (jint)pData->componentId;
            }
#else
            *pId = (jint)pData->suiteId;
#endif /* ENABLE_DYNAMIC_COMPONENTS */
            return ALL_OK; /* IMPL_NOTE: consider SUITE_CORRUPTED_ERROR */
        }

        pData = pData->nextEntry;
    }

    /* suite or component was not found - create a new suite or component ID */
#if ENABLE_DYNAMIC_COMPONENTS
    if (type != COMPONENT_DYNAMIC) {
        status = midp_create_suite_id((SuiteIdType*)pId);
    } else {
        status = midp_create_component_id((ComponentIdType*)pId);
    }
#else
    status = midp_create_suite_id((SuiteIdType*)pId);
#endif /* ENABLE_DYNAMIC_COMPONENTS */

    return (status == ALL_OK) ? NOT_FOUND : status;
}