<commit_before>/**
 * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.wso2.carbon.bpel.core.ode.integration.store.repository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ode.bpel.iapi.ProcessState;
import org.wso2.carbon.bpel.core.BPELConstants;
import org.wso2.carbon.bpel.core.internal.BPELServiceComponent;
import org.wso2.carbon.bpel.core.ode.integration.store.BPELDeploymentContext;
import org.wso2.carbon.bpel.core.ode.integration.store.ProcessConfigurationImpl;
import org.wso2.carbon.bpel.core.ode.integration.store.Utils;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.ProcessManagementException;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.CleanUpListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.CleanUpType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.EnableEventListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.Generate_type1;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.ProcessEventsListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.ScopeEventListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.ScopeEventType;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.utils.RegistryClientUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Central manager who is responsible for managing BPEL archives, BPEL deployment units repository
 * and registry based BPEL deployment unit repository.
 * <p/>
 * How BPEL Packages will get stored in the registry
 * <p/>
 * <BPEL_ROOT>/<BPEL_PACKAGE_NAME>
 * |
 * + prop_bpel.package.latest.version
 * + prop_bpel.package.status
 * + prop_bpel.package.error.log
 * + prop_bpel.package.latest.checksum
 * + versions
 * |
 * +<BPEL_PACKAGE_NAME-version1>
 * +<BPEL_PACKAGE_NAME-version2>
 * +<BPEL_PACKAGE_NAME-version3>
 */
public class BPELPackageRepository {
    private static Log log = LogFactory.getLog(BPELConstants.LOGGER_DEPLOYMENT);

    // Config registry instance use to store BPEL package information for this repository instance.
    private Registry configRegistry;

    // Location of local file system where we keep BPEL deployment units.
    //private File bpelDURepo;

    // Location of local file system where we keep BPEL archives.
    private File bpelArchiveRepo;

    public BPELPackageRepository(Registry configRegistry, File bpelDURepo, File bpelArchiveRepo) {
        this.configRegistry = configRegistry;
        //this.bpelDURepo = bpelDURepo;
        this.bpelArchiveRepo = bpelArchiveRepo;
    }

    /**
     * Initialize the BPEL repository. This method is used to sync the registry and the tenant repositories.
     *
     * @throws RegistryException if there is registry access issue
     */
    public void init() throws Exception {
        if (BPELServiceComponent.getBPELServer().getBpelServerConfiguration().isSyncWithRegistry()) {
            fixLocalBPELArchiveRepository();
        }
    }

    private void addLatestArchiveToRegistryCollection(BPELDeploymentContext bpelDeploymentContext)
            throws FileNotFoundException, RegistryException {

        Resource latestBPELArchive = configRegistry.newResource();

        try(FileInputStream stream = new FileInputStream(bpelDeploymentContext.getBpelArchive())) {
            latestBPELArchive.setContent(stream);
        } catch (IOException exception) {
            log.error("Error reading bpel archive " +
                        bpelDeploymentContext.getBpelArchive().getName() , exception);
        }
        configRegistry.put(BPELPackageRepositoryUtils.
                        getBPELPackageArchiveResourcePath(bpelDeploymentContext.getBpelPackageName()),
                latestBPELArchive);
    }

    /**
     * Handles deployment of new BPEL packages. Stores all the meta data and BPEL Package content
     * in the registry.
     *
     * @param deploymentContext containing information about current deployment
     * @throws RegistryException on registry access error.
     */
    public void handleNewBPELPackageAddition(BPELDeploymentContext deploymentContext)
            throws Exception {
        try {
            if (!isDUCollectionIsThere(deploymentContext)) {
                configRegistry.beginTransaction();
                createBPELPackageParentCollectionWithProperties(deploymentContext);<commit_msg>Fixing build issue due to stream close<commit_after>/**
 * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.wso2.carbon.bpel.core.ode.integration.store.repository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ode.bpel.iapi.ProcessState;
import org.wso2.carbon.bpel.core.BPELConstants;
import org.wso2.carbon.bpel.core.internal.BPELServiceComponent;
import org.wso2.carbon.bpel.core.ode.integration.store.BPELDeploymentContext;
import org.wso2.carbon.bpel.core.ode.integration.store.ProcessConfigurationImpl;
import org.wso2.carbon.bpel.core.ode.integration.store.Utils;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.ProcessManagementException;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.CleanUpListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.CleanUpType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.EnableEventListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.Generate_type1;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.ProcessEventsListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.ScopeEventListType;
import org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.types.ScopeEventType;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.utils.RegistryClientUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Central manager who is responsible for managing BPEL archives, BPEL deployment units repository
 * and registry based BPEL deployment unit repository.
 * <p/>
 * How BPEL Packages will get stored in the registry
 * <p/>
 * <BPEL_ROOT>/<BPEL_PACKAGE_NAME>
 * |
 * + prop_bpel.package.latest.version
 * + prop_bpel.package.status
 * + prop_bpel.package.error.log
 * + prop_bpel.package.latest.checksum
 * + versions
 * |
 * +<BPEL_PACKAGE_NAME-version1>
 * +<BPEL_PACKAGE_NAME-version2>
 * +<BPEL_PACKAGE_NAME-version3>
 */
public class BPELPackageRepository {
    private static Log log = LogFactory.getLog(BPELConstants.LOGGER_DEPLOYMENT);

    // Config registry instance use to store BPEL package information for this repository instance.
    private Registry configRegistry;

    // Location of local file system where we keep BPEL deployment units.
    //private File bpelDURepo;

    // Location of local file system where we keep BPEL archives.
    private File bpelArchiveRepo;

    public BPELPackageRepository(Registry configRegistry, File bpelDURepo, File bpelArchiveRepo) {
        this.configRegistry = configRegistry;
        //this.bpelDURepo = bpelDURepo;
        this.bpelArchiveRepo = bpelArchiveRepo;
    }

    /**
     * Initialize the BPEL repository. This method is used to sync the registry and the tenant repositories.
     *
     * @throws RegistryException if there is registry access issue
     */
    public void init() throws Exception {
        if (BPELServiceComponent.getBPELServer().getBpelServerConfiguration().isSyncWithRegistry()) {
            fixLocalBPELArchiveRepository();
        }
    }

    private void addLatestArchiveToRegistryCollection(BPELDeploymentContext bpelDeploymentContext)
            throws FileNotFoundException, RegistryException {

        Resource latestBPELArchive = configRegistry.newResource();

        FileInputStream stream = new FileInputStream(bpelDeploymentContext.getBpelArchive());
        latestBPELArchive.setContent(stream);
        configRegistry.put(BPELPackageRepositoryUtils.
                        getBPELPackageArchiveResourcePath(bpelDeploymentContext.getBpelPackageName()),
                latestBPELArchive);
    }

    /**
     * Handles deployment of new BPEL packages. Stores all the meta data and BPEL Package content
     * in the registry.
     *
     * @param deploymentContext containing information about current deployment
     * @throws RegistryException on registry access error.
     */
    public void handleNewBPELPackageAddition(BPELDeploymentContext deploymentContext)
            throws Exception {
        try {
            if (!isDUCollectionIsThere(deploymentContext)) {
                configRegistry.beginTransaction();
                createBPELPackageParentCollectionWithProperties(deploymentContext);
{'fraction_non_alphanumeric': 0.08851241166878057, 'fraction_numerical': 0.006885305308932777, 'mean_word_length': 4.031449407474931, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '147012', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3175, 'n_words': 730}