sascha@4067: package de.intevation.aft; sascha@4067: sascha@4072: import java.io.File; sascha@4072: sascha@4073: import java.sql.SQLException; sascha@4073: sascha@4072: import org.w3c.dom.Document; 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@4077: sascha@4072: public static final String CONFIG_FILE = sascha@4072: System.getProperty("config.file", "config.xml"); sascha@4072: 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@4083: log.error("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@4083: log.error("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@4083: log.warn("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@4083: log.warn("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@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@4077: rivers.sync(syncContext); sascha@4073: } sascha@4073: catch (SQLException sqle) { sascha@4083: log.error("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@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 :