Java Code Examples for org.wso2.carbon.registry.core.Resource#discard()

The following examples show how to use org.wso2.carbon.registry.core.Resource#discard() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatewayUtils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Delete the given registry property from the given tenant registry path
 *
 * @param propertyName property name
 * @param path         resource path
 * @param tenantDomain
 * @throws AxisFault
 */
public static void deleteRegistryProperty(String propertyName, String path, String tenantDomain)
        throws AxisFault {

    try {
        UserRegistry registry = getRegistry(tenantDomain);
        PrivilegedCarbonContext.startTenantFlow();
        if (tenantDomain != null && StringUtils.isNotEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext()
                    .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        Resource resource = registry.get(path);
        if (resource != null && resource.getProperty(propertyName) != null) {
            resource.removeProperty(propertyName);
            registry.put(resource.getPath(), resource);
            resource.discard();
        }
    } catch (RegistryException | APIManagementException e) {
        String msg = "Failed to delete secure endpoint password alias " + e.getMessage();
        throw new AxisFault(msg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 2
Source File: SecureVaultPasswordMigrationClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * remove secure vault password property
 *
 * @param name property name
 */
private void removeProperty(String name) {
    log.info("Removing old passwords.");
    try {
        Resource resource = userRegistry.get(SECURE_VAULT_PATH);
        resource.removeProperty(name);
        userRegistry.put(resource.getPath(), resource);
        resource.discard();
    } catch (RegistryException e) {
        throw new MigrationClientException("Error occurred while removing property", e);
    }
}
 
Example 3
Source File: SecureVaultPasswordMigrationClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Method to update property
 *
 * @param name property name
 * @param value property value
 */
private void updateProperty(String name, String value) {
    log.info("Inserting new passwords.");
    try {
        Resource resource = userRegistry.get(SECURE_VAULT_PATH);
        resource.addProperty(name, value);
        userRegistry.put(resource.getPath(), resource);
        resource.discard();
    } catch (RegistryException e) {
        throw new MigrationClientException("Error occurred while updating property", e);
    }
}
 
Example 4
Source File: GatewayUtils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Add/Update the given registry property from the given tenant registry
 * path
 *
 * @param propertyName  property name
 * @param propertyValue property value
 * @param path          resource path
 * @param tenantDomain
 * @throws APIManagementException
 */
public static void setRegistryProperty(String propertyName, String propertyValue, String path, String tenantDomain)
        throws APIManagementException {

    UserRegistry registry = getRegistry(tenantDomain);
    PrivilegedCarbonContext.startTenantFlow();
    if (tenantDomain != null && StringUtils.isNotEmpty(tenantDomain)) {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
    } else {
        PrivilegedCarbonContext.getThreadLocalCarbonContext()
                .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
    }
    try {
        Resource resource = registry.get(path);
        // add or update property
        if (resource.getProperty(propertyName) != null) {
            resource.setProperty(propertyName, propertyValue);
        } else {
            resource.addProperty(propertyName, propertyValue);
        }
        registry.put(resource.getPath(), resource);
        resource.discard();
    } catch (RegistryException e) {
        throw new APIManagementException("Error while reading registry resource " + path + " for tenant " +
                tenantDomain);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 5
Source File: CarbonRepositoryUtils.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Save the given DeploymentSynchronizerConfiguration to the registry. The target
 * configuration registry space will be selected using the specified tenant ID. As a result
 * the configuration will be stored in the configuration registry of the specified
 * tenant.
 *
 * @param config The configuration to be saved
 * @param tenantId Tenant ID to select the configuration registry
 * @throws org.wso2.carbon.registry.core.exceptions.RegistryException if an error occurs while accessing the registry
 */
public static void persistConfiguration(DeploymentSynchronizerConfiguration config,
                                        int tenantId) throws RegistryException {

    Resource resource;
    UserRegistry localRepository = getLocalRepository(tenantId);
    if (!localRepository.resourceExists(DeploymentSynchronizerConstants.CARBON_REPOSITORY)) {
        resource = localRepository.newResource();
    } else {
        resource = localRepository.get(DeploymentSynchronizerConstants.CARBON_REPOSITORY);
    }

    resource.setProperty(DeploymentSynchronizerConstants.AUTO_COMMIT_MODE,
            String.valueOf(config.isAutoCommit()));
    resource.setProperty(DeploymentSynchronizerConstants.AUTO_CHECKOUT_MODE,
            String.valueOf(config.isAutoCheckout()));
    resource.setProperty(DeploymentSynchronizerConstants.AUTO_SYNC_PERIOD,
            String.valueOf(config.getPeriod()));
    resource.setProperty(DeploymentSynchronizerConstants.USE_EVENTING,
            String.valueOf(config.isUseEventing()));
    resource.setProperty(DeploymentSynchronizerConstants.REPOSITORY_TYPE,
            config.getRepositoryType());
    resource.setContent(config.isEnabled() ? "enabled" : "disabled");

    //Get Repository specific configuration parameters from config object.
    RepositoryConfigParameter[] parameters = config.getRepositoryConfigParameters();

    if (parameters != null && parameters.length != 0) {
        //Save each Repository specific configuration parameter in registry.
        for (int i = 0; i < parameters.length; i++) {
            resource.setProperty(parameters[i].getName(), parameters[i].getValue());
        }
    }

    localRepository.put(DeploymentSynchronizerConstants.CARBON_REPOSITORY, resource);
    resource.discard();
}
 
Example 6
Source File: CarbonRepositoryUtils.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Loads the deployment synchronizer configuration from the configuration registry of the
 * specified tenant.
 *
 * @param tenantId Tenant ID
 * @return a DeploymentSynchronizerConfiguration object or null
 * @throws org.wso2.carbon.registry.core.exceptions.RegistryException if the registry cannot be accessed
 */
public static DeploymentSynchronizerConfiguration getDeploymentSyncConfigurationFromRegistry(
        int tenantId) throws RegistryException {

    UserRegistry localRepository = getLocalRepository(tenantId);
    if (!localRepository.resourceExists(DeploymentSynchronizerConstants.CARBON_REPOSITORY)) {
        return null;
    }

    Resource resource = localRepository.get(DeploymentSynchronizerConstants.CARBON_REPOSITORY);
    DeploymentSynchronizerConfiguration config = new DeploymentSynchronizerConfiguration();
    String status = new String((byte[]) resource.getContent());
    if ("enabled".equals(status)) {
        config.setEnabled(true);
    }

    config.setAutoCheckout(Boolean.valueOf(resource.getProperty(
            DeploymentSynchronizerConstants.AUTO_CHECKOUT_MODE)));
    config.setAutoCommit(Boolean.valueOf(resource.getProperty(
            DeploymentSynchronizerConstants.AUTO_COMMIT_MODE)));
    config.setPeriod(Long.valueOf(resource.getProperty(
            DeploymentSynchronizerConstants.AUTO_SYNC_PERIOD)));
    config.setUseEventing(Boolean.valueOf(resource.getProperty(
            DeploymentSynchronizerConstants.USE_EVENTING)));
    config.setRepositoryType(resource.getProperty(
            DeploymentSynchronizerConstants.REPOSITORY_TYPE));

    ArtifactRepository repository =
            RepositoryReferenceHolder.getInstance().getRepositoryByType(config.getRepositoryType());
    if (repository == null) {
        throw new RegistryException("No Repository found for type " + config.getRepositoryType());
    }

    List<RepositoryConfigParameter> parameters = repository.getParameters();

    //If repository specific configuration parameters are found.
    if (parameters != null) {
        //Find the 'value' of each parameter from the registry by parameter 'name' and attach to parameter
        for (RepositoryConfigParameter parameter : parameters) {
            parameter.setValue(resource.getProperty(parameter.getName()));
        }

        //Attach parameter list to config object.
        config.setRepositoryConfigParameters(parameters.toArray(new RepositoryConfigParameter[parameters.size()]));
    }

    resource.discard();
    return config;
}