KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > flow > Loader


1 /*
2  * $Id: Loader.java,v 1.17 2004/06/27 17:42:14 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/flow/Loader.java,v $
4  */

5 package org.infohazard.maverick.flow;
6
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.servlet.ServletConfig JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.infohazard.maverick.util.XML;
16 import org.jdom.Document;
17 import org.jdom.Element;
18
19 /**
20  * Builds the tree of flow objects which process Maverick commands.
21  *
22  * created January 27, 2002
23  * @author Jeff Schnitzer
24  * @version $Revision: 1.17 $ $Date: 2004/06/27 17:42:14 $
25  */

26 public class Loader
27 {
28     /** xml tag for modules, value = 'modules'. */
29     protected final static String JavaDoc TAG_MODULES = "modules";
30     /** xml tag for view factories, value = 'view-factory'. */
31     protected final static String JavaDoc TAG_VIEWFACTORY = "view-factory";
32     /** xml tag for controller factories, value = 'controller-facotry'. */
33     protected final static String JavaDoc TAG_CONTROLLERFACTORY = "controller-factory";
34     /** xml tag for transform factories, value = 'transform-factory'. */
35     protected final static String JavaDoc TAG_TRANSFORMFACTORY = "transform-factory";
36     /** xml tag for shunt factories, value = 'shunt-factory'. */
37     protected final static String JavaDoc TAG_SHUNTFACTORY = "shunt-factory";
38     /** xml tag for shunt provider, value = 'provider'. */
39     protected final static String JavaDoc ATTR_PROVIDER = "provider";
40
41     /** xml tag for commands, value = 'commands'. */
42     protected final static String JavaDoc TAG_COMMANDS = "commands";
43     /** xml tag for a command, value = 'command'. */
44     protected final static String JavaDoc TAG_COMMAND = "command";
45     /** xml tag for a command name, value = 'name'. */
46     protected final static String JavaDoc ATTR_COMMAND_NAME = "name";
47
48     /** xml tag for views, value = 'views'. */
49     protected final static String JavaDoc TAG_VIEWS = "views";
50
51     /** xml tag for the default view type, value = 'default-view-type'. */
52     protected final static String JavaDoc PARAM_DEFAULT_VIEW_TYPE = "default-view-type";
53     /** xml tag for the default transform type, value = 'default-transform-type'. */
54     protected final static String JavaDoc PARAM_DEFAULT_TRANSFORM_TYPE = "default-transform-type";
55
56     /** Logger. */
57     private static Log log = LogFactory.getLog(Loader.class);
58
59     /** Commands get built into this map. */
60     protected Map JavaDoc commands = new HashMap JavaDoc();
61
62     /** Servlet config. */
63     protected ServletConfig JavaDoc servletCfg;
64
65     /** Encapsulates view and transform factories and handles decoration */
66     protected MasterFactory masterFact;
67
68     /** For tracking global views and managing view lists */
69     protected ViewRegistry viewReg;
70
71     /** For creating commands */
72     protected CommandFactory commandFact;
73
74     /**
75      * @param doc An already parsed JDOM Document of the config file.
76      * @param doc the configuration document
77      * @param dispatcherConfig servlet configuration of the dispatcher servlet
78      * @exception ConfigException
79      */

80     public Loader(Document doc, ServletConfig JavaDoc dispatcherConfig) throws ConfigException
81     {
82         this.servletCfg = dispatcherConfig;
83         this.masterFact = new MasterFactory(dispatcherConfig);
84
85         // Assume simple, non-shunted system. These can be replaced later if the
86
// user loads a ShuntFactory module.
87
this.viewReg = new ViewRegistrySimple(this.masterFact);
88         this.commandFact = new CommandFactory(this.viewReg);
89
90         this.setupCoreModules();
91
92         this.loadDocument(doc);
93     }
94
95     /**
96      * Get the commands.
97      * @return Commands map
98      */

99     public Map JavaDoc getCommands()
100     {
101         return this.commands;
102     }
103
104     /**
105      * setup the core modules.
106      * @exception ConfigException
107      */

108     protected void setupCoreModules() throws ConfigException
109     {
110         // Set up the basic transforms
111
TransformFactory docTrans = new org.infohazard.maverick.transform.DocumentTransformFactory();
112         docTrans.init(null, this.servletCfg);
113         this.masterFact.defineTransformFactory("document", docTrans);
114
115         TransformFactory xsltTrans = new org.infohazard.maverick.transform.XSLTransformFactory();
116         xsltTrans.init(null, this.servletCfg);
117         this.masterFact.defineTransformFactory("xslt", xsltTrans);
118
119         // Set up the basic views
120
ViewFactory document = new org.infohazard.maverick.view.DocumentViewFactory();
121         document.init(null, this.servletCfg);
122         this.masterFact.defineViewFactory("document", document);
123
124         ViewFactory redirect = new org.infohazard.maverick.view.RedirectViewFactory();
125         redirect.init(null, this.servletCfg);
126         this.masterFact.defineViewFactory("redirect", redirect);
127
128         ViewFactory trivial = new org.infohazard.maverick.view.TrivialViewFactory();
129         trivial.init(null, this.servletCfg);
130         this.masterFact.defineViewFactory("trivial", trivial);
131
132         ViewFactory nullViewFact = new org.infohazard.maverick.view.NullViewFactory();
133         nullViewFact.init(null, this.servletCfg);
134         this.masterFact.defineViewFactory("null", nullViewFact);
135     }
136
137     /**
138      * load the configuration document.
139      * @param doc the configuration document
140      * @exception ConfigException
141      */

142     protected void loadDocument(Document doc) throws ConfigException
143     {
144         Element root = doc.getRootElement();
145
146         // Maybe default transform type was specified
147
String JavaDoc defaultTransformType = XML.getValue(root, PARAM_DEFAULT_TRANSFORM_TYPE);
148         if (defaultTransformType != null)
149             this.masterFact.setDefaultTransformType(defaultTransformType);
150
151         // Maybe default view type was specified
152
String JavaDoc defaultViewType = XML.getValue(root, PARAM_DEFAULT_VIEW_TYPE);
153         if (defaultViewType != null)
154             this.masterFact.setDefaultViewType(defaultViewType);
155
156         // load modules. probably won't be more than one element, but who knows.
157
// don't need to worry about default type being reset because it won't be.
158
Iterator JavaDoc allModulesIt = root.getChildren(TAG_MODULES).iterator();
159         while (allModulesIt.hasNext())
160         {
161             Element modules = (Element)allModulesIt.next();
162             this.loadModules(modules);
163         }
164
165         // load global views; can be many elements
166
Iterator JavaDoc allViewsIt = root.getChildren(TAG_VIEWS).iterator();
167         while (allViewsIt.hasNext())
168         {
169             Element viewsNode = (Element)allViewsIt.next();
170             this.viewReg.defineGlobalViews(viewsNode);
171         }
172
173         // load commands; can be many elements
174
Iterator JavaDoc allCommandsIt = root.getChildren(TAG_COMMANDS).iterator();
175         while (allCommandsIt.hasNext())
176         {
177             Element commandsNode = (Element)allCommandsIt.next();
178
179             Iterator JavaDoc commandIt = commandsNode.getChildren(TAG_COMMAND).iterator();
180             while (commandIt.hasNext())
181             {
182                 Element commandNode = (Element)commandIt.next();
183
184                 String JavaDoc commandName = commandNode.getAttributeValue(ATTR_COMMAND_NAME);
185
186                 log.info("Creating command: " + commandName);
187
188                 Command cmd = this.commandFact.createCommand(commandNode);
189
190                 this.commands.put(commandName, cmd);
191             }
192         }
193     }
194
195     /**
196      * load the modules.
197      * @param modulesNode the xml node of the modules
198      * @exception ConfigException
199      */

200     protected void loadModules(Element modulesNode) throws ConfigException
201     {
202         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
203         if (classLoader == null)
204         {
205             classLoader = DefaultControllerFactory.class.getClassLoader();
206         }
207
208         // can override the core modules already installed
209
masterFact.defineTransformFactories(modulesNode.getChildren(TAG_TRANSFORMFACTORY));
210         masterFact.defineViewFactories(modulesNode.getChildren(TAG_VIEWFACTORY));
211
212         Element shuntFactoryNode = modulesNode.getChild(TAG_SHUNTFACTORY);
213         if (shuntFactoryNode != null)
214         {
215             String JavaDoc providerName = shuntFactoryNode.getAttributeValue(ATTR_PROVIDER);
216
217             if (providerName == null)
218                 throw new ConfigException("Not a valid shunt factory node: "
219                     + XML.toString(shuntFactoryNode));
220
221             Class JavaDoc providerClass;
222             ShuntFactory instance;
223             try
224             {
225                 providerClass = classLoader.loadClass(providerName);
226                 instance = (ShuntFactory)providerClass.newInstance();
227             }
228             catch (Exception JavaDoc ex)
229             {
230                 throw new ConfigException("Unable to define shunt factory "
231                     + providerName, ex);
232             }
233
234             log.info("Using shunt factory " + providerName);
235             // Give the factory an opportunity to initialize itself from any subnodes
236
instance.init(shuntFactoryNode, this.servletCfg);
237
238             // Replace the RendererRegistry and set view registry property on the cmd factory
239
this.viewReg = new ViewRegistryShunted(this.masterFact, instance);
240             this.commandFact.setViewRegistry(this.viewReg);
241         }
242         Element controllerFactoryNode = modulesNode.getChild(TAG_CONTROLLERFACTORY);
243         if (controllerFactoryNode != null)
244         {
245             String JavaDoc providerName = controllerFactoryNode.getAttributeValue(ATTR_PROVIDER);
246
247             if (providerName == null)
248                 throw new ConfigException("Not a valid controller factory node: "
249                     + XML.toString(controllerFactoryNode));
250
251             Class JavaDoc providerClass;
252             ControllerFactory instance;
253             try
254             {
255                 providerClass = classLoader.loadClass(providerName);
256                 instance = (ControllerFactory)providerClass.newInstance();
257             }
258             catch (Exception JavaDoc ex)
259             {
260                 throw new ConfigException("Unable to define controller factory "
261                     + providerName, ex);
262             }
263
264             log.info("Using controller factory " + providerName);
265
266             // Give the factory an opportunity to initialize itself from any subnodes
267
instance.init(controllerFactoryNode, this.servletCfg);
268
269             this.commandFact.setControllerFactory(instance);
270         }
271     }
272
273 }
274
275
Popular Tags