Pages

Saturday, May 26, 2012

Clustering with Glassfish 3.1

                  

 

 Here I use the command line to create a cluster in the application server Glassfish 3.1 and add 2 instances to it.
Once the cluster is created I deploy an enterprise application and run it in both instances.

Tuesday, May 1, 2012

File realm & role based security with Glassfish 3.1






Here in this video you can see how i create a simple security realm that uses a file in a safe place in the app server to store the credentials. One thing i forgot was to show you how to logout :)
Don't worry is not very difficult, for that you can implement your own logout servlet to invalidate the session.
Here is some example of code for logging out:


1:  @WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"})  
2:   public class LogoutServlet extends HttpServlet {  
3:   @Override  
4:   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
5:    //Invalidates the session  
6:    request.getSession(false).invalidate();  
7:    // Redirects back to the initial page.  
8:    response.sendRedirect(request.getContextPath());  
9:   }  
10:   }  

Also here there is an image to show where can you enable HTTPS for the admin panel:


Note: The SSL certificate that comes with glassfish is expired that is the reason why you will see a warning message before you login.

Saturday, April 7, 2012

Using a fun chalk board to explain how a JEE6 application that uses web services should be organized

I have been studying JAX-RS the last 2 months and i found it very interesting. I wanted to create an interesting video where i explain one of the ways how the .ear application can be organized when using web services. I didn't know that Entities can behave also as JAXB objects. That make things a lot easier.
I think you will like this video, is interesting. It was to big to upload it at once so i divided.
This is the second part of the video. Here i am just showing the code, i am not programming because otherwise the recording would be too long.

I would like to know about what do you think of this architecture? What other alternatives do exist? What are the best practices? Please feel free to comment, i am very interested in reading your comments.

Wednesday, April 4, 2012

The use of marker interfaces

When programming, we often use interfaces, to hide the realization. But in some occasions, we want just want to group classes. We call marker interfaces, to empty interfaces(No methods at all). 

A very simple example is the Serializabe interface: 
It has no methods at all, but all classes that whant to be writen into some kind of file, or network stream, need to implement Serializable. 

In the same way, you could make a Program with classes like Dog,Bird,Cow... and you dont need to add them any functionality extra, just to mark them, because somewhere in the code there will be a method that could look like this : String method(Flyer); So you need to create a marker interface called Flyer, to mark the animals that can fly(So you dont need to have unnecesary variables boolean like isFlyer, in classes Dog and Cow). 




maybe i should give a more pragmatical example, so it is easier to understand. Lets try:

See the folowing code:

1:  //Marking interface flyer  
2:  public interface Flyer {  
3:  //If this is a marker interface, there is no methods in it  
4:  }  
5:  public class Mammal {  
6:  private String someVariable;  
7:  }  
8:  public class Cow extends Mammal {  
9:  //Some code specific to cows  
10:  }  
11:  public class Bat extends Mammal implements Flyer {  
12:  //Some code specific to Mammal  
13:  //Note that the Bat is marked as a Flyer  
14:  }  



I think this example more or less explains my point. Bats are mammals and can fly,so why would you have a boolean variable that can be either true or false, when you can just mark the class. Another reason why i think this is a good approach, i because if the future we need to upgrade the program, we can just mark the classes that can fly with the interface.

Imagine somewhere in the program there is a method like this:

1:  public class AirportControl {  
2:  public void testFly(Animal a) {  
3:  if(a instanceof Flyer) {  
4:  //Perform a test fly  
5:  }  
6:  public void testFly(Vehicle v) {  
7:  if(a instanceof Flyer) {  
8:  //Perform a test fly  
9:  }  
10:  }  


I think this approach is pretty flexible,when creating your domain model.

Monday, April 2, 2012

What is JAXP and what tools does it provide?


I had this video in my hard disk where i explain what XML parsers are and what java offer us by default to perform XML parsing.
I thought it may be handy if someone wants to get a brief  theoretical introduction to the world of  XML parsing using Java. There are lots of other tools out there for doing parsing with java, the video just mentions the ones that are included in the SDK.
If you want to know more, check out the resources:


Monday, March 12, 2012

How to increase performance on overridden methods.


All annotations resolve themselves at compile time, java features like inheritance and polimorfism resolve at runtime on client request.
So this means that things that are already resolved at compile time have a faster performance when the application is running latter.
A little trick to increase performance of overriden methods is to anotate them with the annotation @Override.

Friday, October 28, 2011

Configuring your eclipse JEE6 project to use JPA (Step by Step guide)


Driver definition
The driver is vendor specific and it will allow the application to connect to the database.
To create a new driver definition for your app that follow this steps in eclipse:
         1- Select Window > Preferences from the main menu bar.
        2- Expand Data Management > Connectivity and select Driver Definitions.
        3- Click Add.                 
       4-In the Name/Type tab, indicate the version of Derby on which you want to create the driver definition.

    1. Select Derby Client JDBC Driver for the non embedded version
    2. Select Derby Embedded JDBC Driver for the embedded version
    3.  (Optional) Modify the Driver Name if a driver definition with this name already exists.  
      Location of the driver in windows systems is often: C:\jeeAplicationServer\glassfishv3/javadb/lib/derbyclient.jar
      5-In the JAR List tab, highlight the default derby.jar file and click Edit JAR/Zip to indicate the fully qualified  path of the Derby JAR file.

    1. Browse to the location of the ZIP file.
    2. Click Open.
6- (Optional) In the Properties tab, modify the default properties for the driver definition template.
Options in the Properties tab vary based on database server type. You can edit or enter a property in the Value box.

7- Click Ok

NOTE: THE driver.jar is for emdebed database while driverclient.jar is for normal database(driverclient should be used)

Start the derby database from the console
To be able to work with the derby database, you need first to start it. To do so, follow this steps:
1-Open the command line and place yourself at the glassfish server bin folder
2-And then enter the command: asadmin start-database

Create a new Connection Profile

Lets say that the connection profile is the configuration that will allow you interact with the database.

1-In the Data Source Explorer tab. Right click on Database Connections and select New…
2- Select the Derby profile
3- In the name field type the name of the database(Type the same as you typed in the Properties tab when you reated the driver definition)
5- Click next
6- In the Connection Details page, pick the driver you configured before and type the correct database. Select the save password option
7- Click on Test Connection to see all is correct. (You will receive a Ping Succeeded! message if all is ok)
8-Click Finish


Create a Connection Pool
In a nutshell, a connection pool allows the database recycle the already used connections. So here is explained how to create a connection pool for the previously created connection profile.

1- Once the glassfish server is started, go to http://localhost:4848
2- At the admin panel select connection Pools and click on New…
3- In general settings; give a name to the pool, The resource type is javax.sql.DataSource and the Database Vendor is Derby and click next
4-In the step 2 of 2. Let all by default, but just edit the setting below to look like this:



5- Click on finish

Adding JPA facelet and Referencing the JDBC resource from the application
At this point GlassFish knows about a database, but we still don’t reference it from the application. Let’s do that now. So here what we do is tell our application to use all the configurations that we configured before.

1- Enable JPA in the application by including the JPA facet:
Right click on Project->Properties->And search for project facets and select JPA
2- In the persistence.xml file you should add the name of the database(datasource) as says in the image:

Create entities
Now lets see how it works all we configured. Try creating an entity with a couple of fields and annotate it with the JPA annotations.

Remember that every entity must have an @Id attribute, if it doesn’t exist you should see something like this:
Create tables from entities
How to transform our entities into tables(Object Relational Mapping) follow this steps:

1- Right click on the project and select  Generate Tables from Entities
2- If this message appears in the screen click in Add a connection to JPA project…

3- The last missing piece is to add the connection name so all JPA features can work

4- Back at the schema selection dialog, you can now choose the schema that will be used for storing the tables.
By default USER schema will be used, so click cancel.
5-If a red error that says that the schema ‘USER’ cannot be found appears, just restart(build) the application.




Share with your friends