/*
 *   Copyright 2019 the original author or authors.
 *
 *   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.
 */

package leap.spring.boot.web;

import leap.core.AppConfig;
import leap.core.AppContext;
import leap.core.BeanFactory;
import leap.spring.boot.Global;
import leap.web.AppBootstrap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class BootstrapRunListener implements SpringApplicationRunListener, Ordered, WebApplicationInitializer {

    private static ServletContext sc;

    public BootstrapRunListener() {

    }

    public BootstrapRunListener(SpringApplication application, String[] args) {

    }

    public int getOrder() {
        return HIGHEST_PRECEDENCE + 101;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebConfiguration.startedServletContext = servletContext;
    }

    public void starting() {

    }

    public void environmentPrepared(ConfigurableEnvironment environment) {

    }

    public void contextPrepared(ConfigurableApplicationContext context) {

    }

    public void contextLoaded(ConfigurableApplicationContext context) {

    }

    //spring-boot 1.5
    public void finished(ConfigurableApplicationContext context, Throwable exception) {
        if(null != context.getParent()) {
            return;
        }
        started(context);
    }

    //spring-boot 2.0
    public void started(ConfigurableApplicationContext context) { }
    public void running(ConfigurableApplicationContext context) { }
    public void failed(ConfigurableApplicationContext context, Throwable exception) {}

    protected void boot(ServletContext sc) {
        if(AppBootstrap.isInitialized(sc)) {
            return;
        }

        final AppBootstrap bootstrap = new AppBootstrap();
        Global.leap = new Global.LeapContext() {
            @Override
            public AppConfig config() {
                return bootstrap.getAppConfig();
            }

            @Override
            public BeanFactory factory() {
                return bootstrap.getBeanFactory();
            }

            @Override
            public AppContext context() {
                return bootstrap.getAppContext();
            }
        };
        bootstrap.initialize(sc);
    }
}

@@ -765,26 +765,107 @@
 eap.
-spring.boot.Global
+lang.resource.Resources;%0Aimport leap.spring.boot.Global;%0Aimport leap.spring.boot.LeapResourceLoader
 ;%0Aim
@@ -1001,24 +1001,79 @@
 unListener;%0A
+import org.springframework.context.ApplicationContext;%0A
 import org.s
@@ -2154,88 +2154,292 @@
 ) %7B%0A
-%0A    %7D%0A%0A    public void contextLoaded(ConfigurableApplicationContext context) %7B%0A
+        if(isRealContext(context)) %7B%0A            Resources.setResourceLoader(new LeapResourceLoader(context));%0A        %7D%0A    %7D%0A%0A    public void contextLoaded(ConfigurableApplicationContext context) %7B%0A        if(isRealContext(context)) %7B%0A            Global.context = context;%0A        %7D
 %0A   
@@ -2556,80 +2556,8 @@
 ) %7B%0A
-        if(null != context.getParent()) %7B%0A            return;%0A        %7D%0A
     
@@ -2831,16 +2831,363 @@
 on) %7B%7D%0A%0A
+    protected boolean isRealContext(ApplicationContext context) %7B%0A        boolean web = context instanceof WebApplicationContext;%0A        if(!web) %7B%0A            return false;%0A        %7D%0A        if(null != context.getParent() && context.getParent() instanceof WebApplicationContext) %7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A
     prot

handle real web application context