001 package org.springframework.richclient.context.support; 002 003 import java.util.Locale; 004 005 import org.springframework.beans.BeansException; 006 import org.springframework.beans.factory.BeanInitializationException; 007 import org.springframework.beans.factory.config.BeanPostProcessor; 008 import org.springframework.context.MessageSource; 009 import org.springframework.context.NoSuchMessageException; 010 import org.springframework.context.support.DefaultMessageSourceResolvable; 011 import org.springframework.richclient.core.TitleConfigurable; 012 import org.springframework.util.StringUtils; 013 014 public class TitleConfigurableBeanPostProcessor implements BeanPostProcessor { 015 /** The key fragment used to retrieve the title for a given object. */ 016 public static final String TITLE_KEY = "title"; 017 018 private MessageSource messageSource; 019 020 public TitleConfigurableBeanPostProcessor(MessageSource messageSource) { 021 this.messageSource = messageSource; 022 } 023 024 public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { 025 if (bean instanceof TitleConfigurable) { 026 TitleConfigurable configurable = (TitleConfigurable) bean; 027 028 try { 029 String title = messageSource.getMessage(new DefaultMessageSourceResolvable(name + "." + TITLE_KEY), 030 Locale.getDefault()); 031 032 if (StringUtils.hasText(title)) { 033 configurable.setTitle(title); 034 } 035 } 036 catch (NoSuchMessageException e) { 037 throw new BeanInitializationException("Unable to initialize bean " + name, e); 038 } 039 } 040 041 return bean; 042 } 043 044 public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { 045 return bean; 046 } 047 }