001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.jpublish.component;
019:
020: import java.util.Map;
021: import java.util.HashMap;
022: import java.util.Iterator;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import com.anthonyeden.lib.util.ClassUtilities;
028: import com.anthonyeden.lib.config.Configuration;
029: import com.anthonyeden.lib.config.ConfigurationException;
030:
031: import org.jpublish.SiteContext;
032: import org.jpublish.ComponentManager;
033: import org.jpublish.JPublishComponent;
034:
035: /** Implementation of the ComponentManager interface which maintains
036: a map of components in memory.
037:
038: @author Anthony Eden
039: @since 2.0
040: */
041:
042: public class InMemoryComponentManager implements ComponentManager {
043:
044: private static final Log log = LogFactory
045: .getLog(InMemoryComponentManager.class);
046:
047: private Map components = new HashMap();
048: private SiteContext siteContext;
049:
050: /** Set the SiteContext.
051:
052: @param siteContext The site context
053: */
054:
055: public void setSiteContext(SiteContext siteContext) {
056: this .siteContext = siteContext;
057: }
058:
059: /** Get a component.
060:
061: @param id The id of the component
062: @return The ComponentInstance
063: @throws Exception
064: */
065:
066: public JPublishComponent getComponent(String id) throws Exception {
067: return (JPublishComponent) components.get(id);
068: }
069:
070: /** Return an Iterator of all components.
071:
072: @return An Iterator of Components
073: */
074:
075: public Iterator getComponents() {
076: return components.values().iterator();
077: }
078:
079: /** Load the ComponentManager's configuration.
080:
081: @param configuration The Configuration object
082: @throws ConfigurationException
083: */
084:
085: public void loadConfiguration(Configuration configuration)
086: throws ConfigurationException {
087: Configuration componentsElement = configuration
088: .getChild("components");
089: Iterator componentElements = componentsElement.getChildren(
090: "component").iterator();
091: while (componentElements.hasNext()) {
092: try {
093: Configuration componentElement = (Configuration) componentElements
094: .next();
095:
096: String componentId = componentElement
097: .getAttribute("id");
098: String componentClassName = componentElement
099: .getAttribute("classname");
100:
101: JPublishComponent comp = (JPublishComponent) ClassUtilities
102: .loadClass(componentClassName).newInstance();
103: comp.setSiteContext(siteContext);
104:
105: log.info("Loaded component " + componentId + " ["
106: + componentClassName + "]");
107:
108: comp.loadConfiguration(componentElement);
109: if (log.isDebugEnabled())
110: log.debug("Component " + componentId
111: + " configuration loaded");
112:
113: components.put(componentId, comp);
114: } catch (Exception e) {
115: log.error("Error loading component");
116: e.printStackTrace();
117: throw new ConfigurationException(e);
118: }
119: }
120: }
121:
122: }
|