How to monitor your Oracle DB (Part 1)
Sometimes, you are developing some application that connects to Oracle database and you need to monitor the database to know something about processes, open sessions/cursors and so on, so is necessary to know a little bit about Oracle administration.
Basically, Oracle provides us with a set of fixed tables that allow us to check database settings, processes, parameters, open sessions and so on that gives us important information. Some of these tables are the next:
V$SESSION /*Allows us to see open sessions and details about them*/ V$DATABASE /*Show us database information*/ V$FIXED_TABLE /*IMPORTANT because this shows a list of all fixed tables, learn that and you can get access all*/ V$LOCKED_OBJECT /*Shows us locked objects on the database*/ V$SQLAREA /*Show sql queries executed related to v$session*/
You can check a list of those views in the next link:
http://www.adp-gmbh.ch/ora/misc/dynamic_performance_views.html
Now, how to use that information to do different stuff on the database?
How to use ModelDriven in Struts 2

Talking about Struts 2 again
. I wanna talk (or write) about this because is very interesting, for me it remembers me the way of work on Struts 1. For those that doesn't know what am i talking about, i'm going to explain you. In Struts 1, the way of work with Action and form properties is a little bit different of default Struts 2 way. In struts 1 we have the actions that doesn't contain any form property or getter/setter method, like this:
import....
public class LoginAction extends LookupDispatchAction {
public ActionForward logMeIn(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping,
ActionForm form) {
LoginForm loginForm = (LoginForm) form;
if (loginForm.getUsername().equals("admin") &&
loginForm.getPassword().equals("admin")) {
return mapping.findForward("enter");
} else {
return mapping.findForward("fail");
}
}
}
And, in other class, the Action Form where we can find the properties with respective getter/setter methods and optionally, struts 1 server side validation:
But, in Struts 2 it changes a little bit because properties, getter/setter methods and validation are in the action. For the previous example, it's version on Struts 2 will look like this:
public class LoginAction {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public String logMeIn() {
if (getUsername().equals("admin") &&
getPassword().equals("admin")) {
return "enter";
} else {
return "fail";
}
}
Free from extends and strange return types in methods but more charged with properties and getter/setter methods, for some people it's better, for another people it sucks. In my case i like the fact that i don´t need to extend my actions from some classes (altought you have to do that if you want to inject additional funcionality to your action like validation, prepare scripts and so on) and a simpler way to declare action methods, but i dislike the fact that all is inside the action.
But don't worry people, Struts 1 developer that migrated to Struts 2 can still use the same model Action - Action Form like Struts 1 in struts 2
, how to do that?
How to validate through annotations: Struts 2

Hi to all. I've studied some stuff about struts 2 those days so i've learn a lot of new things like this post topic. Normally, what do you do when you want to implement a basic validation on Struts 2? The steps about how to do that are:
- Assure that you're using defaultStack interceptor stack because inside it are workflow and validation interceptors that are responsible of execute validation logic and redirect to appropiate result when validation triggers.
- Define a result named "input" in action's declaration on struts.xml . This is the result where validation must redirect automatically if exists validation errors. If input result is not defined it will give an error of result not found.
- Override validate() method on your action.
- Write your own validation logic inside it, that must add error messages when it fails to properly validation mechanism. For example, if i validate that a field must be a number and greater than 1000, if it doesn't pass the validation it must add a error message using addActionError() or addFieldError() (To follow standards you should use addFieldError() ), like this:
if (fieldOne == null || fieldOne.equals("")) { addFieldError("fieldOne", "Field one must not be empty."); } if (fieldTwo == null || Integer.valueOf(fieldTwo) < 30) { addFieldError("fieldTwo", "Field two must be greater or equal than 30."); } - (Optional) in your jsp you can define <s:fielderror> or <s:actionerror> tags to print your validation messages. As you can guess, <s:fielderror> prints validation errors added with addFieldError() and <s:actionerror> to print action error messages added with addActionError(). To print validation errors from fieldOne and fieldTwo you have to define <s:fielderror> like this:
<s:fielderror cssClass="cssClassForUllistwithLi" > <s:param>fieldOne</s:param> <s:param>fieldTwo</s:param> </s:fielderror>
- Run your app
This is the basic way. But if you want to validate through annotations is easier and faster, you have to do this:
How to avoid validation on Action call – Struts 2
As you know, like Struts 1, Struts 2 allow us to define server side validations when you call an action. This is very useful because sometimes javascript client side validation is not enought when we want to validate input entries against data from DB or do complex validations.
How to define validations in JAVA Struts 2? First your action must extend ActionSupport class, and override it's method validate that has te following signature:
public void validate() {}
Inside validate() you define your validations and, if you add messages to ActionErrors or FieldErrors, Struts 2 automatically detects that it must redirect to input result defined in action's declaration in struts.xml, like this:
<action name="login"> <interceptor-ref name="defaultStack" /> <result name="homePage">/login/login.jsp</result> <result name="input">/login/login.jsp</result> ... <result name="backToLoginPer">/login/login.jsp</result> </action>
The problem is that sometimes you want that validation applies only on certain action calls, that is, avoid validation on certain action calls. How to do this?
How to solve “cannot find javac” with JAVA SDK App Engine

Recently i decided to try Google App Engine, a service where you can host your applications built in different languages like JAVA, Ruby, Python and so on, using Google's infraestructure. Now is enough for me because i see that you can get this for free:
- 500MB storage
- Enough CPU and bandwidth to serve your applications to 5 million visits per month
So i want to test what kind of JAVA technologies it supports (I don't know, Struts2, Spring, Hibernate JPA) . The proccess to join this is very simple, the steps are the next:
How to save JAVA Exception printStackTrace on String
Probably you've used sometines the Exception object in try/catch block to print the exception stack trace that help us to find where the line of code where the exception is thrown, but more than once i wanted to get that stack trace in some String variable for example, maybe to save in some auditory table or something like that. I read a little bit about the method getStackTrace but really it doesn't give me the full stack trace.
Well, to do that, first you have to understand that when you call ex.printStackTrace() you print a characters stream on screen, so the way to get that characters stream is using the overloaded version of printStackTrace(PrintWriter objWriter) that receives a PrintWriter object to transfer that characters stream to the PrintWriter object, then you can use that writer to "write" it's stream on something like a file or a simple String.
Check this sample code:
import java.io.File;
import java.io.IOException;
class MyClass {
public static void main(String[] arguments)
{
try
{
(new MyClass()).readInexistentFile();
}
catch(IOException ex)
{
//Catch the stack trace?
}
}
public void readInexistentFile() throws IOException
{
File myFile = new File("hi6.txt");
FileReader myReader = new FileReader(myFile);
myReader.read();
}
}
...
How to sort JAVA collection elements (part 2)

Continuing with "How to sort JAVA collection elements", i want to share with you another way to sort collections that have it's differences to the first way (using Comparable interface) and enables you to have different sort options, i'm talking about use the Comparator interface.
class Employee implements Comparable<Employee> {
private String completeName;
private int age;
private boolean sex;
private String position;
public Employee(String completeName, int age, boolean sex, String position) {
setCompleteName(completeName);
setAge(age);
setSex(sex);
setPosition(position);
}
@Override
public String toString()
{
return getCompleteName() + ", " + getAge() + " years, " +
(isSex()?"male, ":"female, ") + getPosition();
}
public String getCompleteName() {
return completeName;
}
public void setCompleteName(String completeName) {
this.completeName = completeName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int compareTo(Employee o) {
return getCompleteName().compareTo(o.getCompleteName());
}
}
Wait... Comparable and Comparator? Yes, you read well. The comparator interface is slightly similar to comparable, because it defines a method called compare() that works like compareTo() of Comparable interface, but it has the difference that you don't need to implement Comparator in the collection's class type. To explain that a little bit well, i'm going to resume the example of the last post. I have my Employee class implementing Comparable interface and it's method compareTo() that orders in ascending order based on completeName field (if you haven't read the first post you can download the sample code >>HERE<<).
Now, additional to it, i wan to have the option of order an Employee list based on age field descending or position ascending. How to do that in JAVA?? Continue reading
How to sort JAVA collection elements

Probably you've worked with List in a moment of your JAVA developer life, and we can say that using a List instead an array is more beautiful because the list grows automatically, you can add elements easily (only objects, but if you want to add primitives use wrapper classes) and so on. But, How to sort the elements of a list?
Suppose that you have a list of employees. Those employees have the next data:
complete name age sex position
So, you have your JAVA bean Employeee, like this: (Continue reading the post)
Dynamic mime type on stream result – Struts 2
Hi to all. A reader (Staci) asks about how to dinamically define the mime type of stream result type on Struts 2, for example define that the stream result will serve a pdf file or excel file. Well, as i see, that can bea achieved in 2 ways:
- Defining a stream result for each mime type that you want to return in the struts.xml
- Using wildcards to define the mime type and file extension in struts.xml
- Using parameters on struts.xml
Both options are viable, but the first extendes more and more the more kind of mime types that you want to support, because you have to define a result for each mime type and then send a parameter or something like that to define the result that you want to get.
But i'm going to write about the second and third options. First: Using wildcards. FIrst, imagine that you have a simple html to call the action method that gives us a file:
<html> <head> <title>Name Collector</title> </head> <body> <a href="/demoStruts2/pdf_pdf_WildCardRaw!giveMeMyFile.action">Get PDF file</a><br /> <a href="/demoStruts2/vnd.ms-excel_xls_WildCardRaw!giveMeMyFile.action">Get XLS file</a> </body> </html>
How to develop and implement native methods in JAVA
This post is a basic tutorial to learn how to write and implement native methods in a simple JAVA application.
But first i want to talk about the context of native methods. Sometimes JAVA applications need to use native code written in other languages, such C and C++, but how can we, literally speaking, call some C/C++ application, script or wathever from JAVA code?
Here is when JNI comes in. JNI is the Java Native Interface, an API that conforms JVM and acts as it name says, an interface between JAVA applications and native code written in C/C++. From JAVA to C/C++ you access them through a method call. To do this, in JAVA side you declare your methods in the next way:
public native void callCmethod();
Looks like a interface method definition, and it has sense because in JAVA the native method is only the definition of the method because the implementation is in native language like C or C++. To explain this a little bit clear, we can see the next diagram:

(A native method is defined in JAVA application, when them method is called, the JVM detects that the method is native so JNI takes action, looking for the method implementation in a associated C/C++ application where the method is implemented and a header file that acts as the interface between JAVA native method and C implemented method).
Let's start.







