| /** |
| * |
| * 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.io.IOException; |
| import java.net.URL; |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EPackage; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
| import org.eclipse.osbp.infogrid.api.IGridSourceCache; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSource; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSourceInput; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSourcePackage; |
| 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; |
| |
| @Component(immediate = true) |
| public class GridSourceCache implements IGridSourceCache { |
| |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(GridSourceCache.class); |
| |
| private ResourceSet resourceSet; |
| private static final String URI_PATTERN = "local://%s"; |
| |
| @Activate |
| protected void activate(ComponentContext context) { |
| resourceSet = new ResourceSetImpl(); |
| |
| // do EMF registration |
| EPackage.Registry.INSTANCE.put( |
| CxGridSourcePackage.eINSTANCE.getNsURI(), |
| CxGridSourcePackage.eINSTANCE); |
| if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap() |
| .containsKey("gridsource_xmi")) { |
| Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put( |
| "gridsource_xmi", new XMIResourceFactoryImpl()); |
| } |
| |
| } |
| |
| @Deactivate |
| protected void deactivate(ComponentContext context) { |
| resourceSet.getResources().clear(); |
| resourceSet = null; |
| } |
| |
| @Override |
| public List<CxGridSource> getAllSources() { |
| List<CxGridSource> result = new ArrayList<>(); |
| for (Resource resource : new ArrayList<>(resourceSet.getResources())) { |
| if (resource.getContents().isEmpty()) { |
| continue; |
| } |
| CxGridSource source = (CxGridSource) resource.getContents().get(0); |
| if (source == null) { |
| continue; |
| } |
| |
| result.add(EcoreUtil.copy(source)); |
| } |
| return result; |
| } |
| |
| @Override |
| public CxGridSource getSource(String id) { |
| if (id == null) { |
| return null; |
| } |
| CxGridSource cxSource = resourceSet.getResources().stream() |
| .filter(r -> r.getContents().size() > 0) |
| .map(r -> (CxGridSource) r.getContents().get(0)).filter(g -> { |
| return id.equals(g.getId()); |
| }).findFirst().orElse(null); |
| |
| return cxSource != null ? EcoreUtil.copy(cxSource) : null; |
| } |
| |
| @Override |
| public List<CxGridSource> getSources(Class<?> inputType) { |
| if (inputType == null) { |
| return Collections.emptyList(); |
| } |
| |
| List<CxGridSource> result = new ArrayList<>(); |
| for (Resource resource : new ArrayList<>(resourceSet.getResources())) { |
| if (resource.getContents().isEmpty()) { |
| continue; |
| } |
| CxGridSource source = (CxGridSource) resource.getContents().get(0); |
| if (source == null) { |
| continue; |
| } |
| |
| if (isFor(source, inputType)) { |
| result.add(EcoreUtil.copy(source)); |
| } |
| } |
| |
| return result; |
| } |
| |
| private boolean isFor(CxGridSource source, Class<?> inputType) { |
| for (CxGridSourceInput input : source.getInputs()) { |
| if (input.getInputTypeFQN() != null |
| && inputType.getCanonicalName().equals( |
| input.getInputTypeFQN())) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| @Override |
| public void registerSources(List<URL> urls) { |
| registerModels(urls); |
| } |
| |
| @Override |
| public void unregisterSources(List<URL> urls) { |
| unregisterModels(urls); |
| } |
| |
| @Override |
| public void registerSource(CxGridSource source) { |
| if (source == null) { |
| return; |
| } |
| |
| if (source.getId() == null) { |
| LOGGER.warn("CxGridSource does not contain id. Is skipped!"); |
| return; |
| } |
| |
| Resource resource = resourceSet.getResource( |
| URI.createURI(createLocalURI(source)), false); |
| if (resource == null) { |
| resource = resourceSet.createResource(URI |
| .createURI(createLocalURI(source))); |
| resource.getContents().add(source); |
| } |
| } |
| |
| @Override |
| public void unregisterSource(CxGridSource source) { |
| if (source == null) { |
| return; |
| } |
| |
| if (source.getId() == null) { |
| LOGGER.warn("CxGridSource does not contain id. Is skipped!"); |
| return; |
| } |
| |
| Resource resource = resourceSet.getResource( |
| URI.createURI(createLocalURI(source)), false); |
| if (resource != null) { |
| resourceSet.getResources().remove(resource); |
| } |
| } |
| |
| private static String createLocalURI(CxGridSource source) { |
| return String.format(URI_PATTERN, source.getId()); |
| } |
| |
| private void registerModels(List<URL> urls) { |
| for (URL url : urls) { |
| Resource resource = resourceSet.getResource( |
| URI.createURI(url.toString()), true); |
| try { |
| resource.load(null); |
| } catch (IOException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| } |
| |
| private void unregisterModels(List<URL> urls) { |
| for (URL url : urls) { |
| Resource resource = resourceSet.getResource( |
| URI.createURI(url.toString()), false); |
| if (resource != null) { |
| resource.unload(); |
| resourceSet.getResources().remove(resource); |
| } |
| } |
| } |
| } |