diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/IOperationListener.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/IOperationListener.java index 8959f297e09adf9fd0f61e5ff097ee3b3368522d..132d557ad6e051978f11d9bbcafebeafd98bf752 100644 --- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/IOperationListener.java +++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/IOperationListener.java @@ -5,10 +5,15 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.operation.IOperationResul import ch.ethz.sis.openbis.generic.server.asapi.v3.IApplicationServerInternalApi; import ch.systemsx.cisd.openbis.generic.shared.dto.Session; +import java.util.Properties; -public interface IOperationListener<OPERATION extends IOperation, RESULT extends IOperationResult> { - public static final String LISTENER_PROPERTY_KEY = "operation-listener"; +public interface IOperationListener<OPERATION extends IOperation, RESULT extends IOperationResult> +{ + public static final String LISTENER_PROPERTY_KEY = "operation-listener"; + public static final String LISTENER_CLASS_KEY = LISTENER_PROPERTY_KEY + ".class"; + public abstract void setup(Properties properties); public abstract void beforeOperation(IApplicationServerInternalApi api, Session session, OPERATION operation); - public abstract void afterOperation(IApplicationServerInternalApi api, Session session, OPERATION operation, RESULT result, RuntimeException runtimeException); + public abstract void afterOperation(IApplicationServerInternalApi api, Session session, OPERATION operation, + RESULT result, RuntimeException runtimeException); } diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/OperationListenerExample.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/OperationListenerExample.java new file mode 100644 index 0000000000000000000000000000000000000000..f979c3698228baff0becc0c5f225853cfe4ebb6c --- /dev/null +++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/OperationListenerExample.java @@ -0,0 +1,37 @@ +package ch.ethz.sis.openbis.generic.server.asapi.v3.executor.operation; + +import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.operation.IOperation; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.operation.IOperationResult; +import ch.ethz.sis.openbis.generic.server.asapi.v3.IApplicationServerInternalApi; +import ch.systemsx.cisd.common.logging.LogCategory; +import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.openbis.generic.shared.dto.Session; +import org.apache.log4j.Logger; + +import java.util.Properties; + +public class OperationListenerExample implements IOperationListener<IOperation, IOperationResult> +{ + + private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, + OperationListenerExample.class); + + @Override + public void setup(Properties properties) + { + + } + + @Override + public void beforeOperation(IApplicationServerInternalApi api, Session session, IOperation operation) + { + operationLog.info("beforeOperation: " + operation.getClass().getSimpleName()); + } + + @Override + public void afterOperation(IApplicationServerInternalApi api, Session session, IOperation operation, + IOperationResult result, RuntimeException runtimeException) + { + operationLog.info("afterOperation: " + operation.getClass().getSimpleName()); + } +} diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/OperationListenerLoader.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/OperationListenerLoader.java new file mode 100644 index 0000000000000000000000000000000000000000..b99c8bdfc814b491a94189127d8455c5a09f6e0e --- /dev/null +++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/operation/OperationListenerLoader.java @@ -0,0 +1,72 @@ +package ch.ethz.sis.openbis.generic.server.asapi.v3.executor.operation; + +import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.operation.IOperation; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.operation.IOperationResult; +import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.common.OperationExecutor; +import ch.systemsx.cisd.common.logging.LogCategory; +import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.common.properties.PropertyParametersUtil; +import ch.systemsx.cisd.common.spring.ExposablePropertyPlaceholderConfigurer; +import ch.systemsx.cisd.openbis.generic.server.CommonServiceProvider; +import org.apache.log4j.Logger; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import javax.annotation.Resource; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import static ch.ethz.sis.openbis.generic.server.asapi.v3.executor.operation.IOperationListener.LISTENER_CLASS_KEY; + +public class OperationListenerLoader implements ApplicationContextAware, InitializingBean, + DisposableBean +{ + private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, OperationListenerLoader.class); + + @Resource(name = ExposablePropertyPlaceholderConfigurer.PROPERTY_CONFIGURER_BEAN_NAME) + private ExposablePropertyPlaceholderConfigurer configurer; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + CommonServiceProvider.setApplicationContext(applicationContext); + } + + @Override + public void afterPropertiesSet() throws Exception + { + try { + List<IOperationListener<IOperation, IOperationResult>> operationListeners = new ArrayList<>(); + Properties properties = configurer.getResolvedProps(); + PropertyParametersUtil.SectionProperties[] operationListenersDefinitions = + PropertyParametersUtil.extractSectionProperties(properties, IOperationListener.LISTENER_PROPERTY_KEY, false); + for (PropertyParametersUtil.SectionProperties sectionProperty:operationListenersDefinitions) + { + String key = sectionProperty.getKey(); + String operationListenerClassName = sectionProperty.getProperties().getProperty(LISTENER_CLASS_KEY); + operationLog.info("Adding: " + key + " Class: " + operationListenerClassName); + Class<?> operationListenerClass = Class.forName(operationListenerClassName); + Constructor<?> operationListenerConstructor = operationListenerClass.getConstructor(); + IOperationListener<IOperation, IOperationResult> operationListener = (IOperationListener) operationListenerConstructor.newInstance(); + operationListeners.add(operationListener); + operationLog.info("Added: " + key + " Class: " + operationListenerClassName); + } + OperationExecutor.setOperationListeners(operationListeners); + operationLog.info("Operation Listeners Set"); + } catch (Exception ex) + { + operationLog.info("Failed to load the configured api listeners.", ex); + } + } + + @Override + public void destroy() throws Exception + { + + } +} diff --git a/openbis/source/java/genericApplicationContext.xml b/openbis/source/java/genericApplicationContext.xml index 32abb636dfb9dd54e0af2563c96eec44ecb48896..6a7adab2ee7ce9396ec817f38589732bdcf8c123 100644 --- a/openbis/source/java/genericApplicationContext.xml +++ b/openbis/source/java/genericApplicationContext.xml @@ -547,6 +547,9 @@ class="ch.systemsx.cisd.openbis.generic.server.MaintenanceTaskStarter"> </bean> + <bean id="operation-listener-loader" + class="ch.ethz.sis.openbis.generic.server.asapi.v3.executor.operation.OperationListenerLoader"> + </bean> <!-- Core plugins registration -->