ExecuteServiceImpl extends AbstractJobProducer implements ExecuteService, ManagedService { protected String doProcess(List<String> arguments, MediaPackage mp, String outFileName, Type expectedType) throws ExecuteException { String params = arguments.remove(1); File outFile = null; MediaPackageElement[] elementsByFlavor = null; try { if (outFileName != null) { File firstElement = workspace.get(mp.getElements()[0].getURI()); outFile = new File(firstElement.getParentFile(), outFileName); } Pattern pat = Pattern.compile("#\\{([^\\{\\}\\(\\)]+)(?:\\(([^\\{\\}\\(\\)]+)\\))?\\}"); Matcher matcher = pat.matcher(params); StringBuffer sb = new StringBuffer(); while (matcher.find()) { if (matcher.group(1).equals("id")) { matcher.appendReplacement(sb, mp.getIdentifier().toString()); } else if (matcher.group(1).equals("flavor")) { elementsByFlavor = mp.getElementsByFlavor(MediaPackageElementFlavor.parseFlavor(matcher.group(2))); if (elementsByFlavor.length == 0) throw new ExecuteException("No elements in the MediaPackage match the flavor '" + matcher.group(2) + "'."); if (elementsByFlavor.length > 1) logger.warn("Found more than one element with flavor '{}'. Using {} by default...", matcher.group(2), elementsByFlavor[0].getIdentifier()); File elementFile = workspace.get(elementsByFlavor[0].getURI()); matcher.appendReplacement(sb, elementFile.getAbsolutePath()); } else if (matcher.group(1).equals("out")) { matcher.appendReplacement(sb, outFile.getAbsolutePath()); } else if (properties.get(matcher.group(1)) != null) { matcher.appendReplacement(sb, (String) properties.get(matcher.group(1))); } else if (bundleContext.getProperty(matcher.group(1)) != null) { matcher.appendReplacement(sb, bundleContext.getProperty(matcher.group(1))); } } matcher.appendTail(sb); params = sb.toString(); } catch (IllegalArgumentException e) { throw new ExecuteException("Tag 'flavor' must specify a valid MediaPackage element flavor.", e); } catch (NotFoundException e) { throw new ExecuteException( "The element '" + elementsByFlavor[0].getURI().toString() + "' does not exist in the workspace.", e); } catch (IOException e) { throw new ExecuteException("Error retrieving MediaPackage element from workspace: '" + elementsByFlavor[0].getURI().toString() + "'.", e); } arguments.addAll(splitParameters(params)); return runCommand(arguments, outFile, expectedType); } ExecuteServiceImpl(); @Override void activate(ComponentContext cc); @Override Job execute(String exec, String params, MediaPackageElement inElement, String outFileName, Type expectedType,
          float load); @Override Job execute(String exec, String params, MediaPackage mp, String outFileName, Type expectedType, float load); void setServiceRegistry(ServiceRegistry serviceRegistry); void setSecurityService(SecurityService securityService); void setUserDirectoryService(UserDirectoryService userDirectoryService); void setOrganizationDirectoryService(OrganizationDirectoryService organizationDirectory); void setWorkspace(Workspace workspace); @Override void updated(@SuppressWarnings("rawtypes") Dictionary properties); static final String COMMANDS_ALLOWED_PROPERTY; static final float DEFAULT_EXECUTE_JOB_LOAD; static final String EXECUTE_JOB_LOAD_KEY; }
@Test public void testNoElements() throws ExecuteException, NotFoundException { List<String> params = new ArrayList<>(); params.add("echo"); params.add(TEXT); try { executor.doProcess(params, (MediaPackageElement) null, null, null); Assert.fail("The input element should never be null"); } catch (NullPointerException e) { } }
@Test public void testWithInputElement() throws ExecuteException, NotFoundException { List<String> params = new ArrayList<>(); params.add("echo"); params.add(pattern); MediaPackageElement element = MediaPackageElementBuilderFactory.newInstance().newElementBuilder() .elementFromURI(baseDirURI); String result = executor.doProcess(params, element, null, null); Assert.assertEquals(result, ""); }
@Test public void testWithGlobalConfigParam() throws ExecuteException, NotFoundException { List<String> params = new ArrayList<>(); params.add("cat"); params.add("#{" + configKey1 + "}"); MediaPackage mp = null; String result = executor.doProcess(params, mp, null, null); Assert.assertEquals(result, ""); }
@Test public void testWithServiceConfigParam() throws ExecuteException, NotFoundException { List<String> params = new ArrayList<>(); params.add("cat"); params.add("#{" + configKey2 + "}"); MediaPackage mp = null; String result = executor.doProcess(params, mp, null, null); Assert.assertEquals(result, ""); }