001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse;
004:
005: import fitnesse.wiki.*;
006: import fitnesse.html.HtmlPageFactory;
007: import fitnesse.responders.ResponderFactory;
008: import fitnesse.authentication.*;
009: import fitnesse.wikitext.WidgetBuilder;
010: import java.util.*;
011: import java.io.*;
012: import java.lang.reflect.*;
013:
014: public class ComponentFactory {
015: private final String endl = System.getProperty("line.separator");
016:
017: public static final String PROPERTIES_FILE = "plugins.properties";
018:
019: public static final String WIKI_PAGE_CLASS = "WikiPage";
020:
021: public static final String HTML_PAGE_FACTORY = "HtmlPageFactory";
022:
023: public static final String RESPONDERS = "Responders";
024:
025: public static final String WIKI_WIDGETS = "WikiWidgets";
026:
027: public static final String AUTHENTICATOR = "Authenticator";
028:
029: private Properties loadedProperties;
030:
031: private String propertiesLocation;
032:
033: public ComponentFactory(String propertiesLocation) {
034: this (propertiesLocation, new Properties());
035: }
036:
037: public ComponentFactory(String propertiesLocation,
038: Properties properties) {
039: this .propertiesLocation = propertiesLocation;
040: this .loadedProperties = properties;
041: loadProperties();
042: }
043:
044: public void loadProperties() {
045: try {
046: loadedProperties.load(new FileInputStream(
047: propertiesLocation + "/" + PROPERTIES_FILE));
048: } catch (IOException e) {
049: // No properties files means all defaults are loaded
050: }
051: }
052:
053: private Object createComponent(String componentType)
054: throws Exception {
055: String componentClassName = loadedProperties
056: .getProperty(componentType);
057: if (componentClassName != null) {
058: Class componentClass = Class.forName(componentClassName);
059: Constructor constructor = componentClass
060: .getConstructor(new Class[] { Properties.class });
061: return constructor
062: .newInstance(new Object[] { loadedProperties });
063: }
064: return null;
065: }
066:
067: public WikiPage getRootPage(WikiPage defaultPage) throws Exception {
068: String rootPageClassName = loadedProperties
069: .getProperty(WIKI_PAGE_CLASS);
070: if (rootPageClassName != null) {
071: Class rootPageClass = Class.forName(rootPageClassName);
072: Method constructorMethod = rootPageClass.getMethod(
073: "makeRoot", new Class[] { Properties.class });
074: return (WikiPage) constructorMethod.invoke(rootPageClass,
075: new Object[] { loadedProperties });
076: } else
077: return defaultPage;
078: }
079:
080: public HtmlPageFactory getHtmlPageFactory(
081: HtmlPageFactory defaultPageFactory) throws Exception {
082: HtmlPageFactory htmlPageFactory = (HtmlPageFactory) createComponent(HTML_PAGE_FACTORY);
083: return htmlPageFactory == null ? defaultPageFactory
084: : htmlPageFactory;
085: }
086:
087: public String loadResponderPlugins(ResponderFactory responderFactory)
088: throws Exception {
089: StringBuffer buffer = new StringBuffer();
090: String responderList = loadedProperties.getProperty(RESPONDERS);
091: if (responderList != null) {
092: buffer.append("\tCustom responders loaded:").append(endl);
093: String[] responderPairs = responderList.split(",");
094: for (int i = 0; i < responderPairs.length; i++) {
095: String pair = responderPairs[i].trim();
096: String[] values = pair.split(":");
097: String responderKey = values[0];
098: Class responderClass = Class.forName(values[1]);
099: responderFactory.addResponder(responderKey,
100: responderClass);
101: buffer.append(
102: "\t\t" + responderKey + ":"
103: + responderClass.getName())
104: .append(endl);
105: }
106: }
107: return buffer.toString();
108: }
109:
110: public Authenticator getAuthenticator(
111: Authenticator defaultAuthenticator) throws Exception {
112: Authenticator authenticator = (Authenticator) createComponent(AUTHENTICATOR);
113: return authenticator == null ? defaultAuthenticator
114: : authenticator;
115: }
116:
117: public String loadWikiWidgetPlugins() throws Exception {
118: StringBuffer buffer = new StringBuffer();
119: String widgetList = loadedProperties.getProperty(WIKI_WIDGETS);
120: if (widgetList != null) {
121: List widgetClasses = new ArrayList(Arrays
122: .asList(WidgetBuilder.htmlWidgetClasses));
123: buffer.append("\tCustom wiki widgets loaded:").append(endl);
124: String[] widgetNames = widgetList.split(",");
125: for (int i = 0; i < widgetNames.length; i++) {
126: String widgetName = widgetNames[i].trim();
127: Class widgetClass = Class.forName(widgetName);
128: widgetClasses.add(widgetClass);
129: buffer.append("\t\t" + widgetClass.getName()).append(
130: endl);
131: }
132: Class[] widgetClassesArray = (Class[]) widgetClasses
133: .toArray(new Class[] {});
134: WidgetBuilder.htmlWidgetBuilder = new WidgetBuilder(
135: widgetClassesArray);
136: }
137: return buffer.toString();
138: }
139: }
|