<commit_before>/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates
 *
 * 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:
 *     Eurotech
 *******************************************************************************/
package org.eclipse.kura.camel.router;

import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.component.bean.BeanComponent;
import org.apache.camel.component.beanclass.ClassComponent;
import org.apache.camel.component.binding.BindingNameComponent;
import org.apache.camel.component.browse.BrowseComponent;
import org.apache.camel.component.controlbus.ControlBusComponent;
import org.apache.camel.component.dataformat.DataFormatComponent;
import org.apache.camel.component.dataset.DataSetComponent;
import org.apache.camel.component.direct.DirectComponent;
import org.apache.camel.component.directvm.DirectVmComponent;
import org.apache.camel.component.file.FileComponent;
import org.apache.camel.component.language.LanguageComponent;
import org.apache.camel.component.log.LogComponent;
import org.apache.camel.component.mock.MockComponent;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.component.ref.RefComponent;
import org.apache.camel.component.rest.RestComponent;
import org.apache.camel.component.scheduler.SchedulerComponent;
import org.apache.camel.component.seda.SedaComponent;
import org.apache.camel.component.stub.StubComponent;
import org.apache.camel.component.test.TestComponent;
import org.apache.camel.component.timer.TimerComponent;
import org.apache.camel.component.validator.ValidatorComponent;
import org.apache.camel.component.vm.VmComponent;
import org.apache.camel.component.xslt.XsltComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.language.simple.SimpleLanguage;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.spi.Language;
import org.apache.camel.component.kura.KuraRouter;
import org.eclipse.kura.camel.RouterConstants;
import org.eclipse.kura.configuration.ConfigurableComponent;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: is this base class an API? If so please add Javadoc.
 * Base class for Camel routers deployable into Eclipse Kura.
 * All CamelRouters are configurable components.
 */
public abstract class CamelRouter extends KuraRouter implements ConfigurableComponent {
	private static final Logger s_logger = LoggerFactory.getLogger(CamelRouter.class);

	/**
	 * Camel route XML, usually configured using SCR property.
	 */
	protected String m_camelRouteXml;
	private Map<String, Object> m_properties;

	// ----------------------------------------------------------------
	//
	//   Activation APIs
	//
	// ----------------------------------------------------------------

	// TODO: Remove these overridden methods as soon as Camel 2.17 is out (see CAMEL-9351)
	protected void activate(ComponentContext componentContext, Map<String, Object> properties) {
		m_properties = properties;
		try {
			start(componentContext.getBundleContext());
			updated(properties); // TODO Keep this line even when Camel 2.17 is out
		} catch (Exception e) {
			s_logger.warn("Problem activating component", e);
		}
	}

	protected void deactivate(ComponentContext componentContext) {
		try {
			stop(componentContext.getBundleContext());
		} catch (Exception e) {
			s_logger.warn("Problem deactivating component", e);
		}
	}

	protected void updated(Map<String, Object> properties) {
		s_logger.debug("Refreshing SCR properties: " + properties);
		refreshCamelRouteXml(m_camelRouteXml, (String) properties.get(RouterConstants.XML_ROUTE_PROPERTY));
	}

	public void refreshCamelRouteXml(String oldCamelRouteXml, String newCamelRouteXml) {
		if (newCamelRouteXml != null && !newCamelRouteXml.isEmpty() &&
				newCamelRouteXml.contains("<route ") &&
				!newCamelRouteXml.equals(oldCamelRouteXml)) {
			this.m_camelRouteXml = newCamelRouteXml;
			if (!m_camelRouteXml.isEmpty()) {
				try {
					ByteArrayInputStream bais =  new ByteArrayInputStream(m_camelRouteXml.getBytes());
					RoutesDefinition routesDefinition = camelContext.loadRoutesDefinition(bais);
					camelContext.addRouteDefinitions(routesDefinition.getRoutes());
				} catch (Exception e) {
					s_logger.warn("Cannot load routes definitions: {}", m_camelRouteXml, e);
				}
			}
		}
	}

	// ASF Camel workarounds

	// TODO: Remove this overridden method as soon as Camel 2.17 is out (see CAMEL-9357)
	@Override
	public void configure() throws Exception {
		s_logger.debug("No programmatic routes configuration found.");
	}

	// TODO: Remove this overridden method as soon as Camel 2.17 is out (see CAMEL-9314)
	@Override
	public void start(BundleContext bundleContext) throws Exception {
		try {
			super.start(bundleContext);
		} catch (Exception e) {
			s_logger.warn("Problem when starting Kura module", e);
			throw e;
		}
	}

	@Override
	protected void beforeStart(CamelContext camelContext) {
		registerComponents();
		super.beforeStart(camelContext);
	}

	protected void modified(Map<String, Object> properties) {<commit_msg>Provide default value for XML route in Kura Camel quickstart #131<commit_after>/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates
 *
 * 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:
 *     Eurotech
 *******************************************************************************/
package org.eclipse.kura.camel.router;

import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.component.bean.BeanComponent;
import org.apache.camel.component.beanclass.ClassComponent;
import org.apache.camel.component.binding.BindingNameComponent;
import org.apache.camel.component.browse.BrowseComponent;
import org.apache.camel.component.controlbus.ControlBusComponent;
import org.apache.camel.component.dataformat.DataFormatComponent;
import org.apache.camel.component.dataset.DataSetComponent;
import org.apache.camel.component.direct.DirectComponent;
import org.apache.camel.component.directvm.DirectVmComponent;
import org.apache.camel.component.file.FileComponent;
import org.apache.camel.component.language.LanguageComponent;
import org.apache.camel.component.log.LogComponent;
import org.apache.camel.component.mock.MockComponent;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.component.ref.RefComponent;
import org.apache.camel.component.rest.RestComponent;
import org.apache.camel.component.scheduler.SchedulerComponent;
import org.apache.camel.component.seda.SedaComponent;
import org.apache.camel.component.stub.StubComponent;
import org.apache.camel.component.test.TestComponent;
import org.apache.camel.component.timer.TimerComponent;
import org.apache.camel.component.validator.ValidatorComponent;
import org.apache.camel.component.vm.VmComponent;
import org.apache.camel.component.xslt.XsltComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.language.simple.SimpleLanguage;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.spi.Language;
import org.apache.camel.component.kura.KuraRouter;
import org.eclipse.kura.camel.RouterConstants;
import org.eclipse.kura.configuration.ConfigurableComponent;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: is this base class an API? If so please add Javadoc.
 * Base class for Camel routers deployable into Eclipse Kura.
 * All CamelRouters are configurable components.
 */
public abstract class CamelRouter extends KuraRouter implements ConfigurableComponent {
	private static final Logger s_logger = LoggerFactory.getLogger(CamelRouter.class);

	/**
	 * Camel route XML, usually configured using SCR property.
	 */
	protected String m_camelRouteXml;
	private Map<String, Object> m_properties;

	// ----------------------------------------------------------------
	//
	//   Activation APIs
	//
	// ----------------------------------------------------------------

	// TODO: Remove these overridden methods as soon as Camel 2.17 is out (see CAMEL-9351)
	protected void activate(ComponentContext componentContext, Map<String, Object> properties) {
		m_properties = properties;
		try {
			start(componentContext.getBundleContext());
			updated(properties); // TODO Keep this line even when Camel 2.17 is out
		} catch (Exception e) {
			s_logger.warn("Problem activating component", e);
		}
	}

	protected void deactivate(ComponentContext componentContext) {
		try {
			stop(componentContext.getBundleContext());
		} catch (Exception e) {
			s_logger.warn("Problem deactivating component", e);
		}
	}

	protected void updated(Map<String, Object> properties) {
		s_logger.debug("Refreshing SCR properties: " + properties);
		refreshCamelRouteXml(m_camelRouteXml, (String) properties.get(RouterConstants.XML_ROUTE_PROPERTY));
	}

	public void refreshCamelRouteXml(String oldCamelRouteXml, String newCamelRouteXml) {
		if (newCamelRouteXml != null && !newCamelRouteXml.isEmpty() &&
				!newCamelRouteXml.equals(oldCamelRouteXml)) {
			this.m_camelRouteXml = newCamelRouteXml;
			try {
				ByteArrayInputStream bais =  new ByteArrayInputStream(m_camelRouteXml.getBytes());
				RoutesDefinition routesDefinition = camelContext.loadRoutesDefinition(bais);
				camelContext.addRouteDefinitions(routesDefinition.getRoutes());
			} catch (Exception e) {
				s_logger.warn("Cannot load routes definitions: {}", m_camelRouteXml, e);
			}
		}
	}

	// ASF Camel workarounds

	// TODO: Remove this overridden method as soon as Camel 2.17 is out (see CAMEL-9357)
	@Override
	public void configure() throws Exception {
		s_logger.debug("No programmatic routes configuration found.");
	}

	// TODO: Remove this overridden method as soon as Camel 2.17 is out (see CAMEL-9314)
	@Override
	public void start(BundleContext bundleContext) throws Exception {
		try {
			super.start(bundleContext);
		} catch (Exception e) {
			s_logger.warn("Problem when starting Kura module", e);
			throw e;
		}
	}

	@Override
	protected void beforeStart(CamelContext camelContext) {
		registerComponents();
		super.beforeStart(camelContext);
	}

	protected void modified(Map<String, Object> properties) {
{'fraction_non_alphanumeric': 0.14512492107874086, 'fraction_numerical': 0.007125462253089203, 'mean_word_length': 4.67741935483871, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1468585', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3119, 'n_words': 715}