sascha@4067: package de.intevation.aft; sascha@4067: sascha@4072: import java.io.File; sascha@4072: sascha@4095: import java.net.URL; sascha@4095: import java.net.MalformedURLException; sascha@4095: sascha@4073: import java.sql.SQLException; sascha@4073: sascha@4072: import org.w3c.dom.Document; sascha@4095: import org.w3c.dom.NodeList; sascha@4095: import org.w3c.dom.Element; sascha@4072: sascha@4083: import org.apache.log4j.Logger; sascha@4083: sascha@4077: import javax.xml.xpath.XPathConstants; sascha@4077: sascha@4072: import de.intevation.utils.XML; sascha@4072: sascha@4072: import de.intevation.db.ConnectionBuilder; sascha@4072: sascha@4067: public class Sync sascha@4067: { sascha@4083: private static Logger log = Logger.getLogger(Sync.class); sascha@4083: sascha@4072: public static final String FLYS = "flys"; sascha@4072: public static final String AFT = "aft"; sascha@4072: sascha@4083: public static final String XPATH_DIPS = "/sync/dips/file/text()"; sascha@4083: public static final String XPATH_REPAIR = "/sync/dips/repair/text()"; sascha@4095: sascha@4094: public static final String XPATH_NOTIFICATIONS = sascha@4094: "/sync/notifications/notification"; sascha@4077: sascha@4072: public static final String CONFIG_FILE = sascha@4072: System.getProperty("config.file", "config.xml"); sascha@4072: sascha@4095: public static void sendNotifications(Document config) { sascha@4095: NodeList notifications = (NodeList)XML.xpath( sascha@4095: config, XPATH_NOTIFICATIONS, XPathConstants.NODESET, null, null); sascha@4095: sascha@4095: if (notifications == null) { sascha@4095: return; sascha@4095: } sascha@4095: sascha@4095: for (int i = 0, N = notifications.getLength(); i < N; ++i) { sascha@4095: Element notification = (Element)notifications.item(i); sascha@4095: String urlString = notification.getAttribute("url"); sascha@4095: sascha@4095: URL url; sascha@4095: try { sascha@4095: url = new URL(urlString); sascha@4095: } sascha@4095: catch (MalformedURLException mfue) { sascha@4110: log.warn("NOTIFY: Invalid URL '" + urlString + "'. Ignored.", mfue); sascha@4095: continue; sascha@4095: } sascha@4095: sascha@4095: Notification n = new Notification(notification); sascha@4095: sascha@4095: Document result = n.sendPOST(url); sascha@4095: sascha@4095: if (result != null) { sascha@4095: log.info("Send notifcation to '" + urlString + "'."); sascha@4095: log.info(XML.toString(result)); sascha@4095: } sascha@4095: } sascha@4095: } sascha@4095: sascha@4067: public static void main(String [] args) { sascha@4072: sascha@4072: File configFile = new File(CONFIG_FILE); sascha@4072: sascha@4072: if (!configFile.isFile() || !configFile.canRead()) { sascha@4083: log.error("cannot read config file"); sascha@4072: System.exit(1); sascha@4072: } sascha@4072: sascha@4072: Document config = XML.parseDocument(configFile, Boolean.FALSE); sascha@4072: sascha@4072: if (config == null) { sascha@4083: log.error("Cannot load config file."); sascha@4072: System.exit(1); sascha@4072: } sascha@4072: sascha@4077: String dipsF = (String)XML.xpath( sascha@4077: config, XPATH_DIPS, XPathConstants.STRING, null, null); sascha@4077: sascha@4077: if (dipsF == null || dipsF.length() == 0) { sascha@4083: log.error("Cannot find path to DIPS XML in config."); sascha@4077: System.exit(1); sascha@4077: } sascha@4077: sascha@4077: File dipsFile = new File(dipsF); sascha@4077: sascha@4077: if (!dipsFile.isFile() || !dipsFile.canRead()) { sascha@4110: log.error("DIPS: Cannot find '" + dipsF + "'."); sascha@4077: System.exit(1); sascha@4077: } sascha@4077: sascha@4077: Document dips = XML.parseDocument(dipsFile, Boolean.FALSE); sascha@4077: sascha@4077: if (dips == null) { sascha@4110: log.error("DIPS: Cannot load DIPS document."); sascha@4077: System.exit(1); sascha@4077: } sascha@4077: sascha@4083: String repairF = (String)XML.xpath( sascha@4083: config, XPATH_REPAIR, XPathConstants.STRING, null, null); sascha@4083: sascha@4083: if (repairF != null && repairF.length() > 0) { sascha@4083: File repairFile = new File(repairF); sascha@4083: if (!repairFile.isFile() || !repairFile.canRead()) { sascha@4110: log.warn("REPAIR: Cannot open DIPS repair XSLT file."); sascha@4083: } sascha@4083: else { sascha@4083: Document fixed = XML.transform(dips, repairFile); sascha@4083: if (fixed == null) { sascha@4110: log.warn("REPAIR: Fixing DIPS failed."); sascha@4083: } sascha@4083: else { sascha@4083: dips = fixed; sascha@4083: } sascha@4083: } sascha@4083: } sascha@4090: sascha@4090: int exitCode = 0; sascha@4083: sascha@4073: ConnectionBuilder aftConnectionBuilder = sascha@4073: new ConnectionBuilder(AFT, config); sascha@4073: sascha@4072: ConnectionBuilder flysConnectionBuilder = sascha@4072: new ConnectionBuilder(FLYS, config); sascha@4072: sascha@4077: SyncContext syncContext = null; sascha@4094: sascha@4094: boolean modified = false; sascha@4073: try { sascha@4077: syncContext = new SyncContext( sascha@4077: aftConnectionBuilder.getConnectedStatements(), sascha@4077: flysConnectionBuilder.getConnectedStatements(), sascha@4077: dips); sascha@4089: syncContext.init(); sascha@4075: Rivers rivers = new Rivers(); sascha@4094: modified = rivers.sync(syncContext); sascha@4073: } sascha@4073: catch (SQLException sqle) { sascha@4110: log.error("SYNC: Syncing failed.", sqle); sascha@4090: exitCode = 1; sascha@4073: } sascha@4073: finally { sascha@4077: if (syncContext != null) { sascha@4077: syncContext.close(); sascha@4075: } sascha@4073: } sascha@4090: sascha@4095: if (modified) { sascha@4104: log.info("Modifications found."); sascha@4095: sendNotifications(config); sascha@4095: } sascha@4104: else { sascha@4104: log.info("No modifications found."); sascha@4104: } sascha@4095: sascha@4090: if (exitCode != 0) { sascha@4090: System.exit(1); sascha@4090: } sascha@4067: } sascha@4067: } sascha@4067: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :