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);
}
}
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;
}
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());
}
}
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());
}
}
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());
}
}
Aggregations