Java Code Examples for org.wso2.carbon.registry.core.Registry#rollbackTransaction()

The following examples show how to use org.wso2.carbon.registry.core.Registry#rollbackTransaction() . 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: SecurityMgtServiceComponent.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private void addKeystores() throws RegistryException {
    Registry registry = SecurityServiceHolder.getRegistryService().getGovernanceSystemRegistry();
    try {
        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }
        if (!registry.resourceExists(SecurityConstants.KEY_STORES)) {
            Collection kstores = registry.newCollection();
            registry.put(SecurityConstants.KEY_STORES, kstores);

            Resource primResource = registry.newResource();
            if (!registry.resourceExists(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE)) {
                registry.put(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE,
                        primResource);
            }
        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}
 
Example 2
Source File: RegistryDataManager.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Encrypt the registry properties by new algorithm and update
 *
 * @param registry
 * @param resource
 * @param properties
 * @throws RegistryException
 * @throws CryptoException
 */
private void updateRegistryProperties(Registry registry, String resource, List<String> properties)
        throws RegistryException, CryptoException {
    if (registry == null || StringUtils.isEmpty(resource) || CollectionUtils.isEmpty(properties)) {
        return;
    }
    if (registry.resourceExists(resource)) {
        try {
            registry.beginTransaction();
            Resource resourceObj = registry.get(resource);
            for (String encryptedPropertyName : properties) {
                String oldValue = resourceObj.getProperty(encryptedPropertyName);
                String newValue = Utility.getNewEncryptedValue(oldValue);
                if (StringUtils.isNotEmpty(newValue)) {
                    resourceObj.setProperty(encryptedPropertyName, newValue);
                }
            }
            registry.put(resource, resourceObj);
            registry.commitTransaction();
        } catch (RegistryException e) {
            registry.rollbackTransaction();
            log.error("Unable to update the registry resource", e);
            throw e;
        }
    }
}
 
Example 3
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private void addKeystores() throws RegistryException {
    Registry registry = SecurityServiceHolder.getRegistryService().getGovernanceSystemRegistry();
    try {
        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }
        if (!registry.resourceExists(SecurityConstants.KEY_STORES)) {
            Collection kstores = registry.newCollection();
            registry.put(SecurityConstants.KEY_STORES, kstores);

            Resource primResource = registry.newResource();
            if (!registry.resourceExists(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE)) {
                registry.put(RegistryResources.SecurityManagement.PRIMARY_KEYSTORE_PHANTOM_RESOURCE,
                        primResource);
            }
        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}
 
Example 4
Source File: RegistryManager.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void remove(String resourcePath) throws RegistryException {
    Registry registry = getRegistry();

    try {
        registry.beginTransaction();
        registry.delete(resourcePath);
        registry.commitTransaction();
    } catch (RegistryException e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException e1) {
            if (log.isErrorEnabled()) {
                log.error("Could not rollback transaction", e1);
            }
        }
        throw new RegistryException("Could not remove registry resource: [resource-path] " + resourcePath, e);
    }
}
 
Example 5
Source File: StratosApiV41Utils.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
private static void clearMetadata(String applicationId) throws RestAPIException {

        PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);

        String resourcePath = METADATA_REG_PATH + applicationId;
        Registry registry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext()
                .getRegistry(RegistryType.SYSTEM_GOVERNANCE);
        try {
            registry.beginTransaction();
            if (registry.resourceExists(resourcePath)) {
                registry.delete(resourcePath);
                log.info(String.format("Application metadata removed: [application-id] %s", applicationId));
            }
            registry.commitTransaction();
        } catch (RegistryException e) {
            try {
                registry.rollbackTransaction();
            } catch (RegistryException e1) {
                log.error("Could not rollback transaction", e1);
            }
            throw new RestAPIException(
                    String.format("Application metadata removed: [application-id] %s", applicationId), e);
        }
    }
 
Example 6
Source File: RegistryManager.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
public synchronized void remove(String resourcePath) throws RegistryException {
    Registry registry = getRegistry();

    try {
        registry.beginTransaction();
        registry.delete(resourcePath);
        registry.commitTransaction();
    } catch (RegistryException e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException e1) {
            if (log.isErrorEnabled()) {
                log.error("Could not rollback transaction", e1);
            }
        }
        throw new RegistryException("Could not remove registry resource: [resource-path] " + resourcePath, e);
    }
}
 
Example 7
Source File: RegistryManager.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
public synchronized void remove(String resourcePath) throws RegistryException {
    Registry registry = getRegistry();

    try {
        registry.beginTransaction();
        registry.delete(resourcePath);
        registry.commitTransaction();
    } catch (RegistryException e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException e1) {
            if (log.isErrorEnabled()) {
                log.error("Could not rollback transaction", e1);
            }
        }
        throw new RegistryException("Could not remove registry resource: [resource-path] " + resourcePath, e);
    }
}
 
Example 8
Source File: RegistryManager.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Persist a serializable object in the registry with the given resource path.
 *
 * @param object object to be persisted.
 */
@Override
public synchronized void persist(String resourcePath, Object object) throws RegistryException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Persisting resource in registry: [resource-path] %s", resourcePath));
    }

    Registry registry = getRegistry();

    try {
        PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);

        registry.beginTransaction();

        Resource nodeResource = registry.newResource();
        nodeResource.setContent(serializeToByteArray(object));
        registry.put(resourcePath, nodeResource);

        registry.commitTransaction();

        if (log.isDebugEnabled()) {
            log.debug(String.format("Resource persisted successfully in registry: [resource-path] %s",
                    resourcePath));
        }
    } catch (Exception e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException e1) {
            if (log.isErrorEnabled()) {
                log.error("Could not rollback transaction", e1);
            }
        }
        throw new RegistryException("Failed to persist resource in registry: [resource-path] " + resourcePath, e);
    }
}
 
Example 9
Source File: RegistryManager.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Persist a serializable object in the registry with the given resource path.
 *
 * @param serializableObject object to be persisted.
 */
public synchronized void persist(String resourcePath, Serializable serializableObject) throws RegistryException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Persisting resource in registry: [resource-path] %s", resourcePath));
    }

    Registry registry = getRegistry();

    try {
        PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        registry.beginTransaction();
        Resource nodeResource = registry.newResource();
        nodeResource.setContent(serializeToByteArray(serializableObject));
        registry.put(resourcePath, nodeResource);
        registry.commitTransaction();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Resource persisted successfully in registry: [resource-path] %s",
                    resourcePath));
        }
    } catch (Exception e) {
        try {
            registry.rollbackTransaction();
        } catch (Exception e1){
            if (log.isErrorEnabled()) {
                log.error("Could not rollback transaction", e1);
            }
        }
        String msg = "Failed to persist resource in registry: " + resourcePath;
        throw new RegistryException(msg, e);
    }
}
 
Example 10
Source File: RegistryManager.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Persist a serializable object in the registry with the given resource path.
 *
 * @param serializableObject object to be persisted.
 */
public synchronized void persist(String resourcePath, Serializable serializableObject) throws RegistryException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Persisting resource in registry: [resource-path] %s", resourcePath));
    }

    Registry registry = getRegistry();

    try {
        PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        registry.beginTransaction();
        Resource nodeResource = registry.newResource();
        nodeResource.setContent(serializeToByteArray(serializableObject));
        registry.put(resourcePath, nodeResource);
        registry.commitTransaction();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Resource persisted successfully in registry: [resource-path] %s",
                    resourcePath));
        }
    } catch (Exception e) {
       try {
           registry.rollbackTransaction();
       }catch (Exception e1){
           if (log.isErrorEnabled()) {
               log.error("Could not rollback transaction", e1);
           }
       }
        String msg = "Failed to persist resource in registry: " + resourcePath;
        throw new RegistryException(msg, e);
    }
}
 
Example 11
Source File: IdentitySTSMgtServiceComponent.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * This method is used to load custom security scenarios used inside Identity STS componsnts.
 *
 * @throws Exception
 */
private void loadSecurityScenarios() throws Exception {

    Registry registry = registryService.getConfigSystemRegistry();

    try {
        // Scenarios are listed in resources/scenario-config.xml
        URL resource = bundleContext.getBundle().getResource("scenario-config.xml");
        XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.openStream(),
                                                                 SecurityConstants.SECURITY_NAMESPACE);

        OMElement[] elements = xmlConfiguration.getElements("//ns:Scenario");

        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }

        for (OMElement scenarioEle : elements) {
            SecurityScenario scenario = new SecurityScenario();
            String scenarioId = scenarioEle.getAttribute(SecurityConstants.ID_QN).getAttributeValue();

            scenario.setScenarioId(scenarioId);
            scenario.setSummary(scenarioEle.getFirstChildWithName(SecurityConstants.SUMMARY_QN).getText());
            scenario.setDescription(scenarioEle.getFirstChildWithName(SecurityConstants.DESCRIPTION_QN).getText());
            scenario.setCategory(scenarioEle.getFirstChildWithName(SecurityConstants.CATEGORY_QN).getText());
            scenario.setWsuId(scenarioEle.getFirstChildWithName(SecurityConstants.WSUID_QN).getText());
            scenario.setType(scenarioEle.getFirstChildWithName(SecurityConstants.TYPE_QN).getText());

            OMElement genPolicyElem = scenarioEle.getFirstChildWithName(SecurityConstants.IS_GEN_POLICY_QN);
            if (genPolicyElem != null && "false".equals(genPolicyElem.getText())) {
                scenario.setGeneralPolicy(false);
            }

            String resourceUri = SecurityConstants.SECURITY_POLICY + "/" + scenarioId;

            for (Iterator modules = scenarioEle.getFirstChildWithName(SecurityConstants.MODULES_QN)
                                               .getChildElements(); modules.hasNext(); ) {
                String module = ((OMElement) modules.next()).getText();
                scenario.addModule(module);
            }

            // Save it in the DB
            SecurityScenarioDatabase.put(scenarioId, scenario);

            // Store the scenario in the Registry
            if (!scenarioId.equals(SecurityConstants.SCENARIO_DISABLE_SECURITY) &&
                !scenarioId.equals(SecurityConstants.POLICY_FROM_REG_SCENARIO)) {
                Resource scenarioResource = new ResourceImpl();
                scenarioResource.setContentStream(
                        bundleContext.getBundle().getResource(scenarioId + "-policy.xml").openStream());
                if (!registry.resourceExists(resourceUri)) {
                    registry.put(resourceUri, scenarioResource);
                }
            }

        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}
 
Example 12
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private void loadSecurityScenarios(Registry registry,
                                   BundleContext bundleContext) throws CarbonException, IOException, RegistryException {

    // TODO: Load into all tenant DBs
    // Load security scenarios
    URL resource = bundleContext.getBundle().getResource("/scenarios/scenario-config.xml");
    XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.openStream(),
            SecurityConstants.SECURITY_NAMESPACE);

    OMElement[] elements = xmlConfiguration.getElements("//ns:Scenario");
    try {
        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }

        for (OMElement scenarioEle : elements) {
            SecurityScenario scenario = new SecurityScenario();
            String scenarioId = scenarioEle.getAttribute(SecurityConstants.ID_QN)
                    .getAttributeValue();

            scenario.setScenarioId(scenarioId);
            scenario.setSummary(scenarioEle.getFirstChildWithName(SecurityConstants.SUMMARY_QN)
                    .getText());
            scenario.setDescription(scenarioEle.getFirstChildWithName(
                    SecurityConstants.DESCRIPTION_QN).getText());
            scenario.setCategory(scenarioEle.getFirstChildWithName(SecurityConstants.CATEGORY_QN)
                    .getText());
            scenario.setWsuId(scenarioEle.getFirstChildWithName(SecurityConstants.WSUID_QN)
                    .getText());
            scenario.setType(scenarioEle.getFirstChildWithName(SecurityConstants.TYPE_QN).getText());

            String resourceUri = SecurityConstants.SECURITY_POLICY + "/" + scenarioId;

            for (Iterator modules = scenarioEle.getFirstChildWithName(SecurityConstants.MODULES_QN)
                    .getChildElements(); modules.hasNext(); ) {
                String module = ((OMElement) modules.next()).getText();
                scenario.addModule(module);
            }

            // Save it in the DB
            SecurityScenarioDatabase.put(scenarioId, scenario);

            // Store the scenario in the Registry
            if (!scenarioId.equals(SecurityConstants.SCENARIO_DISABLE_SECURITY) &&
                    !scenarioId.equals(SecurityConstants.POLICY_FROM_REG_SCENARIO)) {
                Resource scenarioResource = new ResourceImpl();
                scenarioResource.
                        setContentStream(bundleContext.getBundle().
                                getResource("scenarios/" + scenarioId + "-policy.xml").openStream());
                scenarioResource.setMediaType("application/policy+xml");
                if (!registry.resourceExists(resourceUri)) {
                    registry.put(resourceUri, scenarioResource);
                }

                // Cache the resource in-memory in order to add it to the newly created tenants
                SecurityServiceHolder.addPolicyResource(resourceUri, scenarioResource);
            }
        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}