mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-05 04:38:10 +08:00
Config dependency injection. #PL-2822
This commit is contained in:
parent
95a058d920
commit
1bf39b5905
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2013 Haulmont. All rights reserved.
|
||||
* Copyright (c) 2008-2014 Haulmont. All rights reserved.
|
||||
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
|
||||
*/
|
||||
|
||||
@ -9,6 +9,9 @@ import com.haulmont.cuba.core.config.Config;
|
||||
import com.haulmont.cuba.core.config.ConfigHandler;
|
||||
import com.haulmont.cuba.core.global.Configuration;
|
||||
import com.haulmont.cuba.core.sys.ConfigPersisterImpl;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
import javax.annotation.ManagedBean;
|
||||
import java.lang.reflect.Proxy;
|
||||
@ -18,12 +21,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
/**
|
||||
* Server-side implementation of the {@link Configuration} interface.
|
||||
*
|
||||
* <p>$Id$</p>
|
||||
*
|
||||
* @author krivopustov
|
||||
* @version $Id
|
||||
*/
|
||||
@ManagedBean(Configuration.NAME)
|
||||
public class ConfigurationImpl implements Configuration {
|
||||
public class ConfigurationImpl implements Configuration, BeanFactoryPostProcessor {
|
||||
|
||||
protected Map<Class, ConfigHandler> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@ -38,4 +40,9 @@ public class ConfigurationImpl implements Configuration {
|
||||
Object proxy = Proxy.newProxyInstance(classLoader, new Class[]{configInterface}, handler);
|
||||
return configInterface.cast(proxy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
// empty, just to make sure this bean is instantiated before others
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class UserManagementServiceBean implements UserManagementService {
|
||||
protected Resources resources;
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected ServerConfig serverConfig;
|
||||
|
||||
@Inject
|
||||
protected UserSessionSource userSessionSource;
|
||||
@ -158,7 +158,6 @@ public class UserManagementServiceBean implements UserManagementService {
|
||||
Map<User, String> modifiedUsers = updateUserPasswords(userIds, true);
|
||||
|
||||
// email templates
|
||||
ServerConfig serverConfig = configuration.getConfig(ServerConfig.class);
|
||||
String resetPasswordBodyTemplate = serverConfig.getResetPasswordEmailBodyTemplate();
|
||||
String resetPasswordSubjectTemplate = serverConfig.getResetPasswordEmailSubjectTemplate();
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
*/
|
||||
package com.haulmont.cuba.core;
|
||||
|
||||
import com.haulmont.cuba.core.config.TestBeanToInjectConfig;
|
||||
import com.haulmont.cuba.core.config.TestConfig;
|
||||
import com.haulmont.cuba.core.entity.Config;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
@ -163,6 +164,20 @@ public class ConfigProviderTest extends CubaTestCase
|
||||
assertEquals(Boolean.TYPE, boolMethodReturnType);
|
||||
}
|
||||
|
||||
public void testInjectedConfig() throws Exception {
|
||||
TestBeanToInjectConfig bean = AppBeans.get(TestBeanToInjectConfig.class);
|
||||
TestConfig config = bean.getConfig();
|
||||
assertNotNull(config);
|
||||
assertTrue(config.getBooleanPropDef());
|
||||
}
|
||||
|
||||
public void testInjectedConfigBySetter() throws Exception {
|
||||
TestBeanToInjectConfig bean = AppBeans.get(TestBeanToInjectConfig.class);
|
||||
TestConfig config = bean.getConfig2();
|
||||
assertNotNull(config);
|
||||
assertTrue(config.getBooleanPropDef());
|
||||
}
|
||||
|
||||
private void cleanup() {
|
||||
Transaction tx = persistence.createTransaction();
|
||||
try {
|
||||
|
@ -12,6 +12,7 @@ import com.haulmont.cuba.core.global.PasswordEncryption;
|
||||
import com.haulmont.cuba.core.sys.AbstractAppContextLoader;
|
||||
import com.haulmont.cuba.core.sys.AppContext;
|
||||
import com.haulmont.cuba.core.sys.AppContextLoader;
|
||||
import com.haulmont.cuba.core.sys.CubaDefaultListableBeanFactory;
|
||||
import com.haulmont.cuba.core.sys.persistence.PersistenceConfigProcessor;
|
||||
import com.haulmont.cuba.testsupport.TestContext;
|
||||
import com.haulmont.cuba.testsupport.TestDataSource;
|
||||
@ -23,6 +24,7 @@ import org.apache.commons.lang.text.StrSubstitutor;
|
||||
import org.apache.commons.lang.text.StrTokenizer;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
@ -169,7 +171,12 @@ public abstract class CubaTestCase extends TestCase {
|
||||
List<String> locations = tokenizer.getTokenList();
|
||||
locations.add(getTestSpringConfig());
|
||||
|
||||
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext();
|
||||
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext() {
|
||||
@Override
|
||||
protected DefaultListableBeanFactory createBeanFactory() {
|
||||
return new CubaDefaultListableBeanFactory(getInternalParentBeanFactory());
|
||||
}
|
||||
};
|
||||
|
||||
appContext.setConfigLocations(locations.toArray(new String[locations.size()]));
|
||||
appContext.setValidating(false);
|
||||
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2014 Haulmont. All rights reserved.
|
||||
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
|
||||
*/
|
||||
|
||||
package com.haulmont.cuba.core.config;
|
||||
|
||||
import javax.annotation.ManagedBean;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* @author krivopustov
|
||||
* @version $Id$
|
||||
*/
|
||||
@ManagedBean
|
||||
public class TestBeanToInjectConfig {
|
||||
|
||||
@Inject
|
||||
private TestConfig config;
|
||||
|
||||
private TestConfig config2;
|
||||
|
||||
@Inject
|
||||
public void setConfig2(TestConfig config2) {
|
||||
this.config2 = config2;
|
||||
}
|
||||
|
||||
public TestConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public TestConfig getConfig2() {
|
||||
return config2;
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@
|
||||
|
||||
package com.haulmont.cuba.desktop.theme.impl;
|
||||
|
||||
import com.haulmont.cuba.core.global.Configuration;
|
||||
import com.haulmont.cuba.core.global.Resources;
|
||||
import com.haulmont.cuba.desktop.DesktopConfig;
|
||||
import com.haulmont.cuba.desktop.DesktopResources;
|
||||
@ -48,7 +47,7 @@ public class DesktopThemeLoaderImpl implements DesktopThemeLoader {
|
||||
private static final String BORDER_TAG = "border";
|
||||
|
||||
@Inject
|
||||
private Configuration configuration;
|
||||
private DesktopConfig config;
|
||||
|
||||
@Inject
|
||||
private Resources resources;
|
||||
@ -59,7 +58,7 @@ public class DesktopThemeLoaderImpl implements DesktopThemeLoader {
|
||||
private static final Pattern DECIMAL_COLOR_PATTERN = Pattern.compile("^(\\d+)\\s+(\\d+)\\s+(\\d+)$");
|
||||
|
||||
public DesktopTheme loadTheme(String themeName) {
|
||||
final String themeLocations = configuration.getConfig(DesktopConfig.class).getResourceLocations();
|
||||
String themeLocations = config.getResourceLocations();
|
||||
StrTokenizer tokenizer = new StrTokenizer(themeLocations);
|
||||
String[] locationList = tokenizer.getTokenArray();
|
||||
|
||||
|
@ -37,7 +37,7 @@ public class AbstractAppContextLoader {
|
||||
StrTokenizer tokenizer = new StrTokenizer(configProperty);
|
||||
String[] locations = tokenizer.getTokenArray();
|
||||
|
||||
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext();
|
||||
ClassPathXmlApplicationContext appContext = new CubaClassPathXmlApplicationContext();
|
||||
|
||||
appContext.setConfigLocations(locations);
|
||||
appContext.setValidating(false);
|
||||
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2014 Haulmont. All rights reserved.
|
||||
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
|
||||
*/
|
||||
|
||||
package com.haulmont.cuba.core.sys;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author krivopustov
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CubaClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
|
||||
|
||||
@Override
|
||||
protected DefaultListableBeanFactory createBeanFactory() {
|
||||
return new CubaDefaultListableBeanFactory(getInternalParentBeanFactory());
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2014 Haulmont. All rights reserved.
|
||||
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
|
||||
*/
|
||||
|
||||
package com.haulmont.cuba.core.sys;
|
||||
|
||||
import com.haulmont.cuba.core.config.Config;
|
||||
import com.haulmont.cuba.core.global.Configuration;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.TypeConverter;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Helps to inject Config interfaces into Spring beans.
|
||||
*
|
||||
* @author krivopustov
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CubaDefaultListableBeanFactory extends DefaultListableBeanFactory {
|
||||
|
||||
public CubaDefaultListableBeanFactory(BeanFactory beanFactory) {
|
||||
super(beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
|
||||
Field field = descriptor.getField();
|
||||
if (field != null && Config.class.isAssignableFrom(field.getType())) {
|
||||
return getConfig(field.getType());
|
||||
}
|
||||
MethodParameter methodParam = descriptor.getMethodParameter();
|
||||
if (methodParam != null && Config.class.isAssignableFrom(methodParam.getParameterType())) {
|
||||
return getConfig(methodParam.getParameterType());
|
||||
}
|
||||
return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object getConfig(Class configClass) {
|
||||
Configuration configuration = (Configuration) getBean(Configuration.NAME);
|
||||
return configuration.getConfig((Class<? extends Config>) configClass);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2014 Haulmont. All rights reserved.
|
||||
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
|
||||
*/
|
||||
|
||||
package com.haulmont.cuba.core.sys;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
|
||||
/**
|
||||
* @author krivopustov
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CubaXmlWebApplicationContext extends XmlWebApplicationContext {
|
||||
|
||||
@Override
|
||||
protected DefaultListableBeanFactory createBeanFactory() {
|
||||
BeanFactory parent = getParentBeanFactory();
|
||||
if (parent instanceof ApplicationContext)
|
||||
parent = ((ApplicationContext) parent).getAutowireCapableBeanFactory();
|
||||
return new CubaDefaultListableBeanFactory(parent);
|
||||
}
|
||||
}
|
@ -5,7 +5,9 @@
|
||||
|
||||
package com.haulmont.cuba.gui;
|
||||
|
||||
import com.haulmont.cuba.core.config.Config;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import com.haulmont.cuba.core.global.Configuration;
|
||||
import com.haulmont.cuba.core.sys.AppContext;
|
||||
import com.haulmont.cuba.gui.components.Action;
|
||||
import com.haulmont.cuba.gui.components.Component;
|
||||
@ -177,6 +179,10 @@ public class CompanionDependencyInjector {
|
||||
ThemeConstantsManager themeManager = AppBeans.get(ThemeConstantsManager.NAME);
|
||||
return themeManager.getConstants();
|
||||
|
||||
} else if (Config.class.isAssignableFrom(type)) {
|
||||
//noinspection unchecked
|
||||
return AppBeans.get(Configuration.class).getConfig((Class<? extends Config>) type);
|
||||
|
||||
} else {
|
||||
Object instance;
|
||||
// Try to find a Spring bean
|
||||
|
@ -5,7 +5,9 @@
|
||||
|
||||
package com.haulmont.cuba.gui;
|
||||
|
||||
import com.haulmont.cuba.core.config.Config;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import com.haulmont.cuba.core.global.Configuration;
|
||||
import com.haulmont.cuba.core.sys.AppContext;
|
||||
import com.haulmont.cuba.gui.components.AbstractFrame;
|
||||
import com.haulmont.cuba.gui.components.Action;
|
||||
@ -186,6 +188,10 @@ public class ControllerDependencyInjector {
|
||||
ThemeConstantsManager themeManager = AppBeans.get(ThemeConstantsManager.NAME);
|
||||
return themeManager.getConstants();
|
||||
|
||||
} else if (Config.class.isAssignableFrom(type)) {
|
||||
//noinspection unchecked
|
||||
return AppBeans.get(Configuration.class).getConfig((Class<? extends Config>) type);
|
||||
|
||||
} else {
|
||||
Object instance;
|
||||
// Try to find a Spring bean
|
||||
|
@ -43,7 +43,7 @@ public class UserChangePassw extends AbstractEditor {
|
||||
protected Datasource<User> userDs;
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected ClientConfig clientConfig;
|
||||
|
||||
@Inject
|
||||
protected PasswordEncryption passwordEncryption;
|
||||
@ -111,7 +111,6 @@ public class UserChangePassw extends AbstractEditor {
|
||||
errors.add(confirmPasswField, getMessage("passwordsDoNotMatch"));
|
||||
|
||||
} else {
|
||||
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
|
||||
if (clientConfig.getPasswordPolicyEnabled()) {
|
||||
String regExp = clientConfig.getPasswordPolicyRegExp();
|
||||
if (!password.matches(regExp)) {
|
||||
|
@ -47,7 +47,7 @@ public class LoginServiceController {
|
||||
protected Authentication authentication;
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected GlobalConfig globalConfig;
|
||||
|
||||
@Inject
|
||||
protected UserSessionService userSessionService;
|
||||
@ -149,7 +149,6 @@ public class LoginServiceController {
|
||||
protected void setSessionInfo(HttpServletRequest request, UserSession userSession) {
|
||||
userSessionService.setSessionAddress(userSession.getId(), request.getRemoteAddr());
|
||||
|
||||
GlobalConfig globalConfig = configuration.getConfig(GlobalConfig.class);
|
||||
String serverInfo = "REST API (" +
|
||||
globalConfig.getWebHostName() + ":" +
|
||||
globalConfig.getWebPort() + "/" +
|
||||
|
@ -6,6 +6,7 @@
|
||||
package com.haulmont.cuba.portal.sys;
|
||||
|
||||
import com.haulmont.cuba.core.sys.AppContext;
|
||||
import com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.text.StrTokenizer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@ -75,4 +76,9 @@ public class PortalDispatcherServlet extends DispatcherServlet {
|
||||
|
||||
return wac;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getContextClass() {
|
||||
return CubaXmlWebApplicationContext.class;
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,10 @@ public class AnonymousSessionHolder {
|
||||
private Log log = LogFactory.getLog(getClass());
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected GlobalConfig globalConfig;
|
||||
|
||||
@Inject
|
||||
protected PortalConfig portalConfig;
|
||||
|
||||
@Inject
|
||||
protected PasswordEncryption passwordEncryption;
|
||||
@ -68,9 +71,8 @@ public class AnonymousSessionHolder {
|
||||
}
|
||||
|
||||
private UserSession loginAsAnonymous() {
|
||||
PortalConfig config = configuration.getConfig(PortalConfig.class);
|
||||
String login = config.getAnonymousUserLogin();
|
||||
String password = config.getTrustedClientPassword();
|
||||
String login = portalConfig.getAnonymousUserLogin();
|
||||
String password = portalConfig.getTrustedClientPassword();
|
||||
|
||||
GlobalConfig globalConfig = AppBeans.get(Configuration.class).getConfig(GlobalConfig.class);
|
||||
Collection<Locale> locales = globalConfig.getAvailableLocales().values();
|
||||
@ -99,7 +101,6 @@ public class AnonymousSessionHolder {
|
||||
}
|
||||
|
||||
private String getPortalNetworkLocation() {
|
||||
GlobalConfig globalConfig = configuration.getConfig(GlobalConfig.class);
|
||||
return globalConfig.getWebHostName() + ":" +
|
||||
globalConfig.getWebPort() + "/" +
|
||||
globalConfig.getWebContextName();
|
||||
|
@ -42,7 +42,7 @@ public class UserSettingsTools {
|
||||
protected UserSettingService userSettingService;
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected WebConfig webConfig;
|
||||
|
||||
public AppWindow.Mode loadAppWindowMode() {
|
||||
String s = userSettingService.loadSetting(ClientType.WEB, "appWindowMode");
|
||||
@ -53,7 +53,6 @@ public class UserSettingsTools {
|
||||
return AppWindow.Mode.TABBED;
|
||||
}
|
||||
}
|
||||
WebConfig webConfig = configuration.getConfig(WebConfig.class);
|
||||
return AppWindow.Mode.valueOf(webConfig.getAppWindowMode().toUpperCase());
|
||||
}
|
||||
|
||||
@ -66,7 +65,6 @@ public class UserSettingsTools {
|
||||
if (s != null) {
|
||||
return s;
|
||||
}
|
||||
WebConfig webConfig = configuration.getConfig(WebConfig.class);
|
||||
return webConfig.getAppWindowTheme();
|
||||
}
|
||||
|
||||
|
@ -19,10 +19,10 @@ import javax.inject.Inject;
|
||||
public class UserEditorCompanion implements UserEditor.Companion {
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected WebAuthConfig config;
|
||||
|
||||
@Override
|
||||
public void initPasswordField(PasswordField passwordField) {
|
||||
passwordField.setRequired(!configuration.getConfig(WebAuthConfig.class).getUseActiveDirectory());
|
||||
passwordField.setRequired(!config.getUseActiveDirectory());
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
package com.haulmont.cuba.web.sys;
|
||||
|
||||
import com.haulmont.cuba.core.sys.AppContext;
|
||||
import com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.text.StrTokenizer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@ -74,4 +75,9 @@ public class CubaDispatcherServlet extends DispatcherServlet {
|
||||
|
||||
return wac;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getContextClass() {
|
||||
return CubaXmlWebApplicationContext.class;
|
||||
}
|
||||
}
|
@ -19,10 +19,10 @@ import javax.inject.Inject;
|
||||
public class UserEditorCompanion implements UserEditor.Companion {
|
||||
|
||||
@Inject
|
||||
protected Configuration configuration;
|
||||
protected WebAuthConfig config;
|
||||
|
||||
@Override
|
||||
public void initPasswordField(PasswordField passwordField) {
|
||||
passwordField.setRequired(!configuration.getConfig(WebAuthConfig.class).getUseActiveDirectory());
|
||||
passwordField.setRequired(!config.getUseActiveDirectory());
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
package com.haulmont.cuba.web.sys;
|
||||
|
||||
import com.haulmont.cuba.core.sys.AppContext;
|
||||
import com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.text.StrTokenizer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@ -70,4 +71,9 @@ public class CubaDispatcherServlet extends DispatcherServlet {
|
||||
|
||||
return wac;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getContextClass() {
|
||||
return CubaXmlWebApplicationContext.class;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user