/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 * <p>
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * <p>
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.remote.cxf.common;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.CXFBusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.context.ServletContextHelper;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author Carlos Sierra Andrés
 */
public class ServicesRegistrator {

	public ServicesRegistrator(
		BundleContext bundleContext, Map<String, Object> properties) {

		_bundleContext = bundleContext;

		_properties = properties;
	}

	@SuppressWarnings("unused")
	protected void addExtension(
		Map<String, Object> properties, Object extension) {

		Class<?> extensionClass = (Class<?>) properties.get(
			"cxf.extension.class");

		if (extensionClass == null) {
			extensionClass = extension.getClass();
		}

		_extensions.put(extensionClass, extension);
	}

	@SuppressWarnings("unused")
	public void start() {
		Dictionary<String, Object> properties = new Hashtable<>();

		Object contextPathObject = _properties.get("contextPath");

		String contextPath = contextPathObject.toString();

		String contextName = contextPath.substring(1);

		contextName = contextName.replace("/", ".");

		properties.put(
			HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME,
			contextName);
		properties.put(
			HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH,
			contextPath);

		_servletContextHelperServiceRegistration =
			_bundleContext.registerService(
				ServletContextHelper.class,
				new ServletContextHelper(_bundleContext.getBundle()) {
				},
				properties);

		CXFNonSpringServlet cxfNonSpringServlet = new CXFNonSpringServlet();

		CXFBusFactory cxfBusFactory =
			(CXFBusFactory) CXFBusFactory.newInstance(
				CXFBusFactory.class.getName());

		Bus bus = cxfBusFactory.createBus(_extensions);

		properties = new Hashtable<>();

		properties.put(
			HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
			"(osgi.http.whiteboard.context.name=" + contextName + ")");
		properties.put(
			HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME,
			CXFNonSpringServlet.class.getName());
		properties.put(
			HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/*");

		cxfNonSpringServlet.setBus(bus);

		_servletServiceRegistration = _bundleContext.registerService(
			Servlet.class, cxfNonSpringServlet, properties);

		properties = new Hashtable<>();

		properties.put(
			HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH,
			contextPath);

		_busServiceRegistration = _bundleContext.registerService(
			Bus.class, bus, properties);
	}

	@SuppressWarnings("unused")
	public void stop() {
		try {
			_busServiceRegistration.unregister();
		}
		catch (Exception e) {
			if (_logger.isWarnEnabled()) {
				_logger.warn(
					"Unable to unregister CXF bus service registration " +
						_busServiceRegistration);
			}
		}

		try {
			_servletServiceRegistration.unregister();
		}
		catch (Exception e) {
			if (_logger.isWarnEnabled()) {
				_logger.warn(
					"Unable to unregister servlet service registration " +
						_servletServiceRegistration);
			}
		}

		try {
			_servletContextHelperServiceRegistration.unregister();
		}
		catch (Exception e) {
			if (_logger.isWarnEnabled()) {
				_logger.warn(
					"Unable to unregister servlet context helper service " +
						"registration " +
						_servletContextHelperServiceRegistration);
			}
		}
	}

	private static final Logger _logger = LoggerFactory.getLogger(
		ServicesRegistrator.class);

	private final BundleContext _bundleContext;
	private ServiceRegistration<Bus> _busServiceRegistration;
	private final Map<Class<?>, Object> _extensions = new HashMap<>();
	private final Map<String, Object> _properties;
	private ServiceRegistration<ServletContextHelper>
		_servletContextHelperServiceRegistration;
	private ServiceRegistration<Servlet> _servletServiceRegistration;

}

@@ -1418,363 +1418,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unused%22)%0A%09protected void addExtension(%0A%09%09Map%3CString, Object%3E properties, Object extension) %7B%0A%0A%09%09Class%3C?%3E extensionClass = (Class%3C?%3E) properties.get(%0A%09%09%09%22cxf.extension.class%22);%0A%0A%09%09if (extensionClass == null) %7B%0A%09%09%09extensionClass = extension.getClass();%0A%09%09%7D%0A%0A%09%09_extensions.put(extensionClass, extension);%0A%09%7D%0A%0A%09@SuppressWarnings(%22unused%22)%0A
 %09pub
@@ -1437,16 +1437,16 @@
 art() %7B%0A
+
 %09%09Dictio
@@ -2317,27 +2317,16 @@
 eateBus(
-_extensions
 );%0A%0A%09%09pr
@@ -3079,37 +3079,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unused%22)%0A
 %09pub
@@ -4001,16 +4001,16 @@
 ontext;%0A
+
 %09private
@@ -4064,76 +4064,8 @@
 on;%0A
-%09private final Map%3CClass%3C?%3E, Object%3E _extensions = new HashMap%3C%3E();%0A
 %09pri

Remove unused code