Java Code Examples for org.camunda.bpm.engine.impl.util.IoUtil#closeSilently()

The following examples show how to use org.camunda.bpm.engine.impl.util.IoUtil#closeSilently() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OSGiProcessApplicationScanner.java    From camunda-bpm-platform-osgi with Apache License 2.0 6 votes vote down vote up
protected void addResource(URL source, Map<String, byte[]> resourceMap, String resourceRootPath, String resourceName) {

    String resourcePath = (resourceRootPath == null ? "" : resourceRootPath).concat(resourceName);

    log.log(Level.FINEST, "discovered process resource {0}", resourcePath);

    InputStream inputStream = null;

    try {
      inputStream = source.openStream();
      byte[] bytes = IoUtil.readInputStream(inputStream, resourcePath);

      resourceMap.put(resourcePath, bytes);

    } catch (IOException e) {
      throw new ProcessEngineException("Could not open file for reading " + source + ". " + e.getMessage(), e);
    } finally {
      if (inputStream != null) {
        IoUtil.closeSilently(inputStream);
      }
    }
  }
 
Example 2
Source File: ResourceLoadingSecurityFilter.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
protected void loadFilterRules(FilterConfig filterConfig, String applicationPath) throws ServletException {
  String configFileName = filterConfig.getInitParameter("configFile");
  Resource resource = resourceLoader.getResource("classpath:" +webappProperty.getWebjarClasspath() + configFileName);
  InputStream configFileResource;
  try {
    configFileResource = resource.getInputStream();
  } catch (IOException e1) {
    throw new ServletException("Could not read security filter config file '" + configFileName + "': no such resource in servlet context.");
  }
  try {
    filterRules = FilterRules.load(configFileResource, applicationPath);
  } catch (Exception e) {
    throw new RuntimeException("Exception while parsing '" + configFileName + "'", e);
  } finally {
    IoUtil.closeSilently(configFileResource);
  }
}
 
Example 3
Source File: SpinObjectValueSerializer.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected byte[] serializeToByteArray(Object deserializedObject) throws Exception {
  DataFormatMapper mapper = dataFormat.getMapper();
  DataFormatWriter writer = dataFormat.getWriter();

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  OutputStreamWriter outWriter = new OutputStreamWriter(out, Context.getProcessEngineConfiguration().getDefaultCharset());
  BufferedWriter bufferedWriter = new BufferedWriter(outWriter);

  try {
    Object mappedObject = mapper.mapJavaToInternal(deserializedObject);
    writer.writeToWriter(bufferedWriter, mappedObject);
    return out.toByteArray();
  }
  finally {
    IoUtil.closeSilently(out);
    IoUtil.closeSilently(outWriter);
    IoUtil.closeSilently(bufferedWriter);
  }
}
 
Example 4
Source File: SpinObjectValueSerializer.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected Object deserializeFromByteArray(byte[] bytes, String objectTypeName) throws Exception {
  ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
  DataFormatMapper mapper = dataFormat.getMapper();
  DataFormatReader reader = dataFormat.getReader();

  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  InputStreamReader inReader = new InputStreamReader(bais, processEngineConfiguration.getDefaultCharset());
  BufferedReader bufferedReader = new BufferedReader(inReader);

  try {
    Object mappedObject = reader.readInput(bufferedReader);
    return mapper.mapInternalToJava(mappedObject, objectTypeName, getValidator(processEngineConfiguration));
  }
  finally{
    IoUtil.closeSilently(bais);
    IoUtil.closeSilently(inReader);
    IoUtil.closeSilently(bufferedReader);
  }
}
 
Example 5
Source File: SpinValueSerializer.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected byte[] serializeToByteArray(Object deserializedObject) throws Exception {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  OutputStreamWriter outWriter = new OutputStreamWriter(out, Context.getProcessEngineConfiguration().getDefaultCharset());
  BufferedWriter bufferedWriter = new BufferedWriter(outWriter);

  try {
    Spin<?> wrapper = (Spin<?>) deserializedObject;
    wrapper.writeToWriter(bufferedWriter);
    return out.toByteArray();
  }
  finally {
    IoUtil.closeSilently(out);
    IoUtil.closeSilently(outWriter);
    IoUtil.closeSilently(bufferedWriter);
  }
}
 
Example 6
Source File: SpinValueSerializer.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected Object deserializeFromByteArray(byte[] object, ValueFields valueFields) throws Exception {
  ByteArrayInputStream bais = new ByteArrayInputStream(object);
  InputStreamReader inReader = new InputStreamReader(bais, Context.getProcessEngineConfiguration().getDefaultCharset());
  BufferedReader bufferedReader = new BufferedReader(inReader);

  try {
    Object wrapper = dataFormat.getReader().readInput(bufferedReader);
    return dataFormat.createWrapperInstance(wrapper);
  }
  finally{
    IoUtil.closeSilently(bais);
    IoUtil.closeSilently(inReader);
    IoUtil.closeSilently(bufferedReader);
  }

}
 
Example 7
Source File: RepositoryServiceTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = { "org/camunda/bpm/engine/test/repository/drg.dmn" })
public void testGetDecisionRequirementsModel() throws Exception {
  DecisionRequirementsDefinitionQuery query = repositoryService.createDecisionRequirementsDefinitionQuery();

  DecisionRequirementsDefinition decisionRequirementsDefinition = query.singleResult();
  String decisionRequirementsDefinitionId = decisionRequirementsDefinition.getId();

  InputStream decisionRequirementsModel = repositoryService.getDecisionRequirementsModel(decisionRequirementsDefinitionId);

  assertNotNull(decisionRequirementsModel);

  byte[] readInputStream = IoUtil.readInputStream(decisionRequirementsModel, "decisionRequirementsModel");
  String model = new String(readInputStream, "UTF-8");

  assertTrue(model.contains("<definitions id=\"dish\" name=\"Dish\" namespace=\"test-drg\""));
  IoUtil.closeSilently(decisionRequirementsModel);
}
 
Example 8
Source File: ParseProcessesXmlStep.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected boolean isEmptyFile(URL url) {

    InputStream inputStream = null;

    try {
      inputStream = url.openStream();
      return inputStream.available() == 0;

    }
    catch (IOException e) {
      throw LOG.exceptionWhileReadingProcessesXml(url.toString(), e);
    }
    finally {
      IoUtil.closeSilently(inputStream);

    }
  }
 
Example 9
Source File: FileUtil.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public static void writeStringToFile(String value, String filePath, boolean deleteFile) {

    BufferedOutputStream outputStream = null;
    try {
      File file = new File(filePath);
      if (file.exists() && deleteFile) {
        file.delete();
      }

      outputStream = new BufferedOutputStream(new FileOutputStream(file, true));
      outputStream.write(value.getBytes());
      outputStream.flush();

    } catch (Exception e) {
      throw new PerfTestException("Could not write report to file", e);
    } finally {
      IoUtil.closeSilently(outputStream);
    }

  }
 
Example 10
Source File: ProcessApplicationDeploymentProcessor.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected Map<String, byte[]> getDeploymentResources(ProcessArchiveXml processArchive, DeploymentUnit deploymentUnit, VirtualFile processesXmlFile) {

    final Module module = deploymentUnit.getAttachment(MODULE);

    Map<String, byte[]> resources = new HashMap<String, byte[]>();

    // first, add all resources listed in the processe.xml
    List<String> process = processArchive.getProcessResourceNames();
    ModuleClassLoader classLoader = module.getClassLoader();

    for (String resource : process) {
      InputStream inputStream = null;
      try {
        inputStream = classLoader.getResourceAsStream(resource);
        resources.put(resource, IoUtil.readInputStream(inputStream, resource));
      } finally {
        IoUtil.closeSilently(inputStream);
      }
    }

    // scan for process definitions
    if(PropertyHelper.getBooleanProperty(processArchive.getProperties(), ProcessArchiveXml.PROP_IS_SCAN_FOR_PROCESS_DEFINITIONS, process.isEmpty())) {

      //always use VFS scanner on JBoss
      final VfsProcessApplicationScanner scanner = new VfsProcessApplicationScanner();

      String resourceRootPath = processArchive.getProperties().get(ProcessArchiveXml.PROP_RESOURCE_ROOT_PATH);
      String[] additionalResourceSuffixes = StringUtil.split(processArchive.getProperties().get(ProcessArchiveXml.PROP_ADDITIONAL_RESOURCE_SUFFIXES), ProcessArchiveXml.PROP_ADDITIONAL_RESOURCE_SUFFIXES_SEPARATOR);
      URL processesXmlUrl = vfsFileAsUrl(processesXmlFile);
      resources.putAll(scanner.findResources(classLoader, resourceRootPath, processesXmlUrl, additionalResourceSuffixes));
    }

    return resources;
  }
 
Example 11
Source File: ProcessApplicationDeploymentProcessor.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected Map<String, byte[]> getDeploymentResources(ProcessArchiveXml processArchive, DeploymentUnit deploymentUnit, VirtualFile processesXmlFile) {

    final Module module = deploymentUnit.getAttachment(MODULE);

    Map<String, byte[]> resources = new HashMap<String, byte[]>();

    // first, add all resources listed in the processe.xml
    List<String> process = processArchive.getProcessResourceNames();
    ModuleClassLoader classLoader = module.getClassLoader();

    for (String resource : process) {
      InputStream inputStream = null;
      try {
        inputStream = classLoader.getResourceAsStream(resource);
        resources.put(resource, IoUtil.readInputStream(inputStream, resource));
      } finally {
        IoUtil.closeSilently(inputStream);
      }
    }

    // scan for process definitions
    if(PropertyHelper.getBooleanProperty(processArchive.getProperties(), ProcessArchiveXml.PROP_IS_SCAN_FOR_PROCESS_DEFINITIONS, process.isEmpty())) {

      //always use VFS scanner on JBoss
      final VfsProcessApplicationScanner scanner = new VfsProcessApplicationScanner();

      String resourceRootPath = processArchive.getProperties().get(ProcessArchiveXml.PROP_RESOURCE_ROOT_PATH);
      String[] additionalResourceSuffixes = StringUtil.split(processArchive.getProperties().get(ProcessArchiveXml.PROP_ADDITIONAL_RESOURCE_SUFFIXES), ProcessArchiveXml.PROP_ADDITIONAL_RESOURCE_SUFFIXES_SEPARATOR);
      URL processesXmlUrl = vfsFileAsUrl(processesXmlFile);
      resources.putAll(scanner.findResources(classLoader, resourceRootPath, processesXmlUrl, additionalResourceSuffixes));
    }

    return resources;
  }
 
Example 12
Source File: VfsProcessApplicationScanner.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void addResource(VirtualFile virtualFile, VirtualFile processArchiveRoot, Map<String, byte[]> resources) {
  String resourceName = virtualFile.getPathNameRelativeTo(processArchiveRoot);
  try {
    InputStream inputStream = virtualFile.openStream();
    byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
    IoUtil.closeSilently(inputStream);
    resources.put(resourceName, bytes);
  }
  catch (IOException e) {
    LOG.cannotReadInputStreamForFile(resourceName, processArchiveRoot, e);
  }
}
 
Example 13
Source File: PerfTestProcessEngine.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public static Properties loadProperties() {
  InputStream propertyInputStream = null;
  try {
    propertyInputStream = PerfTestProcessEngine.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME);
    Properties properties = new Properties();
    properties.load(propertyInputStream);
    return properties;

  } catch(Exception e) {
    throw new PerfTestException("Cannot load properties from file "+PROPERTIES_FILE_NAME+": "+e);

  } finally {
    IoUtil.closeSilently(propertyInputStream);
  }
}
 
Example 14
Source File: TestHelper.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public static void assertDiagramIsDeployed(boolean deployed, Class<?> clazz, String expectedDiagramResource, String processDefinitionKey) throws IOException {
  ProcessEngine processEngine = ProgrammaticBeanLookup.lookup(ProcessEngine.class);
  Assert.assertNotNull(processEngine);
  RepositoryService repositoryService = processEngine.getRepositoryService();
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
    .processDefinitionKey(processDefinitionKey)
    .singleResult();
  assertNotNull(processDefinition);

  InputStream actualStream = null;
  InputStream expectedStream = null;
  try {
    actualStream = repositoryService.getProcessDiagram(processDefinition.getId());

    if (deployed) {
      byte[] actualDiagram = IoUtil.readInputStream(actualStream, "actualStream");
      assertNotNull(actualDiagram);
      assertTrue(actualDiagram.length > 0);

      expectedStream = clazz.getResourceAsStream(expectedDiagramResource);
      byte[] expectedDiagram = IoUtil.readInputStream(expectedStream, "expectedSteam");
      assertNotNull(expectedDiagram);

      assertTrue(isEqual(expectedStream, actualStream));
    } else {
      assertNull(actualStream);
    }
  } finally {
    IoUtil.closeSilently(actualStream);
    IoUtil.closeSilently(expectedStream);
  }
}
 
Example 15
Source File: JavaObjectSerializer.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected Object deserializeFromByteArray(byte[] bytes, String objectTypeName) throws Exception {
  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  ObjectInputStream ois = null;
  try {
    ois = new ClassloaderAwareObjectInputStream(bais);
    return ois.readObject();
  }
  finally {
    IoUtil.closeSilently(ois);
    IoUtil.closeSilently(bais);
  }
}
 
Example 16
Source File: LdapTestEnvironment.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected Properties loadTestProperties() throws IOException {
  Properties properties = new Properties();
  File file = IoUtil.getFile(configFilePath);
  FileInputStream propertiesStream = null;
  try {
    propertiesStream = new FileInputStream(file);
    properties.load(propertiesStream);
  } finally {
    IoUtil.closeSilently(propertiesStream);
  }
  return properties;
}
 
Example 17
Source File: ProcessesXmlProcessor.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
protected boolean isEmptyFile(URL url) {

    InputStream inputStream = null;

    try {
      inputStream = url.openStream();
      return inputStream.available() == 0;

    } catch (IOException e) {
      throw new ProcessEngineException("Could not open stream for " + url, e);

    } finally {
      IoUtil.closeSilently(inputStream);

    }
  }
 
Example 18
Source File: RepositoryServiceTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Deployment(resources = { "org/camunda/bpm/engine/test/repository/one.cmmn" })
public void testGetCaseModel() throws Exception {
  CaseDefinitionQuery query = repositoryService.createCaseDefinitionQuery();

  CaseDefinition caseDefinition = query.singleResult();
  String caseDefinitionId = caseDefinition.getId();

  InputStream caseModel = repositoryService.getCaseModel(caseDefinitionId);

  assertNotNull(caseModel);

  byte[] readInputStream = IoUtil.readInputStream(caseModel, "caseModel");
  String model = new String(readInputStream, "UTF-8");

  assertTrue(model.contains("<case id=\"one\" name=\"One\">"));

  IoUtil.closeSilently(caseModel);
}
 
Example 19
Source File: DeployProcessArchiveStep.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void performOperationStep(DeploymentOperation operationContext) {

  final PlatformServiceContainer serviceContainer = operationContext.getServiceContainer();
  final AbstractProcessApplication processApplication = operationContext.getAttachment(Attachments.PROCESS_APPLICATION);
  final ClassLoader processApplicationClassloader = processApplication.getProcessApplicationClassloader();

  ProcessEngine processEngine = getProcessEngine(serviceContainer, processApplication.getDefaultDeployToEngineName());

  // start building deployment map
  Map<String, byte[]> deploymentMap = new HashMap<String, byte[]>();

  // add all processes listed in the processes.xml
  List<String> listedProcessResources = processArchive.getProcessResourceNames();
  for (String processResource : listedProcessResources) {
    InputStream resourceAsStream = null;
    try {
      resourceAsStream = processApplicationClassloader.getResourceAsStream(processResource);
      byte[] bytes = IoUtil.readInputStream(resourceAsStream, processResource);
      deploymentMap.put(processResource, bytes);
    } finally {
      IoUtil.closeSilently(resourceAsStream);
    }
  }

  // scan for additional process definitions if not turned off
  if(PropertyHelper.getBooleanProperty(processArchive.getProperties(), ProcessArchiveXml.PROP_IS_SCAN_FOR_PROCESS_DEFINITIONS, true)) {
    String paResourceRoot = processArchive.getProperties().get(ProcessArchiveXml.PROP_RESOURCE_ROOT_PATH);
    String[] additionalResourceSuffixes = StringUtil.split(processArchive.getProperties().get(ProcessArchiveXml.PROP_ADDITIONAL_RESOURCE_SUFFIXES), ProcessArchiveXml.PROP_ADDITIONAL_RESOURCE_SUFFIXES_SEPARATOR);
    deploymentMap.putAll(findResources(processApplicationClassloader, paResourceRoot, additionalResourceSuffixes));
  }

  // perform process engine deployment
  RepositoryService repositoryService = processEngine.getRepositoryService();
  ProcessApplicationDeploymentBuilder deploymentBuilder = repositoryService.createDeployment(processApplication.getReference());

  // set the name for the deployment
  String deploymentName = processArchive.getName();
  if(deploymentName == null || deploymentName.isEmpty()) {
    deploymentName = processApplication.getName();
  }
  deploymentBuilder.name(deploymentName);

  // set the tenant id for the deployment
  String tenantId = processArchive.getTenantId();
  if(tenantId != null && !tenantId.isEmpty()) {
    deploymentBuilder.tenantId(tenantId);
  }

  // enable duplicate filtering
  deploymentBuilder.enableDuplicateFiltering(PropertyHelper.getBooleanProperty(processArchive.getProperties(), ProcessArchiveXml.PROP_IS_DEPLOY_CHANGED_ONLY, false));

  if (PropertyHelper.getBooleanProperty(processArchive.getProperties(), ProcessArchiveXml.PROP_IS_RESUME_PREVIOUS_VERSIONS, true)) {
    enableResumingOfPreviousVersions(deploymentBuilder);
  }

  // add all resources obtained through the processes.xml and through scanning
  for (Entry<String, byte[]> deploymentResource : deploymentMap.entrySet()) {
    deploymentBuilder.addInputStream(deploymentResource.getKey(), new ByteArrayInputStream(deploymentResource.getValue()));
  }

  // allow the process application to add additional resources to the deployment
  processApplication.createDeployment(processArchive.getName(), deploymentBuilder);

  Collection<String> deploymentResourceNames = deploymentBuilder.getResourceNames();
  if(!deploymentResourceNames.isEmpty()) {

    LOG.deploymentSummary(deploymentResourceNames, deploymentName);

    // perform the process engine deployment
    deployment = deploymentBuilder.deploy();

    // add attachment
    Map<String, DeployedProcessArchive> processArchiveDeploymentMap = operationContext.getAttachment(Attachments.PROCESS_ARCHIVE_DEPLOYMENT_MAP);
    if(processArchiveDeploymentMap == null) {
      processArchiveDeploymentMap = new HashMap<String, DeployedProcessArchive>();
      operationContext.addAttachment(Attachments.PROCESS_ARCHIVE_DEPLOYMENT_MAP, processArchiveDeploymentMap);
    }
    processArchiveDeploymentMap.put(processArchive.getName(), new DeployedProcessArchive(deployment));

  }
  else {
    LOG.notCreatingPaDeployment(processApplication.getName());
  }
}
 
Example 20
Source File: ProcessesXmlProcessor.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
protected boolean isEmptyFile(URL url) {

    InputStream inputStream = null;

    try {
      inputStream = url.openStream();
      return inputStream.available() == 0;

    } catch (IOException e) {
      throw new ProcessEngineException("Could not open stream for " + url, e);

    } finally {
      IoUtil.closeSilently(inputStream);

    }
  }