Search in sources :

Example 1 with MessageSource

use of org.springframework.context.MessageSource in project head by mifos.

the class BaseAction method getLocalizedMessage.

protected String getLocalizedMessage(String key) {
    // nothing found so return the key
    String message = key;
    MessageSource messageSource = ApplicationContextProvider.getBean(MessageSource.class);
    if (personnelServiceFacade != null) {
        String value = messageSource.getMessage(key, null, personnelServiceFacade.getUserPreferredLocale());
        if (StringUtils.isNotEmpty(message)) {
            message = value;
        }
    }
    return message;
}
Also used : MessageSource(org.springframework.context.MessageSource)

Example 2 with MessageSource

use of org.springframework.context.MessageSource in project spring-framework by spring-projects.

the class AbstractApplicationContext method initMessageSource.

/**
	 * Initialize the MessageSource.
	 * Use parent's if none defined in this context.
	 */
protected void initMessageSource() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
        this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
        // Make MessageSource aware of parent MessageSource.
        if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
            HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
            if (hms.getParentMessageSource() == null) {
                // Only set parent context as parent MessageSource if no parent MessageSource
                // registered already.
                hms.setParentMessageSource(getInternalParentMessageSource());
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Using MessageSource [" + this.messageSource + "]");
        }
    } else {
        // Use empty MessageSource to be able to accept getMessage calls.
        DelegatingMessageSource dms = new DelegatingMessageSource();
        dms.setParentMessageSource(getInternalParentMessageSource());
        this.messageSource = dms;
        beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME + "': using default [" + this.messageSource + "]");
        }
    }
}
Also used : HierarchicalMessageSource(org.springframework.context.HierarchicalMessageSource) MessageSource(org.springframework.context.MessageSource) HierarchicalMessageSource(org.springframework.context.HierarchicalMessageSource) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 3 with MessageSource

use of org.springframework.context.MessageSource in project spring-framework by spring-projects.

the class ResourceBundleThemeSource method getTheme.

/**
	 * This implementation returns a SimpleTheme instance, holding a
	 * ResourceBundle-based MessageSource whose basename corresponds to
	 * the given theme name (prefixed by the configured "basenamePrefix").
	 * <p>SimpleTheme instances are cached per theme name. Use a reloadable
	 * MessageSource if themes should reflect changes to the underlying files.
	 * @see #setBasenamePrefix
	 * @see #createMessageSource
	 */
@Override
public Theme getTheme(String themeName) {
    if (themeName == null) {
        return null;
    }
    Theme theme = this.themeCache.get(themeName);
    if (theme == null) {
        synchronized (this.themeCache) {
            theme = this.themeCache.get(themeName);
            if (theme == null) {
                String basename = this.basenamePrefix + themeName;
                MessageSource messageSource = createMessageSource(basename);
                theme = new SimpleTheme(themeName, messageSource);
                initParent(theme);
                this.themeCache.put(themeName, theme);
                if (logger.isDebugEnabled()) {
                    logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
                }
            }
        }
    }
    return theme;
}
Also used : Theme(org.springframework.ui.context.Theme) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) HierarchicalMessageSource(org.springframework.context.HierarchicalMessageSource) MessageSource(org.springframework.context.MessageSource)

Example 4 with MessageSource

use of org.springframework.context.MessageSource in project spring-framework by spring-projects.

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFallbackToTargetClass.

@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
    sac.registerSingleton("noInterfaces", NoInterfaces.class);
    sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
    sac.registerSingleton("singletonNoInterceptor", TestBean.class);
    sac.registerSingleton("singletonToBeProxied", TestBean.class);
    sac.registerPrototype("prototypeToBeProxied", TestBean.class);
    sac.refresh();
    MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
    NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
    ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly = (ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
    ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
    ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
    ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
    assertFalse(AopUtils.isCglibProxy(messageSource));
    assertTrue(AopUtils.isCglibProxy(noInterfaces));
    assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
    assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
    assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
    assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonNoInterceptor.getName();
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonToBeProxied.getAge();
    assertEquals(1, tapc.testInterceptor.nrOfInvocations);
    prototypeToBeProxied.getSpouse();
    assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Example 5 with MessageSource

use of org.springframework.context.MessageSource in project spring-framework by spring-projects.

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFallbackToDynamicProxy.

@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
    StaticApplicationContext sac = new StaticApplicationContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("proxyFactoryBean", "false");
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
    sac.registerSingleton("noInterfaces", NoInterfaces.class);
    sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
    sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
    sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
    sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.class);
    sac.refresh();
    MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
    NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
    ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly = (ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
    ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
    ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
    ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
    assertFalse(AopUtils.isCglibProxy(messageSource));
    assertTrue(AopUtils.isCglibProxy(noInterfaces));
    assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
    assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
    assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
    assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonNoInterceptor.getName();
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonToBeProxied.getAge();
    assertEquals(1, tapc.testInterceptor.nrOfInvocations);
    prototypeToBeProxied.getSpouse();
    assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Aggregations

MessageSource (org.springframework.context.MessageSource)13 Test (org.junit.Test)7 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 StaticMessageSource (org.springframework.context.support.StaticMessageSource)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 PersonnelServiceFacade (org.mifos.application.admin.servicefacade.PersonnelServiceFacade)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 HierarchicalMessageSource (org.springframework.context.HierarchicalMessageSource)2 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)2 FooServiceImpl (example.scannable.FooServiceImpl)1 Locale (java.util.Locale)1 TimeZone (java.util.TimeZone)1 LocalizationContext (javax.servlet.jsp.jstl.fmt.LocalizationContext)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 StaticListableBeanFactory (org.springframework.beans.factory.support.StaticListableBeanFactory)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 Theme (org.springframework.ui.context.Theme)1