// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationConstants.java
// public class DataMigrationConstants {
//     public static final String EVENT_PUBLISHER_PATH = "/repository/deployment/server/eventpublishers";
//     public static final String EVENT_RECIEVER_PATH = "/repository/deployment/server/eventreceivers";
//     public static final String CARBON_HOME = "carbon.home";
//     public static final int SUPER_TENANT_ID = -1234;
// }
// 
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationUtil.java
// public class DataMigrationUtil {
// 
//     private DataMigrationUtil() {
// 
//     }
// 
//     public static boolean isNewlyEncrypted(String encryptedValue) throws DataMigrationException, CryptoException {
//         CryptoUtil cryptoUtil;
//         try {
//             cryptoUtil = CryptoUtil.getDefaultCryptoUtil(CarbonCoreDataHolder.getInstance().
//                             getServerConfigurationService(),
//                     CarbonCoreDataHolder.getInstance().getRegistryService());
//         } catch (Exception e) {
//             throw new DataMigrationException("Error while initializing cryptoUtil", e);
//         }
//         return cryptoUtil.base64DecodeAndIsSelfContainedCipherText(encryptedValue);
//     }
// 
//     public static String reEncryptByNewAlgorithm(String value) throws CryptoException {
//         byte[] decryptedValue = CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(
//                 value, "RSA");
//         return CryptoUtil.getDefaultCryptoUtil()
//                 .encryptAndBase64Encode(decryptedValue);
//     }
// }


import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.migration.exception.DataMigrationException;
import org.wso2.migration.internal.MigrationServiceDataHolder;
import org.wso2.migration.util.DataMigrationConstants;
import org.wso2.migration.util.DataMigrationUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
        String carbonHome = System.getProperty(DataMigrationConstants.CARBON_HOME);
        String userStorePath;
        if (tenantId == DataMigrationConstants.SUPER_TENANT_ID) {
            userStorePath = Paths.get(carbonHome,
                    new String[]{"repository", "deployment", "server", "userstores"}).toString();
        } else {
            userStorePath = Paths.get(carbonHome,
                    new String[]{"repository", "tenants", String.valueOf(tenantId), "userstores"}).toString();
        }
        File[] files = new File(userStorePath).listFiles();
        return files != null ? files : new File[0];
    }

    private void updatePassword(String filePath) throws FileNotFoundException, CryptoException, DataMigrationException {

        XMLStreamReader parser = null;
        FileInputStream stream = null;
        try {
            log.info("Migrating password in: " + filePath);
            stream = new FileInputStream(filePath);
            parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement documentElement = builder.getDocumentElement();
            Iterator it = documentElement.getChildElements();
            String newEncryptedPassword = null;
            while (it.hasNext()) {
                OMElement element = (OMElement) it.next();
                if ("password".equals(element.getAttributeValue(new QName("name"))) ||
                        "ConnectionPassword".equals(element.getAttributeValue(new QName("name")))) {
                    String oldValue = element.getText();
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationConstants.java
// public class DataMigrationConstants {
//     public static final String EVENT_PUBLISHER_PATH = "/repository/deployment/server/eventpublishers";
//     public static final String EVENT_RECIEVER_PATH = "/repository/deployment/server/eventreceivers";
//     public static final String CARBON_HOME = "carbon.home";
//     public static final int SUPER_TENANT_ID = -1234;
// }
// 
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationUtil.java
// public class DataMigrationUtil {
// 
//     private DataMigrationUtil() {
// 
//     }
// 
//     public static boolean isNewlyEncrypted(String encryptedValue) throws DataMigrationException, CryptoException {
//         CryptoUtil cryptoUtil;
//         try {
//             cryptoUtil = CryptoUtil.getDefaultCryptoUtil(CarbonCoreDataHolder.getInstance().
//                             getServerConfigurationService(),
//                     CarbonCoreDataHolder.getInstance().getRegistryService());
//         } catch (Exception e) {
//             throw new DataMigrationException("Error while initializing cryptoUtil", e);
//         }
//         return cryptoUtil.base64DecodeAndIsSelfContainedCipherText(encryptedValue);
//     }
// 
//     public static String reEncryptByNewAlgorithm(String value) throws CryptoException {
//         byte[] decryptedValue = CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(
//                 value, "RSA");
//         return CryptoUtil.getDefaultCryptoUtil()
//                 .encryptAndBase64Encode(decryptedValue);
//     }
// }


// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/service/UserStorePasswordMigration.java
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.migration.exception.DataMigrationException;
import org.wso2.migration.internal.MigrationServiceDataHolder;
import org.wso2.migration.util.DataMigrationConstants;
import org.wso2.migration.util.DataMigrationUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

        String carbonHome = System.getProperty(DataMigrationConstants.CARBON_HOME);
        String userStorePath;
        if (tenantId == DataMigrationConstants.SUPER_TENANT_ID) {
            userStorePath = Paths.get(carbonHome,
                    new String[]{"repository", "deployment", "server", "userstores"}).toString();
        } else {
            userStorePath = Paths.get(carbonHome,
                    new String[]{"repository", "tenants", String.valueOf(tenantId), "userstores"}).toString();
        }
        File[] files = new File(userStorePath).listFiles();
        return files != null ? files : new File[0];
    }

    private void updatePassword(String filePath) throws FileNotFoundException, CryptoException, DataMigrationException {

        XMLStreamReader parser = null;
        FileInputStream stream = null;
        try {
            log.info("Migrating password in: " + filePath);
            stream = new FileInputStream(filePath);
            parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement documentElement = builder.getDocumentElement();
            Iterator it = documentElement.getChildElements();
            String newEncryptedPassword = null;
            while (it.hasNext()) {
                OMElement element = (OMElement) it.next();
                if ("password".equals(element.getAttributeValue(new QName("name"))) ||
                        "ConnectionPassword".equals(element.getAttributeValue(new QName("name")))) {
                    String oldValue = element.getText();
                    if (oldValue != null && !DataMigrationUtil.isNewlyEncrypted(oldValue)) {