Mercurial > dive4elements > river
comparison flys-aft/src/main/java/org/dive4elements/etl/aft/Sync.java @ 5824:06643e440d1e
Moved directories to org.dive4elements.etl
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:35:06 +0200 |
parents | flys-aft/src/main/java/de/intevation/aft/Sync.java@f939e1e6cfa4 |
children |
comparison
equal
deleted
inserted
replaced
5823:52e966cc7d35 | 5824:06643e440d1e |
---|---|
1 package de.intevation.aft; | |
2 | |
3 import de.intevation.db.ConnectionBuilder; | |
4 | |
5 import de.intevation.utils.XML; | |
6 | |
7 import java.io.File; | |
8 | |
9 import java.net.MalformedURLException; | |
10 import java.net.URL; | |
11 | |
12 import java.sql.SQLException; | |
13 | |
14 import javax.xml.xpath.XPathConstants; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 import org.w3c.dom.Document; | |
19 import org.w3c.dom.Element; | |
20 import org.w3c.dom.NodeList; | |
21 | |
22 public class Sync | |
23 { | |
24 private static Logger log = Logger.getLogger(Sync.class); | |
25 | |
26 public static final String FLYS = "flys"; | |
27 public static final String AFT = "aft"; | |
28 | |
29 public static final String XPATH_DIPS = "/sync/dips/file/text()"; | |
30 public static final String XPATH_REPAIR = "/sync/dips/repair/text()"; | |
31 | |
32 public static final String XPATH_NOTIFICATIONS = | |
33 "/sync/notifications/notification"; | |
34 | |
35 public static final String CONFIG_FILE = | |
36 System.getProperty("config.file", "config.xml"); | |
37 | |
38 public static void sendNotifications(Document config) { | |
39 NodeList notifications = (NodeList)XML.xpath( | |
40 config, XPATH_NOTIFICATIONS, XPathConstants.NODESET, null, null); | |
41 | |
42 if (notifications == null) { | |
43 return; | |
44 } | |
45 | |
46 for (int i = 0, N = notifications.getLength(); i < N; ++i) { | |
47 Element notification = (Element)notifications.item(i); | |
48 String urlString = notification.getAttribute("url"); | |
49 | |
50 URL url; | |
51 try { | |
52 url = new URL(urlString); | |
53 } | |
54 catch (MalformedURLException mfue) { | |
55 log.warn("NOTIFY: Invalid URL '" + urlString + "'. Ignored.", mfue); | |
56 continue; | |
57 } | |
58 | |
59 Notification n = new Notification(notification); | |
60 | |
61 Document result = n.sendPOST(url); | |
62 | |
63 if (result != null) { | |
64 log.info("Send notifcation to '" + urlString + "'."); | |
65 log.info(XML.toString(result)); | |
66 } | |
67 } | |
68 } | |
69 | |
70 public static void main(String [] args) { | |
71 | |
72 File configFile = new File(CONFIG_FILE); | |
73 | |
74 if (!configFile.isFile() || !configFile.canRead()) { | |
75 log.error("cannot read config file"); | |
76 System.exit(1); | |
77 } | |
78 | |
79 Document config = XML.parseDocument(configFile, Boolean.FALSE); | |
80 | |
81 if (config == null) { | |
82 log.error("Cannot load config file."); | |
83 System.exit(1); | |
84 } | |
85 | |
86 String dipsF = (String)XML.xpath( | |
87 config, XPATH_DIPS, XPathConstants.STRING, null, null); | |
88 | |
89 if (dipsF == null || dipsF.length() == 0) { | |
90 log.error("Cannot find path to DIPS XML in config."); | |
91 System.exit(1); | |
92 } | |
93 | |
94 File dipsFile = new File(dipsF); | |
95 | |
96 if (!dipsFile.isFile() || !dipsFile.canRead()) { | |
97 log.error("DIPS: Cannot find '" + dipsF + "'."); | |
98 System.exit(1); | |
99 } | |
100 | |
101 Document dips = XML.parseDocument(dipsFile, Boolean.FALSE); | |
102 | |
103 if (dips == null) { | |
104 log.error("DIPS: Cannot load DIPS document."); | |
105 System.exit(1); | |
106 } | |
107 | |
108 String repairF = (String)XML.xpath( | |
109 config, XPATH_REPAIR, XPathConstants.STRING, null, null); | |
110 | |
111 if (repairF != null && repairF.length() > 0) { | |
112 File repairFile = new File(repairF); | |
113 if (!repairFile.isFile() || !repairFile.canRead()) { | |
114 log.warn("REPAIR: Cannot open DIPS repair XSLT file."); | |
115 } | |
116 else { | |
117 Document fixed = XML.transform(dips, repairFile); | |
118 if (fixed == null) { | |
119 log.warn("REPAIR: Fixing DIPS failed."); | |
120 } | |
121 else { | |
122 dips = fixed; | |
123 } | |
124 } | |
125 } | |
126 | |
127 int exitCode = 0; | |
128 | |
129 ConnectionBuilder aftConnectionBuilder = | |
130 new ConnectionBuilder(AFT, config); | |
131 | |
132 ConnectionBuilder flysConnectionBuilder = | |
133 new ConnectionBuilder(FLYS, config); | |
134 | |
135 SyncContext syncContext = null; | |
136 | |
137 boolean modified = false; | |
138 try { | |
139 syncContext = new SyncContext( | |
140 aftConnectionBuilder.getConnectedStatements(), | |
141 flysConnectionBuilder.getConnectedStatements(), | |
142 dips); | |
143 syncContext.init(); | |
144 Rivers rivers = new Rivers(); | |
145 modified = rivers.sync(syncContext); | |
146 } | |
147 catch (SQLException sqle) { | |
148 log.error("SYNC: Syncing failed.", sqle); | |
149 exitCode = 1; | |
150 } | |
151 finally { | |
152 if (syncContext != null) { | |
153 syncContext.close(); | |
154 } | |
155 } | |
156 | |
157 if (modified) { | |
158 log.info("Modifications found."); | |
159 sendNotifications(config); | |
160 } | |
161 else { | |
162 log.info("No modifications found."); | |
163 } | |
164 | |
165 if (exitCode != 0) { | |
166 System.exit(1); | |
167 } | |
168 } | |
169 } | |
170 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |