001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.interceptor.component;
006:
007: import com.opensymphony.xwork.ObjectFactory;
008: import com.opensymphony.xwork.XworkException;
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.w3c.dom.Element;
012: import org.w3c.dom.Node;
013: import org.w3c.dom.NodeList;
014: import org.xml.sax.SAXException;
015:
016: import javax.xml.parsers.DocumentBuilder;
017: import javax.xml.parsers.DocumentBuilderFactory;
018: import javax.xml.parsers.FactoryConfigurationError;
019: import javax.xml.parsers.ParserConfigurationException;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.Serializable;
023: import java.util.*;
024:
025: /**
026: * @author joew@thoughtworks.com
027: * @author plightbo
028: * @version $Revision: 1071 $
029: */
030: public class ComponentConfiguration implements Serializable {
031:
032: private static final Log log = LogFactory
033: .getLog(ComponentConfiguration.class);
034:
035: private Map componentsByScope = new HashMap();
036:
037: public void addComponentDefinition(String className, String scope,
038: String enablerClass) {
039: getComponents(scope).add(
040: new ComponentDefinition(className, enablerClass));
041: }
042:
043: /**
044: * Configure a newly instantiated component manager by initializing all of the required components for the
045: * current configuration and setting up the component enablers.
046: *
047: * @param componentManager
048: */
049: public void configure(ComponentManager componentManager,
050: String scope) {
051: componentManager.reset();
052: for (Iterator iterator = getComponents(scope).iterator(); iterator
053: .hasNext();) {
054: ComponentDefinition componentDefinition = (ComponentDefinition) iterator
055: .next();
056:
057: Class resource = loadClass(componentDefinition.className);
058: Class enabler = loadClass(componentDefinition.enablerClass);
059: componentManager.addEnabler(resource, enabler);
060: }
061: componentManager.setConfig(this );
062: componentManager.setScope(scope);
063: }
064:
065: public boolean hasComponents(String scope) {
066: return componentsByScope.containsKey(scope);
067: }
068:
069: public void loadFromXml(InputStream is) throws IOException,
070: SAXException {
071: DocumentBuilder db = null;
072:
073: try {
074: db = DocumentBuilderFactory.newInstance()
075: .newDocumentBuilder();
076: } catch (ParserConfigurationException e) {
077: log.error("ParserConfigurationException occured", e);
078: } catch (FactoryConfigurationError factoryConfigurationError) {
079: log.error("FactoryConfigurationError occured",
080: factoryConfigurationError);
081: }
082:
083: Element componentsElement = db.parse(is).getDocumentElement();
084: NodeList components = componentsElement.getChildNodes();
085:
086: for (int i = 0; i < components.getLength(); i++) {
087: Node componentNode = components.item(i);
088:
089: if (componentNode instanceof Element) {
090: Element componentElement = (Element) componentNode;
091: NodeList componentElementChildren = componentElement
092: .getChildNodes();
093:
094: String className = null;
095: String scope = null;
096: String enabler = null;
097:
098: for (int j = 0; j < componentElementChildren
099: .getLength(); j++) {
100: Node elementChildNode = componentElementChildren
101: .item(j);
102:
103: if (elementChildNode instanceof Element) {
104: Element childElement = (Element) elementChildNode;
105:
106: if ("class".equals(childElement.getNodeName())) {
107: className = childElement.getChildNodes()
108: .item(0).getNodeValue();
109: } else if ("scope".equals(childElement
110: .getNodeName())) {
111: scope = childElement.getChildNodes()
112: .item(0).getNodeValue();
113: } else if ("enabler".equals(childElement
114: .getNodeName())) {
115: enabler = childElement.getChildNodes()
116: .item(0).getNodeValue();
117: }
118: }
119: }
120:
121: if ((className != null) && (scope != null)
122: && (enabler != null)) {
123: addComponentDefinition(className.trim(), scope
124: .trim(), enabler.trim());
125: }
126: }
127: }
128: }
129:
130: private List getComponents(String scope) {
131: if (!componentsByScope.containsKey(scope)) {
132: componentsByScope.put(scope, new ArrayList(10));
133: }
134:
135: return (List) componentsByScope.get(scope);
136: }
137:
138: private Class loadClass(String enablerClass) {
139: try {
140: return ObjectFactory.getObjectFactory().getClassInstance(
141: enablerClass);
142: } catch (ClassNotFoundException e) {
143: log.fatal("Cannot load class : " + enablerClass, e);
144: throw new XworkException("Cannot load class : "
145: + enablerClass);
146: }
147: }
148:
149: private class ComponentDefinition implements Serializable {
150: private String className;
151: private String enablerClass;
152:
153: public ComponentDefinition(String className, String enablerClass) {
154: this.enablerClass = enablerClass;
155: this.className = className;
156: }
157: }
158: }
|