| /** |
| * |
| * Copyright (c) 2011, 2016 - 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: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.infogrid.services; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.osbp.infogrid.api.IGridSourceCache; |
| import org.eclipse.osbp.infogrid.api.IGridSourceDescriptor; |
| import org.eclipse.osbp.infogrid.api.IGridSourceDescriptor.ConfigCallback; |
| import org.eclipse.osbp.infogrid.api.IGridSourceDescriptorProvider; |
| import org.eclipse.osbp.infogrid.api.IGridSourceFacade; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSource; |
| import org.osgi.framework.ServiceReference; |
| 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.osgi.service.component.annotations.Reference; |
| import org.osgi.service.component.annotations.ReferenceCardinality; |
| import org.osgi.service.component.annotations.ReferencePolicy; |
| |
| @Component(immediate = true) |
| public class GridSourceFacadeImpl implements IGridSourceFacade { |
| |
| private ComponentContext context; |
| private List<ServiceReference<IGridSourceDescriptorProvider>> descriptorFactories = new ArrayList<>(); |
| |
| private IGridSourceCache cache; |
| |
| @Activate |
| protected void activate(ComponentContext context) { |
| this.context = context; |
| } |
| |
| @Deactivate |
| protected void deactivate(ComponentContext context) { |
| this.context = null; |
| } |
| |
| @Override |
| public CxGridSource getSource(String id) { |
| CxGridSource source = cache.getSource(id); |
| return source != null ? EcoreUtil.copy(source) : null; |
| } |
| |
| @Override |
| public List<CxGridSource> getSources(Class<?> inputType) { |
| List<CxGridSource> sources = cache.getSources(inputType); |
| if (sources == null) { |
| return Collections.emptyList(); |
| } |
| |
| List<CxGridSource> copies = new ArrayList<CxGridSource>(sources.size()); |
| for (CxGridSource source : sources) { |
| copies.add(EcoreUtil.copy(source)); |
| } |
| |
| return copies; |
| } |
| |
| @Override |
| public IGridSourceDescriptor getDescriptor(Class<?> inputType, String sourceId, |
| IGridSourceDescriptor.ConfigCallback callback) { |
| CxGridSource cxSource = getSource(sourceId); |
| if (cxSource == null) { |
| return null; |
| } |
| |
| return getDescriptor(inputType, cxSource, null); |
| } |
| |
| public IGridSourceDescriptor getDescriptor(Class<?> inputType, |
| CxGridSource cxSource, IGridSourceDescriptor.ConfigCallback callback) { |
| List<ServiceReference<IGridSourceDescriptorProvider>> temp = new ArrayList<>( |
| descriptorFactories); |
| |
| // try to find a descriptor provider that handles the gridSourceId |
| // directly |
| for (ServiceReference<IGridSourceDescriptorProvider> ref : temp) { |
| String prop = (String) ref |
| .getProperty(IGridSourceDescriptorProvider.PROP_KIND); |
| if (prop != null && prop.equals(cxSource.getId())) { |
| return createDescriptor(ref, inputType, cxSource, callback); |
| } |
| } |
| |
| // try to find a descriptor provider that handles the type |
| for (ServiceReference<IGridSourceDescriptorProvider> ref : temp) { |
| String prop = (String) ref |
| .getProperty(IGridSourceDescriptorProvider.PROP_KIND); |
| if (prop != null && prop.equals(cxSource.getKind())) { |
| return createDescriptor(ref, inputType, cxSource, callback); |
| } |
| } |
| |
| return null; |
| } |
| |
| private IGridSourceDescriptor createDescriptor( |
| ServiceReference<IGridSourceDescriptorProvider> ref, |
| Class<?> inputType, CxGridSource cxSource, IGridSourceDescriptor.ConfigCallback callback) { |
| IGridSourceDescriptorProvider provider = context.getBundleContext() |
| .getService(ref); |
| return provider.createDescriptor(inputType, cxSource, callback); |
| } |
| |
| @Override |
| public List<IGridSourceDescriptor> getDescriptors(Class<?> inputType, |
| IGridSourceDescriptor.ConfigCallback callback) { |
| List<CxGridSource> sources = getSources(inputType); |
| if (sources.isEmpty()) { |
| return Collections.emptyList(); |
| } |
| |
| List<IGridSourceDescriptor> result = new ArrayList<>(sources.size()); |
| for (CxGridSource source : sources) { |
| IGridSourceDescriptor descriptor = getDescriptor(inputType, source, |
| callback); |
| if (descriptor != null) { |
| result.add(descriptor); |
| } |
| } |
| |
| return result; |
| } |
| |
| @Reference(service = IGridSourceDescriptorProvider.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, unbind = "removeDescriptorProvider") |
| protected void addDescriptorProvider( |
| ServiceReference<IGridSourceDescriptorProvider> ref) { |
| descriptorFactories.add(ref); |
| } |
| |
| protected void removeDescriptorProvider( |
| ServiceReference<IGridSourceDescriptorProvider> ref) { |
| descriptorFactories.remove(ref); |
| } |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC, unbind = "unbindGridSourceCache") |
| protected void bindGridSourceCache(IGridSourceCache cache) { |
| this.cache = cache; |
| } |
| |
| protected void unbindGridSourceCache(IGridSourceCache cache) { |
| this.cache = null; |
| } |
| |
| } |