blob: 26fe324a210ec401cad1c44b9eb11b06ae6e1d1d [file] [log] [blame]
/**
* Copyright (c) 2011, 2014 - 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.vaaclipse.addons.common.state;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.osbp.runtime.common.state.ISharedStateContext;
import org.eclipse.osbp.runtime.common.state.ISharedStateContextProvider;
import org.eclipse.osbp.vaaclipse.addons.common.api.IE4Constants;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This adapter ensures, that the application id is added to the id of the
* context as prefix. <code>{applicationId}_{id}</code><br>
* The {@link IE4Constants#APPLICATION_ID} is also added to the OSGi service
* properties.
*/
public class SharedStateContextAdapter implements ISharedStateContextProvider {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(SharedStateContextAdapter.class);
/** The application instance id. */
@Inject
@Named("e4ApplicationInstanceId")
String applicationInstanceId;
/**
* Instantiates a new shared state context adapter.
*/
public SharedStateContextAdapter() {
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.common.state.ISharedStateContextProvider#getContext(java.lang.String, java.util.Map)
*/
@Override
public ISharedStateContext getContext(String id,
Map<String, Object> properties) {
ISharedStateContextProvider delegate = getDelegate();
if (delegate == null) {
LOGGER.error("ISharedStateContextProvider could not be found!");
return null;
}
// add the application id to the OSGi service properties
properties = properties != null ? properties
: new HashMap<String, Object>();
properties.put(IE4Constants.APPLICATION_ID, applicationInstanceId);
return delegate.getContext(createApplicationId(id), properties);
}
/**
* Creates the application id.
*
* @param id
* the id
* @return the string
*/
private String createApplicationId(String id) {
return applicationInstanceId + "_" + id;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.common.state.ISharedStateContextProvider#unget(org.eclipse.osbp.runtime.common.state.ISharedStateContext)
*/
@Override
public void unget(ISharedStateContext context) {
ISharedStateContextProvider delegate = getDelegate();
if (delegate == null) {
LOGGER.error("ISharedStateContextProvider could not be found!");
return;
}
delegate.unget(context);
}
/**
* Gets the delegate.
*
* @return the delegate
*/
protected ISharedStateContextProvider getDelegate() {
BundleContext context = FrameworkUtil.getBundle(getClass())
.getBundleContext();
ServiceReference<ISharedStateContextProvider> reference = context
.getServiceReference(ISharedStateContextProvider.class);
return context.getService(reference);
}
}