KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > config > ControllerConfigurer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Jan-Frederic Linde <jfl@neteye.de>
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.config;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
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 JavaDoc {
81         Assert.notNull(transactionManager, "A transactionManager must be set.");
82         configureComponenControllers();
83         configureComponentListControllers();
84     }
85
86     protected void configureComponenControllers() {
87         Map JavaDoc controllers = applicationContext.getBeansOfType(
88                         ComponentController.class);
89
90         Iterator JavaDoc it = controllers.entrySet().iterator();
91         while (it.hasNext()) {
92             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc controllers = applicationContext.getBeansOfType(
110                         ComponentListController.class);
111
112         Iterator JavaDoc it = controllers.entrySet().iterator();
113         while (it.hasNext()) {
114             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc controllerId = (String JavaDoc) entry.getKey();
134             repository.registerController(controllerId, controller);
135             String JavaDoc[] 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