View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.configuration;
22  
23  import goldengate.common.database.exception.GoldenGateDatabaseException;
24  import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
25  import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
26  import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
27  import goldengate.common.file.DirInterface;
28  import goldengate.common.logging.GgInternalLogger;
29  import goldengate.common.logging.GgInternalLoggerFactory;
30  import goldengate.common.xml.XmlDecl;
31  import goldengate.common.xml.XmlHash;
32  import goldengate.common.xml.XmlType;
33  import goldengate.common.xml.XmlUtil;
34  import goldengate.common.xml.XmlValue;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import openr66.context.task.TaskType;
42  import openr66.database.DbConstant;
43  import openr66.database.data.DbRule;
44  import openr66.protocol.configuration.Configuration;
45  import openr66.protocol.exception.OpenR66ProtocolNoDataException;
46  import openr66.protocol.exception.OpenR66ProtocolSystemException;
47  import openr66.protocol.utils.FileUtils;
48  
49  import org.dom4j.Document;
50  import org.dom4j.DocumentException;
51  import org.dom4j.DocumentHelper;
52  import org.dom4j.Element;
53  import org.dom4j.io.SAXReader;
54  import org.dom4j.tree.DefaultElement;
55  
56  /**
57   * Rule File Based Configuration
58   *
59   * @author Frederic Bregier
60   *
61   */
62  public class RuleFileBasedConfiguration {
63      /**
64       * Internal Logger
65       */
66      private static final GgInternalLogger logger = GgInternalLoggerFactory
67              .getLogger(RuleFileBasedConfiguration.class);
68  
69      private static final String MULTIPLEROOT = "rules";
70      private static final String ROOT = "rule";
71      private static final String XIDRULE = "idrule";
72      public static final String XHOSTIDS = "hostids";
73      public static final String XHOSTID = "hostid";
74      private static final String XMODE = "mode";
75      private static final String XRECVPATH = "recvpath";
76      private static final String XSENDPATH = "sendpath";
77      private static final String XARCHIVEPATH = "archivepath";
78      private static final String XWORKPATH = "workpath";
79      private static final String XRPRETASKS = "rpretasks";
80      private static final String XRPOSTTASKS = "rposttasks";
81      private static final String XRERRORTASKS = "rerrortasks";
82      private static final String XSPRETASKS = "spretasks";
83      private static final String XSPOSTTASKS = "sposttasks";
84      private static final String XSERRORTASKS = "serrortasks";
85      public static final String XTASKS = "tasks";
86      public static final String XTASK = "task";
87  
88      private static final String HOSTIDS_HOSTID = "/"+XHOSTIDS+"/"+XHOSTID;
89  
90      private static final String TASK = "/tasks/task";
91      
92      private static final XmlDecl [] taskDecl = {
93          new XmlDecl(XmlType.STRING, DbRule.TASK_TYPE),
94          new XmlDecl(XmlType.STRING, DbRule.TASK_PATH),
95          new XmlDecl(XmlType.LONG, DbRule.TASK_DELAY),
96      };
97      public static final XmlDecl [] tasksDecl = {
98          new XmlDecl(XTASK, XmlType.XVAL, TASK, taskDecl, true)
99      };
100     private static final XmlDecl [] subruleDecls = {
101         new XmlDecl(XmlType.STRING, XIDRULE), 
102         new XmlDecl(XHOSTIDS, XmlType.STRING, HOSTIDS_HOSTID, true),
103         new XmlDecl(XmlType.INTEGER, XMODE), 
104         new XmlDecl(XmlType.STRING, XRECVPATH), 
105         new XmlDecl(XmlType.STRING, XSENDPATH), 
106         new XmlDecl(XmlType.STRING, XARCHIVEPATH),
107         new XmlDecl(XmlType.STRING, XWORKPATH),
108         new XmlDecl(XRPRETASKS, XmlType.XVAL, XRPRETASKS, tasksDecl, false),
109         new XmlDecl(XRPOSTTASKS, XmlType.XVAL, XRPOSTTASKS, tasksDecl, false),
110         new XmlDecl(XRERRORTASKS, XmlType.XVAL, XRERRORTASKS, tasksDecl, false),
111         new XmlDecl(XSPRETASKS, XmlType.XVAL, XSPRETASKS, tasksDecl, false),
112         new XmlDecl(XSPOSTTASKS, XmlType.XVAL, XSPOSTTASKS, tasksDecl, false),
113         new XmlDecl(XSERRORTASKS, XmlType.XVAL, XSERRORTASKS, tasksDecl, false)
114     };
115     private static final XmlDecl [] ruleDecls = {
116         new XmlDecl(ROOT, XmlType.XVAL, ROOT, subruleDecls, false)
117     };
118     private static final XmlDecl [] multipleruleDecls = {
119         new XmlDecl(MULTIPLEROOT, XmlType.XVAL, "/"+MULTIPLEROOT+"/"+ROOT, subruleDecls, true)
120     };
121     public static final XmlDecl [] hostsDecls = {
122         new XmlDecl(XHOSTIDS, XmlType.STRING, HOSTIDS_HOSTID, true),
123     };
124     
125     /**
126      * Extension of rule files
127      */
128     public static final String EXT_RULE = ".rule.xml";
129     /**
130      * Extension of multiple rules in one file
131      */
132     public static final String EXT_RULES = ".rules.xml";
133 
134     /**
135      * Import all Rule files into the HashTable of Rules
136      *
137      * @param configDirectory
138      * @throws OpenR66ProtocolSystemException
139      * @throws GoldenGateDatabaseException
140      */
141     public static void importRules(File configDirectory)
142             throws OpenR66ProtocolSystemException, GoldenGateDatabaseException {
143         File[] files = FileUtils.getFiles(configDirectory,
144                 new ExtensionFilter(EXT_RULE));
145         for (File file: files) {
146             DbRule rule = getFromFile(file);
147             logger.debug(rule.toString());
148         }
149         files = FileUtils.getFiles(configDirectory,
150                 new ExtensionFilter(EXT_RULES));
151         for (File file: files) {
152             getMultipleFromFile(file);
153         }
154     }
155 
156     /**
157      * Utility function
158      * @param value
159      * @return the array of tasks or empty array if in error.
160      */
161     @SuppressWarnings("unchecked")
162     public static String [][] getTasksRule(XmlValue value) {
163         List<XmlValue[]> list = (List<XmlValue[]>) value.getList();
164         if (list == null || list.isEmpty()) {
165             logger.debug("NoRule for "+value.getName());
166             //Unable to find the tasks for Rule, setting to the default
167             return new String[0][0];
168         }
169         String[][] taskArray = new String[list.size()][3];
170         for (int i = 0; i < list.size(); i ++) {
171             taskArray[i][0] = null;
172             taskArray[i][1] = null;
173             taskArray[i][2] = null;
174         }
175         int rank = 0;
176         for (XmlValue[] subvals: list) {
177             XmlHash hash = new XmlHash(subvals);
178             XmlValue valtype = hash.get(DbRule.TASK_TYPE);
179             if (valtype == null || (valtype.isEmpty()) || valtype.getString().isEmpty()) {
180                 continue;
181             }
182             XmlValue valpath = hash.get(DbRule.TASK_PATH);
183             if (valpath == null || (valpath.isEmpty()) || valtype.getString().isEmpty()) {
184                 continue;
185             }
186             XmlValue valdelay = hash.get(DbRule.TASK_DELAY);
187             String delay;
188             if (valdelay == null || (valdelay.isEmpty())) {
189                 delay = Long
190                     .toString(Configuration.configuration.TIMEOUTCON);
191             } else {
192                 delay = valdelay.getIntoString();
193             }
194             taskArray[rank][0] = valtype.getString().toUpperCase();
195             // CHECK TASK_TYPE 
196             try {
197                 TaskType.valueOf(taskArray[rank][0]);
198             } catch (IllegalArgumentException e) {
199                 // Bad Type
200                 logger.warn("Bad Type of Task: "+taskArray[rank][0]);
201                 continue;
202             }
203             taskArray[rank][1] = valpath.getString();
204             taskArray[rank][2] = delay;
205             rank++;
206             hash.clear();
207         }
208         list.clear();
209         list = null;
210         return taskArray;
211     }
212     /**
213      * 
214      * @param value the XmlValue hosting hostids/hostid
215      * @return the array of HostIds allowed for the current rule
216      */
217     public static String[] getHostIds(XmlValue value) {
218         String []idsArray = null;
219         if (value == null || (value.getList() == null) || value.getList().isEmpty()) {
220             logger
221             .info("Unable to find the id for Rule, setting to the default");
222         } else {
223             @SuppressWarnings("unchecked")
224             List<String> ids = (List<String>) value.getList();
225             idsArray = new String[ids.size()];
226             int i = 0;
227             for (String sval: ids) {
228                 if (sval.isEmpty()) {
229                     continue;
230                 }
231                 idsArray[i] = sval;
232                 i ++;
233             }
234             ids.clear();
235             ids = null;
236         }
237         return idsArray;
238     }
239     /**
240      * Load and update a Rule from a file
241      * @param file
242      * @return the newly created R66Rule from XML File
243      * @throws OpenR66ProtocolSystemException
244      * @throws GoldenGateDatabaseException
245      * @throws GoldenGateDatabaseNoDataException
246      * @throws GoldenGateDatabaseSqlException
247      * @throws GoldenGateDatabaseNoConnectionException
248      * @throws OpenR66ProtocolNoDataException
249      */
250     public static DbRule getFromFile(File file)
251             throws OpenR66ProtocolSystemException, GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException, GoldenGateDatabaseNoDataException, GoldenGateDatabaseException {
252         DbRule newRule = null;
253         Document document = null;
254         // Open config file
255         try {
256             document = new SAXReader().read(file);
257         } catch (DocumentException e) {
258             logger.error("Unable to read the XML Rule file: " + file.getName(),
259                     e);
260             throw new OpenR66ProtocolSystemException(
261                     "Unable to read the XML Rule file", e);
262         }
263         if (document == null) {
264             logger.error("Unable to read the XML Rule file: " + file.getName());
265             throw new OpenR66ProtocolSystemException(
266                     "Unable to read the XML Rule file");
267         }
268         XmlValue[] values = XmlUtil.read(document, ruleDecls);
269         newRule = getFromXmlValue(values);
270         values = null;
271         return newRule;
272     }
273     /**
274      * Load and update multiple Rules from one file
275      * @param file
276      * @return a list of newly created R66Rule from XML File
277      * @throws OpenR66ProtocolSystemException
278      * @throws GoldenGateDatabaseException
279      * @throws GoldenGateDatabaseNoDataException
280      * @throws GoldenGateDatabaseSqlException
281      * @throws GoldenGateDatabaseNoConnectionException
282      * @throws OpenR66ProtocolNoDataException
283      */
284     public static List<DbRule> getMultipleFromFile(File file)
285             throws OpenR66ProtocolSystemException, GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException, GoldenGateDatabaseNoDataException, GoldenGateDatabaseException {
286         Document document = null;
287         // Open config file
288         try {
289             document = new SAXReader().read(file);
290         } catch (DocumentException e) {
291             logger.error("Unable to read the XML Rule file: " + file.getName(),
292                     e);
293             throw new OpenR66ProtocolSystemException(
294                     "Unable to read the XML Rule file", e);
295         }
296         if (document == null) {
297             logger.error("Unable to read the XML Rule file: " + file.getName());
298             throw new OpenR66ProtocolSystemException(
299                     "Unable to read the XML Rule file");
300         }
301         XmlValue[] values = XmlUtil.read(document, multipleruleDecls);
302         if (values.length <=0) {
303             return new ArrayList<DbRule>(0);
304         }
305         XmlValue value = values[0];
306         @SuppressWarnings("unchecked")
307         List<XmlValue[]> list = (List<XmlValue[]>)value.getList();
308         List<DbRule> result = new ArrayList<DbRule>(list.size());
309         for (XmlValue []xmlValue: list) {
310             result.add(getFromXmlValue(xmlValue));
311         }
312         values = null;
313         return result;
314     }
315     /**
316      * Load and update one Rule from a XmlValue
317      * @param root
318      * @return the newly created R66Rule from XML File
319      * @throws OpenR66ProtocolSystemException
320      * @throws GoldenGateDatabaseException
321      * @throws GoldenGateDatabaseNoDataException
322      * @throws GoldenGateDatabaseSqlException
323      * @throws GoldenGateDatabaseNoConnectionException
324      * @throws OpenR66ProtocolNoDataException
325      */
326     private static DbRule getFromXmlValue(XmlValue []root)
327             throws OpenR66ProtocolSystemException, GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException, GoldenGateDatabaseNoDataException, GoldenGateDatabaseException {
328         DbRule newRule = null;
329         XmlHash hash = new XmlHash(root);
330         XmlValue value = hash.get(XIDRULE);
331         if (value == null || (value.isEmpty()) || value.getString().length() == 0) {
332             logger.error("Unable to find in Rule field: " + XIDRULE);
333             throw new OpenR66ProtocolSystemException();
334         }
335         String idrule = value.getString();
336         value = hash.get(XMODE);
337         if (value == null || (value.isEmpty())) {
338             logger.error("Unable to find in Rule field: " + XMODE);
339             throw new OpenR66ProtocolSystemException();
340         }
341         int mode = value.getInteger();
342         String recvpath;
343         value = hash.get(XRECVPATH);
344         if (value == null || (value.isEmpty()) || value.getString().length() == 0) {
345             recvpath = Configuration.configuration.inPath;
346         } else {
347             recvpath = DirInterface.SEPARATOR + value.getString();
348         }
349         String sendpath;
350         value = hash.get(XSENDPATH);
351         if (value == null || (value.isEmpty()) || value.getString().length() == 0) {
352             sendpath = Configuration.configuration.outPath;
353         } else {
354             sendpath = DirInterface.SEPARATOR + value.getString();
355         }
356         String archivepath;
357         value = hash.get(XARCHIVEPATH);
358         if (value == null || (value.isEmpty()) || value.getString().length() == 0) {
359             archivepath = Configuration.configuration.archivePath;
360         } else {
361             archivepath = DirInterface.SEPARATOR + value.getString();
362         }
363         String workpath;
364         value = hash.get(XWORKPATH);
365         if (value == null || (value.isEmpty()) || value.getString().length() == 0) {
366             workpath = Configuration.configuration.workingPath;
367         } else {
368             workpath = DirInterface.SEPARATOR + value.getString();
369         }
370         String[] idsArray = null;
371         value = hash.get(XHOSTIDS);
372         idsArray = getHostIds(value);
373         String[][] rpretasks = new String[0][0];
374         value = hash.get(XRPRETASKS);
375         if (value != null && (! value.isEmpty())) {
376             XmlValue [] subvalues = value.getSubXml();
377             if (subvalues.length > 0) {
378                 rpretasks = getTasksRule(subvalues[0]);
379             }
380         }
381         String[][] rposttasks = new String[0][0];
382         value = hash.get(XRPOSTTASKS);
383         if (value != null && (! value.isEmpty())) {
384             XmlValue [] subvalues = value.getSubXml();
385             if (subvalues.length > 0) {
386                 rposttasks = getTasksRule(subvalues[0]);
387             }
388         }
389         String[][] rerrortasks = new String[0][0];
390         value = hash.get(XRERRORTASKS);
391         if (value != null && (! value.isEmpty())) {
392             XmlValue [] subvalues = value.getSubXml();
393             if (subvalues.length > 0) {
394                 rerrortasks = getTasksRule(subvalues[0]);
395             }
396         }
397         String[][] spretasks = new String[0][0];
398         value = hash.get(XSPRETASKS);
399         if (value != null && (! value.isEmpty())) {
400             XmlValue [] subvalues = value.getSubXml();
401             if (subvalues.length > 0) {
402                 spretasks = getTasksRule(subvalues[0]);
403             }
404         }
405         String[][] sposttasks = new String[0][0];
406         value = hash.get(XSPOSTTASKS);
407         if (value != null && (! value.isEmpty())) {
408             XmlValue [] subvalues = value.getSubXml();
409             if (subvalues.length > 0) {
410                 sposttasks = getTasksRule(subvalues[0]);
411             }
412         }
413         String[][] serrortasks = new String[0][0];
414         value = hash.get(XSERRORTASKS);
415         if (value != null && (! value.isEmpty())) {
416             XmlValue [] subvalues = value.getSubXml();
417             if (subvalues.length > 0) {
418                 serrortasks = getTasksRule(subvalues[0]);
419             }
420         }
421 
422         newRule = new DbRule(DbConstant.admin.session, idrule, idsArray, mode, recvpath, sendpath,
423                 archivepath, workpath, rpretasks, rposttasks, rerrortasks,
424                 spretasks, sposttasks, serrortasks);
425         if (DbConstant.admin != null && DbConstant.admin.session != null) {
426             if (newRule.exist()) {
427                 newRule.update();
428             } else {
429                 newRule.insert();
430             }
431         } else {
432             // put in hashtable
433             newRule.insert();
434         }
435         hash.clear();
436         return newRule;
437     }
438     /**
439      * Construct a new Element with value
440      * @param name
441      * @param value
442      * @return the new Element
443      */
444     private static final Element newElement(String name, String value) {
445         Element node = new DefaultElement(name);
446         node.addText(value);
447         return node;
448     }
449     /**
450      * Add a rule from root element (ROOT = rule)
451      * @param element
452      * @param rule
453      */
454     private static void addToElement(Element element, DbRule rule) {
455         Element root = element;
456         root.add(newElement(XIDRULE, rule.idRule));
457         Element hosts = new DefaultElement(XHOSTIDS);
458         if (rule.idsArray != null) {
459             for (String host: rule.idsArray) {
460                 hosts.add(newElement(XHOSTID, host));
461             }
462         }
463         root.add(hosts);
464         root.add(newElement(XMODE, Integer.toString(rule.mode)));
465         if (rule.recvPath != null) {
466             root.add(newElement(XRECVPATH, rule.recvPath.substring(1)));
467         }
468         if (rule.sendPath != null) {
469             root.add(newElement(XSENDPATH, rule.sendPath.substring(1)));
470         }
471         if (rule.archivePath != null) {
472             root.add(newElement(XARCHIVEPATH, rule.archivePath.substring(1)));
473         }
474         if (rule.workPath != null) {
475             root.add(newElement(XWORKPATH, rule.workPath.substring(1)));
476         }
477         Element tasks = new DefaultElement(XRPRETASKS);
478         Element roottasks = new DefaultElement(XTASKS);
479         int rank = 0;
480         String [][] array = rule.rpreTasksArray;
481         if (array != null) {
482             for (rank = 0; rank < array.length; rank++) {
483                 Element task = new DefaultElement(XTASK);
484                 task.add(newElement(DbRule.TASK_TYPE, array[rank][0]));
485                 task.add(newElement(DbRule.TASK_PATH, array[rank][1]));
486                 task.add(newElement(DbRule.TASK_DELAY, array[rank][2]));
487                 roottasks.add(task);
488             }
489         }
490         tasks.add(roottasks);
491         root.add(tasks);
492         tasks = new DefaultElement(XRPOSTTASKS);
493         roottasks = new DefaultElement(XTASKS);
494         array = rule.rpostTasksArray;
495         if (array != null) {
496             for (rank = 0; rank < array.length; rank++) {
497                 Element task = new DefaultElement(XTASK);
498                 task.add(newElement(DbRule.TASK_TYPE, array[rank][0]));
499                 task.add(newElement(DbRule.TASK_PATH, array[rank][1]));
500                 task.add(newElement(DbRule.TASK_DELAY, array[rank][2]));
501                 roottasks.add(task);
502             }
503         }
504         tasks.add(roottasks);
505         root.add(tasks);
506         tasks = new DefaultElement(XRERRORTASKS);
507         roottasks = new DefaultElement(XTASKS);
508         array = rule.rerrorTasksArray;
509         if (array != null) {
510             for (rank = 0; rank < array.length; rank++) {
511                 Element task = new DefaultElement(XTASK);
512                 task.add(newElement(DbRule.TASK_TYPE, array[rank][0]));
513                 task.add(newElement(DbRule.TASK_PATH, array[rank][1]));
514                 task.add(newElement(DbRule.TASK_DELAY, array[rank][2]));
515                 roottasks.add(task);
516             }
517         }
518         tasks.add(roottasks);
519         root.add(tasks);
520         tasks = new DefaultElement(XSPRETASKS);
521         roottasks = new DefaultElement(XTASKS);
522         array = rule.spreTasksArray;
523         if (array != null) {
524             for (rank = 0; rank < array.length; rank++) {
525                 Element task = new DefaultElement(XTASK);
526                 task.add(newElement(DbRule.TASK_TYPE, array[rank][0]));
527                 task.add(newElement(DbRule.TASK_PATH, array[rank][1]));
528                 task.add(newElement(DbRule.TASK_DELAY, array[rank][2]));
529                 roottasks.add(task);
530             }
531         }
532         tasks.add(roottasks);
533         root.add(tasks);
534         tasks = new DefaultElement(XSPOSTTASKS);
535         roottasks = new DefaultElement(XTASKS);
536         array = rule.spostTasksArray;
537         if (array != null) {
538             for (rank = 0; rank < array.length; rank++) {
539                 Element task = new DefaultElement(XTASK);
540                 task.add(newElement(DbRule.TASK_TYPE, array[rank][0]));
541                 task.add(newElement(DbRule.TASK_PATH, array[rank][1]));
542                 task.add(newElement(DbRule.TASK_DELAY, array[rank][2]));
543                 roottasks.add(task);
544             }
545         }
546         tasks.add(roottasks);
547         root.add(tasks);
548         tasks = new DefaultElement(XSERRORTASKS);
549         roottasks = new DefaultElement(XTASKS);
550         array = rule.serrorTasksArray;
551         if (array != null) {
552             for (rank = 0; rank < array.length; rank++) {
553                 Element task = new DefaultElement(XTASK);
554                 task.add(newElement(DbRule.TASK_TYPE, array[rank][0]));
555                 task.add(newElement(DbRule.TASK_PATH, array[rank][1]));
556                 task.add(newElement(DbRule.TASK_DELAY, array[rank][2]));
557                 roottasks.add(task);
558             }
559         }
560         tasks.add(roottasks);
561         root.add(tasks);
562     }
563     /**
564      * Write the rule to a file from filename
565      * @param filename
566      * @param rule
567      * @throws OpenR66ProtocolSystemException
568      */
569     private static final void writeXML(String filename, DbRule rule) throws OpenR66ProtocolSystemException {
570         Document document = DocumentHelper.createDocument();
571         Element root = document.addElement(ROOT);
572         addToElement(root, rule);
573         try {
574             XmlUtil.writeXML(filename, null, document);
575         } catch (IOException e) {
576             throw new OpenR66ProtocolSystemException("Cannot write file: "+filename, e);
577         }
578     }
579     /**
580      * Write to directory files prefixed by hostname all Rules from database
581      * @param directory
582      * @param hostname
583      * @throws GoldenGateDatabaseNoConnectionException
584      * @throws GoldenGateDatabaseSqlException
585      * @throws OpenR66ProtocolSystemException
586      */
587     public static final void writeXml(String directory, String hostname) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException, OpenR66ProtocolSystemException {
588         File dir = new File(directory);
589         if (! dir.isDirectory()) {
590             dir.mkdirs();
591         }
592         DbRule []rules = DbRule.getAllRules(DbConstant.admin.session);
593         for (DbRule rule: rules) {
594             String filename = dir.getAbsolutePath()+File.separator+hostname+"_"+rule.idRule+
595                 RuleFileBasedConfiguration.EXT_RULE;
596             RuleFileBasedConfiguration.writeXML(filename, rule);
597         }
598     }
599     /**
600      * Write to directory 1 file prefixed by hostname all Rules from database
601      * @param directory
602      * @param hostname
603      * @return the filename
604      * @throws GoldenGateDatabaseNoConnectionException
605      * @throws GoldenGateDatabaseSqlException
606      * @throws OpenR66ProtocolSystemException
607      */
608     public static String writeOneXml(String directory, String hostname) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException, OpenR66ProtocolSystemException {
609         File dir = new File(directory);
610         if (! dir.isDirectory()) {
611             dir.mkdirs();
612         }
613         DbRule []rules = DbRule.getAllRules(DbConstant.admin.session);
614         String filename = dir.getAbsolutePath()+File.separator+hostname+
615             RuleFileBasedConfiguration.EXT_RULES;
616         Document document = DocumentHelper.createDocument();
617         Element root = document.addElement(MULTIPLEROOT);
618         for (DbRule rule: rules) {
619             Element element = root.addElement(ROOT);
620             addToElement(element, rule);
621         }
622         try {
623             XmlUtil.writeXML(filename, null, document);
624         } catch (IOException e) {
625             throw new OpenR66ProtocolSystemException("Cannot write file: "+filename, e);
626         }
627         return filename;
628     }
629 }