| /** |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.dsl.dto.lib.impl; |
| |
| import java.util.Collection; |
| |
| import org.eclipse.osbp.runtime.common.filter.IDTOService; |
| import org.osgi.framework.Bundle; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.FrameworkUtil; |
| import org.osgi.framework.InvalidSyntaxException; |
| import org.osgi.framework.ServiceReference; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * Is responsible to return mappers for the given types. |
| */ |
| public class DtoServiceAccess { |
| |
| /** The Constant LOGGER. */ |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(DtoServiceAccess.class); |
| |
| /** |
| * Returns a proper mapper for the dto. |
| * |
| * @param <D> |
| * the generic type |
| * @param dto |
| * the dto |
| * @return the service |
| */ |
| public static <D> IDTOService<D> getService(Class<D> dto) { |
| return getService(dto.getCanonicalName()); |
| } |
| |
| /** |
| * Returns a proper mapper for the dto. |
| * |
| * @param <D> |
| * the generic type |
| * @param dtoName |
| * the dto name |
| * @return the service |
| */ |
| @SuppressWarnings({ "unchecked", "rawtypes" }) |
| public static <D> IDTOService<D> getService(String dtoName) { |
| Bundle bundle = FrameworkUtil.getBundle(DtoServiceAccess.class); |
| |
| String filterString = String.format("(&(objectClass=%s)(dto=%s))", |
| IDTOService.class.getCanonicalName(), dtoName); |
| try { |
| BundleContext context = bundle.getBundleContext(); |
| Collection<ServiceReference<IDTOService>> references = context |
| .getServiceReferences(IDTOService.class, filterString); |
| if (!references.isEmpty()) { |
| ServiceReference<IDTOService> reference = references.iterator() |
| .next(); |
| IDTOService<D> service = context.getService(reference); |
| return service; |
| } |
| } catch (InvalidSyntaxException e) { |
| LOGGER.error("{}", e); |
| } |
| |
| LOGGER.error("No dtoService available for dto: {}", dtoName); |
| |
| return null; |
| } |
| } |