Search in sources :

Example 1 with IOAdvisor

use of eu.esdihumboldt.hale.common.core.io.IOAdvisor in project hale by halestudio.

the class ActionProjectFile method apply.

/**
 * @see ProjectFile#apply()
 */
@SuppressWarnings("unchecked")
@Override
public void apply() {
    if (applyFile == null) {
        return;
    }
    try {
        // load the temporary file using an ImportProvider
        LocatableInputSupplier<? extends InputStream> tmpIn = new FileIOSupplier(applyFile);
        // get the action
        IOAction action = IOActionExtension.getInstance().get(loadActionId);
        checkState(ImportProvider.class.isAssignableFrom(action.getProviderType()), "Load action not compatible to ImportProvider");
        // try specified provider
        ImportProvider provider = null;
        if (loadProviderId != null) {
            try {
                provider = (ImportProvider) HaleIO.createIOProvider(action.getProviderType(), null, loadProviderId);
            } catch (Exception e) {
                // ignore
                log.error("Could not get specified import provider, trying auto-detection instead", e);
            }
        }
        // find provider if necessary
        if (provider == null) {
            provider = (ImportProvider) HaleIO.findIOProvider(action.getProviderType(), tmpIn, null);
        }
        if (provider == null) {
            throw new IllegalStateException("No provider for loading project file found (Action ID " + action.getId() + ")");
        }
        // find advisor
        @SuppressWarnings("rawtypes") IOAdvisor advisor = getLoadAdvisor(loadActionId, serviceProvider);
        checkState(advisor != null, "No advisor for loading project file found");
        // configure provider
        // set given parameters
        setParameters(provider, loadParameters);
        // set source
        provider.setSource(tmpIn);
        // execute the provider
        executeProvider(provider, advisor);
    } catch (Exception e) {
        // project file apply fails currently are one logged (the project
        // reader is not involved with it)
        log.error("Error applying loaded project file", e);
    } finally {
        if (!applyFile.delete()) {
            applyFile.deleteOnExit();
            applyFile = null;
        }
    }
}
Also used : IOAdvisor(eu.esdihumboldt.hale.common.core.io.IOAdvisor) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) IOException(java.io.IOException)

Example 2 with IOAdvisor

use of eu.esdihumboldt.hale.common.core.io.IOAdvisor in project hale by halestudio.

the class HeadlessIO method executeConfiguration.

/**
 * Execute a single I/O configuration. If no matching advisor is given for
 * the configuration, first the extension point is queried for an advisor,
 * if not found it is ignored.
 *
 * @param conf the I/O configuration
 * @param advisors map of advisors, action ID mapped to responsible advisor
 * @param reportHandler the report handler, may be <code>null</code>
 * @param serviceProvider the service provider
 * @throws IOException if an error occurs executing a configuration
 */
public static void executeConfiguration(IOConfiguration conf, Map<String, IOAdvisor<?>> advisors, ReportHandler reportHandler, ServiceProvider serviceProvider) throws IOException {
    // get advisor ...
    final String actionId = conf.getActionId();
    IOAdvisor<?> advisor = advisors.get(actionId);
    if (advisor == null) {
        // try to find registered advisor for action (e.g. lookup)
        List<IOAdvisorFactory> regAdvisors = IOAdvisorExtension.getInstance().getFactories(new FactoryFilter<IOAdvisor<?>, IOAdvisorFactory>() {

            @Override
            public boolean acceptFactory(IOAdvisorFactory factory) {
                return factory.getActionID().equals(actionId);
            }

            @Override
            public boolean acceptCollection(ExtensionObjectFactoryCollection<IOAdvisor<?>, IOAdvisorFactory> collection) {
                return true;
            }
        });
        if (regAdvisors != null && !regAdvisors.isEmpty()) {
            try {
                advisor = regAdvisors.get(0).createAdvisor(actionId, serviceProvider);
                log.info(MessageFormat.format("No advisor for action {0} given, using advisor registered through extension point.", actionId));
            } catch (Exception e) {
                log.error(MessageFormat.format("Failed to load registered advisor for action {0}.", actionId));
                if (advisor != null) {
                    // shouldn't happen, but seems to happen anyway
                    log.error("Advisor is set in spite of error", e);
                }
            }
        }
    }
    if (advisor == null) {
        log.info(MessageFormat.format("No advisor for action {0} given, I/O configuration is ignored.", actionId));
        // ignore this configuration
        return;
    }
    // ... and provider
    IOProvider provider = loadProvider(conf);
    if (provider != null) {
        if (serviceProvider != null) {
            // set service provider, just in case the advisor does not do it
            provider.setServiceProvider(serviceProvider);
        }
        // execute provider
        executeProvider(provider, advisor, null, reportHandler);
    // XXX progress?!!
    } else {
        throw new IOException(MessageFormat.format("Could not execute I/O configuration, provider with ID {0} not found.", conf.getProviderId()));
    }
}
Also used : IOAdvisorFactory(eu.esdihumboldt.hale.common.core.io.extension.IOAdvisorFactory) IOAdvisor(eu.esdihumboldt.hale.common.core.io.IOAdvisor) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with IOAdvisor

use of eu.esdihumboldt.hale.common.core.io.IOAdvisor in project hale by halestudio.

the class ActionProjectFile method store.

/**
 * @see ProjectFile#store(LocatableOutputSupplier)
 */
@SuppressWarnings("unchecked")
@Override
public void store(final LocatableOutputSupplier<OutputStream> target) throws Exception {
    // get the action
    IOAction action = IOActionExtension.getInstance().get(saveActionId);
    checkState(ExportProvider.class.isAssignableFrom(action.getProviderType()), "Save action not compatible to ExportProvider");
    // get specified provider
    ExportProvider provider = (ExportProvider) HaleIO.createIOProvider(action.getProviderType(), null, saveProviderId);
    if (provider == null) {
        throw new IllegalStateException("No provider for saving project file found");
    }
    // find advisor
    @SuppressWarnings("rawtypes") IOAdvisor advisor = advisorRegister.findAdvisor(saveActionId, serviceProvider);
    checkState(advisor != null, "No advisor for saving project file found");
    // configure provider
    // set given parameters
    setParameters(provider, saveParameters);
    // set target
    provider.setTarget(target);
    // execute the provider -> error is propagated outside (so project
    // writer knows of it)
    executeProvider(provider, advisor);
}
Also used : IOAdvisor(eu.esdihumboldt.hale.common.core.io.IOAdvisor) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) ExportProvider(eu.esdihumboldt.hale.common.core.io.ExportProvider)

Example 4 with IOAdvisor

use of eu.esdihumboldt.hale.common.core.io.IOAdvisor in project hale by halestudio.

the class ProjectResourcesUtil method executeConfiguration.

/**
 * Execute a single I/O configuration with a custom advisor.
 *
 * @param conf the I/O configuration
 * @param customAdvisor the custom advisor to use or <code>null</code>
 * @param publishReport if the report should be published
 * @param cacheCallback call back that is notified on cache changes for the
 *            I/O configuration, may be <code>null</code>
 */
public static void executeConfiguration(IOConfiguration conf, IOAdvisor<?> customAdvisor, boolean publishReport, CacheCallback cacheCallback) {
    // get provider ...
    IOProvider provider = null;
    IOProviderDescriptor descriptor = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
    if (descriptor != null) {
        try {
            provider = descriptor.createExtensionObject();
        } catch (Exception e) {
            log.error(MessageFormat.format("Could not execute I/O configuration, provider with ID {0} could not be created.", conf.getProviderId()), e);
            return;
        }
        // ... and advisor
        final String actionId = conf.getActionId();
        IOAdvisor<?> advisor = customAdvisor;
        if (advisor == null) {
            List<IOAdvisorFactory> advisors = IOAdvisorExtension.getInstance().getFactories(new FactoryFilter<IOAdvisor<?>, IOAdvisorFactory>() {

                @Override
                public boolean acceptFactory(IOAdvisorFactory factory) {
                    return factory.getActionID().equals(actionId);
                }

                @Override
                public boolean acceptCollection(ExtensionObjectFactoryCollection<IOAdvisor<?>, IOAdvisorFactory> collection) {
                    return true;
                }
            });
            if (advisors != null && !advisors.isEmpty()) {
                try {
                    advisor = advisors.get(0).createAdvisor(actionId, HaleUI.getServiceProvider());
                } catch (Exception e) {
                    log.error(MessageFormat.format("Could not execute I/O configuration, advisor with ID {0} could not be created.", advisors.get(0).getIdentifier()), e);
                    return;
                }
            }
        }
        if (advisor != null) {
            // configure settings
            provider.loadConfiguration(conf.getProviderConfiguration());
            if (provider instanceof CachingImportProvider) {
                ((CachingImportProvider) provider).setCache(conf.getCache());
            }
            // execute provider
            executeProvider(provider, advisor, publishReport, cacheCallback);
        } else {
            log.error(MessageFormat.format("Could not execute I/O configuration, no advisor for action {0} found.", actionId));
        }
    } else {
        log.error(MessageFormat.format("Could not execute I/O configuration, provider with ID {0} not found.", conf.getProviderId()));
    }
}
Also used : IOAdvisorFactory(eu.esdihumboldt.hale.common.core.io.extension.IOAdvisorFactory) IOAdvisor(eu.esdihumboldt.hale.common.core.io.IOAdvisor) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

IOAdvisor (eu.esdihumboldt.hale.common.core.io.IOAdvisor)4 IOAction (eu.esdihumboldt.hale.common.core.io.IOAction)2 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)2 IOAdvisorFactory (eu.esdihumboldt.hale.common.core.io.extension.IOAdvisorFactory)2 IOException (java.io.IOException)2 CachingImportProvider (eu.esdihumboldt.hale.common.core.io.CachingImportProvider)1 ExportProvider (eu.esdihumboldt.hale.common.core.io.ExportProvider)1 ImportProvider (eu.esdihumboldt.hale.common.core.io.ImportProvider)1 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1