Search in sources :

Example 6 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class ExecuteServiceImpl method runCommand.

private String runCommand(List<String> command, File outFile, Type expectedType) throws ExecuteException {
    Process p = null;
    int result = 0;
    try {
        logger.info("Running command {}", command.get(0));
        logger.debug("Starting subprocess {} with arguments {}", command.get(0), StringUtils.join(command.subList(1, command.size()), ", "));
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(true);
        p = pb.start();
        result = p.waitFor();
        logger.debug("Command {} finished with result {}", command.get(0), result);
        if (result == 0) {
            // Read the command output
            if (outFile != null) {
                if (outFile.isFile()) {
                    URI newURI = workspace.putInCollection(ExecuteService.COLLECTION, outFile.getName(), new FileInputStream(outFile));
                    if (outFile.delete()) {
                        logger.debug("Deleted the local copy of the encoded file at {}", outFile.getAbsolutePath());
                    } else {
                        logger.warn("Unable to delete the encoding output at {}", outFile.getAbsolutePath());
                    }
                    return MediaPackageElementParser.getAsXml(MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(newURI, expectedType, null));
                } else {
                    throw new ExecuteException("Expected output file does not exist: " + outFile.getAbsolutePath());
                }
            }
            return "";
        } else {
            // 'Scanner' reads tokens delimited by an specific character (set).
            // By telling a Scanner to use the 'beginning of the input boundary' character as delimiter, which of course
            // will never find, yields the whole String as the next token.
            String line;
            try (Scanner scanner = new Scanner(p.getInputStream())) {
                scanner.useDelimiter("\\A");
                line = scanner.next();
            } catch (NoSuchElementException e) {
                line = "";
            }
            throw new ExecuteException(String.format("Process %s returned error code %d with this output:\n%s", command.get(0), result, line.trim()));
        }
    } catch (InterruptedException e) {
        throw new ExecuteException("The executor thread has been unexpectedly interrupted", e);
    } catch (IOException e) {
        // Only log the first argument, the executable, as other arguments may contain sensitive values
        // e.g. MySQL password/user, paths, etc. that should not be shown to caller
        logger.error("Could not start subprocess {}", command.get(0));
        throw new ExecuteException("Could not start subprocess: " + command.get(0), e);
    } catch (UnsupportedElementException e) {
        throw new ExecuteException("Couldn't create a new MediaPackage element of type " + expectedType.toString(), e);
    } catch (ConfigurationException e) {
        throw new ExecuteException("Couldn't instantiate a new MediaPackage element builder", e);
    } catch (MediaPackageException e) {
        throw new ExecuteException("Couldn't serialize a new Mediapackage element of type " + expectedType.toString(), e);
    } finally {
        IoSupport.closeQuietly(p);
    }
}
Also used : Scanner(java.util.Scanner) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) ConfigurationException(org.opencastproject.util.ConfigurationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class IdBuilderFactory method newIdBuilder.

/**
 * Factory method that returns an instance of an id builder.
 * <p>
 * It uses the following ordered lookup procedure to determine which implementation of the {@link IdBuilder} interface
 * to use:
 * <ul>
 * <li>Implementation specified using the <code>opencast.idbuilder</code> system property</li>
 * <li>Platform default implementation</li>
 * </ul>
 *
 * @return the id builder
 * @throws ConfigurationException
 *           If the builder cannot be instantiated
 */
public IdBuilder newIdBuilder() throws ConfigurationException {
    if (builder == null) {
        try {
            Class<?> builderClass = Class.forName(builderClassName);
            builder = (IdBuilder) builderClass.newInstance();
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException("Class not found while creating id builder: " + e.getMessage(), e);
        } catch (InstantiationException e) {
            throw new ConfigurationException("Instantiation exception while creating id builder: " + e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new ConfigurationException("Access exception while creating id builder: " + e.getMessage(), e);
        }
    }
    return builder;
}
Also used : ConfigurationException(org.opencastproject.util.ConfigurationException)

Example 8 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class MediaPackageSerializerTest method testRelativeLinuxPaths.

@Test
public void testRelativeLinuxPaths() {
    try {
        XPath xPath = XPathFactory.newInstance().newXPath();
        // Create a media package and add an element
        MediaPackage mediaPackage = mediaPackageBuilder.createNew();
        mediaPackage.add(linuxURI);
        // Test relative path, using serializer
        MediaPackageSerializer serializer = null;
        serializer = new DefaultMediaPackageSerializerImpl(new File(linuxRootURI));
        Document xml = MediaPackageParser.getAsXml(mediaPackage, serializer);
        // Test linux file relative to media package root
        String expected = linuxURI.toString().substring(linuxRootURI.toString().length() + 1);
        assertEquals(expected, xPath.evaluate("//url", xml));
    } catch (MediaPackageException e) {
        fail("Media package excpetion while reading media package from manifest: " + e.getMessage());
    } catch (ConfigurationException e) {
        fail("Configuration exception while reading media package from manifest: " + e.getMessage());
    } catch (MalformedURLException e) {
        fail("Exception while creating url: " + e.getMessage());
    } catch (UnsupportedElementException e) {
        fail("Error while creating media package: " + e.getMessage());
    } catch (XPathExpressionException e) {
        fail("Selecting node form xml document failed: " + e.getMessage());
    }
}
Also used : XPath(javax.xml.xpath.XPath) MalformedURLException(java.net.MalformedURLException) ConfigurationException(org.opencastproject.util.ConfigurationException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) File(java.io.File) Test(org.junit.Test)

Example 9 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class MediaPackageSerializerTest method testRelativeWindowsPaths.

@Test
public void testRelativeWindowsPaths() {
    try {
        XPath xPath = XPathFactory.newInstance().newXPath();
        // Create a media package and add an element
        MediaPackage mediaPackage = mediaPackageBuilder.createNew();
        mediaPackage.add(windowsURI);
        // Test relative path, using serializer
        MediaPackageSerializer serializer = null;
        serializer = new DefaultMediaPackageSerializerImpl(new File(windowsRootURI));
        Document xml = MediaPackageParser.getAsXml(mediaPackage, serializer);
        // Test windows file relative to media package root
        String expected = windowsURI.toString().substring(windowsRootURI.toString().length() + 1);
        assertEquals(expected, xPath.evaluate("//url", xml));
    } catch (MediaPackageException e) {
        fail("Media package excpetion while reading media package from manifest: " + e.getMessage());
    } catch (ConfigurationException e) {
        fail("Configuration exception while reading media package from manifest: " + e.getMessage());
    } catch (MalformedURLException e) {
        fail("Exception while creating url: " + e.getMessage());
    } catch (UnsupportedElementException e) {
        fail("Error while creating media package: " + e.getMessage());
    } catch (XPathExpressionException e) {
        fail("Selecting node form xml document failed: " + e.getMessage());
    }
}
Also used : XPath(javax.xml.xpath.XPath) MalformedURLException(java.net.MalformedURLException) ConfigurationException(org.opencastproject.util.ConfigurationException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) File(java.io.File) Test(org.junit.Test)

Example 10 with ConfigurationException

use of org.opencastproject.util.ConfigurationException in project opencast by opencast.

the class MediaPackageTest method testElementUrls.

@Test
public void testElementUrls() throws ParserConfigurationException, SAXException, IOException {
    try {
        XPath xPath = XPathFactory.newInstance().newXPath();
        // Create a media package and add an element
        MediaPackage mediaPackage = mediaPackageBuilder.createNew();
        mediaPackage.add(dcFile.toURI());
        // Test url
        String xmlString = MediaPackageParser.getAsXml(mediaPackage);
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document xml = docBuilder.parse(new ByteArrayInputStream(xmlString.getBytes()));
        String expected = dcFile.toURI().toURL().toExternalForm();
        assertEquals(expected, xPath.evaluate("//url", xml));
    // TODO: Add more
    } catch (MediaPackageException e) {
        fail("Media package excpetion while reading media package from manifest: " + e.getMessage());
    } catch (ConfigurationException e) {
        fail("Configuration exception while reading media package from manifest: " + e.getMessage());
    } catch (MalformedURLException e) {
        fail("Exception while creating url: " + e.getMessage());
    } catch (UnsupportedElementException e) {
        fail("Error while creating media package: " + e.getMessage());
    } catch (XPathExpressionException e) {
        fail("Selecting node form xml document failed: " + e.getMessage());
    }
}
Also used : XPath(javax.xml.xpath.XPath) MalformedURLException(java.net.MalformedURLException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfigurationException(org.opencastproject.util.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

ConfigurationException (org.opencastproject.util.ConfigurationException)13 Test (org.junit.Test)5 MalformedURLException (java.net.MalformedURLException)4 XPath (javax.xml.xpath.XPath)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 Document (org.w3c.dom.Document)4 HashMap (java.util.HashMap)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1 Scanner (java.util.Scanner)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1