How to create and use a custom property files in a Process Services

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create and use a custom property files in a Process Services

fmalagrino
Occasional Visitor
1 0 5,081

If you need to create a custom property files and you want to use this custom property file in  Process Services you can do it using a Java Delegate or a Spring bean.

In my case, I used a Java Delegate.

First of all, create your custom property file and upload it to the following folder:

/alfresco/process-services-1.6.0/tomcat/webapps/activiti-app/WEB-INF/classes/

Now it is time to create a java delegate: But first of all it is important to understand what is the command to load the file properties:

It is just one line of code and it's the following line:

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("generic.properties");

With this code of line, you tell the java delegate which property file to load.

After you loaded it you can start to save your properties inside the property file and they will be used in Process Services.

String host = "";

String username = "";

String password = "";

Properties properties = new Properties();

properties.load(inputStream);

host = properties.getProperty("generic.host");

username   = properties.getProperty("generic.username");

password = properties.getProperty("generic.password");

execution.setVariable("host", host);

execution.setVariable("username", username);

execution.setVariable("password", password);

Let's explain a bit the code

So first as the best practice, we create some string and we set the value to empty. Otherwise you can have a null pointer exception.

After that, we load inside the Properties object our custom file property

Then we start to set our string with the value of the property that is set inside our file property.

In our example, generic.host will have some value inside the property file.

After we have assigned our string with all the value that we need from the property file we can set them as a variable 

execution.setVariable("host", host);

So this piece of code means that you have inside your process a variable called host and you are assigning this variable to the string value of the host entry inside the property file (generic.host)

After you finish the code it is time to export it as a jar and add the jar inside the following path:

/alfresco/process-services-1.6.0/tomcat/webapps/activiti-app/WEB-INF/lib/

To apply your new java delegate you need to create a service task and apply under the class field  the name of your class. Below are few screenshots how to do this:

1. A very simple example workflow

the process

2. Configure the global variable:

3. The variables set in step 2.

4. Setup the java delegate (adding the class name to the "class" field:

I created 2 forms for testing that my Java delegate is loaded correctly.

If the host variable will be empty the process should go one way and if it is correct it should go to the form that will display text with all our information of the file properties 

How to configure this:

1. Create the condition on one arrow :

Condition on arrow

2. Enter the condition (host: not empty) that will allow to the form success:

3. And the second condition:

If the host is not empty the process should go to the human task with the following form:

The reference form here is "success" and this is how it looks like:

Otherwise, if there is some problem from the java delegate it will go to the other form called "wrong":

For testing you can use the following snippets:

Java code:

package my.Test;

import java.io.InputStream;
import java.util.Properties;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;


public class GenericCustomProperties implements JavaDelegate {
String host = "";
String username = "";
String password = "";

   



    public void execute(DelegateExecution execution) throws Exception {
         InputStream inputStream = this.getClass().getClassLoader()
         .getResourceAsStream("generic.properties");
          Properties properties = new Properties();
          properties.load(inputStream);
          host = properties.getProperty("generic.host");
          username   = properties.getProperty("generic.username");
          password = properties.getProperty("generic.password");
          execution.setVariable("host", host);
          execution.setVariable("username", username);
          execution.setVariable("password", password);

         

    }


}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Please be sure to create a java project with a package called my.Test create containing a class called

GenericCustomProperties.java

Now that you created this java class export it as jar and upload at the following path: /alfresco/process-services/tomcat/webapps/activiti-app/WEB-INF/lib/

As custom properties, you can create a file called generic.properties, open it and write the following snippet:

generic.host=127.0.0.1:8080
generic.username=username
generic.password=password‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

The file needs be located inside the following path:

/alfresco/process-services-1.6.0/tomcat/webapps/activiti-app/WEB-INF/classes/