Search in sources :

Example 1 with ImexProperties

use of com.liferay.imex.core.api.configuration.model.ImexProperties in project liferay-imex by jpdacunha.

the class ImexConfigurationServiceImpl method loadConfigurationMap.

private <E> Map<String, Properties> loadConfigurationMap(List<String> bundleNames, Map<String, ServiceReference<E>> references) {
    Map<String, Properties> bundleConf = new HashMap<>();
    for (Map.Entry<String, ServiceReference<E>> entry : references.entrySet()) {
        ServiceReference<E> reference = entry.getValue();
        Bundle bundle = reference.getBundle();
        ImexProperties props = new ImexProperties();
        loadBundleConfiguration(bundle, props);
        bundleConf.put(bundle.getSymbolicName(), props.getProperties());
    }
    return bundleConf;
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with ImexProperties

use of com.liferay.imex.core.api.configuration.model.ImexProperties in project liferay-imex by jpdacunha.

the class ImexExportServiceImpl method doExport.

@Override
public String doExport(List<String> bundleNames, String profileId, boolean debug) {
    // Generate an unique identifier for this export process
    ProcessIdentifierGenerator identifier = new ExporterProcessIdentifierGenerator();
    String identifierStr = identifier.getOrGenerateUniqueIdentifier();
    LoggingContext.put(ImexExecutionReportService.IDENTIFIER_KEY, identifierStr);
    try {
        if (imexCoreService.tryLock()) {
            reportService.getSeparator(_log);
            if (bundleNames != null && bundleNames.size() > 0) {
                reportService.getStartMessage(_log, "PARTIAL export process for [" + bundleNames.toString() + "]");
            } else {
                reportService.getStartMessage(_log, "ALL export process");
            }
            try {
                if (debug) {
                    reportService.getMessage(_log, "Running DEBUG mode ...");
                }
                Map<String, ServiceReference<Exporter>> exporters = trackerService.getFilteredExporters(bundleNames);
                if (exporters == null || exporters.size() == 0) {
                    reportService.getMessage(_log, "There is no exporters to execute. Please check : ");
                    reportService.getMessage(_log, "- All importers are correctly registered in OSGI container");
                    if (bundleNames != null) {
                        reportService.getMessage(_log, "- A registered bundle exists for each typed name [" + bundleNames + "]");
                    }
                    reportService.printKeys(trackerService.getExporters(), _log);
                } else {
                    // Reading configuration
                    ImexProperties coreConfig = new ImexProperties();
                    configurationService.loadCoreConfiguration(coreConfig);
                    reportService.displayConfigurationLoadingInformation(coreConfig, _log);
                    // Archive actual files before importing
                    imexArchiverService.archiveDataDirectory(coreConfig.getProperties(), identifier);
                    File exportDir = initializeExportDirectory();
                    reportService.getPropertyMessage(_log, "IMEX export path", exportDir.toString());
                    List<Company> companies = companyLocalService.getCompanies();
                    // For each Liferay company
                    for (Company company : companies) {
                        reportService.getStartMessage(_log, company);
                        long companyId = company.getCompanyId();
                        String companyName = company.getName();
                        File companyDir = initializeCompanyExportDirectory(exportDir, company);
                        executeRegisteredExporters(exporters, companyDir, companyId, companyName, profileId, debug);
                    }
                    reportService.getSeparator(_log);
                    // Executing raw export
                    executeRawExport((ExporterProcessIdentifierGenerator) identifier, debug);
                }
            } catch (Exception e) {
                reportService.getError(_log, "doExport", e);
            }
            reportService.getEndMessage(_log, "export process");
        } else {
            reportService.getMessage(_log, "##");
            reportService.getMessage(_log, "## " + ImexCoreService.LOCKED_MESSAGE);
            reportService.getMessage(_log, "##");
        }
    } finally {
        imexCoreService.releaseLock();
    }
    return identifierStr;
}
Also used : ExporterProcessIdentifierGenerator(com.liferay.imex.core.service.exporter.model.ExporterProcessIdentifierGenerator) Company(com.liferay.portal.kernel.model.Company) ExporterProcessIdentifierGenerator(com.liferay.imex.core.service.exporter.model.ExporterProcessIdentifierGenerator) ProcessIdentifierGenerator(com.liferay.imex.core.api.identifier.ProcessIdentifierGenerator) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) File(java.io.File) ImexException(com.liferay.imex.core.util.exception.ImexException) PortalException(com.liferay.portal.kernel.exception.PortalException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ImexProperties

use of com.liferay.imex.core.api.configuration.model.ImexProperties in project liferay-imex by jpdacunha.

the class ImexExportServiceImpl method executeRegisteredExporters.

private void executeRegisteredExporters(Map<String, ServiceReference<Exporter>> exporters, File companyDir, long companyId, String companyName, String profileId, boolean debug) throws ImexException {
    User user = UserUtil.getDefaultAdmin(companyId);
    if (user == null) {
        reportService.getError(_log, "Company [" + companyName + "]", "Missing omni admin user");
        return;
    }
    for (Map.Entry<String, ServiceReference<Exporter>> entry : exporters.entrySet()) {
        ServiceReference<Exporter> reference = entry.getValue();
        Bundle bundle = reference.getBundle();
        Exporter exporter = bundle.getBundleContext().getService(reference);
        reportService.getStartMessage(_log, exporter.getProcessDescription(), 1);
        // Loading configuration for each exporter
        ImexProperties config = new ImexProperties();
        configurationService.loadExporterAndCoreConfiguration(bundle, config);
        reportService.displayConfigurationLoadingInformation(config, _log, bundle);
        Properties configAsProperties = config.getProperties();
        if (configAsProperties == null || configAsProperties.size() == 0) {
            reportService.getMessage(_log, bundle, "has no defined configuration.");
        } else {
            reportService.displayProperties(configAsProperties, bundle, _log);
        }
        // Manage Root directory
        String exporterRootDirName = exporter.getRootDirectoryName();
        File exporterRootDir = new File(companyDir, exporterRootDirName);
        boolean success = exporterRootDir.mkdirs();
        if (!success) {
            throw new ImexException("Failed to create directory " + exporterRootDir);
        }
        File destDir = exporterRootDir;
        // Manage Profile
        String profileDirName = getValidProfile(profileId, bundle, exporter, configAsProperties);
        if (Validator.isNotNull(profileDirName)) {
            File profileDir = new File(exporterRootDir, profileDirName);
            success = profileDir.mkdirs();
            if (!success) {
                throw new ImexException("Failed to create directory " + profileDir);
            }
            destDir = profileDir;
        } else {
            _log.debug("Exporter [" + bundle.getSymbolicName() + "] is not configured to use profiles");
        }
        try {
            Company company = companyLocalService.getCompany(companyId);
            // Trigger Exporter specific code
            exporter.doExport(user, configAsProperties, destDir, companyId, company.getLocale(), this.rawExportContentList, debug);
        } catch (PortalException e) {
            _log.error(e, e);
        }
        reportService.getEndMessage(_log, exporter.getProcessDescription(), 1);
    }
}
Also used : Company(com.liferay.portal.kernel.model.Company) User(com.liferay.portal.kernel.model.User) Bundle(org.osgi.framework.Bundle) Exporter(com.liferay.imex.core.api.exporter.Exporter) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) ImexException(com.liferay.imex.core.util.exception.ImexException) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) PortalException(com.liferay.portal.kernel.exception.PortalException) Map(java.util.Map) File(java.io.File)

Example 4 with ImexProperties

use of com.liferay.imex.core.api.configuration.model.ImexProperties in project liferay-imex by jpdacunha.

the class ImexExportServiceImpl method executeRawExport.

private void executeRawExport(ExporterProcessIdentifierGenerator rootIdentifier, boolean debug) throws ImexException, IOException {
    ImexProperties config = new ImexProperties();
    configurationService.loadCoreConfiguration(config);
    Properties coreConfigAsProperties = config.getProperties();
    boolean rawExportEnabled = GetterUtil.getBoolean(coreConfigAsProperties.get(ImExCorePropsKeys.RAW_CONTENT_EXPORTER_ENABLED));
    if (rawExportEnabled) {
        ProcessIdentifierGenerator identifier = new RawExporterProcessIdentifierGeneratorWrapper(rootIdentifier);
        String randomAlphanumeric = RandomStringUtils.randomAlphanumeric(4);
        File tempDir = Files.createTempDirectory(randomAlphanumeric).toFile();
        if (!tempDir.exists()) {
            throw new ImexException("Failed to create directory " + tempDir);
        }
        File exportDir = initializeRawExportDirectory();
        reportService.getStartMessage(_log, "Raw export process (human readable format)", 1);
        // Since this a synchronized list
        synchronized (rawExportContentList) {
            for (ExporterRawContent content : rawExportContentList) {
                writeRawContentInDir(tempDir, content, debug);
            }
        }
        imexArchiverService.archiveAndClean(coreConfigAsProperties, tempDir, exportDir, identifier);
        FileUtil.deltree(tempDir);
        rawExportContentList.clear();
        reportService.getEndMessage(_log, "Raw export process", 1);
    } else {
        if (rawExportContentList.size() > 0) {
            reportService.getDisabled(_log, "raw content export");
            reportService.getMessage(_log, "See [" + ImExCorePropsKeys.RAW_CONTENT_EXPORTER_ENABLED + "] to enable this feature", 4);
        }
    }
}
Also used : ImexException(com.liferay.imex.core.util.exception.ImexException) ExporterProcessIdentifierGenerator(com.liferay.imex.core.service.exporter.model.ExporterProcessIdentifierGenerator) ProcessIdentifierGenerator(com.liferay.imex.core.api.identifier.ProcessIdentifierGenerator) RawExporterProcessIdentifierGeneratorWrapper(com.liferay.imex.core.service.exporter.model.RawExporterProcessIdentifierGeneratorWrapper) ExporterRawContent(com.liferay.imex.core.api.exporter.model.ExporterRawContent) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) Properties(java.util.Properties) File(java.io.File)

Example 5 with ImexProperties

use of com.liferay.imex.core.api.configuration.model.ImexProperties in project liferay-imex by jpdacunha.

the class ImexImportServiceImpl method doImport.

@Override
public String doImport(List<String> bundleNames, String profileId, boolean debug) {
    // Generate an unique identifier for this import process
    ProcessIdentifierGenerator identifier = new ImporterProcessIdentifier();
    String identifierStr = identifier.getOrGenerateUniqueIdentifier();
    LoggingContext.put(ImexExecutionReportService.IDENTIFIER_KEY, identifierStr);
    try {
        if (imexCoreService.tryLock()) {
            reportService.getSeparator(_log);
            if (bundleNames != null && bundleNames.size() > 0) {
                reportService.getStartMessage(_log, "PARTIAL import process for [" + bundleNames.toString() + "]");
            } else {
                reportService.getStartMessage(_log, "ALL import process");
            }
            try {
                if (debug) {
                    reportService.getMessage(_log, "Running DEBUG mode ...");
                }
                Map<String, ServiceReference<Importer>> importers = trackerService.getFilteredImporters(bundleNames);
                if (importers == null || importers.size() == 0) {
                    reportService.getMessage(_log, "There is no importers to execute. Please check : ");
                    reportService.getMessage(_log, "- All importers are correctly registered in OSGI container");
                    if (bundleNames != null) {
                        reportService.getMessage(_log, "- A registered bundle exists for each typed name [" + bundleNames + "]");
                    }
                    reportService.printKeys(trackerService.getImporters(), _log);
                } else {
                    // Archive actual files before importing
                    ImexProperties coreConfig = new ImexProperties();
                    configurationService.loadCoreConfiguration(coreConfig);
                    reportService.displayConfigurationLoadingInformation(coreConfig, _log);
                    imexArchiverService.archiveDataDirectory(coreConfig.getProperties(), identifier);
                    File importDir = getImportDirectory();
                    reportService.getPropertyMessage(_log, "IMEX import path", importDir.toString());
                    List<Company> companies = companyLocalService.getCompanies();
                    // For each Liferay company
                    for (Company company : companies) {
                        long companyId = company.getCompanyId();
                        String companyName = company.getName();
                        File companyDir = getCompanyImportDirectory(importDir, company);
                        if (companyDir != null) {
                            executeRegisteredImporters(importers, companyDir, companyId, companyName, profileId, debug);
                        }
                    }
                }
            } catch (Exception e) {
                reportService.getError(_log, "doImport", e);
            }
            reportService.getEndMessage(_log, "import process");
        } else {
            reportService.getMessage(_log, "##");
            reportService.getMessage(_log, "## " + ImexCoreService.LOCKED_MESSAGE);
            reportService.getMessage(_log, "##");
        }
    } finally {
        imexCoreService.releaseLock();
    }
    return identifierStr;
}
Also used : Company(com.liferay.portal.kernel.model.Company) ProcessIdentifierGenerator(com.liferay.imex.core.api.identifier.ProcessIdentifierGenerator) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) File(java.io.File) ImporterProcessIdentifier(com.liferay.imex.core.service.importer.model.ImporterProcessIdentifier) ImexException(com.liferay.imex.core.util.exception.ImexException) PortalException(com.liferay.portal.kernel.exception.PortalException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ImexProperties (com.liferay.imex.core.api.configuration.model.ImexProperties)14 Properties (java.util.Properties)11 ServiceReference (org.osgi.framework.ServiceReference)8 Bundle (org.osgi.framework.Bundle)7 File (java.io.File)6 Map (java.util.Map)6 ProcessIdentifierGenerator (com.liferay.imex.core.api.identifier.ProcessIdentifierGenerator)4 ImexException (com.liferay.imex.core.util.exception.ImexException)4 PortalException (com.liferay.portal.kernel.exception.PortalException)4 Company (com.liferay.portal.kernel.model.Company)4 Exporter (com.liferay.imex.core.api.exporter.Exporter)3 Importer (com.liferay.imex.core.api.importer.Importer)3 OrderedProperties (com.liferay.imex.core.api.configuration.model.OrderedProperties)2 ExporterProcessIdentifierGenerator (com.liferay.imex.core.service.exporter.model.ExporterProcessIdentifierGenerator)2 User (com.liferay.portal.kernel.model.User)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ExporterRawContent (com.liferay.imex.core.api.exporter.model.ExporterRawContent)1 Trigger (com.liferay.imex.core.api.trigger.Trigger)1