2017-05-17 16:45:38 +02:00
package org.gcube.data.transfer.service.transfers.engine.impl ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.ServiceLoader ;
import javax.inject.Singleton ;
2017-05-25 15:55:49 +02:00
import org.gcube.data.transfer.model.ExecutionReport ;
2017-05-17 16:45:38 +02:00
import org.gcube.data.transfer.model.PluginDescription ;
import org.gcube.data.transfer.model.PluginInvocation ;
import org.gcube.data.transfer.plugin.AbstractPlugin ;
import org.gcube.data.transfer.plugin.AbstractPluginFactory ;
import org.gcube.data.transfer.plugin.fails.PluginException ;
2017-12-05 18:18:13 +01:00
import org.gcube.data.transfer.plugin.fails.PluginExecutionException ;
2017-06-30 15:25:58 +02:00
import org.gcube.data.transfer.plugin.model.DataTransferContext ;
2017-05-17 16:45:38 +02:00
import org.gcube.data.transfer.service.transfers.engine.PluginManager ;
import org.gcube.data.transfer.service.transfers.engine.faults.PluginNotFoundException ;
2017-06-30 15:25:58 +02:00
import org.gcube.smartgears.ContextProvider ;
2017-05-17 16:45:38 +02:00
import lombok.Synchronized ;
import lombok.extern.slf4j.Slf4j ;
@Slf4j
public class PluginManagerImpl implements PluginManager {
private static ServiceLoader < AbstractPluginFactory > abstractFactoryLoader = null ;
private static Map < String , PluginDescription > installedPlugins = null ;
2017-12-13 15:04:13 +01:00
private static PluginManagerImpl instance = null ;
public static synchronized PluginManagerImpl get ( ) {
if ( instance = = null )
instance = new PluginManagerImpl ( ) ;
return instance ;
}
// INSTANCE
2017-12-07 17:51:19 +01:00
public PluginManagerImpl ( ) {
2017-12-11 15:38:51 +01:00
load ( ) ;
2017-12-07 17:51:19 +01:00
}
2017-05-17 16:45:38 +02:00
@Override
public Map < String , PluginDescription > getInstalledPlugins ( ) {
2017-12-07 17:51:19 +01:00
return installedPlugins ;
2017-05-17 16:45:38 +02:00
}
@Synchronized
2017-12-11 15:38:51 +01:00
private static Map < String , PluginDescription > load ( ) {
2017-05-17 16:45:38 +02:00
if ( installedPlugins = = null ) {
Map < String , PluginDescription > toSet = new HashMap < String , PluginDescription > ( ) ;
log . trace ( " Loading plugins descriptors.. " ) ;
2017-10-30 15:26:08 +01:00
abstractFactoryLoader = ServiceLoader . load ( AbstractPluginFactory . class ) ;
2017-05-17 16:45:38 +02:00
for ( AbstractPluginFactory factory : abstractFactoryLoader ) {
2017-12-11 15:38:51 +01:00
toSet . put ( factory . getID ( ) , new PluginDescription ( factory . getID ( ) , factory . getDescription ( ) , factory . getParameters ( ) ) ) ;
2017-05-17 16:45:38 +02:00
}
installedPlugins = toSet ;
}
return installedPlugins ;
}
2017-12-11 15:38:51 +01:00
@Override
public void initPlugins ( ) {
for ( AbstractPluginFactory factory : abstractFactoryLoader ) {
log . debug ( " Initializing {}, under {} " , factory . getID ( ) , TokenUtils . getCurrentScope ( ) ) ;
try {
factory . init ( new DataTransferContext ( ContextProvider . get ( ) ) ) ;
} catch ( Throwable e ) {
log . warn ( " Unable to initialize plugin {} " , factory . getID ( ) , e ) ;
}
}
}
2017-05-17 16:45:38 +02:00
@Override
2017-05-29 17:45:29 +02:00
public ExecutionReport execute ( PluginInvocation invocation , String transferredFile ) throws PluginException , PluginNotFoundException {
2017-05-17 16:45:38 +02:00
log . debug ( " Executing invocation {} " , invocation ) ;
2017-05-26 12:32:46 +02:00
if ( ! getInstalledPlugins ( ) . containsKey ( invocation . getPluginId ( ) ) ) throw new PluginNotFoundException ( " Plugin with ID " + invocation . getPluginId ( ) + " is not available. " ) ;
2017-05-17 16:45:38 +02:00
AbstractPluginFactory factory = getFactory ( invocation . getPluginId ( ) ) ;
log . debug ( " Loaded factory {} " , factory . getClass ( ) ) ;
2017-05-26 12:32:46 +02:00
AbstractPlugin plugin = null ;
2017-05-17 16:45:38 +02:00
try {
2017-05-26 12:32:46 +02:00
log . debug ( " Checking invocation {} " , invocation ) ;
2017-05-29 17:45:29 +02:00
PluginInvocation modifiedInvocation = factory . checkInvocation ( invocation , transferredFile ) ;
plugin = factory . createWorker ( modifiedInvocation ) ;
2017-05-26 16:46:30 +02:00
ExecutionReport report = plugin . execute ( ) ;
log . debug ( " Plugin execution report is {} " , report ) ;
switch ( report . getFlag ( ) ) {
case FAILED_EXECUTION :
case WRONG_PARAMETER :
case UNABLE_TO_EXECUTE : throw new PluginException ( " Wrong status after plugin execution. Report is " + report ) ;
case FAILED_CLEANUP : log . warn ( " Plugin failed to clean up. " ) ;
case SUCCESS :
}
return report ;
2017-05-17 16:45:38 +02:00
} catch ( PluginException e ) {
2017-05-19 18:03:11 +02:00
log . error ( " Unable to execute plguin invocation {} " , invocation , e ) ;
2017-05-26 12:32:46 +02:00
throw e ;
2017-05-17 16:45:38 +02:00
}
}
private AbstractPluginFactory getFactory ( String pluginId ) throws PluginNotFoundException {
2017-12-05 18:18:13 +01:00
log . debug ( " Getting factory by ID {} " , pluginId ) ;
2017-05-17 16:45:38 +02:00
for ( AbstractPluginFactory factory : abstractFactoryLoader ) {
if ( factory . getID ( ) . equals ( pluginId ) ) return factory ;
}
throw new PluginNotFoundException ( " Plugin with ID " + pluginId + " not found " ) ;
}
2017-05-26 12:32:46 +02:00
@Override
public void shutdown ( ) {
log . trace ( " Shutting down plugins.. " ) ;
for ( PluginDescription desc : getInstalledPlugins ( ) . values ( ) ) {
try {
AbstractPluginFactory factory = getFactory ( desc . getId ( ) ) ;
log . debug ( " Shutting down {} " , desc . getId ( ) ) ;
factory . shutDown ( ) ;
} catch ( Throwable e ) {
log . warn ( " Unexpected error while shutting down {} " , desc . getId ( ) , e ) ;
}
}
installedPlugins = null ;
}
2017-12-05 18:18:13 +01:00
@Override
public Object getPluginInfo ( String pluginID ) throws PluginNotFoundException , PluginExecutionException {
2017-12-07 17:51:19 +01:00
Object toReturn = getFactory ( pluginID ) . getInfo ( ) ;
log . trace ( " Serving plugin {} info {} " , pluginID , toReturn ) ;
return toReturn ;
2017-12-05 18:18:13 +01:00
}
2017-05-17 16:45:38 +02:00
}