List of usage examples for java.lang Process getErrorStream
public abstract InputStream getErrorStream();
From source file:ch.kostceco.tools.kostval.validation.modulepdfa.impl.ValidationAvalidationAiModuleImpl.java
@Override public boolean validate(File valDatei, File directoryOfLogfile) throws ValidationApdfvalidationException { @SuppressWarnings("unused") boolean valid = false; // Version & Level herausfinden String pdfa1 = getConfigurationService().pdfa1(); String pdfa2 = getConfigurationService().pdfa2(); Integer pdfaVer1 = 0;/*w w w. j av a2 s .com*/ Integer pdfaVer2 = 0; /* Nicht vergessen in "src/main/resources/config/applicationContext-services.xml" beim * entsprechenden Modul die property anzugeben: <property name="configurationService" * ref="configurationService" /> */ // Vorbereitung fr eine allfllige Festhaltung bei unterschiedlichen Validierungsresultaten in // einer PDF_Diagnosedatei File pdfDia = null; String pdfDiaPath = getConfigurationService().getPathToDiagnose(); try { pdfDia = new File(pdfDiaPath + File.separator + "PDF-Diagnosedaten.kost-val.xml"); if (!pdfDia.exists()) { pdfDia.createNewFile(); PrintWriter output; BufferedWriter buffer; FileWriter fileWriter; fileWriter = new FileWriter(pdfDia); buffer = new BufferedWriter(fileWriter); output = new PrintWriter(buffer); try { output.print(getTextResourceService().getText(MESSAGE_XML_DIAHEADER) + "\n"); output.print(getTextResourceService().getText(MESSAGE_XML_DIAEND)); } finally { output.close(); buffer.close(); fileWriter.close(); } } File xslDiaOrig = new File("resources" + File.separator + "kost-val_PDFdia.xsl"); File xslDiaCopy = new File(pdfDiaPath + File.separator + "kost-val_PDFdia.xsl"); if (!xslDiaCopy.exists()) { Util.copyFile(xslDiaOrig, xslDiaCopy); } } catch (IOException e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); } /* Neu soll die Validierung mit PDFTron konfigurier bar sein Mgliche Werte 1A, 1B und no sowie * 2A, 2B, 2U und no Da Archive beide Versionen erlauben knnen sind es 2 config eintrge Es * gibt mehre Mglichkeiten das PDF in der gewnschten Version zu testen - Unterscheidung anhand * DROID --> braucht viel Zeit auch mit KaD_Signaturefile - Unterscheidung anhand PDF/A-Eintrag * wie Droid aber selber programmiert --> ist viel schneller */ if (pdfa2.equals("2A") || pdfa2.equals("2B") || pdfa2.equals("2U")) { // gltiger Konfigurationseintrag und V2 erlaubt pdfaVer2 = 2; } else { // v2 nicht erlaubt oder falscher eintrag pdfa2 = "no"; } if (pdfa1.equals("1A") || pdfa1.equals("1B")) { // gltiger Konfigurationseintrag und V1 erlaubt pdfaVer1 = 1; } else { // v1 nicht erlaubt oder falscher eintrag pdfa1 = "no"; } if (pdfa1 == "no" && pdfa2 == "no") { // keine Validierung mglich. keine PDFA-Versionen konfiguriert getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_NOCONFIG)); valid = false; return false; } String level = "no"; // Richtiges Level definieren if (pdfaVer1 != 1) { // Level 1 nicht erlaubt --> Level 2 level = pdfa2; } else if (pdfaVer2 != 2) { // Level 2 nicht erlaubt --> Level 1 level = pdfa1; } else { try { // Beide sind mglich --> Level je nach File auswhlen pdfaVer1 = 0; pdfaVer2 = 0; BufferedReader in = new BufferedReader(new FileReader(valDatei)); String line; while ((line = in.readLine()) != null) { // hufige Partangaben: pdfaid:part>1< pdfaid:part='1' pdfaid:part="1" if (line.contains("pdfaid:part")) { // pdfaid:part if (line.contains("pdfaid:part>1<")) { level = pdfa1; pdfaVer1 = 1; } else if (line.contains("pdfaid:part='1'")) { level = pdfa1; pdfaVer1 = 1; } else if (line.contains("pdfaid:part=\"1\"")) { level = pdfa1; pdfaVer1 = 1; } else if (line.contains("pdfaid:part>2<")) { level = pdfa2; pdfaVer2 = 2; } else if (line.contains("pdfaid:part='2'")) { level = pdfa2; pdfaVer2 = 2; } else if (line.contains("pdfaid:part=\"2\"")) { level = pdfa2; pdfaVer2 = 2; } else if (line.contains("pdfaid:part") && line.contains("1")) { // PDFA-Version = 1 level = pdfa1; pdfaVer1 = 1; } else if (line.contains("pdfaid:part") && line.contains("2")) { // PDFA-Version = 2 level = pdfa2; pdfaVer2 = 2; } } if (pdfaVer1 == 0 && pdfaVer2 == 0) { // der Part wurde nicht gefunden --> Level 2 level = pdfa2; } } } catch (Throwable e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); } } getMessageService().logError(getTextResourceService().getText(MESSAGE_PDFAVALIDATION_VL, level)); // Start mit der Erkennung // Eine PDF Datei (.pdf / .pdfa) muss mit %PDF [25504446] beginnen if (valDatei.isDirectory()) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_ISDIRECTORY)); return false; } else if ((valDatei.getAbsolutePath().toLowerCase().endsWith(".pdf") || valDatei.getAbsolutePath().toLowerCase().endsWith(".pdfa"))) { FileReader fr = null; try { fr = new FileReader(valDatei); BufferedReader read = new BufferedReader(fr); // Hex 25 in Char umwandeln String str1 = "25"; int i1 = Integer.parseInt(str1, 16); char c1 = (char) i1; // Hex 50 in Char umwandeln String str2 = "50"; int i2 = Integer.parseInt(str2, 16); char c2 = (char) i2; // Hex 44 in Char umwandeln String str3 = "44"; int i3 = Integer.parseInt(str3, 16); char c3 = (char) i3; // Hex 46 in Char umwandeln String str4 = "46"; int i4 = Integer.parseInt(str4, 16); char c4 = (char) i4; // auslesen der ersten 4 Zeichen der Datei int length; int i; char[] buffer = new char[4]; length = read.read(buffer); for (i = 0; i != length; i++) ; // die beiden charArrays (soll und ist) mit einander vergleichen IST = c1c2c3c4 char[] charArray1 = buffer; char[] charArray2 = new char[] { c1, c2, c3, c4 }; if (Arrays.equals(charArray1, charArray2)) { // hchstwahrscheinlich ein PDF da es mit 25504446 respektive %PDF beginnt valid = true; } else { // Droid-Erkennung, damit Details ausgegeben werden knnen String nameOfSignature = getConfigurationService().getPathToDroidSignatureFile(); if (nameOfSignature == null) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(MESSAGE_XML_CONFIGURATION_ERROR_NO_SIGNATURE)); return false; } // existiert die SignatureFile am angebenen Ort? File fnameOfSignature = new File(nameOfSignature); if (!fnameOfSignature.exists()) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(MESSAGE_XML_CA_DROID)); return false; } Droid droid = null; try { /* kleiner Hack, weil die Droid libraries irgendwo ein System.out drin haben, welche den * Output stren Util.switchOffConsole() als Kommentar markieren wenn man die * Fehlermeldung erhalten mchte */ Util.switchOffConsole(); droid = new Droid(); droid.readSignatureFile(nameOfSignature); } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_CANNOT_INITIALIZE_DROID)); return false; } finally { Util.switchOnConsole(); } File file = valDatei; String puid = ""; IdentificationFile ifile = droid.identify(file.getAbsolutePath()); for (int x = 0; x < ifile.getNumHits(); x++) { FileFormatHit ffh = ifile.getHit(x); FileFormat ff = ffh.getFileFormat(); puid = ff.getPUID(); } getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_INCORRECTFILE, puid)); return false; } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_INCORRECTFILE)); return false; } } else { // die Datei endet nicht mit pdf oder pdfa -> Fehler getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_INCORRECTFILEENDING)); return false; } // Ende der Erkennung boolean isValid = false; boolean dual = false; // Initialisierung PDFTron -> berprfen der Angaben: existiert die PdftronExe am angebenen Ort? String pathToPdftronExe = getConfigurationService().getPathToPdftronExe(); String producerFirstValidator = getConfigurationService().firstValidator(); String dualValidation = getConfigurationService().dualValidation(); /* Nicht vergessen in "src/main/resources/config/applicationContext-services.xml" beim * entsprechenden Modul die property anzugeben: <property name="configurationService" * ref="configurationService" /> */ if (dualValidation.contentEquals("dual")) { // Duale Validierung gewnscht dual = true; } if (!producerFirstValidator.contentEquals("PDFTron")) { // nicht der Validator von PDFTron --> PDFTools wird verwendet producerFirstValidator = "PDFTools"; } File fPdftronExe = new File(pathToPdftronExe); if (!fPdftronExe.exists() || !fPdftronExe.getName().equals("pdfa.exe")) { // Keine Duale Validierung mglich if (dualValidation.contentEquals("dual") || producerFirstValidator.contentEquals("PDFTron")) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_PDFTRON_MISSING)); dual = false; producerFirstValidator = "PDFTools"; } } pathToPdftronExe = "\"" + pathToPdftronExe + "\""; String pdfTools = ""; String pdfTron = ""; String newPdfDiaTxt = ""; try { int iCategory = 999999999; // Create object PdfValidatorAPI docPdf = new PdfValidatorAPI(); // TODO: Erledigt Start mit PDFTron if (producerFirstValidator.contentEquals("PDFTron")) { // zuerst mit PDFTron und danach ggf mit PDFTools File report; Document doc = null; try { // Pfad zum Programm Pdftron File pdftronExe = new File(pathToPdftronExe); File output = directoryOfLogfile; String pathToPdftronOutput = output.getAbsolutePath(); StringBuffer command = new StringBuffer(pdftronExe + " "); command.append("-l " + level); command.append(" -o "); command.append("\""); command.append(output.getAbsolutePath()); command.append("\""); command.append(" "); command.append("\""); command.append(valDatei.getAbsolutePath()); command.append("\""); Process proc = null; Runtime rt = null; try { /* Der Name des generierten Reports lautet per default report.xml und es scheint keine * Mglichkeit zu geben, dies zu bersteuern. */ report = new File(pathToPdftronOutput, "report.xml"); // falls das File bereits existiert, z.B. von einem vorhergehenden Durchlauf, lschen // wir es if (report.exists()) { report.delete(); } Util.switchOffConsole(); rt = Runtime.getRuntime(); proc = rt.exec(command.toString().split(" ")); // .split(" ") ist notwendig wenn in einem Pfad ein Doppelleerschlag vorhanden ist! // Fehleroutput holen StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); // Output holen StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); // Threads starten errorGobbler.start(); outputGobbler.start(); // Warte, bis wget fertig ist proc.waitFor(); Util.switchOnConsole(); } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_SERVICEFAILED, e.getMessage())); return false; } finally { if (proc != null) { closeQuietly(proc.getOutputStream()); closeQuietly(proc.getInputStream()); closeQuietly(proc.getErrorStream()); } } // Ende PDFTRON direkt auszulsen String pathToPdftronReport = report.getAbsolutePath(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathToPdftronReport)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(bis); doc.normalize(); Integer passCount = new Integer(0); NodeList nodeLstI = doc.getElementsByTagName("Pass"); // Valide pdfa-Dokumente enthalten "<Validation> <Pass FileName..." Anzahl pass = anzahl // Valider pdfa for (int s = 0; s < nodeLstI.getLength(); s++) { passCount = passCount + 1; // Valide PDFA-Datei Module A-J sind Valid isValid = true; } if (passCount == 0) { if (dual) { // Duale Validierung mit PDFTools if (docPdf.open(valDatei.getAbsolutePath(), "", NativeLibrary.COMPLIANCE.ePDFUnk)) { // PDF Konnte geffnet werden docPdf.setStopOnError(true); docPdf.setReportingLevel(1); } else { docPdf.setStopOnError(true); docPdf.setReportingLevel(1); if (docPdf.getErrorCode() == NativeLibrary.ERRORCODE.PDF_E_PASSWORD) { getMessageService().logError(getTextResourceService() .getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFTOOLS_ENCRYPTED)); return false; } else { getMessageService().logError(getTextResourceService() .getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFTOOLS_DAMAGED)); return false; } } /* ePDFA1a 5122 ePDFA1b 5121 ePDFA2a 5891 ePDFA2b 5889 ePDFA2u 5890 */ if (level.contentEquals("1A")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5122)) { docPdf.validate(); } } else if (level.contentEquals("1B")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5121)) { docPdf.validate(); } } else if (level.contentEquals("2A")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5891)) { docPdf.validate(); } } else if (level.contentEquals("2B")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5889)) { docPdf.validate(); } } else if (level.contentEquals("2U")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5890)) { docPdf.validate(); } } else { // Validierung nach 2b level = "2B"; if (docPdf.open(valDatei.getAbsolutePath(), "", 5889)) { docPdf.validate(); } } // Error Category iCategory = docPdf.getCategories(); /* die Zahl kann auch eine Summe von Kategorien sein z.B. 6144=2048+4096 -> * getCategoryText gibt nur die erste Kategorie heraus (z.B. 2048) */ int success = 0; /* ErrorCode kann ungleich Null sein, wenn es nur eine Information zu einer nicht * einhaltung einer Empfehlung gefunden wurde. * * Entsprechend wird der ErrorCode ignoriert. */ PdfError err = docPdf.getFirstError(); PdfError err1 = docPdf.getFirstError(); @SuppressWarnings("unused") int iError = 0; while (err != null) { iError = err1.getErrorCode(); success = success + 1; // Get next error err = docPdf.getNextError(); } if (success == 0 && iCategory == 0) { // valide isValid = true; // Diskrepanz => PDF-Diagnosedaten ErrorCodes von PDFTron holen NodeList nodeLst = doc.getElementsByTagName("Error"); String errorCodes = ""; for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); errorCodes = errorCodes + " " + errorCode; } pdfTools = "<PDFTools><iCategory>0</iCategory><iError>0</iError></PDFTools>"; pdfTron = "<PDFTron><Code>" + errorCodes + "</Code></PDFTron>"; newPdfDiaTxt = "<Validation><ValFile>" + valDatei.getAbsolutePath() + "</ValFile><PdfaVL>" + level + "</PdfaVL>" + pdfTools + pdfTron + "</Validation>\n" + getTextResourceService().getText(MESSAGE_XML_DIAEND); Util.pdfDia(newPdfDiaTxt, pdfDia); Util.amp(pdfDia); } else { // invalid isValid = false; } } else { // keine duale Validierung -> invalid isValid = false; } } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); return false; } } else { // TODO: Erledigt Start mit PDFTools // zuerst mit PDFTools und danach ggf mit PDFTron if (docPdf.open(valDatei.getAbsolutePath(), "", NativeLibrary.COMPLIANCE.ePDFUnk)) { // PDF Konnte geffnet werden } else { if (docPdf.getErrorCode() == NativeLibrary.ERRORCODE.PDF_E_PASSWORD) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFTOOLS_ENCRYPTED)); return false; } else { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFTOOLS_DAMAGED)); return false; } } /* ePDFA1a 5122 ePDFA1b 5121 ePDFA2a 5891 ePDFA2b 5889 ePDFA2u 5890 */ if (level.contentEquals("1A")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5122)) { docPdf.validate(); } } else if (level.contentEquals("1B")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5121)) { docPdf.validate(); } } else if (level.contentEquals("2A")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5891)) { docPdf.validate(); } } else if (level.contentEquals("2B")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5889)) { docPdf.validate(); } } else if (level.contentEquals("2U")) { if (docPdf.open(valDatei.getAbsolutePath(), "", 5890)) { docPdf.validate(); } } else { // Validierung nach 2b level = "2B"; if (docPdf.open(valDatei.getAbsolutePath(), "", 5889)) { docPdf.validate(); } } docPdf.setStopOnError(false); docPdf.setReportingLevel(2); // Error Category iCategory = docPdf.getCategories(); /* die Zahl kann auch eine Summe von Kategorien sein z.B. 6144=2048+4096 -> getCategoryText * gibt nur die erste Kategorie heraus (z.B. 2048) */ int success = 0; PdfError err = docPdf.getFirstError(); PdfError err1 = docPdf.getFirstError(); int iError = 0; while (err != null) { iError = err1.getErrorCode(); success = success + 1; // Get next error err = docPdf.getNextError(); } if (success == 0 && iCategory == 0) { // valide isValid = true; } else { if (dual) { // duale Validierung File report; Document doc = null; try { // Pfad zum Programm Pdftron File pdftronExe = new File(pathToPdftronExe); File output = directoryOfLogfile; String pathToPdftronOutput = output.getAbsolutePath(); StringBuffer command = new StringBuffer(pdftronExe + " "); command.append("-l " + level); command.append(" -o "); command.append("\""); command.append(output.getAbsolutePath()); command.append("\""); command.append(" "); command.append("\""); command.append(valDatei.getAbsolutePath()); command.append("\""); Process proc = null; Runtime rt = null; try { /* Der Name des generierten Reports lautet per default report.xml und es scheint * keine Mglichkeit zu geben, dies zu bersteuern. */ report = new File(pathToPdftronOutput, "report.xml"); // falls das File bereits existiert, z.B. von einemvorhergehenden Durchlauf, lschen // wir es if (report.exists()) { report.delete(); } Util.switchOffConsole(); rt = Runtime.getRuntime(); proc = rt.exec(command.toString().split(" ")); // .split(" ") ist notwendig wenn in einem Pfad ein Doppelleerschlag vorhanden ist! // Fehleroutput holen StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); // Output holen StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); // Threads starten errorGobbler.start(); outputGobbler.start(); // Warte, bis wget fertig ist proc.waitFor(); Util.switchOnConsole(); } catch (Exception e) { getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_SERVICEFAILED, e.getMessage())); return false; } finally { if (proc != null) { closeQuietly(proc.getOutputStream()); closeQuietly(proc.getInputStream()); closeQuietly(proc.getErrorStream()); } } // Ende PDFTRON direkt auszulsen String pathToPdftronReport = report.getAbsolutePath(); BufferedInputStream bis = new BufferedInputStream( new FileInputStream(pathToPdftronReport)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(bis); doc.normalize(); Integer passCount = new Integer(0); NodeList nodeLstI = doc.getElementsByTagName("Pass"); // Valide pdfa-Dokumente enthalten "<Validation> <Pass FileName..." Anzahl pass = // anzahl Valider pdfa for (int s = 0; s < nodeLstI.getLength(); s++) { passCount = passCount + 1; // Valide PDFA-Datei Module A-J sind Valid isValid = true; // Diskrepanz => PDF-Diagnosedaten pdfTools = "<PDFTools><iCategory>" + iCategory + "</iCategory><iError>" + iError + "</iError></PDFTools>"; pdfTron = "<PDFTron><Code>Pass</Code></PDFTron>"; newPdfDiaTxt = "<Validation><ValFile>" + valDatei.getAbsolutePath() + "</ValFile><PdfaVL>" + level + "</PdfaVL>" + pdfTools + pdfTron + "</Validation>\n" + getTextResourceService().getText(MESSAGE_XML_DIAEND); Util.pdfDia(newPdfDiaTxt, pdfDia); Util.amp(pdfDia); } if (passCount == 0) { // Invalide PDFA-Datei (doppelt besttigt) isValid = false; } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_SERVICEFAILED, e.getMessage())); return false; } } else { // keine duale Validierung -> invalid isValid = false; } } } // TODO: Erledigt: Fehler Auswertung if (!isValid) { // Invalide PDFA-Datei boolean exponent0 = false; boolean exponent1 = false; boolean exponent2 = false; boolean exponent3 = false; boolean exponent4 = false; boolean exponent5 = false; boolean exponent6 = false; boolean exponent7 = false; boolean exponent8 = false; boolean exponent9 = false; boolean exponent10 = false; boolean exponent11 = false; boolean exponent12 = false; boolean exponent13 = false; boolean exponent14 = false; boolean exponent15 = false; boolean exponent16 = false; boolean exponent17 = false; boolean exponent18 = false; int iExp0 = (int) Math.pow(2, 0); int iExp1 = (int) Math.pow(2, 1); int iExp2 = (int) Math.pow(2, 2); int iExp3 = (int) Math.pow(2, 3); int iExp4 = (int) Math.pow(2, 4); int iExp5 = (int) Math.pow(2, 5); int iExp6 = (int) Math.pow(2, 6); int iExp7 = (int) Math.pow(2, 7); int iExp8 = (int) Math.pow(2, 8); int iExp9 = (int) Math.pow(2, 9); int iExp10 = (int) Math.pow(2, 10); int iExp11 = (int) Math.pow(2, 11); int iExp12 = (int) Math.pow(2, 12); int iExp13 = (int) Math.pow(2, 13); int iExp14 = (int) Math.pow(2, 14); int iExp15 = (int) Math.pow(2, 15); int iExp16 = (int) Math.pow(2, 16); int iExp17 = (int) Math.pow(2, 17); int iExp18 = (int) Math.pow(2, 18); if (producerFirstValidator.contentEquals("PDFTools") || dual) { // Invalide Kategorien von PDF-Tools if (iCategory >= iExp18) { exponent18 = true; iCategory = iCategory - iExp18; } if (iCategory >= iExp17) { exponent17 = true; iCategory = iCategory - iExp17; } if (iCategory >= iExp16) { exponent16 = true; iCategory = iCategory - iExp16; } if (iCategory >= iExp15) { exponent15 = true; iCategory = iCategory - iExp15; } if (iCategory >= iExp14) { exponent14 = true; iCategory = iCategory - iExp14; } if (iCategory >= iExp13) { exponent13 = true; iCategory = iCategory - iExp13; } if (iCategory >= iExp12) { exponent12 = true; iCategory = iCategory - iExp12; } if (iCategory >= iExp11) { exponent11 = true; iCategory = iCategory - iExp11; } if (iCategory >= iExp10) { exponent10 = true; iCategory = iCategory - iExp10; } if (iCategory >= iExp9) { exponent9 = true; iCategory = iCategory - iExp9; } if (iCategory >= iExp8) { exponent8 = true; iCategory = iCategory - iExp8; } if (iCategory >= iExp7) { exponent7 = true; iCategory = iCategory - iExp7; } if (iCategory >= iExp6) { exponent6 = true; iCategory = iCategory - iExp6; } if (iCategory >= iExp5) { exponent5 = true; iCategory = iCategory - iExp5; } if (iCategory >= iExp4) { exponent4 = true; iCategory = iCategory - iExp4; } if (iCategory >= iExp3) { exponent3 = true; iCategory = iCategory - iExp3; } if (iCategory >= iExp2) { exponent2 = true; iCategory = iCategory - iExp2; } if (iCategory >= iExp1) { exponent1 = true; iCategory = iCategory - iExp1; } if (iCategory >= iExp0) { exponent0 = true; iCategory = iCategory - iExp0; } } else { iCategory = 0; } File report = new File(directoryOfLogfile.getAbsolutePath(), "report.xml"); Document doc = null; if (producerFirstValidator.contentEquals("PDFTron") || dual) { // aus dem Output von Pdftron die Fehlercodes extrahieren und bersetzen String pathToPdftronReport = report.getAbsolutePath(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathToPdftronReport)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(bis); doc.normalize(); // Bsp. fr einen Error Code: <Error Code="e_PDFA173" die erste Ziffer nach e_PDFA ist der // Error Code. } /** Modul A **/ if (exponent1) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_AI_1, "iCategory_1")); } if (exponent2) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_AI_2, "iCategory_2")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // aus dem Output von Pdftron die Fehlercodes extrahieren und bersetzen String errorDigitA = "Fehler"; NodeList nodeLst = doc.getElementsByTagName("Error"); /* Bsp. fr einen Error Code: <Error Code="e_PDFA173" die erste Ziffer nach e_PDFA ist der * Error Code. */ for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); errorDigitA = errorCode.substring(6, 7); // der Error Code kann auch "Unknown" sein, dieser wird in den Code "0" bersetzt if (errorDigitA.equals("U")) { errorDigitA = "0"; } if (errorDigitA.equals("n")) { errorDigitA = "0"; } try { if (errorDigitA.equals("0")) { // Allgemeiner Fehler -> A isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } if (errorDigitA.equals("Fehler")) { // Fehler bei der Initialisierung Passierte bei einem Leerschlag im Namen isValid = false; getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_A_PDFA_INIT)); return false; } } /** Modul B **/ if (exponent0) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_B_PDFA) + getTextResourceService().getText(ERROR_XML_AI_0, "iCategory_0")); } if (exponent7) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_B_PDFA) + getTextResourceService().getText(ERROR_XML_AI_7, "iCategory_7")); } if (exponent18) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_B_PDFA) + getTextResourceService().getText(ERROR_XML_AI_18, "iCategory_18")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("1")) { // Struktur Fehler -> B isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_B_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_B_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul C **/ if (exponent3) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_C_PDFA) + getTextResourceService().getText(ERROR_XML_AI_3, "iCategory_3")); } if (exponent4) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_C_PDFA) + getTextResourceService().getText(ERROR_XML_AI_4, "iCategory_4")); } if (exponent5) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_C_PDFA) + getTextResourceService().getText(ERROR_XML_AI_5, "iCategory_5")); } if (exponent6) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_C_PDFA) + getTextResourceService().getText(ERROR_XML_AI_6, "iCategory_6")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("2")) { // Grafik Fehler -> C isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_C_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_C_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul D **/ if (exponent8) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_D_PDFA) + getTextResourceService().getText(ERROR_XML_AI_8, "iCategory_8")); } if (exponent9) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_D_PDFA) + getTextResourceService().getText(ERROR_XML_AI_9, "iCategory_9")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("3")) { // Schrift Fehler -> D isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_D_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_D_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul E **/ if (exponent10) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_E_PDFA) + getTextResourceService().getText(ERROR_XML_AI_10, "iCategory_10")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("4")) { // Transparenz Fehler -> E isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_E_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_E_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul F **/ if (exponent11) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_F_PDFA) + getTextResourceService().getText(ERROR_XML_AI_11, "iCategory_11")); } if (exponent12) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_F_PDFA) + getTextResourceService().getText(ERROR_XML_AI_12, "iCategory_12")); } if (exponent13) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_F_PDFA) + getTextResourceService().getText(ERROR_XML_AI_13, "iCategory_13")); } if (exponent14) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_F_PDFA) + getTextResourceService().getText(ERROR_XML_AI_14, "iCategory_14")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("5")) { // Annotations Fehler -> F isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_F_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_F_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul G **/ if (exponent15) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_G_PDFA) + getTextResourceService().getText(ERROR_XML_AI_15, "iCategory_15")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("6")) { // Aktions Fehler -> G isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_G_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } // neu sind die Interaktionen (J) bei den Aktionen (G) if (errorDigit.equals("9")) { // Interaktions Fehler -> J isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_G_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_G_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul H **/ if (exponent16) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_H_PDFA) + getTextResourceService().getText(ERROR_XML_AI_16, "iCategory_16")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("7")) { // Metadaten Fehler -> H isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_H_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_H_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul I **/ if (exponent17) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_I_PDFA) + getTextResourceService().getText(ERROR_XML_AI_17, "iCategory_17")); } if (producerFirstValidator.contentEquals("PDFTron") || dual) { // Analog Modul A NodeList nodeLst = doc.getElementsByTagName("Error"); for (int s = 0; s < nodeLst.getLength(); s++) { Node dateiNode = nodeLst.item(s); NamedNodeMap nodeMap = dateiNode.getAttributes(); Node errorNode = nodeMap.getNamedItem("Code"); String errorCode = errorNode.getNodeValue(); String errorCodeMsg = "error.xml.ai." + errorCode.substring(2); Node errorNodeM = nodeMap.getNamedItem("Message"); String errorMessage = errorNodeM.getNodeValue(); String errorDigit = errorCode.substring(6, 7); try { if (errorDigit.equals("8")) { // Zugnglichkeit Fehler -> I isValid = false; getMessageService() .logError(getTextResourceService().getText(MESSAGE_XML_MODUL_I_PDFA) + getTextResourceService().getText(errorCodeMsg, errorCode)); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_I_PDFA) + getTextResourceService().getText(ERROR_XML_AI_TRANSLATE, errorCode, errorMessage)); } } } /** Modul J **/ // neu sind die Interaktionen (J) bei den Aktionen (G) docPdf.close(); // Destroy the object docPdf.destroyObject(); } } catch (Exception e) { getMessageService().logError(getTextResourceService().getText(MESSAGE_XML_MODUL_A_PDFA) + getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); } return isValid; }
From source file:gov.lanl.adore.djatoka.plugin.ExtractPDF.java
/** * Extracts region defined in DjatokaDecodeParam as BufferedImage * @param input absolute file path of PDF file. * @param params DjatokaDecodeParam instance containing region and transform settings. * @return extracted region as a BufferedImage * @throws DjatokaException//from ww w . j a va2 s.co m */ @Override public BufferedImage process(String input, DjatokaDecodeParam params) throws DjatokaException { logger.debug("ExtractPDF.process:\n\tinput: " + input + "\n\tparams: " + params); if (input == null) throw new DjatokaException("Unknown failure while converting file: no image produced."); try { setPDFCommandsPath(); } catch (IllegalStateException e) { logger.error("Failed to set PDF commands path: ", e); throw e; } int page_number = 1 + params.getCompositingLayer(); // From 0-based to 1-based. int status = 0; BufferedImage processedImage = null; try { /* // First get max physical dim of bounding box of the page // to compute the DPI to ask for.. otherwise some AutoCAD // drawings can produce enormous files even at 75dpi, for // 48" drawings.. int dpi = 0; Dimension pageSize = getPDFPageSize(input, page_number); if (pageSize == null) { logger.error("Sanity check: Did not find \"Page " + page_number + " size\" line in output of pdfinfo, file="+input); throw new IllegalArgumentException("Failed to get \"Page " + page_number + " size\" of PDF with pdfinfo."); } else { double w = pageSize.getWidth(); double h = pageSize.getHeight(); int maxdim = (int)Math.max(Math.abs(w), Math.abs(h)); dpi = Math.min(MAX_DPI, (MAX_PX * 72 / maxdim)); logger.debug("DPI: pdfinfo method got dpi="+dpi+" for max dim="+maxdim+" (points, 1/72\")"); } */ // Scale int dpi = getScaledDPI(params); // Requires Sun JAI imageio additions to read ppm directly. // this will get "-[0]+1.ppm" appended to it by pdftoppm File outPrefixF = File.createTempFile("pdftopng", "out"); String outPrefix = outPrefixF.toString(); outPrefixF.delete(); //String pdfCmd[] = PDFTOPPM_COMMAND.clone(); ArrayList<String> pdfCmd = new ArrayList<String>(Arrays.asList(PDFTOPPM_COMMAND)); pdfCmd.set(PDFTOPPM_COMMAND_POSITION_BIN, pdftoppmPath); pdfCmd.set(PDFTOPPM_COMMAND_POSITION_FIRSTPAGE, "" + page_number); pdfCmd.set(PDFTOPPM_COMMAND_POSITION_LASTPAGE, "" + page_number); pdfCmd.set(PDFTOPPM_COMMAND_POSITION_DPI, String.valueOf(dpi)); pdfCmd.set(PDFTOPPM_COMMAND_POSITION_FILE, input.toString()); pdfCmd.set(PDFTOPPM_COMMAND_POSITION_OUTPUTFILE, outPrefix); // Crop Rectangle crop = getCropParam(params); if (crop != null) { String[] cropParams = { "-x", "" + (int) crop.getX(), "-y", "" + (int) crop.getY(), "-W", "" + (int) crop.getWidth(), "-H", "" + (int) crop.getHeight() }; pdfCmd.addAll(PDFTOPPM_COMMAND_POSITION_OPTIONAL_EXTRAS, Arrays.asList(cropParams)); } String[] pdfCmdA = pdfCmd.toArray(new String[pdfCmd.size()]); logger.debug("Running pdftoppm command: " + Arrays.deepToString(pdfCmdA)); //logger.debug("Running pdftoppm command: " + pdfCmd.toString()); File outf = null; Process pdfProc = null; try { pdfProc = Runtime.getRuntime().exec(pdfCmdA); status = pdfProc.waitFor(); logger.debug("status: " + status); // pdftoppm uses variable numbers of padding 0s to the output prefix. // E.g., may be prefix-000001.png, prefix-001.png or even prefix-01.png. // Version 0.12.3 (Poppler, not XPDF) seems to consider the total number of pages. // So, for example, in a PDF with 90 pages, the output will be "prefix-02.png"; // for a PDF with 350 pages, the output will be "prefix-002.png". // FIXME: try some approach where the PDF number of pages is considered without // running pdfinfo command again, thus making it simpler to determine the number // of padding zeros. Right now we going "brute force" because we do not know if // it is feasable to once again run the pdfinfo command. String tests[] = { outPrefix + "-" + page_number + ".png", outPrefix + "-0" + page_number + ".png", outPrefix + "-00" + page_number + ".png", outPrefix + "-000" + page_number + ".png", outPrefix + "-0000" + page_number + ".png", outPrefix + "-00000" + page_number + ".png" }; for (String outname : tests) { if ((new File(outname)).exists()) { outf = new File(outname); break; } } logger.debug("PDFTOPPM output is: " + outf + ", exists=" + outf != null ? outf.exists() : "!"); processedImage = ImageIO.read(outf); // Rotate if (params.getRotationDegree() > 0) { processedImage = ImageProcessingUtils.rotate(processedImage, params.getRotationDegree()); } } catch (InterruptedException e) { logger.error("Failed converting PDF file to image: ", e); throw new IllegalArgumentException("Failed converting PDF file to image: ", e); } finally { if (outf != null) outf.delete(); // Our exec() should not produce any output, but we want to stay safe. // http://mark.koli.ch/2011/01/leaky-pipes-remember-to-close-your-streams-when-using-javas-runtimegetruntimeexec.html org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getOutputStream()); org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getInputStream()); org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getErrorStream()); } } catch (Exception e) { logger.error("Failed converting PDF file to image: ", e); throw new IllegalArgumentException("Failed converting PDF file to image: ", e); } finally { if (status != 0) logger.error("PDF conversion proc failed, exit status=" + status + ", file=" + input); } return processedImage; }
From source file:de.interactive_instruments.ShapeChange.Target.FeatureCatalogue.FeatureCatalogue.java
public void initialise(PackageInfo p, Model m, Options o, ShapeChangeResult r, boolean diagOnly) throws ShapeChangeAbortException { pi = p;// w w w . j a v a 2 s .co m model = m; options = o; result = r; try { if (!initialised) { initialised = true; String pathToJavaExe_ = options.parameter(this.getClass().getName(), PARAM_JAVA_EXE_PATH); if (pathToJavaExe_ != null && pathToJavaExe_.trim().length() > 0) { pathToJavaExe = pathToJavaExe_.trim(); if (!pathToJavaExe.startsWith("\"")) { pathToJavaExe = "\"" + pathToJavaExe; } if (!pathToJavaExe.endsWith("\"")) { pathToJavaExe = pathToJavaExe + "\""; } String jo_tmp = options.parameter(this.getClass().getName(), PARAM_JAVA_OPTIONS); if (jo_tmp != null && jo_tmp.trim().length() > 0) { javaOptions = jo_tmp.trim(); } /* * check path - and potentially also options - by invoking * the exe */ List<String> cmds = new ArrayList<String>(); cmds.add(pathToJavaExe); if (javaOptions != null) { cmds.add(javaOptions); } cmds.add("-version"); ProcessBuilder pb = new ProcessBuilder(cmds); try { Process proc = pb.start(); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream()); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream()); errorGobbler.start(); outputGobbler.start(); errorGobbler.join(); outputGobbler.join(); int exitVal = proc.waitFor(); if (exitVal != 0) { if (errorGobbler.hasResult()) { MessageContext mc = result.addFatalError(this, 21, StringUtils.join(cmds, " "), "" + exitVal); mc.addDetail(this, 27, errorGobbler.getResult()); } else { result.addFatalError(this, 21, StringUtils.join(cmds, " "), "" + exitVal); } throw new ShapeChangeAbortException(); } } catch (InterruptedException e) { result.addFatalError(this, 22); throw new ShapeChangeAbortException(); } } encounteredAppSchemasByName = new TreeMap<String, Integer>(); initialiseFromOptions(); String s = null; Model refModel_tmp = getReferenceModel(); if (refModel_tmp != null) { /* * Ensure that IDs used in the reference model are unique to * that model and do not get mixed up with the IDs of the * input model. * * REQUIREMENT for model diff: two objects with equal ID * must represent the same model element. If a model element * is deleted in the reference model, then a new model * element in the input model must not have the same ID. * * It looks like this cannot be guaranteed. Therefore we add * a prefix to the IDs of the model elements in the * reference model. */ refModel = new GenericModel(refModel_tmp); refModel_tmp.shutdown(); refModel.addPrefixToModelElementIDs("refmodel_"); } String xmlName = outputFilename + ".tmp.xml"; // Check whether we can use the given output directory File outputDirectoryFile = new File(outputDirectory); boolean exi = outputDirectoryFile.exists(); if (!exi) { outputDirectoryFile.mkdirs(); exi = outputDirectoryFile.exists(); } boolean dir = outputDirectoryFile.isDirectory(); boolean wrt = outputDirectoryFile.canWrite(); boolean rea = outputDirectoryFile.canRead(); if (!exi || !dir || !wrt || !rea) { result.addFatalError(this, 12, outputDirectory); throw new ShapeChangeAbortException(); } String encoding_ = encoding == null ? "UTF-8" : model.characterEncoding(); OutputStream fout = new FileOutputStream(outputDirectory + "/" + xmlName); OutputStream bout = new BufferedOutputStream(fout, streamBufferSize); OutputStreamWriter outputXML = new OutputStreamWriter(bout, encoding_); writer = new XMLWriter(outputXML, encoding_); writer.forceNSDecl("http://www.w3.org/2001/XMLSchema-instance", "xsi"); writer.startDocument(); writer.processingInstruction("xml-stylesheet", "type='text/xsl' href='./html.xsl'"); writer.comment("Feature catalogue created using ShapeChange"); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("http://www.w3.org/2001/XMLSchema-instance", "noNamespaceSchemaLocation", "xsi:noNamespaceSchemaLocation", "CDATA", "FC.xsd"); writer.startElement("", "FeatureCatalogue", "", atts); s = options.parameter(this.getClass().getName(), "name"); if (s != null && s.length() > 0) writer.dataElement("name", s); else writer.dataElement("name", "unknown"); s = options.parameter(this.getClass().getName(), "scope"); if (s != null && s.length() > 0) PrintLineByLine(s, "scope", null); else { writer.dataElement("scope", "unknown"); } s = options.parameter(this.getClass().getName(), "versionNumber"); if (s != null && s.length() > 0) writer.dataElement("versionNumber", s); else writer.dataElement("versionNumber", "unknown"); s = options.parameter(this.getClass().getName(), "versionDate"); if (s != null && s.length() > 0) writer.dataElement("versionDate", s); else writer.dataElement("versionDate", "unknown"); s = options.parameter(this.getClass().getName(), "producer"); if (s != null && s.length() > 0) writer.dataElement("producer", s); else writer.dataElement("producer", "unknown"); } // we need to compute the diff for each application schema if (refModel != null) { SortedSet<PackageInfo> set = refModel.schemas(p.name()); if (set.size() == 1) { /* * Get the full names of classes (in lower case) from the * input schema so that later we can look them up by their * full name (within the schema, not in the model). */ inputSchemaClassesByFullNameInSchema = new HashMap<String, ClassInfo>(); for (ClassInfo ci : model.classes(pi)) { inputSchemaClassesByFullNameInSchema.put(ci.fullNameInSchema().toLowerCase(Locale.ENGLISH), ci); } // compute diffs differ = new Differ(); refPackage = set.iterator().next(); SortedMap<Info, SortedSet<DiffElement>> pi_diffs = differ.diff(p, refPackage); // merge diffs for pi with existing diffs (from other // schemas) differ.merge(diffs, pi_diffs); // log the diffs found for pi for (Entry<Info, SortedSet<DiffElement>> me : pi_diffs.entrySet()) { MessageContext mc = result.addInfo( "Model difference - " + me.getKey().fullName().replace(p.fullName(), p.name())); for (DiffElement diff : me.getValue()) { String s = diff.change + " " + diff.subElementType; if (diff.subElementType == ElementType.TAG) s += "(" + diff.tag + ")"; if (diff.subElement != null) s += " " + diff.subElement.name(); else if (diff.diff != null) s += " " + (new diff_match_patch()).diff_prettyHtml(diff.diff); else s += " ???"; mc.addDetail(s); } } /* * switch to default xslt for html diff - unless the * configuration explicitly names an XSLT file to use */ if (options.parameter(this.getClass().getName(), "xslhtmlFile") == null) { xslhtmlfileName = DEFAULT_XSL_HTML_DIFF_FILE_NAME; } } else { result.addWarning(null, 308, p.name()); refModel = null; } } writer.startElement("ApplicationSchema", "id", "_P" + pi.id()); /* * Determine if app schema with same name has been encountered * before, and choose name accordingly */ String nameForAppSchema = null; if (encounteredAppSchemasByName.containsKey(pi.name())) { int count = encounteredAppSchemasByName.get(pi.name()).intValue(); count++; nameForAppSchema = pi.name() + " (" + count + ")"; encounteredAppSchemasByName.put(pi.name(), new Integer(count)); } else { nameForAppSchema = pi.name(); encounteredAppSchemasByName.put(pi.name(), new Integer(1)); } // now set the name of the application schema writer.dataElement("name", nameForAppSchema); String s = pi.definition(); if (s != null && s.length() > 0) { PrintLineByLine(s, "definition", null); } s = pi.description(); if (s != null && s.length() > 0) { PrintLineByLine(s, "description", null); } s = pi.version(); if (s != null && s.length() > 0) { writer.dataElement("versionNumber", s); } writer.startElement("taggedValues"); s = pi.taggedValue(TransformationConstants.TRF_TV_NAME_GENERATIONDATETIME); if (s != null && s.trim().length() > 0) { writer.dataElement(TransformationConstants.TRF_TV_NAME_GENERATIONDATETIME, PrepareToPrint(s)); } writer.endElement("taggedValues"); if (pi.getDiagrams() != null) { appendImageInfo(pi.getDiagrams()); } writer.endElement("ApplicationSchema"); /* * Check if there are any deletions of classes or packages that are * owned by the application schema package. */ if (hasDiff(pi, ElementType.SUBPACKAGE, Operation.DELETE)) { Set<DiffElement> pkgdiffs = getDiffs(pi, ElementType.SUBPACKAGE, Operation.DELETE); for (DiffElement diff : pkgdiffs) { // child package was deleted PrintPackage((PackageInfo) diff.subElement, Operation.DELETE); } } printContainedPackages(pi); /* * NOTE: inserted or unchanged classes are handled in * process(ClassInfo) method */ printDeletedClasses(pi); } catch (Exception e) { String msg = e.getMessage(); if (msg != null) { result.addError(msg); } e.printStackTrace(System.err); } }
From source file:com.cloudera.sqoop.mapreduce.MySQLDumpMapper.java
/** * Import the table into HDFS by using mysqldump to pull out the data from * the database and upload the files directly to HDFS. *//* ww w.j a va 2 s . co m*/ public void map(String splitConditions, NullWritable val, Context context) throws IOException, InterruptedException { LOG.info("Beginning mysqldump fast path import"); ArrayList<String> args = new ArrayList<String>(); String tableName = conf.get(MySQLUtils.TABLE_NAME_KEY); // We need to parse the connect string URI to determine the database name. // Using java.net.URL directly on the connect string will fail because // Java doesn't respect arbitrary JDBC-based schemes. So we chop off the // scheme (everything before '://') and replace it with 'http', which we // know will work. String connectString = conf.get(MySQLUtils.CONNECT_STRING_KEY); String databaseName = JdbcUrl.getDatabaseName(connectString); String hostname = JdbcUrl.getHostName(connectString); int port = JdbcUrl.getPort(connectString); if (null == databaseName) { throw new IOException("Could not determine database name"); } LOG.info("Performing import of table " + tableName + " from database " + databaseName); args.add(MySQLUtils.MYSQL_DUMP_CMD); // requires that this is on the path. String password = conf.get(MySQLUtils.PASSWORD_KEY); String passwordFile = null; Process p = null; AsyncSink sink = null; AsyncSink errSink = null; PerfCounters counters = new PerfCounters(); try { // --defaults-file must be the first argument. if (null != password && password.length() > 0) { passwordFile = MySQLUtils.writePasswordFile(conf); args.add("--defaults-file=" + passwordFile); } // Don't use the --where="<whereClause>" version because spaces in it can // confuse Java, and adding in surrounding quotes confuses Java as well. String whereClause = conf.get(MySQLUtils.WHERE_CLAUSE_KEY, "(1=1)") + " AND (" + splitConditions + ")"; args.add("-w"); args.add(whereClause); args.add("--host=" + hostname); if (-1 != port) { args.add("--port=" + Integer.toString(port)); } args.add("--skip-opt"); args.add("--compact"); args.add("--no-create-db"); args.add("--no-create-info"); args.add("--quick"); // no buffering args.add("--single-transaction"); String username = conf.get(MySQLUtils.USERNAME_KEY); if (null != username) { args.add("--user=" + username); } // If the user supplied extra args, add them here. String[] extra = conf.getStrings(MySQLUtils.EXTRA_ARGS_KEY); if (null != extra) { for (String arg : extra) { args.add(arg); } } args.add(databaseName); args.add(tableName); // begin the import in an external process. LOG.debug("Starting mysqldump with arguments:"); for (String arg : args) { LOG.debug(" " + arg); } // Actually start the mysqldump. p = Runtime.getRuntime().exec(args.toArray(new String[0])); // read from the stdout pipe into the HDFS writer. InputStream is = p.getInputStream(); if (MySQLUtils.outputDelimsAreMySQL(conf)) { LOG.debug("Output delimiters conform to mysqldump; " + "using straight copy"); sink = new CopyingAsyncSink(context, counters); } else { LOG.debug("User-specified delimiters; using reparsing import"); LOG.info("Converting data to use specified delimiters."); LOG.info("(For the fastest possible import, use"); LOG.info("--mysql-delimiters to specify the same field"); LOG.info("delimiters as are used by mysqldump.)"); sink = new ReparsingAsyncSink(context, conf, counters); } // Start an async thread to read and upload the whole stream. counters.startClock(); sink.processStream(is); // Start an async thread to send stderr to log4j. errSink = new LoggingAsyncSink(LOG); errSink.processStream(p.getErrorStream()); } finally { // block until the process is done. int result = 0; if (null != p) { while (true) { try { result = p.waitFor(); } catch (InterruptedException ie) { // interrupted; loop around. continue; } break; } } // Remove the password file. if (null != passwordFile) { if (!new File(passwordFile).delete()) { LOG.error("Could not remove mysql password file " + passwordFile); LOG.error("You should remove this file to protect your credentials."); } } // block until the stream sink is done too. int streamResult = 0; if (null != sink) { while (true) { try { streamResult = sink.join(); } catch (InterruptedException ie) { // interrupted; loop around. continue; } break; } } // Try to wait for stderr to finish, but regard any errors as advisory. if (null != errSink) { try { if (0 != errSink.join()) { LOG.info("Encountered exception reading stderr stream"); } } catch (InterruptedException ie) { LOG.info("Thread interrupted waiting for stderr to complete: " + ie.toString()); } } LOG.info("Transfer loop complete."); if (0 != result) { throw new IOException("mysqldump terminated with status " + Integer.toString(result)); } if (0 != streamResult) { throw new IOException("Encountered exception in stream sink"); } counters.stopClock(); LOG.info("Transferred " + counters.toString()); } }
From source file:eu.numberfour.n4js.npmexporter.ui.NpmExportWizard.java
@Override public boolean performFinish() { String destination = exportPage.getDestinationValue(); List<IProject> toExport = exportPage.getChosenProjects(); boolean shouldPackAsTarball = exportPage.getShouldPackAsTarball(); File folder = new File(destination); // remap all IProjects List<? extends IN4JSProject> toExportIN4JSProjects = mapToIN4JSProjects(toExport); if (runTools() && toolRunnerPage.isToolrunRequested()) { // bring to front. ((WizardDialog) getContainer()).showPage(toolRunnerPage); }/*from w w w. j a v a 2 s. c o m*/ try { npmExporter.export(toExportIN4JSProjects, folder); if (shouldPackAsTarball) { npmExporter.tarAndZip(toExportIN4JSProjects, folder); } boolean runIt = runTools() && toolRunnerPage.queryRunTool(); if (runIt) { final List<String> toolCommand = toolRunnerPage.getCommand(); getContainer().run(true, true, new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { List<String> cmds = newArrayList(); // cmds.add("echo"); // prepend with echo for debug TODO remove // cmds.addAll(toolCommand); cmds.add("bash"); cmds.add("-login"); cmds.add("-c"); // cmds.addAll(toolCommand); cmds.add(Joiner.on(" ").join(toolCommand)); System.out.println("Comman will be: " + Joiner.on(" :: ").join(cmds)); for (IN4JSProject p : toExportIN4JSProjects) { String info = "Processing " + p.toString() + "\n"; System.out.println(info); toolRunnerPage.appendText(info); File dir = npmExporter.exportDestination(p, folder); ProcessBuilder pb = new ProcessBuilder(); pb.directory(dir); pb.command(cmds); pb.redirectErrorStream(true); Process proc = pb.start(); // handle each of proc's streams in a separate thread ExecutorService handlerThreadPool = Executors.newFixedThreadPool(3); // handlerThreadPool.submit(new Runnable() { // @Override // public void run() { // // we want to write to the stdin of the process // BufferedWriter stdin = new BufferedWriter( // new OutputStreamWriter(proc.getOutputStream())); // // // read from our own stdin so we can write it to proc's stdin // BufferedReader myStdin = // new BufferedReader(new InputStreamReader(System.in)); // String line = null; // try { // do { // line = myStdin.readLine(); // stdin.write(String.format("%s%n", line)); // stdin.flush(); // } while(! "exit".equalsIgnoreCase(line)); // } catch(IOException e) { // e.printStackTrace(); // } // } // }); handlerThreadPool.submit(new Runnable() { @Override public void run() { // we want to read the stdout of the process BufferedReader stdout = new BufferedReader( new InputStreamReader(proc.getInputStream())); String line; try { while (null != (line = stdout.readLine())) { System.err.printf("[stderr] %s%n", line); toolRunnerPage.appendConsoleOut(line); } } catch (IOException e) { e.printStackTrace(); } } }); handlerThreadPool.submit(new Runnable() { @Override public void run() { // we want to read the stderr of the process BufferedReader stderr = new BufferedReader( new InputStreamReader(proc.getErrorStream())); String line; try { while (null != (line = stderr.readLine())) { System.err.printf("[stderr] %s%n", line); toolRunnerPage.appendConsoleErr(line); } } catch (IOException e) { e.printStackTrace(); } } }); // wait for the process to terminate int exitCode = proc.waitFor(); System.out.printf("Process terminated with exit code %d%n", exitCode); handlerThreadPool.shutdown(); } // done with all projects. // wait for close. toolRunnerPage.queryCloseDialog(); } catch (Exception e) { throw new InvocationTargetException(e); } } }); } } catch (IOException | ArchiveException | CompressorException e) { e.printStackTrace(); Status s = new Status(ERROR, NpmExporterActivator.PLUGIN_ID, "Error occured during export.", e); N4JSActivator.getInstance().getLog().log(s); return false; } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // successfully done, then store relevant history: exportPage.finish(); if (runTools()) { toolRunnerPage.finish(); } return true; }
From source file:marytts.tools.voiceimport.HTKLabeler.java
/** * Force Align database for Automatic labels * @throws Exception//from www .j a va 2 s.co m */ private void hviteAligning() throws Exception { String hvite = getProp(HTKDIR) + File.separator + "HVite"; // -A -D -V -T 1 "; // to add -A -D -V -T 1 in every function File htkFile = new File(hvite); if (!htkFile.exists()) { throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist"); } String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf"; String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list"; // Virtual sp change_ phoneList should be a member? // Without sp: /*String phoneList = getProp(HTDIR)+File.separator +"etc"+File.separator+"htk.phone2.list";*/ // Whit sp: String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list"; String hmmDef = getProp(HTDIR) + File.separator + "hmm" + File.separator + "hmm-final" + File.separator + "hmmdefs"; String macros = getProp(HTDIR) + File.separator + "hmm" + File.separator + "hmm-final" + File.separator + "macros"; // Virtual sp change_ phoneMlf should be a member? // Without sp: /*String phoneMlf = getProp(HTDIR)+File.separator +"etc"+File.separator+"htk.phones2.mlf";*/ // Whit sp: String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones3.mlf"; String alignedMlf = getProp(HTDIR) + File.separator + "aligned.mlf"; String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.dict"; String labDir = getProp(HTDIR) + File.separator + "lab"; Runtime rtime = Runtime.getRuntime(); //get a shell Process process = rtime.exec("/bin/bash"); //get an output stream to write to the shell //when no sp use (-m)! PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream())); System.out.println("( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -o W -C " + configFile + " -a -H " + macros + " -H " + hmmDef + " -i " + alignedMlf + " -t 250.0 -y lab" + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > log_hviteAligning.txt" + "; exit )\n"); pw.println("( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -o W -C " + configFile + " -a -H " + macros + " -H " + hmmDef + " -i " + alignedMlf + " -t 250.0 -y lab" + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > log_hviteAligning.txt" + "; exit )\n"); pw.flush(); //shut down pw.close(); process.waitFor(); // check exit value if (process.exitValue() != 0) { BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); throw new MaryConfigurationException(errorReader.readLine()); } }
From source file:marytts.tools.voiceimport.HTKLabeler.java
/** * Initialize HTK Training process/* ww w . j ava 2s .c o m*/ * @throws Exception */ private void initialiseHTKTrain() throws Exception { String hcompv = getProp(HTKDIR) + File.separator + "HCompV"; File htkFile = new File(hcompv); if (!htkFile.exists()) { throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist"); } String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf"; String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list"; Runtime rtime = Runtime.getRuntime(); //get a shell Process process = rtime.exec("/bin/bash"); //get an output stream to write to the shell PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream())); System.out.println("( cd " + getProp(HTDIR) + " ; mkdir hmm/hmm-dummy ; " + " mkdir hmm/hmm-final ; " + hcompv + " " + HTK_SO + " -C " + configFile + " -f 0.01 -m -S " + listFile + " -M " + getProp(HTDIR) + File.separator + "hmm/hmm-dummy " + getProp(HTDIR) + File.separator + "config" + File.separator + "htk.proto" + " > log_initialiseHTKTrain.txt" + "; exit )\n"); pw.print("( cd " + getProp(HTDIR) + " ; mkdir hmm/hmm-dummy ; " + " mkdir hmm/hmm-final ; " + hcompv + " " + HTK_SO + " -C " + configFile + " -f 0.01 -m -S " + listFile + " -M " + getProp(HTDIR) + File.separator + "hmm/hmm-dummy " + getProp(HTDIR) + File.separator + "config" + File.separator + "htk.proto" + " > log_initialiseHTKTrain.txt" + "; exit )\n"); pw.flush(); //shut down pw.close(); process.waitFor(); // check exit value if (process.exitValue() != 0) { BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); throw new MaryConfigurationException(errorReader.readLine()); } }
From source file:com.twosigma.beaker.core.rest.PluginServiceLocatorRest.java
/** * locatePluginService/*from w w w . j a v a 2s. c o m*/ * locate the service that matches the passed-in information about a service and return the * base URL the client can use to connect to the target plugin service. If such service * doesn't exist, this implementation will also start the service. * * @param pluginId * @param command name of the starting script * @param nginxRules rules to help setup nginx proxying * @param startedIndicator string indicating that the plugin has started * @param startedIndicatorStream stream to search for indicator, null defaults to stdout * @param recordOutput boolean, record out/err streams to output log service or not, null defaults * to false * @param waitfor if record output log service is used, string to wait for before logging starts * @return the base url of the service * @throws InterruptedException * @throws IOException */ @GET @Path("/{plugin-id}") @Produces(MediaType.TEXT_PLAIN) public Response locatePluginService(@PathParam("plugin-id") String pluginId, @QueryParam("command") String command, @QueryParam("nginxRules") @DefaultValue("rest") String nginxRules, @QueryParam("startedIndicator") String startedIndicator, @QueryParam("startedIndicatorStream") @DefaultValue("stdout") String startedIndicatorStream, @QueryParam("recordOutput") @DefaultValue("false") boolean recordOutput, @QueryParam("waitfor") String waitfor) throws InterruptedException, IOException, ExecutionException { PluginConfig pConfig = this.plugins.get(pluginId); if (pConfig != null && pConfig.isStarted()) { logger.info("plugin service " + pluginId + " already started at" + pConfig.getBaseUrl()); return buildResponse(pConfig.getBaseUrl(), false); } String password = RandomStringUtils.random(40, true, true); Process proc = null; String restartId = ""; /* * Only one plugin can be started at a given time since we need to find a free port. * We serialize starting of plugins and we parallelize nginx configuration reload with the actual plugin * evaluator start. */ synchronized (this) { // find a port to use for proxypass between nginx and the plugin final int port = getNextAvailablePort(this.portSearchStart); final String baseUrl = generatePrefixedRandomString(pluginId, 12).replaceAll("[\\s]", ""); pConfig = new PluginConfig(port, nginxRules, baseUrl, password); this.portSearchStart = pConfig.port + 1; this.plugins.put(pluginId, pConfig); if (nginxRules.startsWith("ipython")) { generateIPythonConfig(pluginId, port, password, command); if (isIPython4OrNewer(getIPythonVersion(pluginId, command))) { new JupyterWidgetsExtensionProcessor(pluginId, this.pluginDir).copyJupyterExtensionIfExists(); } } // reload nginx config restartId = generateNginxConfig(); Process restartproc = Runtime.getRuntime().exec(this.nginxRestartCommand, this.nginxEnv); startGobblers(restartproc, "restart-nginx-" + pluginId, null, null); restartproc.waitFor(); ArrayList<String> fullCommand = new ArrayList<String>(Arrays.asList(command.split("\\s+"))); String args; fullCommand.set(0, (this.pluginLocations.containsKey(pluginId) ? this.pluginLocations.get(pluginId) : this.pluginDir) + "/" + fullCommand.get(0)); if (Files.notExists(Paths.get(fullCommand.get(0)))) { throw new PluginServiceNotFoundException("plugin service " + pluginId + " not found at " + command); } List<String> extraArgs = this.pluginArgs.get(pluginId); if (extraArgs != null) { fullCommand.addAll(extraArgs); } fullCommand.add(Integer.toString(pConfig.port)); String[] env = buildEnv(pluginId, password); if (windows()) { String python = this.config.getInstallDirectory() + "\\python\\python"; fullCommand.add(0, python); } logger.info("Running"); for (int i = 0; i < fullCommand.size(); i++) { logger.info(i + ": " + fullCommand.get(i)); } proc = Runtime.getRuntime().exec(listToArray(fullCommand), env); } if (startedIndicator != null && !startedIndicator.isEmpty()) { InputStream is = startedIndicatorStream.equals("stderr") ? proc.getErrorStream() : proc.getInputStream(); InputStreamReader ir = new InputStreamReader(is); BufferedReader br = new BufferedReader(ir); String line = ""; while ((line = br.readLine()) != null) { logger.info("looking on " + startedIndicatorStream + " found:" + line); if (line.indexOf(startedIndicator) >= 0) { logger.info("Acknowledge " + pluginId + " plugin started due to " + startedIndicator); break; } } if (null == line) { throw new PluginServiceNotFoundException("plugin service: " + pluginId + " failed to start"); } } startGobblers(proc, pluginId, recordOutput ? this.outputLogService : null, waitfor); // check that nginx did actually restart String url = "http://127.0.0.1:" + this.restartPort + "/restart." + restartId + "/present.html"; try { spinCheck(url); } catch (Throwable t) { logger.warn("time out plugin = {}", pluginId); this.plugins.remove(pluginId); if (windows()) { new WinProcess(proc).killRecursively(); } else { proc.destroy(); // send SIGTERM } throw new NginxRestartFailedException( "nginx restart failed.\n" + "url=" + url + "\n" + "message=" + t.getMessage()); } pConfig.setProcess(proc); logger.info("Done starting " + pluginId); return buildResponse(pConfig.getBaseUrl(), true); }
From source file:eu.udig.omsbox.core.OmsScriptExecutor.java
/** * Execute an OMS script.//from w ww .ja v a2 s . c o m * * @param script the script file or the script string. * @param internalStream * @param errorStream * @param loggerLevelGui the log level as presented in the GUI, can be OFF|ON. This is not the OMS logger level, which * in stead has to be picked from the {@link OmsBoxConstants#LOGLEVELS_MAP}. * @param ramLevel the heap size to use in megabytes. * @return the process. * @throws Exception */ public Process exec(String script, final PrintStream internalStream, final PrintStream errorStream, String loggerLevelGui, String ramLevel) throws Exception { if (loggerLevelGui == null) loggerLevelGui = OmsBoxConstants.LOGLEVEL_GUI_OFF; File scriptFile = new File(script); if (!scriptFile.exists()) { // if the file doesn't exist, it is a script, let's put it into a file scriptFile = File.createTempFile("omsbox_script_", ".oms"); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(scriptFile)); bw.write(script); } finally { bw.close(); } } else { // it is a script in a file, read it to log it BufferedReader br = null; StringBuilder sb = new StringBuilder(); try { br = new BufferedReader(new FileReader(scriptFile)); String line = null; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } } finally { br.close(); } script = sb.toString(); } // tmp folder String tempdir = System.getProperty("java.io.tmpdir"); File omsTmp = new File(tempdir + File.separator + "oms"); if (!omsTmp.exists()) omsTmp.mkdirs(); List<String> arguments = new ArrayList<String>(); arguments.add(javaFile); // ram usage String ramExpr = "-Xmx" + ramLevel + "m"; arguments.add(ramExpr); // modules jars List<String> modulesJars = OmsModulesManager.getInstance().getModulesJars(); StringBuilder sb = new StringBuilder(); for (String moduleJar : modulesJars) { sb.append(File.pathSeparator).append(moduleJar); } String modulesJarsString = sb.toString().replaceFirst(File.pathSeparator, ""); String resourcesFlag = "-Doms.sim.resources=\"" + modulesJarsString + "\""; arguments.add(resourcesFlag); // grass gisbase String grassGisbase = OmsBoxPlugin.getDefault().getGisbasePreference(); if (grassGisbase != null && grassGisbase.length() > 0) { arguments.add("-D" + OmsBoxConstants.GRASS_ENVIRONMENT_GISBASE_KEY + "=" + grassGisbase); } String grassShell = OmsBoxPlugin.getDefault().getShellPreference(); if (grassShell != null && grassShell.length() > 0) { arguments.add("-D" + OmsBoxConstants.GRASS_ENVIRONMENT_SHELL_KEY + "=" + grassShell); } // all the arguments arguments.add("-cp"); arguments.add(classPath); arguments.add(CLI.class.getCanonicalName()); arguments.add("-r "); arguments.add("\"" + scriptFile.getAbsolutePath() + "\""); String homeDir = System.getProperty("java.io.tmpdir"); File homeFile = new File(homeDir); StringBuilder runSb = new StringBuilder(); for (String arg : arguments) { runSb.append(arg).append(" "); } String[] args; if (Platform.getOS().equals(Platform.OS_WIN32)) { File tmpRunFile = new File(homeFile, "udig_spatialtoolbox.bat"); FileUtils.writeStringToFile(tmpRunFile, "@echo off\n" + runSb.toString()); args = new String[] { "cmd", "/c", tmpRunFile.getAbsolutePath() }; } else { File tmpRunFile = new File(homeFile, "udig_spatialtoolbox.sh"); FileUtils.writeStringToFile(tmpRunFile, runSb.toString()); args = new String[] { "sh", tmpRunFile.getAbsolutePath() }; } // {javaFile, ramExpr, resourcesFlag, "-cp", classPath, // CLI.class.getCanonicalName(), "-r", // scriptFile.getAbsolutePath()}; ProcessBuilder processBuilder = new ProcessBuilder(args); // work in home // processBuilder.directory(homeFile); // environment Map<String, String> environment = processBuilder.environment(); // environment.put("CLASSPATH", classPath); final Process process = processBuilder.start(); internalStream.println( "Process started: " + new DateTime().toString(OmsBoxConstants.dateTimeFormatterYYYYMMDDHHMMSS)); internalStream.println(""); // command launched if (loggerLevelGui.equals(OmsBoxConstants.LOGLEVEL_GUI_ON)) { internalStream.println("------------------------------>8----------------------------"); internalStream.println("Launching command: "); internalStream.println("------------------"); List<String> command = processBuilder.command(); for (String arg : command) { internalStream.print(arg); internalStream.print(" "); } internalStream.println("\n"); internalStream.println("(you can run the above from command line, customizing the content)"); internalStream.println("----------------------------------->8---------------------------------"); internalStream.println(""); // script run internalStream.println("Script run: "); internalStream.println("-----------"); internalStream.println(script); internalStream.println(""); internalStream.println("------------------------------>8----------------------------"); internalStream.println(""); // environment used internalStream.println("Environment used: "); internalStream.println("-----------------"); Set<Entry<String, String>> entrySet = environment.entrySet(); for (Entry<String, String> entry : entrySet) { internalStream.print(entry.getKey()); internalStream.print(" =\t"); internalStream.println(entry.getValue()); } internalStream.println("------------------------------>8----------------------------"); internalStream.println(""); } internalStream.println(""); isRunning = true; new Thread() { public void run() { BufferedReader br = null; try { InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { internalStream.println(line); } } catch (Exception e) { e.printStackTrace(); errorStream.println(e.getLocalizedMessage()); } finally { if (br != null) try { br.close(); } catch (IOException e) { e.printStackTrace(); } isRunning = false; updateListeners(); } internalStream.println(""); internalStream.println(""); internalStream.println("Process finished: " + new DateTime().toString(OmsBoxConstants.dateTimeFormatterYYYYMMDDHHMMSS)); }; }.start(); new Thread() { public void run() { BufferedReader br = null; try { InputStream is = process.getErrorStream(); InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { /* * remove of ugly recurring geotools warnings. Not nice, but * at least users do not get confused. */ if (ConsoleMessageFilter.doRemove(line)) { continue; } errorStream.println(line); } } catch (Exception e) { e.printStackTrace(); errorStream.println(e.getLocalizedMessage()); } finally { if (br != null) try { br.close(); } catch (IOException e) { e.printStackTrace(); } } }; }.start(); return process; }
From source file:com.cloudera.sqoop.manager.DirectPostgresqlManager.java
@Override /**/*ww w.j av a2 s . c o m*/ * Import the table into HDFS by using psql to pull the data out of the db * via COPY FILE TO STDOUT. */ public void importTable(ImportJobContext context) throws IOException, ImportException { String tableName = context.getTableName(); SqoopOptions options = context.getOptions(); LOG.info("Beginning psql fast path import"); if (options.getFileLayout() != SqoopOptions.FileLayout.TextFile) { // TODO(aaron): Support SequenceFile-based load-in LOG.warn("File import layout" + options.getFileLayout() + " is not supported by"); LOG.warn("Postgresql direct import; import will proceed as text files."); } String commandFilename = null; String passwordFilename = null; Process p = null; AsyncSink sink = null; AsyncSink errSink = null; PerfCounters counters = new PerfCounters(); try { // Get the COPY TABLE command to issue, write this to a file, and pass // it in to psql with -f filename. Then make sure we delete this file // in our finally block. String copyCmd = getCopyCommand(tableName); commandFilename = writeCopyCommand(copyCmd); // Arguments to pass to psql on the command line. ArrayList<String> args = new ArrayList<String>(); // Environment to pass to psql. List<String> envp = Executor.getCurEnvpStrings(); // We need to parse the connect string URI to determine the database // name and the host and port. If the host is localhost and the port is // not specified, we don't want to pass this to psql, because we want to // force the use of a UNIX domain socket, not a TCP/IP socket. String connectString = options.getConnectString(); String databaseName = JdbcUrl.getDatabaseName(connectString); String hostname = JdbcUrl.getHostName(connectString); int port = JdbcUrl.getPort(connectString); if (null == databaseName) { throw new ImportException("Could not determine database name"); } LOG.info("Performing import of table " + tableName + " from database " + databaseName); args.add(PSQL_CMD); // requires that this is on the path. args.add("--tuples-only"); args.add("--quiet"); String username = options.getUsername(); if (username != null) { args.add("--username"); args.add(username); String password = options.getPassword(); if (null != password) { passwordFilename = writePasswordFile(password); // Need to send PGPASSFILE environment variable specifying // location of our postgres file. envp.add("PGPASSFILE=" + passwordFilename); } } args.add("--host"); args.add(hostname); if (port != -1) { args.add("--port"); args.add(Integer.toString(port)); } if (null != databaseName && databaseName.length() > 0) { args.add(databaseName); } // The COPY command is in a script file. args.add("-f"); args.add(commandFilename); // begin the import in an external process. LOG.debug("Starting psql with arguments:"); for (String arg : args) { LOG.debug(" " + arg); } // This writer will be closed by AsyncSink. SplittableBufferedWriter w = DirectImportUtils.createHdfsSink(options.getConf(), options, context); // Actually start the psql dump. p = Runtime.getRuntime().exec(args.toArray(new String[0]), envp.toArray(new String[0])); // read from the stdout pipe into the HDFS writer. InputStream is = p.getInputStream(); sink = new PostgresqlAsyncSink(w, options, counters); LOG.debug("Starting stream sink"); counters.startClock(); sink.processStream(is); errSink = new LoggingAsyncSink(LOG); errSink.processStream(p.getErrorStream()); } finally { // block until the process is done. LOG.debug("Waiting for process completion"); int result = 0; if (null != p) { while (true) { try { result = p.waitFor(); } catch (InterruptedException ie) { // interrupted; loop around. continue; } break; } } // Remove any password file we wrote if (null != passwordFilename) { if (!new File(passwordFilename).delete()) { LOG.error("Could not remove postgresql password file " + passwordFilename); LOG.error("You should remove this file to protect your credentials."); } } if (null != commandFilename) { // We wrote the COPY comand to a tmpfile. Remove it. if (!new File(commandFilename).delete()) { LOG.info("Could not remove temp file: " + commandFilename); } } // block until the stream sink is done too. int streamResult = 0; if (null != sink) { while (true) { try { streamResult = sink.join(); } catch (InterruptedException ie) { // interrupted; loop around. continue; } break; } } // Attempt to block for stderr stream sink; errors are advisory. if (null != errSink) { try { if (0 != errSink.join()) { LOG.info("Encountered exception reading stderr stream"); } } catch (InterruptedException ie) { LOG.info("Thread interrupted waiting for stderr to complete: " + ie.toString()); } } LOG.info("Transfer loop complete."); if (0 != result) { throw new IOException("psql terminated with status " + Integer.toString(result)); } if (0 != streamResult) { throw new IOException("Encountered exception in stream sink"); } counters.stopClock(); LOG.info("Transferred " + counters.toString()); } }