1 24 package org.riotfamily.components.config; 25 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import org.riotfamily.cachius.Cache; 30 import org.riotfamily.components.ComponentController; 31 import org.riotfamily.components.ComponentListController; 32 import org.riotfamily.components.ComponentListLocator; 33 import org.riotfamily.components.ComponentRepository; 34 import org.riotfamily.components.dao.ComponentDao; 35 import org.springframework.beans.factory.InitializingBean; 36 import org.springframework.context.ApplicationContext; 37 import org.springframework.context.ApplicationContextAware; 38 import org.springframework.transaction.PlatformTransactionManager; 39 import org.springframework.util.Assert; 40 41 public class ControllerConfigurer implements ApplicationContextAware, 42 InitializingBean { 43 44 private ApplicationContext applicationContext; 45 46 private Cache cache; 47 48 private ComponentDao componentDao; 49 50 private ComponentListLocator locator; 51 52 private ComponentRepository repository; 53 54 private PlatformTransactionManager transactionManager; 55 56 public void setApplicationContext(ApplicationContext applicationContext) { 57 this.applicationContext = applicationContext; 58 } 59 60 public void setCache(Cache cache) { 61 this.cache = cache; 62 } 63 64 public void setComponentDao(ComponentDao componentDao) { 65 this.componentDao = componentDao; 66 } 67 68 public void setLocator(ComponentListLocator locator) { 69 this.locator = locator; 70 } 71 72 public void setTransactionManager(PlatformTransactionManager transactionManager) { 73 this.transactionManager = transactionManager; 74 } 75 76 public void setRepository(ComponentRepository repository) { 77 this.repository = repository; 78 } 79 80 public void afterPropertiesSet() throws Exception { 81 Assert.notNull(transactionManager, "A transactionManager must be set."); 82 configureComponenControllers(); 83 configureComponentListControllers(); 84 } 85 86 protected void configureComponenControllers() { 87 Map controllers = applicationContext.getBeansOfType( 88 ComponentController.class); 89 90 Iterator it = controllers.entrySet().iterator(); 91 while (it.hasNext()) { 92 Map.Entry entry = (Map.Entry ) it.next(); 93 ComponentController controller = 94 (ComponentController) entry.getValue(); 95 96 controller.setTransactionManager(transactionManager); 97 98 if (controller.getComponentDao() == null) { 99 controller.setComponentDao(componentDao); 100 } 101 if (controller.getComponentRepository() == null) { 102 controller.setComponentRepository(repository); 103 } 104 } 105 } 106 107 protected void configureComponentListControllers() { 108 repository.clearControllers(); 109 Map controllers = applicationContext.getBeansOfType( 110 ComponentListController.class); 111 112 Iterator it = controllers.entrySet().iterator(); 113 while (it.hasNext()) { 114 Map.Entry entry = (Map.Entry ) it.next(); 115 ComponentListController controller = 116 (ComponentListController) entry.getValue(); 117 118 controller.setTransactionManager(transactionManager); 119 120 if (controller.getCache() == null) { 121 controller.setCache(cache); 122 } 123 if (controller.getComponentDao() == null) { 124 controller.setComponentDao(componentDao); 125 } 126 if (controller.getComponentRepository() == null) { 127 controller.setComponentRepository(repository); 128 } 129 if (controller.getLocator() == null) { 130 controller.setLocator(locator); 131 } 132 133 String controllerId = (String ) entry.getKey(); 134 repository.registerController(controllerId, controller); 135 String [] aliases = applicationContext.getAliases(controllerId); 136 for (int i = 0; i < aliases.length; i++) { 137 repository.registerController(aliases[i], controller); 138 } 139 } 140 } 141 142 } 143 | Popular Tags |