use of eu.esdihumboldt.hale.common.core.io.CachingImportProvider in project hale by halestudio.
the class IOWizard method performFinish.
/**
* @see Wizard#performFinish()
*
* @return <code>true</code> if executing the I/O provider was successful
*/
@Override
public boolean performFinish() {
if (getProvider() == null) {
return false;
}
if (!applyConfiguration()) {
return false;
}
// create default report
IOReporter defReport = provider.createReporter();
// validate and execute provider
try {
// validate configuration
provider.validate();
ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
URI projectLoc = ps.getLoadLocation() == null ? null : ps.getLoadLocation();
boolean isProjectResource = false;
if (actionId != null) {
// XXX instead move project resource to action?
ActionUI factory = ActionUIExtension.getInstance().findActionUI(actionId);
isProjectResource = factory.isProjectResource();
}
// prevent loading of duplicate resources
if (isProjectResource && provider instanceof ImportProvider && !getProviderFactory().allowDuplicateResource()) {
String currentResource = ((ImportProvider) provider).getSource().getLocation().toString();
URI currentAbsolute = URI.create(currentResource);
if (projectLoc != null && !currentAbsolute.isAbsolute()) {
currentAbsolute = projectLoc.resolve(currentAbsolute);
}
for (IOConfiguration conf : ((Project) ps.getProjectInfo()).getResources()) {
Value otherResourceValue = conf.getProviderConfiguration().get(ImportProvider.PARAM_SOURCE);
if (otherResourceValue == null) {
continue;
}
String otherResource = otherResourceValue.as(String.class);
URI otherAbsolute = URI.create(otherResource);
if (projectLoc != null && !otherAbsolute.isAbsolute()) {
otherAbsolute = projectLoc.resolve(otherAbsolute);
}
String action = conf.getActionId();
// resource is already loaded into the project
if (currentAbsolute.equals(otherAbsolute) && Objects.equal(actionId, action)) {
// check if the resource is loaded with a provider that
// allows duplicates
boolean allowDuplicate = false;
IOProviderDescriptor providerFactory = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
if (providerFactory != null) {
allowDuplicate = providerFactory.allowDuplicateResource();
}
if (!allowDuplicate) {
log.userError("Resource is already loaded. Loading duplicate resources is aborted!");
return false;
}
}
}
}
// enable provider internal caching
if (isProjectResource && provider instanceof CachingImportProvider) {
((CachingImportProvider) provider).setProvideCache();
}
IOReport report = execute(provider, defReport);
if (report != null) {
// add report to report server
ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
repService.addReport(report);
// show message to user
if (report.isSuccess()) {
// let advisor handle results
try {
getContainer().run(true, false, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Completing operation...", IProgressMonitor.UNKNOWN);
try {
advisor.handleResults(getProvider());
} finally {
monitor.done();
}
}
});
} catch (InvocationTargetException e) {
log.userError("Error processing results:\n" + e.getCause().getLocalizedMessage(), e.getCause());
return false;
} catch (Exception e) {
log.userError("Error processing results:\n" + e.getLocalizedMessage(), e);
return false;
}
// add to project service if necessary
if (isProjectResource)
ps.rememberIO(actionId, getProviderFactory().getIdentifier(), provider);
return true;
} else {
// error message
log.userError(report.getSummary() + "\nPlease see the report for details.");
return false;
}
} else
return true;
} catch (IOProviderConfigurationException e) {
// user feedback
log.userError("Validation of the provider configuration failed:\n" + e.getLocalizedMessage(), e);
return false;
}
}
use of eu.esdihumboldt.hale.common.core.io.CachingImportProvider in project hale by halestudio.
the class HeadlessIO method loadProvider.
/**
* Load and configure the I/O provider specified by the given I/O
* configuration.
*
* @param conf the I/O configuration
* @return the provider or <code>null</code> if it was not found or could
* not be created
*/
public static IOProvider loadProvider(IOConfiguration conf) {
IOProvider provider = null;
IOProviderDescriptor descriptor = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
if (descriptor != null) {
try {
provider = descriptor.createExtensionObject();
// configure settings
provider.loadConfiguration(conf.getProviderConfiguration());
if (provider instanceof CachingImportProvider) {
((CachingImportProvider) provider).setCache(conf.getCache());
}
} catch (Exception e) {
log.error("Could not instantiate I/O provider.", e);
}
}
return provider;
}
use of eu.esdihumboldt.hale.common.core.io.CachingImportProvider 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()));
}
}
use of eu.esdihumboldt.hale.common.core.io.CachingImportProvider in project hale by halestudio.
the class ProjectResourcesUtil method executeProvider.
/**
* Execute the given I/O provider with the given I/O advisor.
*
* @param provider the I/O provider
* @param advisor the I/O advisor
* @param publishReport if the report should be published
* @param cacheCallback call back that is notified on cache changes for the
* I/O provider, may be <code>null</code>
* @return the future yielding the report on success
*/
public static ListenableFuture<IOReport> executeProvider(final IOProvider provider, @SuppressWarnings("rawtypes") final IOAdvisor advisor, final boolean publishReport, final CacheCallback cacheCallback) {
final SettableFuture<IOReport> result = SettableFuture.create();
IRunnableWithProgress op = new IRunnableWithProgress() {
@SuppressWarnings("unchecked")
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
if (cacheCallback != null && provider instanceof CachingImportProvider) {
// enable cache generation
((CachingImportProvider) provider).setProvideCache();
}
IOReporter reporter = provider.createReporter();
ATransaction trans = log.begin(reporter.getTaskName());
try {
// use advisor to configure provider
advisor.prepareProvider(provider);
advisor.updateConfiguration(provider);
// execute
IOReport report = provider.execute(new ProgressMonitorIndicator(monitor));
if (publishReport) {
// publish report
ReportService rs = PlatformUI.getWorkbench().getService(ReportService.class);
rs.addReport(report);
}
// handle cache update
if (cacheCallback != null && provider instanceof CachingImportProvider) {
CachingImportProvider cip = (CachingImportProvider) provider;
if (cip.isCacheUpdate()) {
Value cache = cip.getCache();
cacheCallback.update(cache);
}
}
// handle results
if (report.isSuccess()) {
advisor.handleResults(provider);
}
result.set(report);
} catch (Exception e) {
log.error("Error executing an I/O provider.", e);
result.setException(e);
} finally {
trans.end();
}
}
};
try {
ThreadProgressMonitor.runWithProgressDialog(op, provider.isCancelable());
} catch (Exception e) {
log.error("Error executing an I/O provider.", e);
result.setException(e);
}
return result;
}
use of eu.esdihumboldt.hale.common.core.io.CachingImportProvider in project hale by halestudio.
the class ProjectServiceImpl method rememberIO.
/**
* @see ProjectService#rememberIO(String, String, IOProvider)
*/
@Override
public void rememberIO(String actionId, String providerId, IOProvider provider) {
// populate an IOConfiguration from the given data
IOConfiguration conf = new IOConfiguration();
conf.setActionId(actionId);
conf.setProviderId(providerId);
provider.storeConfiguration(conf.getProviderConfiguration());
if (provider instanceof CachingImportProvider) {
conf.setCache(((CachingImportProvider) provider).getCache());
}
// add configuration to project
synchronized (this) {
main.getResources().add(conf);
}
setChanged();
notifyResourceAdded(actionId, new IOConfigurationResource(conf, projectLocation));
}
Aggregations