/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * Licensed 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.
 * #L%
 */
package org.wisdom.template.thymeleaf.tracker;

import org.apache.commons.io.IOUtils;
import org.apache.felix.ipojo.annotations.*;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.util.tracker.BundleTracker;
import org.osgi.util.tracker.BundleTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wisdom.template.thymeleaf.ThymeleafTemplateCollector;
import org.wisdom.template.thymeleaf.impl.ThymeLeafTemplateImplementation;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * A Chameleon deployer tracking template from bundles.
 */
@Component
@Provides
@Instantiate
public class TemplateTracker implements BundleTrackerCustomizer<List<ThymeLeafTemplateImplementation>> {

    private static final Logger LOGGER = LoggerFactory.getLogger(TemplateTracker.class);

    @Requires(proxy = false)
    ThymeleafTemplateCollector engine;

    @Context
    BundleContext context;

    private static final String TEMPLATE_DIRECTORY_IN_BUNDLES = "/templates";

    /**
     * The directory containing templates.
     */
    private File directory;

    private BundleTracker<List<ThymeLeafTemplateImplementation>> tracker;


    @Validate
    public void start() {
        LOGGER.info("Starting Thymeleaf template tracker");
        tracker = new BundleTracker<>(context, Bundle.ACTIVE, this);
        tracker.open();
    }

    @Invalidate
    public void stop() {
        try {
            if (tracker != null) {
                tracker.close();
            }
        } catch (IllegalStateException e) { //NOSONAR
            // We have to catch the exception because of FELIX-4488
        }
    }

    @Override
    public List<ThymeLeafTemplateImplementation> addingBundle(Bundle bundle, BundleEvent bundleEvent) {
        List<ThymeLeafTemplateImplementation> list = new ArrayList<ThymeLeafTemplateImplementation>();
        Enumeration<URL> urls = bundle.findEntries(TEMPLATE_DIRECTORY_IN_BUNDLES, "*.html", true);
        if (urls == null) {
            return list;
        }
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            // Check it's the thymeleaf template.
            if (url.toExternalForm().endsWith(engine.extension())) {
                ThymeLeafTemplateImplementation template = engine.addTemplate(url);
                list.add(template);
            }
        }
        return list;
    }

    @Override
    public void modifiedBundle(Bundle bundle, BundleEvent bundleEvent, List<ThymeLeafTemplateImplementation> o) {
        for (ThymeLeafTemplateImplementation template : o) {
            engine.updatedTemplate(template);
        }
    }

    @Override
    public void removedBundle(Bundle bundle, BundleEvent bundleEvent, List<ThymeLeafTemplateImplementation> o) {
        for (ThymeLeafTemplateImplementation template : o) {
            LOGGER.info("Thymeleaf template deleted for {} from {}", template.fullName(), bundle.getSymbolicName());
            engine.deleteTemplate(template);

        }
    }
}

@@ -3780,16 +3780,108 @@
 ame());%0A
+            // Check whether we still have an engine.%0A            if (engine != null) %7B%0A    
         
@@ -3917,16 +3917,29 @@
 plate);%0A
+            %7D
 %0A       

Fix NPE on hot reload of the configuration file.