001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.xml;
011:
012: import java.io.File;
013: import java.util.*;
014:
015: import org.w3c.dom.*;
016:
017: import org.mmbase.module.core.*;
018: import org.mmbase.model.*;
019: import org.mmbase.storage.search.SearchQueryException;
020: import org.mmbase.util.logging.Logger;
021: import org.mmbase.util.logging.Logging;
022: import org.mmbase.util.xml.applicationdata.ContextDepthDataWriter;
023: import org.mmbase.util.xml.applicationdata.FullBackupDataWriter;
024: import org.mmbase.util.*;
025:
026: /**
027: * @javadoc
028: * @author Daniel Ockeloen
029: * @version $Id: ApplicationWriter.java,v 1.7 2008/01/10 16:29:36 michiel Exp $
030: */
031: public class ApplicationWriter extends DocumentWriter {
032:
033: private static final Logger log = Logging
034: .getLoggerInstance(ApplicationWriter.class);
035:
036: protected ApplicationReader reader;
037: protected MMBase mmbase;
038:
039: /**
040: * Constructs the document writer.
041: * The constructor calls its super to create a basic document, based on the application document type.
042: * @param reader the reader for the original application
043: * @param mmbase the mmbase instance to get the application data from
044: */
045: public ApplicationWriter(ApplicationReader reader, MMBase mmbase)
046: throws DOMException {
047: super ("application", ApplicationReader.PUBLIC_ID_APPLICATION,
048: XMLEntityResolver.DOMAIN
049: + XMLEntityResolver.DTD_SUBPATH
050: + ApplicationReader.DTD_APPLICATION);
051: this .reader = reader;
052: this .mmbase = mmbase;
053: getMessageRetriever("org.mmbase.util.xml.resources.applicationwriter");
054: }
055:
056: /**
057: * Generates the document. Can only be called once.
058: * @throws DOMException when an error occurred during generation
059: */
060: protected void generate() throws DOMException {
061: Element root = document.getDocumentElement();
062: addComment("application.configuration", reader.getName(),
063: reader.getDescription(), root);
064: root.setAttribute("name", reader.getName());
065: root.setAttribute("maintainer", reader.getMaintainer());
066: root.setAttribute("version", "" + reader.getVersion());
067: root.setAttribute("autodeploy", "" + reader.hasAutoDeploy());
068:
069: addRequirements(root);
070: addNeededBuilderList(root);
071: addNeededRelDefList(root);
072: addAllowedRelationList(root);
073: addDataSourceList(root);
074: addRelationSourceList(root);
075: addContextSourceList(root);
076:
077: addComment("application.installnotice", root);
078: addContentElement("installnotice", reader.getInstallNotice(),
079: root);
080: addComment("application.description", root);
081: addContentElement("description", reader.getDescription(), root);
082: }
083:
084: protected void addRequirements(Element root) {
085: addComment("application.requirements", root);
086: Element elementSet = document.createElement("requirements");
087: root.appendChild(elementSet);
088: List<Map<String, String>> requirements = reader
089: .getRequirements();
090: for (Map<String, String> bset : requirements) {
091: Element requirement = document.createElement("requirement");
092: elementSet.appendChild(requirement);
093: requirement.setAttribute("name", bset.get("name"));
094: requirement.setAttribute("maintainer", bset
095: .get("maintainer"));
096: requirement.setAttribute("version", bset.get("version"));
097: String type = bset.get("type");
098: if (type == null)
099: type = "application";
100: requirement.setAttribute("type", type);
101: }
102: }
103:
104: protected void addNeededBuilderList(Element root) {
105: addComment("application.neededbuilderlist", root);
106: Element elementSet = document
107: .createElement("neededbuilderlist");
108: root.appendChild(elementSet);
109: List<Map<String, String>> builders = reader.getNeededBuilders();
110: for (Map<String, String> bset : builders) {
111: Element builder = document.createElement("builder");
112: elementSet.appendChild(builder);
113: builder.setAttribute("name", bset.get("name"));
114: builder.setAttribute("maintainer", bset.get("maintainer"));
115: builder.setAttribute("version", bset.get("version"));
116: }
117: }
118:
119: protected void addNeededRelDefList(Element root) {
120: addComment("application.neededreldeflist", root);
121: Element elementSet = document.createElement("neededreldeflist");
122: root.appendChild(elementSet);
123: List<Map<String, String>> reldefs = reader.getNeededRelDefs();
124: for (Map<String, String> bset : reldefs) {
125: Element reldef = document.createElement("reldef");
126: elementSet.appendChild(reldef);
127: reldef.setAttribute("source", bset.get("source"));
128: reldef.setAttribute("target", bset.get("target"));
129: reldef.setAttribute("direction", bset.get("direction"));
130: reldef.setAttribute("guisourcename", bset
131: .get("guisourcename"));
132: reldef.setAttribute("guitargetname", bset
133: .get("guitargetname"));
134: String builder = bset.get("builder");
135: if (builder != null) {
136: reldef.setAttribute("builder", builder);
137: }
138: }
139: }
140:
141: protected void addAllowedRelationList(Element root) {
142: addComment("application.allowedrelationlist", root);
143: Element elementSet = document
144: .createElement("allowedrelationlist");
145: root.appendChild(elementSet);
146: List<Map<String, String>> relations = reader
147: .getAllowedRelations();
148: for (Map<String, String> bset : relations) {
149: Element relation = document.createElement("relation");
150: elementSet.appendChild(relation);
151: relation.setAttribute("from", bset.get("from"));
152: relation.setAttribute("to", bset.get("to"));
153: relation.setAttribute("type", bset.get("type"));
154: }
155: }
156:
157: protected void addDataSourceList(Element root) {
158: addComment("application.datasourcelist", root);
159: Element elementSet = document.createElement("datasourcelist");
160: root.appendChild(elementSet);
161: List<Map<String, String>> sources = reader.getDataSources();
162: for (Map<String, String> bset : sources) {
163: Element source = document.createElement("datasource");
164: elementSet.appendChild(source);
165: source.setAttribute("path", bset.get("path"));
166: source.setAttribute("builder", bset.get("builder"));
167: }
168: }
169:
170: protected void addRelationSourceList(Element root) {
171: addComment("application.relationsourcelist", root);
172: Element elementSet = document
173: .createElement("relationsourcelist");
174: root.appendChild(elementSet);
175: List<Map<String, String>> sources = reader.getRelationSources();
176: for (Map<String, String> bset : sources) {
177: Element source = document.createElement("relationsource");
178: elementSet.appendChild(source);
179: source.setAttribute("path", bset.get("path"));
180: source.setAttribute("builder", bset.get("builder"));
181: }
182: }
183:
184: protected void addContextSourceList(Element root) {
185: addComment("application.contextsourcelist", root);
186: Element elementSet = document
187: .createElement("contextsourcelist");
188: root.appendChild(elementSet);
189: List<Map<String, String>> sources = reader.getContextSources();
190: for (Map<String, String> bset : sources) {
191: Element source = document.createElement("contextsource");
192: elementSet.appendChild(source);
193: source.setAttribute("path", bset.get("path"));
194: source.setAttribute("type", bset.get("type"));
195: source.setAttribute("goal", bset.get("goal"));
196: }
197: }
198:
199: /**
200: * Generates the documents for this application and store it as a set of files in the given path.
201: * @param targetPath the filepath (directory) where the configuration is to be stored
202: * @param logger This thing must receive the errors
203: * @throws TransformerException if one or more documents are malformed
204: * @throws IOException if one or more files cannot be written
205: * @throws SearchQueryException if data could not be obtained from the database
206: */
207: public void writeToPath(String targetPath, Logger logger)
208: throws java.io.IOException, org.xml.sax.SAXException,
209: SearchQueryException {
210: //writeToFile(targetPath + "/" + reader.getName() + ".xml");
211: CloudModel cm = ModelsManager.getModel(reader.getName());
212: log.info("CMW=" + cm);
213: if (cm != null)
214: cm
215: .writeToFile(targetPath + "/" + reader.getName()
216: + ".xml");
217:
218: // now the tricky part starts figure out what nodes to write
219: writeDateSources(targetPath, logger);
220: // now write the context files itself
221: writeContextSources(targetPath);
222: // now as a backup write all the needed builders
223: // that the application maker claimed we needed
224: writeBuilders(targetPath, logger);
225: logger.info("Writing Application file : " + targetPath + "/"
226: + reader.getName() + ".xml");
227: }
228:
229: private void writeDateSources(String targetPath, Logger logger)
230: throws org.xml.sax.SAXException, java.io.IOException,
231: SearchQueryException {
232: List<Map<String, String>> sources = reader.getContextSources();
233: for (Map<String, String> bset : sources) {
234: String path = bset.get("path");
235: String type = bset.get("type");
236: String goal = bset.get("goal");
237:
238: logger.info("save type : " + type);
239: logger.info("save goal : " + goal);
240:
241: if (type.equals("depth")) {
242: XMLContextDepthReader contextReader = new XMLContextDepthReader(
243: ResourceLoader.getConfigurationRoot()
244: .getDocument("/applications/" + path));
245: ContextDepthDataWriter.writeContext(reader,
246: contextReader, targetPath, mmbase, logger);
247: } else if (type.equals("full")) {
248: FullBackupDataWriter.writeContext(reader, targetPath,
249: mmbase, logger);
250: }
251: }
252: }
253:
254: private void writeContextSources(String targetPath)
255: throws org.xml.sax.SAXException, java.io.IOException {
256: List<Map<String, String>> sources = reader.getContextSources();
257: for (Map<String, String> bset : sources) {
258: String path = bset.get("path");
259: String type = bset.get("type");
260: if (type.equals("depth")) {
261: XMLContextDepthReader contextReader = new XMLContextDepthReader(
262: ResourceLoader.getConfigurationRoot()
263: .getDocument("/applications/" + path));
264: ContextDepthDataWriter.writeContextXML(contextReader,
265: targetPath + "/" + path);
266: }
267: }
268: }
269:
270: private void writeBuilders(String targetPath, Logger logger) {
271: // create the dir for the Data & resource files
272: File file = new File(targetPath + "/" + reader.getName()
273: + "/builders");
274: file.mkdirs();
275: // get the default model.
276: CloudModel cm = ModelsManager.getModel("default");
277: log.info("CM=" + cm);
278: List<Map<String, String>> builders = reader.getNeededBuilders();
279: for (Map<String, String> bset : builders) {
280: String name = bset.get("name");
281: MMObjectBuilder builder = mmbase.getBuilder(name);
282: if (builder != null) {
283: logger.info("save builder : " + name);
284: CloudModelBuilder cmb = cm.getModelBuilder(name);
285: cmb.writeToFile(targetPath + "/" + reader.getName()
286: + "/builders/" + name + ".xml");
287:
288: /*
289: BuilderWriter builderOut = new BuilderWriter(builder);
290: builderOut.setIncludeComments(includeComments());
291: builderOut.setExpandBuilder(false);
292: builderOut.writeToFile(targetPath + "/" + reader.getName() + "/builders/" + name + ".xml");
293: */
294: }
295: }
296: }
297:
298: }
|