/**
 * Licensed to the Rhiot under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.rhiot.component.kura.router;

import org.apache.camel.component.kura.KuraRouter;
import org.apache.camel.model.RoutesDefinition;
import org.eclipse.kura.configuration.ConfigurableComponent;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;

import java.io.ByteArrayInputStream;
import java.util.Map;

/**
 * Base class for Camel routers deployable into Eclipse Kura. All RhiotKuraRouters are configurable components managable
 * from the Everyware cloud.
 */
public abstract class RhiotKuraRouter extends KuraRouter implements ConfigurableComponent {

    // Constants

    public static final String XML_ROUTE_PROPERTY = "camel.route.xml";

    // Members

    /**
     * Camel route XML, usually configured using SCR property.
     */
    protected String camelRouteXml;

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

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

    // 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 {
        log.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 (Throwable e) {
            String errorMessage = "Problem when starting Kura module " + getClass().getName() + ":";
            log.warn(errorMessage, e);

            // Print error to the Kura console.
            System.err.println(errorMessage);
            e.printStackTrace();

            throw e;
        }
    }

    // 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) throws Exception {
        start(componentContext.getBundleContext());
        updated(properties); // TODO Keep this line even when Camel 2.17 is out
    }

    protected void deactivate(ComponentContext componentContext) throws Exception{
        stop(componentContext.getBundleContext());
    }

}
@@ -807,16 +807,76 @@
 outer;%0A%0A
+import java.io.ByteArrayInputStream;%0Aimport java.util.Map;%0A%0A
 import o
@@ -1125,68 +1125,8 @@
 t;%0A%0A
-import java.io.ByteArrayInputStream;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -1190,16 +1190,19 @@
 ura. All
+%0A *
  RhiotKu
@@ -1248,19 +1248,16 @@
 anagable
-%0A *
  from th
@@ -1267,16 +1267,19 @@
 veryware
+%0A *
  cloud.%0A
@@ -1379,98 +1379,8 @@
  %7B%0A%0A
-    // Constants%0A%0A    public static final String XML_ROUTE_PROPERTY = %22camel.route.xml%22;%0A%0A
     
@@ -1699,16 +1699,35 @@
 ies.get(
+RhiotKuraConstants.
 XML_ROUT
@@ -1846,16 +1846,17 @@
       if
+ 
 (newCame
@@ -2107,16 +2107,45 @@
 lContext
+%0A                            
 .loadRou
@@ -2565,32 +2565,39 @@
 2.17 is out (see
+%0A    //
  CAMEL-9357)%0A   
@@ -2795,32 +2795,39 @@
 2.17 is out (see
+%0A    //
  CAMEL-9314)%0A   
@@ -3378,16 +3378,23 @@
 out (see
+%0A    //
  CAMEL-9
@@ -3473,16 +3473,17 @@
 %3CString,
+ 
 Object%3E 
@@ -3733,16 +3733,17 @@
 xception
+ 
 %7B%0A      

move static string to cst class