KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > component > DefaultComponentManager


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.component;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.Map JavaDoc;
54
55 import com.anthonyeden.lib.config.Configuration;
56 import com.anthonyeden.lib.config.ConfigurationException;
57 import com.anthonyeden.lib.util.ClassUtilities;
58 import com.anthonyeden.lib.util.MessageUtilities;
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61 import org.jpublish.JPublishEngine;
62 import org.jpublish.SiteContext;
63
64 /**
65  * Implementation of the ComponentManager interface which maintains a map of components in memory.
66  *
67  * @author Anthony Eden
68  */

69
70 public class DefaultComponentManager implements ComponentManager {
71
72     private static Log log = LogFactory.getLog(DefaultComponentManager.class);
73
74     private Map JavaDoc components = new HashMap JavaDoc();
75     private SiteContext siteContext;
76
77     /**
78      * Set the SiteContext.
79      *
80      * @param siteContext The site context
81      */

82
83     public void setSiteContext(SiteContext siteContext) {
84         this.siteContext = siteContext;
85     }
86
87     /**
88      * Get a component.
89      *
90      * @param id The id of the component
91      * @return The ComponentInstance
92      * @throws ComponentNotFoundException
93      */

94
95     public Component getComponent(String JavaDoc id) throws ComponentNotFoundException {
96         return (Component) components.get(id);
97     }
98
99     /**
100      * Return an Iterator of all components.
101      *
102      * @return An Iterator of Components
103      */

104
105     public Iterator JavaDoc getComponents() {
106         return components.values().iterator();
107     }
108
109     /**
110      * Load the ComponentManager's configuration.
111      *
112      * @param configuration The Configuration object
113      * @throws ConfigurationException
114      */

115
116     public void loadConfiguration(Configuration configuration)
117             throws ConfigurationException {
118         Configuration componentsElement = configuration.getChild("components");
119         Iterator JavaDoc componentElements =
120                 componentsElement.getChildren("component").iterator();
121         while (componentElements.hasNext()) {
122             String JavaDoc componentClassName = null;
123             String JavaDoc componentId = null;
124             try {
125                 Configuration componentElement =
126                         (Configuration) componentElements.next();
127
128                 componentId = componentElement.getAttribute("id");
129                 componentClassName = componentElement.getAttribute("classname");
130
131                 Component comp = (Component) ClassUtilities.loadClass(componentClassName).newInstance();
132                 comp.setSiteContext(siteContext);
133
134                 log.info("Loaded component " + componentId + " [" +
135                         componentClassName + "]");
136
137                 comp.loadConfiguration(componentElement);
138                 if (log.isDebugEnabled()) {
139                     log.debug("Component " + componentId +
140                             " configuration loaded");
141                 }
142
143                 components.put(componentId, comp);
144             } catch (ClassNotFoundException JavaDoc e) {
145                 Object JavaDoc[] args = {componentId, componentClassName};
146                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
147                         JPublishEngine.MESSAGE_PACKAGE,
148                         "componentClassNotFound", args);
149                 throw new ConfigurationException(msg, e);
150             } catch (Exception JavaDoc e) {
151                 Object JavaDoc[] args = {componentId};
152                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
153                         JPublishEngine.MESSAGE_PACKAGE, "componentLoadError", args);
154                 log.error(msg);
155                 throw new ConfigurationException(msg, e);
156             }
157         }
158     }
159
160 }
161
Popular Tags