blob: a13164d66aade53969243bc97ad1700763969af5 [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.impl;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import org.eclipse.osbp.runtime.common.dispose.IDisposable;
import org.eclipse.osbp.runtime.common.dispose.IReferenceCountable;
import org.eclipse.osbp.runtime.common.state.ISharedStateContext;
import org.eclipse.osbp.runtime.common.state.ISharedStateContextProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class SharedStateContextProvider.
*/
@Component(property = { "service.ranking=100" }, immediate = true)
public class SharedStateContextProvider implements ISharedStateContextProvider {
/** The Constant OSGI_PROP__ENV_ID. */
private static final String OSGI_PROP__ENV_ID = "sharedState.id";
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(SharedStateContextProvider.class);
/** The component context. */
private ComponentContext componentContext;
/** The registrations. */
private Map<String, ServiceRegistration<ISharedStateContext>> registrations = new HashMap<String, ServiceRegistration<ISharedStateContext>>();
/* (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) {
ISharedStateContext context = find(id);
if (context == null) {
context = createContext(id, properties);
}
((IReferenceCountable) context).increaseUsageCount();
return context;
}
/**
* Creates the context.
*
* @param id
* the id
* @param inProperties
* the in properties
* @return the i shared state context
*/
protected ISharedStateContext createContext(String id,
Map<String, Object> inProperties) {
SharedStateContext context = new SharedStateContext(id);
// register as service
Dictionary<String, Object> properties = new Hashtable<String, Object>();
properties.put(OSGI_PROP__ENV_ID, id);
// add input properties
if (inProperties != null) {
for (Map.Entry<String, Object> entry : inProperties.entrySet()) {
properties.put(entry.getKey(), entry.getValue());
}
}
ServiceRegistration<ISharedStateContext> reg = componentContext
.getBundleContext().registerService(ISharedStateContext.class,
context, properties);
registrations.put(id, reg);
return context;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.common.state.ISharedStateContextProvider#unget(org.eclipse.osbp.runtime.common.state.ISharedStateContext)
*/
@Override
public void unget(ISharedStateContext context) {
IReferenceCountable countable = (IReferenceCountable) context;
countable.decreaseUsageCount();
if (countable.getUsageCount() <= 0) {
ServiceRegistration<ISharedStateContext> reg = registrations
.remove(context.getId());
if (reg != null) {
ISharedStateContext service = componentContext
.getBundleContext().getService(reg.getReference());
if (service instanceof IDisposable) {
((IDisposable) service).dispose();
}
reg.unregister();
}
}
}
/**
* Activate.
*
* @param context
* the context
*/
@Activate
protected void activate(ComponentContext context) {
this.componentContext = context;
}
/**
* Deactivate.
*
* @param context
* the context
*/
@Deactivate
protected void deactivate(ComponentContext context) {
for (ServiceRegistration<ISharedStateContext> reg : registrations
.values()) {
ISharedStateContext service = context.getBundleContext()
.getService(reg.getReference());
if (service instanceof IDisposable) {
((IDisposable) service).dispose();
}
reg.unregister();
}
this.componentContext = null;
}
/**
* Find.
*
* @param id
* the id
* @return the i shared state context
*/
protected ISharedStateContext find(String id) {
try {
BundleContext bc = componentContext.getBundleContext();
Collection<ServiceReference<ISharedStateContext>> references = bc
.getServiceReferences(ISharedStateContext.class,
createFilter(id));
if (!references.isEmpty()) {
ISharedStateContext sharedStateContext = bc
.getService(references.iterator().next());
return sharedStateContext;
}
} catch (InvalidSyntaxException e) {
LOGGER.error("{}", e);
}
return null;
}
/**
* Creates the filter.
*
* @param id
* the id
* @return the string
* @throws InvalidSyntaxException
* the invalid syntax exception
*/
protected String createFilter(String id) throws InvalidSyntaxException {
return String.format("(%s=%s)", OSGI_PROP__ENV_ID, id);
}
}