001: /*
002: * Project: AMODA - Abstract Modeled Application
003: * Class: de.gulden.framework.amoda.generic.core.GenericApplicationEnvironmentFactory
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the AMODA 0.2 development branch,
009: * it is not released as a seperate version.
010: * For AMODA, see http://amoda.berlios.de/.
011: *
012: * This is licensed under the GNU Lesser General Public License (LGPL)
013: * and comes with NO WARRANTY.
014: *
015: * Author: Jens Gulden
016: * Email: amoda@jensgulden.de
017: */
018:
019: package de.gulden.framework.amoda.generic.core;
020:
021: import de.gulden.framework.amoda.generic.metadata.*;
022: import de.gulden.framework.amoda.generic.option.*;
023: import de.gulden.framework.amoda.model.core.*;
024: import de.gulden.framework.amoda.model.core.ApplicationEnvironmentFactory;
025: import de.gulden.util.Toolbox;
026: import de.gulden.util.xml.*;
027: import de.gulden.util.xml.XMLToolbox;
028: import de.gulden.util.xml.serializer.*;
029: import java.lang.*;
030: import java.util.*;
031: import javax.xml.parsers.*;
032: import org.w3c.dom.*;
033:
034: /**
035: * Class GenericApplicationEnvironmentFactory.
036: *
037: * @author Jens Gulden
038: * @version snapshot-beautyj-1.1
039: */
040: public abstract class GenericApplicationEnvironmentFactory implements
041: ApplicationEnvironmentFactory {
042:
043: // ------------------------------------------------------------------------
044: // --- fields ---
045: // ------------------------------------------------------------------------
046:
047: protected String[] args;
048:
049: protected DocumentBuilder documentBuilder;
050:
051: // ------------------------------------------------------------------------
052: // --- constructors ---
053: // ------------------------------------------------------------------------
054:
055: public GenericApplicationEnvironmentFactory() {
056: try {
057: setDocumentBuilder(XMLToolbox.createDefaultDocumentBuilder(
058: false, false, true, true, false, true));
059: } catch (Exception e) {
060: fatalError("cannot create DocumentBuilder", e);
061: }
062: }
063:
064: public GenericApplicationEnvironmentFactory(String[] args) {
065: this ();
066: setArgs(args);
067: }
068:
069: // ------------------------------------------------------------------------
070: // --- methods ---
071: // ------------------------------------------------------------------------
072:
073: public ApplicationEnvironment createApplicationEnvironment() {
074: try {
075: XMLSerializerFactory factory = getXMLSerializerFactory();//new AutoReflectionXMLSerializerFactory();
076: XMLSerializer ser = factory.createXMLSerializer();
077: GenericApplicationEnvironment env = (GenericApplicationEnvironment) (getApplicationEnvironmentClass()
078: .newInstance());
079: env.setFactory(this );
080: initFromXML(env, ser, this .getClass()); // recursively initialize
081: return env;
082: } catch (de.gulden.util.xml.XMLException e) {
083: fatalError(
084: "cannot parse application environment XML configuration",
085: e);
086: return null;
087: } catch (Exception iae) { // IllegalAccessException, InstatiationException
088: fatalError("cannot create application environment", iae);
089: return null;
090: }
091: }
092:
093: public String[] getArgs() {
094: return args;
095: }
096:
097: public void setArgs(String[] _args) {
098: args = _args;
099: }
100:
101: public DocumentBuilder getDocumentBuilder() {
102: return documentBuilder;
103: }
104:
105: public void setDocumentBuilder(DocumentBuilder _documentBuilder) {
106: documentBuilder = _documentBuilder;
107: }
108:
109: public XMLSerializerFactory getXMLSerializerFactory() {
110: de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializerFactory factory = new de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializerFactory();
111: factory
112: .registerTagname(
113: "metadata",
114: de.gulden.framework.amoda.generic.metadata.GenericMetadata.class); // inner elements of metadata parsed by GenericMetadata itself
115: factory
116: .registerTagname(
117: "feature-group",
118: de.gulden.framework.amoda.generic.core.GenericFeatureGroup.class);
119: factory
120: .registerTagname(
121: "feature",
122: de.gulden.framework.amoda.generic.core.GenericFeature.class);
123: factory
124: .registerTagname(
125: "options",
126: de.gulden.framework.amoda.generic.option.GenericOptions.class);
127: factory
128: .registerTagname(
129: "option",
130: de.gulden.framework.amoda.generic.option.GenericOptionEntry.class);
131: factory
132: .registerTagname(
133: "option-choice",
134: de.gulden.framework.amoda.generic.option.GenericOptionChoice.class);
135: factory
136: .registerTagname(
137: "options-group",
138: de.gulden.framework.amoda.generic.option.GenericOptions.class);
139: factory
140: .registerTagname(
141: "command",
142: de.gulden.framework.amoda.generic.behaviour.CommandWrapper.class);
143: factory
144: .registerTagname(
145: "condition",
146: de.gulden.framework.amoda.generic.behaviour.ConditionWrapper.class);
147: factory
148: .registerTagname(
149: "message",
150: de.gulden.framework.amoda.generic.interaction.GenericMessage.class);
151: factory
152: .registerTagname(
153: "error-message",
154: de.gulden.framework.amoda.generic.interaction.GenericErrorMessage.class);
155: factory
156: .registerTagname(
157: "question",
158: de.gulden.framework.amoda.generic.interaction.GenericQuestion.class);
159: factory
160: .registerTagname(
161: "dialog",
162: de.gulden.framework.amoda.generic.interaction.GenericDialog.class);
163: factory
164: .registerTagname(
165: "wizard",
166: de.gulden.framework.amoda.generic.interaction.GenericWizard.class);
167: factory
168: .registerTagname(
169: "module",
170: de.gulden.framework.amoda.model.core.ApplicationModule.class,
171: true); // requires attribute "class="
172: return factory;
173: }
174:
175: protected Document getEnvironmentConfigurationDocument(Class cls) {
176: try {
177: java.io.InputStream inputStream = GenericApplicationEnvironment
178: .getXMLResourceForClass(cls).openStream();
179: Document d = getDocumentBuilder().parse(inputStream);
180: return d;
181: } catch (Exception e) {
182: fatalError(
183: "cannot initialize environment XML configuration",
184: e);
185: return null;
186: }
187: }
188:
189: protected abstract Class getApplicationEnvironmentClass();
190:
191: protected void initFromXML(ApplicationEnvironment env,
192: XMLSerializer ser, Class factoryClass) throws XMLException {
193: // first init by factory-superclass
194: Class super class = factoryClass.getSuperclass();
195: if (ApplicationEnvironmentFactory.class
196: .isAssignableFrom(super class)) {
197: initFromXML(env, ser, super class);
198: }
199: org.w3c.dom.Element applicationEnvironmentTag = getEnvironmentConfigurationDocument(
200: factoryClass).getDocumentElement();
201: ser.xmlDeserialize(env, applicationEnvironmentTag);
202: }
203:
204: // ------------------------------------------------------------------------
205: // --- static method ---
206: // ------------------------------------------------------------------------
207:
208: public static void fatalError(String msg, Throwable t) {
209: System.out.println(msg);
210: System.out.println(t.getMessage());
211: t.printStackTrace(System.out);
212: if (t instanceof de.gulden.util.xml.XMLException) {
213: de.gulden.util.xml.XMLException exc = (de.gulden.util.xml.XMLException) t;
214: Throwable wrapped = exc.getWrappedThrowable();
215: if (wrapped != null) {
216: System.out.println("*** WRAPPED EXCEPTION ***");
217: System.out.println(wrapped.getMessage());
218: wrapped.printStackTrace(System.out);
219: }
220: }
221: System.exit(5);
222: }
223:
224: } // end GenericApplicationEnvironmentFactory
|