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

hu.icellmobilsoft.coffee.rest.cdi.BaseApplicationContainer Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*-
 * #%L
 * Coffee
 * %%
 * Copyright (C) 2020 - 2021 i-Cell Mobilsoft Zrt.
 * %%
 * 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 hu.icellmobilsoft.coffee.rest.cdi;

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

import jakarta.annotation.Resource;
import jakarta.enterprise.context.ApplicationScoped;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.ConfigProvider;

import hu.icellmobilsoft.coffee.cdi.config.IConfigKey;

/**
 * Application scoped container class
 *
 * @author karoly.tamas
 * @since 1.6.0
 */
@ApplicationScoped
public class BaseApplicationContainer {

    @Resource(lookup = "java:app/AppName")
    private String appName;

    private Map objectMap;

    /**
     * Default constructor, constructs a new object.
     */
    public BaseApplicationContainer() {
        super();
    }

    /**
     * Object map
     *
     * @return object map
     */
    public synchronized Map getObjectMap() {
        if (objectMap == null) {
            objectMap = new HashMap<>();
        }
        return objectMap;
    }

    /**
     * Coffee app name from config or lookup is not set
     *
     * @return coffee app name
     */
    public synchronized String getCoffeeAppName() {
        final String coffeeAppName;
        if (getObjectMap().containsKey(IConfigKey.COFFEE_APP_NAME) && getObjectMap().get(IConfigKey.COFFEE_APP_NAME) instanceof String) {
            coffeeAppName = (String) getObjectMap().get(IConfigKey.COFFEE_APP_NAME);
        } else {
            Optional coffeeAppNameConfig = ConfigProvider.getConfig().getOptionalValue(IConfigKey.COFFEE_APP_NAME, String.class);
            if (coffeeAppNameConfig.isEmpty()) {
                coffeeAppName = appName;
            } else {
                coffeeAppName = StringUtils.defaultIfBlank(coffeeAppNameConfig.get(), appName);
            }
            getObjectMap().put(IConfigKey.COFFEE_APP_NAME, coffeeAppName);
        }

        return coffeeAppName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy