Search in sources :

Example 1 with Config

use of com.haulmont.cuba.core.config.Config in project cuba by cuba-platform.

the class AbstractComponentLoader method loadShortcutFromFQNConfig.

protected String loadShortcutFromFQNConfig(String shortcut) {
    if (shortcut.contains("#")) {
        String[] splittedShortcut = shortcut.split("#");
        if (splittedShortcut.length != 2) {
            String message = "An error occurred while loading shortcut: incorrect format of shortcut.";
            throw new GuiDevelopmentException(message, context.getFullFrameId());
        }
        String fqnConfigName = splittedShortcut[0].substring(2);
        String methodName = splittedShortcut[1].substring(0, splittedShortcut[1].length() - 1);
        // noinspection unchecked
        Class<Config> configClass = (Class<Config>) scripting.loadClass(fqnConfigName);
        if (configClass != null) {
            Config config = configuration.getConfig(configClass);
            try {
                String shortcutValue = (String) MethodUtils.invokeMethod(config, methodName, null);
                if (StringUtils.isNotEmpty(shortcutValue)) {
                    return shortcutValue;
                }
            } catch (NoSuchMethodException e) {
                String message = String.format("An error occurred while loading shortcut: " + "can't find method \"%s\" in \"%s\"", methodName, fqnConfigName);
                throw new GuiDevelopmentException(message, context.getFullFrameId());
            } catch (IllegalAccessException | InvocationTargetException e) {
                String message = String.format("An error occurred while loading shortcut: " + "can't invoke method \"%s\" in \"%s\"", methodName, fqnConfigName);
                throw new GuiDevelopmentException(message, context.getFullFrameId());
            }
        } else {
            String message = String.format("An error occurred while loading shortcut: " + "can't find config interface \"%s\"", fqnConfigName);
            throw new GuiDevelopmentException(message, context.getFullFrameId());
        }
    }
    return null;
}
Also used : Config(com.haulmont.cuba.core.config.Config) LayoutLoaderConfig(com.haulmont.cuba.gui.xml.layout.LayoutLoaderConfig) ClientConfig(com.haulmont.cuba.client.ClientConfig) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) MetaClass(com.haulmont.chile.core.model.MetaClass) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Config

use of com.haulmont.cuba.core.config.Config in project cuba by cuba-platform.

the class ControllerDependencyInjector method getInjectedInstance.

protected Object getInjectedInstance(Class<?> type, String name, Class annotationClass, AnnotatedElement element) {
    if (annotationClass == WindowParam.class) {
        // Injecting a parameter
        return params.get(name);
    } else if (Component.class.isAssignableFrom(type)) {
        // Injecting a UI component
        return frame.getComponent(name);
    } else if (Datasource.class.isAssignableFrom(type)) {
        // Injecting a datasource
        return frame.getDsContext().get(name);
    } else if (DsContext.class.isAssignableFrom(type)) {
        // Injecting the DsContext
        return frame.getDsContext();
    } else if (DataSupplier.class.isAssignableFrom(type)) {
        // Injecting the DataSupplier
        return frame.getDsContext().getDataSupplier();
    } else if (FrameContext.class.isAssignableFrom(type)) {
        // Injecting the FrameContext
        return frame.getContext();
    } else if (Action.class.isAssignableFrom(type)) {
        // Injecting an action
        return ComponentsHelper.findAction(name, frame);
    } else if (ExportDisplay.class.isAssignableFrom(type)) {
        // Injecting an ExportDisplay
        return AppConfig.createExportDisplay(frame);
    } else if (ThemeConstants.class.isAssignableFrom(type)) {
        // Injecting a Theme
        ThemeConstantsManager themeManager = (ThemeConstantsManager) applicationContext.getBean(ThemeConstantsManager.NAME);
        return themeManager.getConstants();
    } else if (Config.class.isAssignableFrom(type)) {
        ClientConfiguration configuration = (ClientConfiguration) applicationContext.getBean(Configuration.NAME);
        // noinspection unchecked
        return configuration.getConfigCached((Class<? extends Config>) type);
    } else if (Logger.class == type && element instanceof Field) {
        return LoggerFactory.getLogger(((Field) element).getDeclaringClass());
    } else {
        Object instance;
        // Try to find a Spring bean
        Map<String, ?> beans = applicationContext.getBeansOfType(type, true, true);
        if (!beans.isEmpty()) {
            instance = beans.get(name);
            // If a bean with required name found, return it. Otherwise return first found.
            if (instance != null) {
                return instance;
            } else {
                return beans.values().iterator().next();
            }
        }
        // There are no Spring beans of required type - the last option is Companion
        if (frame instanceof AbstractFrame) {
            instance = ((AbstractFrame) frame).getCompanion();
            if (instance != null && type.isAssignableFrom(instance.getClass())) {
                return instance;
            }
        }
    }
    return null;
}
Also used : DsContext(com.haulmont.cuba.gui.data.DsContext) ThemeConstantsManager(com.haulmont.cuba.gui.theme.ThemeConstantsManager) Config(com.haulmont.cuba.core.config.Config) Logger(org.slf4j.Logger) Field(java.lang.reflect.Field) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) ClientConfiguration(com.haulmont.cuba.client.ClientConfiguration)

Aggregations

Config (com.haulmont.cuba.core.config.Config)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 ClientConfiguration (com.haulmont.cuba.client.ClientConfiguration)1 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 DsContext (com.haulmont.cuba.gui.data.DsContext)1 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)1 ThemeConstantsManager (com.haulmont.cuba.gui.theme.ThemeConstantsManager)1 LayoutLoaderConfig (com.haulmont.cuba.gui.xml.layout.LayoutLoaderConfig)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Logger (org.slf4j.Logger)1