/**
 * Copyright (c) 2010-2014, openHAB.org and others.
 *
 * 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
 */
package org.openhab.core.transform;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransformationHelper {

	private static Logger logger = LoggerFactory.getLogger(TransformationHelper.class);

	private static Map<String, TransformationService> cachedServices = new ConcurrentHashMap<String, TransformationService>();
	
	/**
	 * Queries the OSGi service registry for a service that provides a transformation service of
	 * a given transformation type (e.g. REGEX, XSLT, etc.)
	 * 
	 * @param transformationType the desired transformation type
	 * @return a service instance or null, if none could be found
	 */
	static public TransformationService getTransformationService(BundleContext context, String transformationType) {
		
		if (StringUtils.isBlank(transformationType)) {
			return null;
		}
		
		if (cachedServices.containsKey(transformationType)) {
			return cachedServices.get(transformationType);
		}
		
		if(context!=null) {
			String filter = "(openhab.transform=" + transformationType + ")";
			try {
				Collection<ServiceReference<TransformationService>> refs = context.getServiceReferences(TransformationService.class, filter);
				if(refs!=null && refs.size() > 0) {
					TransformationService service = (TransformationService) context.getService(refs.iterator().next());
					cachedServices.put(transformationType, service);
					return service;
				} else {
					logger.warn("Cannot get service reference for transformation service of type " + transformationType);
				}
			} catch (InvalidSyntaxException e) {
				logger.warn("Cannot get service reference for transformation service of type " + transformationType, e);
			}
		}
		return null;
	}

}

@@ -372,121 +372,8 @@
 on;%0A
-import java.util.Map;%0Aimport java.util.concurrent.ConcurrentHashMap;%0A%0Aimport org.apache.commons.lang.StringUtils;
 %0Aimp
@@ -689,134 +689,8 @@
 );%0A%0A
-%09private static Map%3CString, TransformationService%3E cachedServices = new ConcurrentHashMap%3CString, TransformationService%3E();%0A%09%0A
 %09/**
@@ -1094,196 +1094,8 @@
 ) %7B%0A
-%09%09%0A%09%09if (StringUtils.isBlank(transformationType)) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09%0A%09%09if (cachedServices.containsKey(transformationType)) %7B%0A%09%09%09return cachedServices.get(transformationType);%0A%09%09%7D%0A%09%09%0A
 %09%09if
@@ -1369,39 +1369,14 @@
 %09%09%09%09
-TransformationService service =
+return
  (Tr
@@ -1444,83 +1444,8 @@
 ));%0A
-%09%09%09%09%09cachedServices.put(transformationType, service);%0A%09%09%09%09%09return service;%0A
 %09%09%09%09

rollback of transformationhelper change