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;
}
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;
}
Aggregations