Search in sources :

Example 1 with ExecutionResult

use of org.alfresco.util.exec.RuntimeExec.ExecutionResult in project alfresco-repository by Alfresco.

the class ImageMagickContentTransformerWorker method afterPropertiesSet.

/**
 * Checks for the JMagick and ImageMagick dependencies, using the common
 * {@link #transformInternal(File, String, File, String, TransformationOptions) transformation method} to check
 * that the sample image can be converted.
 */
@Override
public void afterPropertiesSet() {
    if (enabled) {
        if (!remoteTransformerClientConfigured() && executer == null) {
            throw new AlfrescoRuntimeException("System runtime executer not set");
        }
        super.afterPropertiesSet();
        if (!remoteTransformerClientConfigured()) {
            if (isAvailable()) {
                try {
                    // On some platforms / versions, the -version command seems to return an error code whilst still
                    // returning output, so let's not worry about the exit code!
                    ExecutionResult result = this.checkCommand.execute();
                    this.versionString = result.getStdOut().trim();
                } catch (Throwable e) {
                    setAvailable(false);
                    logger.error(getClass().getSimpleName() + " not available: " + (e.getMessage() != null ? e.getMessage() : ""));
                    // debug so that we can trace the issue if required
                    logger.debug(e);
                }
            }
        } else {
            Pair<Boolean, String> result = remoteTransformerClient.check(logger);
            Boolean isAvailable = result.getFirst();
            if (isAvailable != null && isAvailable) {
                setAvailable(true);
                versionString = result.getSecond().trim();
                logger.debug("Using legacy ImageMagick: " + versionString);
            } else {
                setAvailable(false);
                versionString = "unknown";
                String message = "Legacy remote ImageMagick is not available for transformations. " + result.getSecond();
                if (isAvailable == null) {
                    logger.debug(message);
                } else {
                    logger.error(message);
                }
            }
        }
    }
}
Also used : AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ExecutionResult(org.alfresco.util.exec.RuntimeExec.ExecutionResult)

Example 2 with ExecutionResult

use of org.alfresco.util.exec.RuntimeExec.ExecutionResult in project alfresco-repository by Alfresco.

the class RemoteTransformerClient method check.

/**
 * Helper method that returns the same result type as {@link #check(Log)} given a local checkCommand.
 */
public static Pair<Boolean, String> check(RuntimeExec checkCommand) {
    ExecutionResult result = checkCommand.execute();
    Boolean success = result.getSuccess();
    String output = success ? result.getStdOut().trim() : result.toString();
    return new Pair<>(success, output);
}
Also used : ExecutionResult(org.alfresco.util.exec.RuntimeExec.ExecutionResult) Pair(org.alfresco.util.Pair)

Example 3 with ExecutionResult

use of org.alfresco.util.exec.RuntimeExec.ExecutionResult in project alfresco-repository by Alfresco.

the class RuntimeExecutableContentTransformerWorker method transform.

/**
 * Converts the source and target content to temporary files with the
 * correct extensions for the mimetype that they map to.
 */
public final void transform(ContentReader reader, ContentWriter writer, TransformationOptions options) throws Exception {
    // get mimetypes
    String sourceMimetype = getMimetype(reader);
    String targetMimetype = getMimetype(writer);
    // get the extensions to use
    String sourceExtension = getMimetypeService().getExtension(sourceMimetype);
    String targetExtension = getMimetypeService().getExtension(targetMimetype);
    if (sourceExtension == null || targetExtension == null) {
        throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" + "   source mimetype: " + sourceMimetype + "\n" + "   source extension: " + sourceExtension + "\n" + "   target mimetype: " + targetMimetype + "\n" + "   target extension: " + targetExtension);
    }
    // create required temp files
    File sourceFile = TempFileProvider.createTempFile(getClass().getSimpleName() + "_source_", "." + sourceExtension);
    File targetFile = TempFileProvider.createTempFile(getClass().getSimpleName() + "_target_", "." + targetExtension);
    Map<String, String> properties = new HashMap<String, String>(5);
    // copy options over
    Map<String, Object> optionsMap = options.toMap();
    for (Map.Entry<String, Object> entry : optionsMap.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        properties.put(key, (value == null ? null : value.toString()));
    }
    // add the source and target properties
    properties.put(VAR_SOURCE, sourceFile.getAbsolutePath());
    properties.put(VAR_TARGET, targetFile.getAbsolutePath());
    properties.put(VAR_PAGE_RANGE, "0-" + (options.getPageLimit() >= 0 ? options.getPageLimit() : ""));
    // pull reader file into source temp file
    reader.getContent(sourceFile);
    // execute the transformation command
    long timeoutMs = options.getTimeoutMs();
    ExecutionResult result = null;
    try {
        result = transformCommand.execute(properties, timeoutMs);
    } catch (Throwable e) {
        throw new ContentIOException("Transformation failed during command execution: \n" + transformCommand, e);
    }
    // check
    if (!result.getSuccess()) {
        throw new ContentIOException("Transformation failed - status indicates an error: \n" + result);
    }
    // check that the file was created
    if (!targetFile.exists()) {
        throw new ContentIOException("Transformation failed - target file doesn't exist: \n" + result);
    }
    // copy the target file back into the repo
    writer.putContent(targetFile);
    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Transformation completed: \n" + "   source: " + reader + "\n" + "   target: " + writer + "\n" + "   options: " + options + "\n" + "   result: \n" + result);
    }
}
Also used : HashMap(java.util.HashMap) ExecutionResult(org.alfresco.util.exec.RuntimeExec.ExecutionResult) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ExecutionResult

use of org.alfresco.util.exec.RuntimeExec.ExecutionResult in project alfresco-repository by Alfresco.

the class JmxDumpUtil method updateOSNameAttributeForLinux.

/**
 * Adds a Linux version
 *
 * @param osName os.name attribute
 * @return String
 */
public static String updateOSNameAttributeForLinux(String osName) {
    RuntimeExec exec = new RuntimeExec();
    Map<String, String[]> commandMap = new HashMap<String, String[]>(3, 1.0f);
    commandMap.put("Linux", new String[] { "lsb_release", "-d" });
    exec.setCommandsAndArguments(commandMap);
    ExecutionResult ret = exec.execute();
    if (ret.getSuccess()) {
        osName += " (" + ret.getStdOut().replace("\n", "") + ")";
    } else {
        commandMap.put("Linux", new String[] { "uname", "-a" });
        exec.setCommandsAndArguments(commandMap);
        ret = exec.execute();
        if (ret.getSuccess()) {
            osName += " (" + ret.getStdOut().replace("\n", "") + ")";
        } else {
            osName += " (Unknown)";
        }
    }
    return osName;
}
Also used : HashMap(java.util.HashMap) RuntimeExec(org.alfresco.util.exec.RuntimeExec) ExecutionResult(org.alfresco.util.exec.RuntimeExec.ExecutionResult)

Aggregations

ExecutionResult (org.alfresco.util.exec.RuntimeExec.ExecutionResult)4 HashMap (java.util.HashMap)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 File (java.io.File)1 Map (java.util.Map)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 Pair (org.alfresco.util.Pair)1 RuntimeExec (org.alfresco.util.exec.RuntimeExec)1