Dirigible enables dynamic in-memory Java runtime
- We start by creating a new project named gamification. If you have troubles creating a new project, please
refer to the previous posts:
NOTE: Since we’ll create a very simple application, let’s differ from the already known vertical application development scenario. Instead of first creating a domain model and then exposing it through RESTful services, we’ll jump one step ahead and create a Java service.
- From the main menu, select New -> Scripting Service -> Java Service.
- Name it EmployeeScore. This will be the main entity.
- Replace the generated file content with the following code:
private String name;
private int score;
this.name = name;
this.score = score;
}
public int compareTo(EmployeeScore o) {
// Descending order
int result = (int) (o.score – this.score);
if (result == 0) {
result = o.name.compareTo(this.name);
}
return result;
}
public String toString() {
return name + ” – ” + score + ” point(s)”;
}
}
- Create another Java service.
- Name it Scores. This will be a helper class that will provide you with sample data.
- Replace the generated file content with the following code:
import java.util.List;
import java.util.Random;
private static final int MAX_SCORE = 100;
private static final Random RANDOM = new Random();
List<EmployeeScore> result = new ArrayList<EmployeeScore>();
for (String name : employees) {
result.add(new EmployeeScore(name, getRandomScore()));
}
return result;
}
return RANDOM.nextInt(MAX_SCORE);
}
}
- Generate one more Java service.
- Name it just Service. This will be the service to process the incoming requests.
- Again, replace the generated file content with the following code:
import java.util.regex.Matcher;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sap.dirigible.runtime.mail.MailSender;
private static final String EMAIL_PATTERN = “^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$“;
private static final Pattern pattern = Pattern.compile(EMAIL_PATTERN);
private static final String FROM = “dirigible@sap.com“;
private static final String TITLE = “Gamification Scores”;
List<EmployeeScore> scores = Scores.calculate(getEmployees());
Collections.sort(scores);
String body = getBody(scores);
String sendTo = request.getParameter(“sendTo”);
MailSender mailSender = (MailSender) scope.get(“mail”);
mailSender.sendMail(FROM, sendTo, TITLE, body);
response.getWriter().println(“Email sent to \”” + sendTo + “\””);
} else {
response.getWriter().println(body);
}
}
return Arrays.asList(new String[] { “Yordan”, “Nedelcho”, “Martin”, “Peter”, “John” });
}
StringBuilder result = new StringBuilder();
for(EmployeeScore employeeScore : employeeScores) {
result.append(employeeScore.toString() + “\n”);
}
return result.toString();
}
boolean isValid = false;
if(email != null) {
Matcher matcher = pattern.matcher(email);
isValid = matcher.matches();
}
return isValid;
}
-
Good! We are ready with this simple application. Let’s now test it!
-
To display the scores: open the Preview tab, and from the Workspace Explorer, click on Service.
-
To send the results to your e-mail: in the URL bar, append “?sendTo=<your_e-mail>“
The project site: http://www.dirigible.io
The source code is available at GitHub – http://github.com/SAP/cloud-dirigible
Forum: http://forum.dirigible.io
Twitter: https://twitter.com/dirigible_io
Youtube: https://www.youtube.com/channel/UCYnsiVQ0M9iQLqP5DXCLMBA/
Help: http://help.dirigible.io
Samples: http://samples.dirigible.io
Google Group: https://plus.google.com/111171361109860650442/posts
Blog: http://dirigible-logbook.blogspot.com/
Dirigible on SAP HANA Cloud Platform
Dirigible – Extensions vs Configurations
Dirigible is the fast track to your HCP HANA DB instance
Well done!
Really nice! 🙂
The Dirigible project seem to not loosing any steam since it was open to the community!
When I use Java I know that turnarounds (code -> build -> package -> deploy -> start) occupy almost half of my time and this being said - from productivity point of view this feature is amazing (ops, I mean AMAZING!!!).
I can't wait for modularity that you plan - so I can easily prepare a pack that enables in system java development for my developer account using Dirigible cloud IDE.
I became more and more engaged in this project and really enjoy the spirit there!