1 package org.apache.turbine.services.assemblerbroker;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.apache.turbine.modules.Assembler;
32 import org.apache.turbine.services.InitializationException;
33 import org.apache.turbine.services.TurbineBaseService;
34 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
35 import org.apache.turbine.util.TurbineException;
36
37 /***
38 * TurbineAssemblerBrokerService allows assemblers (like screens,
39 * actions and layouts) to be loaded from one or more AssemblerFactory
40 * classes. AssemblerFactory classes are registered with this broker
41 * by adding them to the TurbineResources.properties file.
42 *
43 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
44 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
45 * @version $Id: TurbineAssemblerBrokerService.java 534527 2007-05-02 16:10:59Z tv $
46 */
47 public class TurbineAssemblerBrokerService
48 extends TurbineBaseService
49 implements AssemblerBrokerService
50 {
51 /*** Logging */
52 private static Log log
53 = LogFactory.getLog(TurbineAssemblerBrokerService.class);
54
55 /*** A structure that holds the registered AssemblerFactories */
56 private Map factories = null;
57
58 /***
59 * Get a list of AssemblerFactories of a certain type
60 *
61 * @param type type of Assembler
62 * @return list of AssemblerFactories
63 */
64 private List getFactoryGroup(String type)
65 {
66 if (!factories.containsKey(type))
67 {
68 factories.put(type, new Vector());
69 }
70 return (List) factories.get(type);
71 }
72
73 /***
74 * Utiltiy method to register all factories for a given type.
75 *
76 * @param type type of Assembler
77 * @throws TurbineException
78 */
79 private void registerFactories(String type)
80 throws TurbineException
81 {
82 List names = getConfiguration().getList(type);
83
84 log.info("Registering " + names.size() + " " + type + " factories.");
85
86 for (Iterator it = names.iterator(); it.hasNext(); )
87 {
88 String factory = (String) it.next();
89 try
90 {
91 Object o = Class.forName(factory).newInstance();
92 registerFactory(type, (AssemblerFactory) o);
93 }
94
95 catch (ThreadDeath e)
96 {
97 throw e;
98 }
99 catch (OutOfMemoryError e)
100 {
101 throw e;
102 }
103
104
105 catch (Throwable t)
106 {
107 throw new TurbineException("Failed registering " + type
108 + " factory: " + factory, t);
109 }
110 }
111 }
112
113 /***
114 * Initializes the AssemblerBroker and loads the AssemblerFactory
115 * classes registered in TurbineResources.Properties.
116 *
117 * @throws InitializationException
118 */
119 public void init()
120 throws InitializationException
121 {
122 factories = new HashMap();
123 try
124 {
125 registerFactories(AssemblerBrokerService.ACTION_TYPE);
126 registerFactories(AssemblerBrokerService.SCREEN_TYPE);
127 registerFactories(AssemblerBrokerService.NAVIGATION_TYPE);
128 registerFactories(AssemblerBrokerService.LAYOUT_TYPE);
129 registerFactories(AssemblerBrokerService.PAGE_TYPE);
130 registerFactories(AssemblerBrokerService.SCHEDULEDJOB_TYPE);
131 }
132 catch (TurbineException e)
133 {
134 throw new InitializationException(
135 "AssemblerBrokerService failed to initialize", e);
136 }
137 setInit(true);
138 }
139
140 /***
141 * Register a new AssemblerFactory under a certain type
142 *
143 * @param type type of Assembler
144 * @param factory factory to register
145 */
146 public void registerFactory(String type, AssemblerFactory factory)
147 {
148 getFactoryGroup(type).add(factory);
149 }
150
151 /***
152 * Attempt to retrieve an Assembler of a given type with
153 * a name. Cycle through all the registered AssemblerFactory
154 * classes of type and return the first non-null assembly
155 * found. If an assembly was not found return null.
156 *
157 * @param type type of Assembler
158 * @param name name of the requested Assembler
159 * @return an Assembler or null
160 * @throws TurbineException
161 */
162 public Assembler getAssembler(String type, String name)
163 throws TurbineException
164 {
165 List facs = getFactoryGroup(type);
166
167 Assembler assembler = null;
168 for (Iterator it = facs.iterator(); (assembler == null) && it.hasNext();)
169 {
170 AssemblerFactory fac = (AssemblerFactory) it.next();
171 try
172 {
173 assembler = fac.getAssembler(name);
174 }
175 catch (Exception e)
176 {
177 throw new TurbineException("Failed to load an assembler for "
178 + name + " from the "
179 + type + " factory "
180 + fac.getClass().getName(), e);
181 }
182 }
183 return assembler;
184 }
185 }