com.haulmont.cuba.core.config.ConfigStorageCommon.java Source code

Java tutorial

Introduction

Here is the source code for com.haulmont.cuba.core.config.ConfigStorageCommon.java

Source

/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * 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 com.haulmont.cuba.core.config;

import com.google.common.base.Strings;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.GlobalConfig;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.core.sys.SecurityContext;
import com.haulmont.cuba.security.auth.AuthenticationService;
import com.haulmont.cuba.security.auth.TrustedClientCredentials;
import com.haulmont.cuba.security.global.LoginException;
import com.haulmont.cuba.security.global.UserSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.text.StrBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

@Component("cuba_ConfigStorageCommon")
public class ConfigStorageCommon {

    private final Logger log = LoggerFactory.getLogger(ConfigStorageCommon.class);

    @Inject
    protected Configuration configuration;
    @Inject
    protected AuthenticationService authenticationService;

    public String printAppProperties(String prefix) {
        List<String> list = new ArrayList<>();
        for (String name : AppContext.getPropertyNames()) {
            if (prefix == null || name.startsWith(prefix)) {
                list.add(name + "=" + AppContext.getProperty(name));
            }
        }
        Collections.sort(list);
        return new StrBuilder().appendWithSeparators(list, "\n").toString();
    }

    public String getAppProperty(String name) {
        if (StringUtils.isBlank(name))
            return "Enter a property name";

        return name + "=" + AppContext.getProperty(name);
    }

    public String setAppProperty(String name, String value) {
        if (StringUtils.isBlank(name))
            return "Enter a property name";
        if (StringUtils.isBlank(value))
            return "Enter a property value";

        AppContext.setProperty(name, value);
        return String.format("Property %s set to %s", name, value);
    }

    /**
     * Method returns a result of config method invocation
     * @param classFQN fully qualified configuration interface name
     * @param methodName config getter method name
     * @param userLogin parameter is used for authentication if there is no security context bound to the current thread
     *                  and configuration method source is DATABASE
     * @param userPassword see userLogin parameter description
     * @return configuration method invocation result
     */
    public String getConfigValue(String classFQN, String methodName, String userLogin, String userPassword) {
        Class<?> aClass;
        try {
            aClass = Class.forName(classFQN);
        } catch (ClassNotFoundException e) {
            return String
                    .format("Class %s not found.\nPlease ensure that you entered a fully qualified class name and "
                            + "that you class is in a proper application module (core, web or portal).", classFQN);
        }

        if (Config.class.isAssignableFrom(aClass)) {
            Config config = configuration.getConfig((Class<? extends Config>) aClass);
            Method method;
            boolean logoutRequired = false;
            try {
                method = aClass.getMethod(methodName);

                //if there is no security context bound to the current thread and the source of the config method is
                //DATABASE, then login attempt with 'userLogin' and 'userPassword' will be made
                if (AppContext.getSecurityContext() == null) {
                    SourceType sourceType;
                    Source methodSourceAnnotation = method.getAnnotation(Source.class);
                    if (methodSourceAnnotation != null) {
                        sourceType = methodSourceAnnotation.type();
                    } else {
                        Source classSourceAnnotation = aClass.getAnnotation(Source.class);
                        sourceType = classSourceAnnotation.type();
                    }

                    if (sourceType != null && sourceType == SourceType.DATABASE) {
                        if (Strings.isNullOrEmpty(userLogin)) {
                            return "No security context bound to the current thread. Please specify the user name.";
                        } else {
                            try {
                                Map<String, Locale> availableLocales = configuration.getConfig(GlobalConfig.class)
                                        .getAvailableLocales();
                                Locale defaultLocale = availableLocales.values().iterator().next();

                                TrustedClientCredentials credentials = new TrustedClientCredentials(userLogin,
                                        userPassword, defaultLocale);

                                UserSession session = authenticationService.login(credentials).getSession();
                                AppContext.setSecurityContext(new SecurityContext(session));
                                logoutRequired = true;
                            } catch (LoginException e) {
                                log.error(ExceptionUtils.getStackTrace(e));
                                return "Login error: " + e.getMessage();
                            }
                        }
                    }
                }

                Object result = method.invoke(config);
                return result == null ? null : result.toString();
            } catch (NoSuchMethodException e) {
                return String.format("Method %s() not found in class %s", methodName, classFQN);
            } catch (InvocationTargetException | IllegalAccessException e) {
                return ExceptionUtils.getStackTrace(e);
            } finally {
                if (logoutRequired) {
                    try {
                        authenticationService.logout();
                    } finally {
                        AppContext.setSecurityContext(null);
                    }
                }
            }
        } else {
            return String.format("Class %s is not an implementation of Config interface", classFQN);
        }
    }
}