All Downloads are FREE. Search and download functionalities are using the official Maven repository.

leap.spring.boot.Global Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2018 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;

import leap.core.AppConfig;
import leap.core.AppContext;
import leap.core.BeanFactory;
import leap.core.DefaultAppConfig;
import leap.lang.Factory;
import leap.lang.Strings;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.HashMap;
import java.util.Map;

public class Global {

    public static final String SPRING_PACKAGE_PREFIX = "org.springframework.";
    public static final String LEAP_PACKAGE_PREFIX   = "leap.";

    public static String    bp; //base package.
    public static String[]  bps;
    public static String    profile;
    public static ConfigurableEnvironment env;
    public static ApplicationContext      context;
    public static LeapContext             leap;

    public interface LeapContext {
        AppConfig   config();
        BeanFactory factory();
        AppContext  context();
    }

    private static Boolean springReady = null;

    public static boolean isSpringReady() {
        if(null != springReady) {
            return springReady;
        }

        if(null == context) {
            return false;
        }

        if(context instanceof ConfigurableApplicationContext) {
            if(((ConfigurableApplicationContext) context).isActive()) {
                springReady = true;
                return true;
            }
        }

        return false;
    }

    public static AppContext context() {
        return null == leap ? null : leap.context();
    }

    public static AppConfig config() {
        return null == leap ? null : leap.config();
    }

    public static BeanFactory factory() {
        return null == leap ? null : leap.factory();
    }

    public static Map extraInitPropertiesFromEnv() {
        if(null == env) {
            throw new IllegalStateException("Spring env must be initialized");
        }
        Map props = new HashMap<>();
        for(String name : AppConfig.INIT_PROPERTIES) {
            String v = env.getProperty(name);
            if(!Strings.isEmpty(v)) {
                props.put(name, v);
            }
        }
        return props;
    }

    static void start() {
        Starter starter = Factory.newInstance(Starter.class, StandaloneStarter.class);
        starter.start();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy