blob: dfb9b0f73ce88266b0fb736b654220b40cc1a4ba [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.common.state;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osbp.runtime.common.coordination.CoordinationManager;
import org.osgi.service.coordinator.Coordination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class SharedStateUnitOfWork.
*
* @param <R>
* the generic type
*/
public abstract class SharedStateUnitOfWork<R> {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(SharedStateUnitOfWork.class);
/**
* Instantiates a new shared state unit of work.
*/
public SharedStateUnitOfWork() {
}
/**
* Executes the unit of work creating an implicit {@link Coordination} and
* passing the env as variables.
*
* @param context
* the context
* @return the data returned by {@link #doExecute()}
*/
public R execute(final ISharedStateContext context) {
if (context != null) {
Coordination peek = null;
CoordinationManager coordinationManager = new CoordinationManager();
try {
Map<Class<?>, Object> props = new HashMap<Class<?>, Object>();
props.put(ISharedStateContext.class, context);
peek = coordinationManager.createCurrentCoordination(props);
if (peek != null) {
return doExecute();
} else {
LOGGER.error("No coordination service available!");
}
} finally {
if (peek != null) {
peek.end();
}
// release the coordination manager
coordinationManager.release();
}
} else {
return doExecute();
}
return null;
}
/**
* The unit of work to do.
*
* @return any kind of data. May also be <code>null</code>.
*/
protected abstract R doExecute();
}