1 package org.apache.turbine.services.assemblerbroker;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 // JDK Stuff
58 import java.util.Vector;
59 import java.util.Hashtable;
60
61 // Turbine Stuff
62 import org.apache.turbine.modules.Assembler;
63 import org.apache.turbine.services.TurbineServices;
64 import org.apache.turbine.services.TurbineBaseService;
65 import org.apache.turbine.services.InitializationException;
66 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
67 import org.apache.turbine.services.resources.TurbineResources;
68 import org.apache.turbine.util.TurbineException;
69
70 /***
71 * TurbineAssemblerBrokerService allows assemblers (like screens,
72 * actions and layouts) to be loaded from one or more AssemblerFactory
73 * classes. AssemblerFactory classes are registered with this broker
74 * by adding them to the TurbineResources.properties file.
75 *
76 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
77 */
78 public class TurbineAssemblerBrokerService
79 extends TurbineBaseService
80 implements AssemblerBrokerService
81
82 {
83 /*** A structure that holds the registered AssemblerFactories*/
84 private Hashtable factories = null;
85
86 /***
87 * Get a list of AssemblerFactories of a certain type
88 */
89 private Vector getFactoryGroup (String type)
90 {
91 if (!factories.containsKey (type))
92 {
93 factories.put (type, new Vector());
94 }
95 return (Vector)factories.get(type);
96 }
97
98 /***
99 * Utiltiy method to register all factories for a given
100 * type.
101 */
102 private void registerFactories (String type)
103 throws TurbineException
104 {
105 String key = TurbineServices.SERVICE_PREFIX+
106 AssemblerBrokerService.SERVICE_NAME+
107 "."+
108 type;
109
110 String[] names = TurbineResources.getStringArray(key);
111
112 // Log.info ("Registering " + names.length + " " + type + " factories.");
113
114 for (int i=0; i<names.length; i++)
115 {
116 try
117 {
118 Object o = Class.forName ( names[i] ).newInstance();
119 registerFactory ( type, (AssemblerFactory)o );
120 }
121 // these must be passed to the VM
122 catch(ThreadDeath e)
123 {
124 throw e;
125 }
126 catch(OutOfMemoryError e)
127 {
128 throw e;
129 }
130 // when using Class.forName(), NoClassDefFoundErrors are likely
131 // to happen (missing jar files)
132 catch (Throwable t)
133 {
134 throw new TurbineException("Failed registering " + type + " factories", t);
135 }
136 }
137 }
138
139
140 /***
141 * Initializes the AssemblerBroker and loads the AssemblerFactory
142 * classes registerd in TurbineResources.Properties.
143 */
144 public void init()
145 throws InitializationException
146 {
147 factories = new Hashtable();
148 try
149 {
150 registerFactories (AssemblerBrokerService.ACTION_TYPE);
151 registerFactories (AssemblerBrokerService.SCREEN_TYPE);
152 registerFactories (AssemblerBrokerService.NAVIGATION_TYPE);
153 registerFactories (AssemblerBrokerService.LAYOUT_TYPE);
154 registerFactories (AssemblerBrokerService.PAGE_TYPE);
155 registerFactories (AssemblerBrokerService.SCHEDULEDJOB_TYPE);
156 }
157 catch(TurbineException e)
158 {
159 throw new InitializationException("AssemblerBrokerService failed to initailize", e);
160 }
161 setInit(true);
162 }
163
164 /***
165 * Register a new AssemblerFactory under a certain type
166 */
167 public void registerFactory( String type, AssemblerFactory factory )
168 {
169 getFactoryGroup(type).add (factory);
170 }
171
172 /***
173 * Attempt to retrieve an Assembler of a given type with
174 * a name. Cycle through all the registered AssemblerFactory
175 * classes of type and retrun the first non-null assembly
176 * found. If an assembly was not found return null.
177 */
178 public Assembler getAssembler( String type, String name )
179 throws TurbineException
180 {
181 Vector facs = getFactoryGroup(type);
182
183 for (int i=0; i<facs.size(); i++)
184 {
185 AssemblerFactory fac = (AssemblerFactory)facs.get(i);
186 Assembler assembler = null;
187 try
188 {
189 assembler = fac.getAssembler (name);
190 }
191 catch (Exception e)
192 {
193 throw new TurbineException("Failed to find the "+type+" named "+name, e);
194 }
195
196 if (assembler != null)
197 return assembler;
198 }
199 return null;
200 }
201 }
This page was automatically generated by Maven