Thursday, December 27, 2012

What's for Java in 2013?

Hi all. I just wanted to share some information about what's for the Java technology in 2013, this is from my point of view and from the conferences I've attended this year, but as the Oracle safe harbor statement says: "... While these forward-looking statements represent our current judgment on what the future holds, they are subject to risks and uncertainties that could cause actual results to differ materially ..."

  • A new Java User Group will be born in Cali, Colombia. So if you are around stay tuned. This is my contribution to the community, hopefully we'll get many active members and lots of hackathons, meetings and information sharing.
  • Java SE 6 End-Of-Life (EOL) is estimated for February 2013. It means that you won't get any other update for your Java SE 6 (unless you buy the Java SE Support), but it doesn't mean that Java SE 6 will disappear, you can still download this version in the Java Archive, but you are encouraged to update to Java SE 7, which has been around since July 2011.
  • Oracle will be promoting the Oracle Java Embedded Suite: a full suite for embedded devices which consists of Java SE Embedded 7 runtime, Java DB, Glassfish Server and Jersey WS Framework. You can install this suite on the Raspberry Pi for your projects... nice!
  • JavaFx will continue its development. There's lot of documentation online to learn this technology and you can even install JavaFx apps on embedded systems like the RaspberryPi, check this post by Stephen Chin.
  • Java EE 7 will be released on the first quarter of 2013, around April. Great new features, check them out!
  • Java 8 will be here! The new release of the Java SE will be announced during the second semester of the year, so you better start using Java SE 7 before you get outdated... 
  • Java One 2013 Save the Date: now we have dates for the conferences around the globe, I guess Java One LAD (Brazil) will be held on December at São Paulo.

A lot to come! I hope you can join or create a Java User Group in your area, or blog about the Java technology, or attend to the conferences... anything would be great for the community.


Happy new year!

Wednesday, December 26, 2012

Iterator Pattern and Java

Hello all, in this post we'll be checking on the Iterator Pattern. A design pattern that I know many of you have already used, but maybe you didn't realize it was pattern or didn't know its great value. According to the book Head First Design

The Iterator  Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlaying representation.

Whaaaaat? Well, it says that no matter what data structure (arrays, lists, hashtables,etc.) you are using, you can always traverse it in the same way if you implement this pattern. It gives you a uniform way of accessing the elements of your data structures (aggregates), but you don't have to know what kind of data structure you are traversing... nice!
Also, it sets the responsibility of iteration on the Iterator object not on your data structure, which simplifies the coding in your data structure.

Let's check the classic class diagram for the Iterator pattern:


The actual class diagram for the Iterator Pattern has a few changes, specially in the Iterator class (interface), where we now have different methods as we'll see in a minute, but first, lets review each of the previous classes (or interfaces):

  • Aggregate: This is the base class (or interface) of our data structures, you can think of it as the java.util.Collection interface, which defines a lot of methods for the collection classes.
  • ConcreteAggregate: This is the concrete data structure that we'll be iterating, for example, java.util.ArrayList, java.util.Vector, etc.
  • Iterator: Base class (or interface) for iterators. You can find one in Java at java.util.Iterator. You can notice that the Java version has different methods that we'll be discussing later in this post. Here you define de methods you need in order to traverse the data structures.
  • ConcreteIterator: As you want to traverse different data structures you need different iterators. So a concreteIterator is an Iterator for the data structure you want to traverse.  

Now, let's take a look at the Java implementation of the Iterator Pattern. The following diagram was generated using Architexa's Free Tool for Understanding Code and it shows the relations between some classes of the Java Collections Framework where we can see a structure similar to the classic class diagram:


The above diagram shows only one implementation of the pattern in Java, there are a lot more, but they always use the java.util.Iterator interface; this is the interface you should use in your implementations of the Iterator Pattern when coding in Java. Let's compare both diagrams:


Classic Diagram
ConcreteIterator

Notice that the methods of the Iterator object in the Java example are different from the methods in the classic class diagram:
  • There is no +First() method. If you need to go to the first element, you have to instance a new iterator.
  • The +IsDone() method has been renamed to +hasNext().
  • The +Next() and +CurrentItem() has been merged into +next().
  • The +remove() method has been added.

So, if you ever have to work with different data structures and need a uniform way to traverse them and/or access their items, think about the Iterator Pattern:

//... in a class
  /**
   * Traverse a list, hashtable, vector, etc. what ever that implements
   * the Iterator Pattern
   */
  public void traverse(Iterator iter)
  {
     while(iter.hasNext())
     {
 System.out.println(iter.next());
     }
  }

Of course, you will always have to create the ConcreteIterator class for your data structure, but if you are using classes from the Java Collections Framework, it's already done.

One last thing, remember the most important OO Principle of all: Always use the simplest solution that meets your needs, even if it doesn't include a pattern.


See ya!


References:

Freeman Eric and Freeman Elisabeth and Sierra Kathy and Bates Bert (2004). Head First Design Patterns. United States of America: O'Reilly Media, Inc.

Wednesday, December 12, 2012

JavaOne LAD (Brazil) 2012 Days 2 and 3

Hi, I want to share my experiences in the last couple days at JavaOne LAD (Brazil). Day one has been already reviewed here.

Day Two

There was a conference about Oracle Developer Cloud Services which I liked a lot. Great move from Oracle who is moving all its apps and  infrastructure services to the cloud. With such product, you have software lifecycle tools ready to be used. Tools such as: source control management (GIT), issue tracking, Hudson CI and Wiki collaboration. The services is in Preview mode, so you have to contact Oracle in order to test it.




Next I attended a session called "Do You Like Coffee with Your Dessert? Java and the Raspberry Pi" (clever...) which introduced us to the "Raspberry Pi" a credit-card sized computer, imagine that! And you can embed java in it!! There were two live example of embedding Java in a Raspberry Pi. The first one, was a kiosk with all JavaOne LAD session information. Really nice kiosk with touch support and JavaFx.
The second one was a robot created by Vinicius Senger which uses Raspberry Pi and Glassfish Server so you can control the robot using REST WS, awesome!!





The final session of the day was given by Arun Gupta. "Java EE 7 Platform: More Productivity and Integrated HTML". Really nice session packed with the latest news about the JavaEE platform which is schedule to be released on the first quarter of 2013.

At night, Oracle invited the JUG leaders to a nice dinner. Pizza and beers for all of us and networking all around. Was great to meet several Brazil JUG Leaders and learn from their experiences and their culture.


Day Three

Last day of JavaOne LAD. Thanks to Otavio Santana who showed me the correct route to reach the expo center by Metrô as detailed in my previous post. Unfortunately, some sessions were rescheduled and I didn´t noticed that, so I missed the session "JavaFX: Implementing Multitouch with TUIO for Interactive Devices" who was given by Steven Lizarazo, the only Colombian speaker at JavaOne LAD. Congrats!

Next, Stephen Chin talked about JavaFx and HTML5, showing one demo about a JavaFx application which contains map (google map) interaction. It was nice to see how you can integrate both technologies. I skipped the next two conferences because I was presenting a quick demo about Fork/Join Framework at the OTN lounge.

The last conference I attended, was about SQA: "Testing JavaServer Faces Applications with Arquillian and Selenium" given by José Rodolfo Freitas. He showed several ways to test our applications using those frameworks. It was a crowded session, but there was no translation available... so I'm glad the slides were in english... Anyway, thanks José for checking if I was understanding portuguese. 

At the end of the day, the Community Keynote and the first ever Duke's Choice Awards where I was part of the judging panel.

The hosts of the event were Yara Senger and Bruno Sousa (the Brazil JavaMan), great JUG leaders in Brazil. They joked and talked about how we can get involved and participate in the future java by joining/creating a JUG or Adopting a JSR. They also presented some startups that leverage the power of Java for their business ideas.

The entertainment was provided by several JUG leaders who played in a band. They performed some Beatles and Java songs and it was really, really funnny. The Duke's choice Awards winners were presented. We had like 24 nominees, but only 3 could be selected as winners. It was a tough decision since there were a lot of innovation in all projects. I liked that we had several Java technologies competing, from Desktop to Web apps. And the winners were: Tqtvd tots, Cpqd and Tivit. I do have to write another post about the projects I liked the most but were not selected as winners, so you can meet them, specially two Colombian Projects: Kuwaiba and ZathuraCode.






As a regional JavaOne, it is different from the JavaOne SFO, it's smaller. Both events  Oracle OpenWorld and JavaOne are run in the same place, so some times you may find executives wearing suites and ties or developers wearing T-shirts and jeans... I heard from some other attendees that this year was a better JavaOne LAD.

What I liked:
Strong community (Brazil).
Oracle sent several speakers from JavaOne SFO.
Dinner invitation from Oracle ;)
Party invitation from Google :)
Hospitality from the JUG leaders I met.

What I didn't like:
Lack of participation from other south american countries. I mean, is JavaOne LAD (Latinamerican) but most of the attendees and speakers were from Brazil.
The food could be better or cheaper (or included in the full conference pass :p)
Poor twitter or website updates when sessions were rescheduled.
There were no Oracle stores.

Anyway, I hope I can make it for JavaOne LAD 2013 so I can go with other Colombian friends and why not, maybe we can have a first ever Duke's Soccer Cup at Brazil... nice!


see ya!

Saturday, December 8, 2012

Arriving to JavaOne LAD by Metrô

Hello all. I'm writing this post because I found some difficulties when trying to reach the Transamerica Expo Center in São Paulo where the JavaOne LAD event was taking place and I wanted to share with you some advices when taking the Metrô:
  1. Visit the Transamerica Expo Center location page. There you will find information about different ways to reach the expo center. Specially, notice that the final station when using metro is "Santo Amaro".
  2. Use google maps in order to find the stations that are near to the place you are staying at.
  3. Use the Trip Planner in the Metrô website. Using this tool you will receive specific directions for boarding and landing in the different stations. For Embarque (boarding) use the station that is near to the place you are staying at and for Desembarque (landing) use "Santo Amaro", which is the closest station to the expo center (at least at the date of this post Dec-2012).
The advantages of using the Metrô are:
  1. Cheaper than taxi.
  2. Faster than taxi, specially if you are staying away of the expo center.
  3. It's a "must do" when visiting São Paulo.
When you reach the "Santo Amaro" station, use the exit Av. das Nações Unidas, Av. Pe. José Maria:


From there, it is a 10 minutes walking to reach the expo center. Follow this instructions and you will avoid disembarking in another station like "Largo Treze" which appears near, but is not, it is a 30 minutes walking from there and believe me, you don't want to walk around there...

I was staying near Av. Paulista and this is the route I should have used from the beginning:



Ver JavaOneLAD en un mapa más grande


Special thanks to @otaviojava who helped me find the correct route.


see ya!

Tuesday, December 4, 2012

JavaOne Brazil 2012 Day One

Hello all, today I had the opportunity to attend JavaOne LAD (Brazil) 2012.

I'm writing this post right from my mobile device, so I'll be brief.
The community keynote was hosted by several java luminaries and Java Champions from Brazil such as
Fabiane Nardon, Yara Senger, Vinicius Senger, Bruno Souza and a special guest from jFokus. The talk was about getting involved with the java community: Jugs, Java Champions, JCP, etc. Inspiring.

The next conference was about The Internet of Things... Really interesting. You can find more about this at: Things-API.org

Unfortunately I was confused and I left early, missing the java technical keynote... Can't believe it!! grgr

Anyway, tomorrow is another day full of conferences and I'll be posting about the ones I like the most.

See ya!





Wednesday, November 28, 2012

JDeveloper 11gR2 and Java 7

Hello all. For an ADF Essentials application I'm working on I wanted to use the great set of new features introduced by Java 7, however JDeveloper 11gR2 (11.1.2.3) is not certified for JDK 7 so I had to look for help and found some tricks that let you use the new JDK.

This is what you need to do:
  1. Download and Install the new JDK. You can find the latest release using this link.
  2. Add the new JDK to JDeveloper's libraries: Tools -> Manage Libraries.
  3. Configure JDeveloper to run using the new JDK.
  4. Configure the Integrated Weblogic server to run using the new JDK.

The first step is pretty simple. Just go to that location, download the latest release of Java 7 and install it on your machine. In my case, I didn't have to create any environment variable for the JAVA_HOME or PATH, but your case may be different.

Open JDeveloper and go to the menu Tools and then open Manage Libraries:



The Manage Libraries dialog appears, select the title Java SE Definitions and then click on the button New... 



The Create Java SE dialog appears. For me, the Browse... button didn't work when trying to get in the Program Files directory, so I opened Windows Explorer and went to new JDK installation directory (for me it was: C:\Program Files\java\jdk1.7.0_09) then I copied the path from Windows Explorer and pasted it on the Java SE Executable inputText. I also added  \bin\java.exe





Notice that after you complete the path to the Java SE executable (\bin\java.exe), Jdeveloper will load the rest of the information. Hit the OK button to finish.

Now, JDeveloper comes with its own JDK and we need to change it for the new one you just installed. Close JDeveloper, open Windows Explorer and go to {ORACLE_HOME}\jdeveloper\jdev\bin where {ORACLE_HOME} is the directory you used when installing JDeveloper. There you will find a file called jdev.conf open it with a text editor and look for the entry SetJavaHome on line 31 (line numbers may differ in other versions of Jdeveloper), comment it and create a new SetJavaHome entry below that one, but this time use the new JDK installation folder:




What we just did tells JDeveloper to start with the new JDK. Now we need to configure the Integrated Weblogic server so it starts with the new JDK as well.

We need to find the value of the -Dide.pref.dir.base entry as described here:
http://www.java-n-me.com/2012/11/jdeveloper-11gr2-wont-start.html

In my case the value of the entry is: C:\Users\{MY_USER}\AppData\Roaming, where {MY_USER} is my user name, you may need to enable the "show hidden files" property of the Windows Explorer in order to be able to locate that directory. There,  look for the JDeveloper\system11.x.x.x.. directory where the x's are numbers. Open DefaultDomain\bin directory and edit setDomainEnv.cmd using a text editor:



Comment the line 45 and below that line set the SUN_JAVA_HOME property to point to the new JDK. Repeat the same procedure for the JAVA_HOME  property found at line 55 (line numbers may differ in other versions of Jdeveloper):



That's it, now JDeveloper and its Integrated Weblogic Server will use the new JDK. Open JDeveloper and go to Help->About->Version and you will notice that it is using the new JDK:



As I didn't want to keep using the old JDK I removed it from the Tools->Manage Libraries menu option so my new JDK become the Default JDK and all my new projects will be using Java 7:



I hope this workaround works for you as it did for me.

see ya!


References:

Using WebLogic Server with JDK 7. Oracle [online].
Available on Internet: http://docs.oracle.com/cd/E23943_01/doc.1111/e14142/jdk7.htm
[accessed on Novemeber 25th 2012].

Nick Aiva's blog! [online].
Available on Internet: http://nickaiva.blogspot.com/2011/04/jdeveloper-11g-setting-up-newest-java.html
[accessed on Novemeber 25th 2012].

Sunday, November 25, 2012

ADF Essentials Hackathon at RightHandCorp

Hello all, last saturday (24-Nov-2012) we had a lot of fun with the RightHandCorp's development team in their first ever ADF Essentials hackathon. We built a proof of concept for one of their in-house web application and the result was more than satisfactory. They were surprised about how easy it is to build great good looking enterprise applications using this framework. Used JDeveloper 11gR2, ADF Business components,  ADF Faces, Oracle Database and Glassfish server.



We had a great time, lot of networking and knowledge sharing. Looking forward for the next one.

see ya!

Sunday, November 11, 2012

JDeveloper 11gR2 won't start

Hello all, I just wanted to share with you a workaround to "restart" your JDeveloper 11gR2 (11.1.2.3) if it is not starting. I struggle with this for several hours, until I found some documentation that helped me out.

My case
  • JDeveloepr 11gR2 (11.1.2.3)
  • OS = Win Vista Sp2 
  • RAM= 4GB
  • Installation by default (C:\Oracle\Middleware).

I'm developing an ADF Essentials application and after updating some extensions my JDeveloper installation stopped working. I did several reinstallations and registry cleaning but nothing worked, JDeveloper just didn't start and it freezed when "Restoring Editors":



Fortunately, I found this blog: http://adfhowto.blogspot.in/2011/03/troubleshooting-jdeveloper-will-not.html which gives a tip about the JDeveloper preferences directory. What it says is that you can delete or modify the name of the JDeveloper preferences directory, and JDeveloper will create another one with default preferences. Something you should be aware of is that this procedure deletes all your preferences: SVN, extension preferences, general preferences...etc. So it's up to you.

The Solution
I decided that this procedure fitted my needs and started to search for that directory. There is an easy way to find the preferences directory:
  • Open a DOS terminal (cmd).
  • Go to your JDeveloper installation directory (in my case C:\Oracle\Middleware\jdeveloper\).
  • We need to execute the file jdev.exe which is in {JDEV_INSTALLATION_DIR}\jdev\bin
  • Type this command: jdev.exe -verbose
The previous procedure will try to start JDeveloper and will show all the steps required to do so. Look for the -Dide.pref.dir.base entry:



The final step is: go to that location and rename or delete (rename is better) the directory system11.x.x.x... for something else. Restart JDeveloper and it will start and create a new directory with default preferences.

I hope this post helps so you don't have to spend several hours trying to put JDeveloper to work.

see ya!

Friday, November 9, 2012

Java 7: File Filtering using NIO.2 - Part 3

Hello all. This is Part 3 of the File Filtering using NIO.2 series. For those of you who haven't read Part 1 or Part 2, here's a recap.

NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, we already reviewed two similar ways in Part 1 and Part 2, but now we are going to see a more powerful approach.

What you need
NetBeans 7+ or any other IDE that supports Java 7

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:
  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It's like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.DirectoryStream.filter<T>: A nested interface whose objects decide whether an element in a directory should be filtered or not.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using objects that implement the java.nio.file.DirectoryStream.filter<T> interface. This interface declares only one method +accept(T):boolean which as the JavaDoc says: "returns true if the directory entry should be accepted". So it's up to you to implement this method and decide whether a directory entry should be accepted based on whatever attribute you want to use: by hidden, by size, by owner, by creation date, etc. This is important to remember, using this method you are no longer tied to filter only by name, you can use any other attribute.

If you only want directories, you can use the java.nio.file.Files class and its +isDirectory(Path, LinkOption...):boolean method when creating the filter:

//in a class...

    /**
     * Creates a filter for directories only
     * @return Object which implements DirectoryStream.Filter
     * interface and that accepts directories only.
     */
    public static DirectoryStream.Filter<Path> 
                                 getDirectoriesFilter() {

        DirectoryStream.Filter<Path> filter = 
                           new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.isDirectory(entry);
            }
        };

        return filter;
    }


Or if you only want hidden files, you can use the java.nio.file.Files class and its +isHidden(Path):boolean method when creating the filter:

//in a class...

    /**
     * Creates a filter for hidden files only
     * @return Object which implements DirectoryStream.Filter
     * interface and that accepts hidden files only.
     */
    public static DirectoryStream.Filter<Path> 
                                 getHiddenFilesFilter() {

        DirectoryStream.Filter<Path> filter = 
                           new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.isHidden(entry);
            }
        };

        return filter;
    }


Or if you want files belonging to a specific user, you have to ask for a user and compare it with the owner of the directory entry. To obtain the owner of a directory entry, you can use the java.nio.file.Files class and its +getOwner(Path, LinkOption...):UserPrincipal method (watch out, not all OS support this).
To obtain a specific user on the filesystem use the java.nio.file.FileSystem class and its +getUserPrincipalLookupService():

//in a class...

    /**
     * Creates a filter for owners
     * @return Object which implements DirectoryStream.Filter
     * interface and that accepts files that belongs to the 
     * owner passed as parameter.
     */
    public static DirectoryStream.Filter<Path> 
          getOwnersFilter(String ownerName) throws IOException{

        UserPrincipalLookupService lookup = FileSystems.getDefault().getUserPrincipalLookupService();

        final UserPrincipal me = 
                    lookup.lookupPrincipalByName(ownerName);

        DirectoryStream.Filter<Path> filter = 
                           new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.getOwner(entry).equals(me);
            }
        };

        return filter;
    }


The following piece of code defines a method which scans a directory using any of the previous filters:

//in a class...
    
    /**
     * Scans the directory using the filter passed as parameter.
     * @param folder directory to scan
     * @param filter Object which decides whether a 
     * directory entry should be accepted
     */
    private static void scan(String folder
                           , DirectoryStream.Filter<Path> filter) 
    {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println("No such directory!");
            return;
        }
        //validate the filter
        if (filter == null) {
            System.out.println("Please provide a filter.");
            return;
        }

        //Try with resources... so nice!
        try (DirectoryStream<Path> ds = 
                      Files.newDirectoryStream(dir, filter)) {
            //iterate over the filtered content of the directory 
            int count = 0;
            for (Path path : ds) {
                System.out.println(path.getFileName());
                count++;
            }
            System.out.println();
            System.out.printf(
                     "%d entries were accepted\n", count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

We can execute the previous code passing the following parameters to the main method (check the source code at the end of this post):

  • Directory to scan: C:\ or / dependening on your OS. 
  • Filter: hidden

When executing the code we get the following:




In a windows machine, you can obtain the hidden files using the command: dir /AAH Notice that we are getting the same result:




And on my Linux virtual machine:




Using the command ls -ald .* we get similar results:



Again, Write once, run everywhere!

I hope you enjoyed the File Filtering using NIO.2 series. One last word, all the filtering methods we reviewed worked on one directory only, if you want to scan a complete tree of directories, you'll have to make use of the java.nio.file.SimpleFileVisitor class.


Click here to download the source code of this post.


See ya!

References:

Reese Richard and Reese Jennifer (2012). Java 7 New Features Cookbook. United Kingdom: Packt Publishing Ltd.

Thursday, November 1, 2012

Duke's Choice Awards 2012 LAD nomination extended

Just to let you know, the nominations for the Duke's Choice Awards 2012 LAD has been extended, the new final date is Novemeber 7th, 2012.

The program looks for the most compelling implementations of Java technology. Innovation is the only criteria. The nomination form can be found on the Duke's Choice Java.net site


Winners will be announced on stage during JavaOne LAD (Brasil) December 4th through 6th, and will also be featured in the January/February issue of Java Magazine. Winners will receive a free JavaOne LAD full conference pass and a Duke's Choice Award Statue.

So, you are in Latin America (LAD) and have an innovative Java project? what are you waiting for? Remember the end date for nominations is November 07th, 2012.

Don't you know if you got what it takes? Check the 2011 winners (when there was only one global program).


see ya!

Wednesday, October 31, 2012

Java 7: File Filtering using NIO.2 - Part 2

Hello all. This is Part 2 of the File Filtering using NIO.2 series. For those of you who haven't read Part 1, here's a recap.

NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, we already review one way in Part 1 and now we are going to see  another approach.

What you need
NetBeans 7+ or any other IDE that supports Java 7

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:
  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It's like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.PathMatcher: Interface that allows objects to perform match operations on paths.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using objects that implement the java.nio.file.PathMatcher interface. We can get one of these objects with the help of the java.nio.file.Files class, using the method +getPathMatcher(String):PathMatcher. This method supports both "glob" and "regex" patterns. You can check Part 1 of File Filtering using NIO.2 for more information about "glob" and for "regex" visit the java.util.regex.Pattern class. The pattern is matched against the name of the files, directories, etc. That live inside the directory. This is important to remember, using this method you can only filter by the name of the file, directory, etc.

For example, if you want to filter .png and .jpg images, you should use one of the following syntax and pattern (notice the colon between the syntax and the pattern):
  • "glob:*.{png,jpg}"
  • "regex:([^\s]+(\.(?i)(png|jpg))$)"

Of course, "glob" syntax is much simpler, but you have the option of using regular expressions for a more detailed match. Anyway, you may be wondering why you should use this approach if the java.nio.files.DirectoryStream interface allows you to filter directly using "glob"... Well, let's suppose that you already have a filter, but you need to perform more than one filtering operation, that's when you need to use this approach.

The following piece of code defines a method which scans a directory using different patterns:

//in a class...
    
    /**
     * Scans the directory using the patterns passed 
     * as parameters. 
     * Only 3 patterns will be used.
     * @param folder directory to scan
     * @param patterns The first pattern will be used
     * as the glob pattern for the DirectoryStream.     
     */
    private static void scan(String folder, String... patterns) {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println("No such directory!");
            return;
        }
        //validate at least the glob pattern
        if (patterns == null || patterns.length < 1) {
            System.out.println(
                "Please provide at least the glob pattern.");
            return;
        }

        //obtain the objects that implements PathMatcher
        PathMatcher extraFilterOne = null;
        PathMatcher extraFilterTwo = null;
        if (patterns.length > 1 && patterns[1] != null) {
            extraFilterOne = FileSystems.getDefault().
                                 getPathMatcher(patterns[1]);
        }
        if (patterns.length > 2 && patterns[2] != null) {
            extraFilterTwo = FileSystems.getDefault().
                                 getPathMatcher(patterns[2]);
        }

        //Try with resources... so nice!
        try (DirectoryStream ds = 
                  Files.newDirectoryStream(dir, patterns[0])) {
            //iterate over the content of the directory and apply 
            //any other extra pattern
            int count = 0;
            for (Path path : ds) {
                System.out.println(
                          "Evaluating " + path.getFileName());

                if (extraFilterOne != null && 
                    extraFilterOne.matches(path.getFileName())) {
                    System.out.println(
                                  "Match found Do something!");
                }

                if (extraFilterTwo != null && 
                    extraFilterTwo.matches(path.getFileName())) {
                    System.out.println(
                             "Match found Do something else!");
                }

                count++;
            }
            System.out.println();
            System.out.printf(
                 "%d Files match the global pattern\n", count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

You can try invoking the last method with the following parameters:

  • C:\Images or /Images depending on your OS.
  • ?_*.jpg This pattern specifies that you want all .jpg images whose name starts with one digit followed by an underscore.
  • glob:2_* Specifies another filter (using glob syntax) where you only want items whose name starts with the number two followed by an underscore.
  • glob:3_* Specifies another filter (using glob syntax) where you only want items whose name starts with the number three followed by an underscore.

Having several filters allows you to take different actions for matched items.
Following is the result of the execution on my windows machine:



And on my Linux virtual machine:



Again, Write once, run everywhere! However, notice that the ordering of the items is system dependent, so do not ever hardcode the position of a file or directory.

I hope you enjoyed this post, there is another more powerful way to filter the content of a directory and we'll explore it in Part 3.

Click here to download the source code.


See ya!

References:

Reese Richard and Reese Jennifer (2012). Java 7 New Features Cookbook. United Kingdom: Packt Publishing Ltd.

Saturday, October 27, 2012

Book Review: Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications

Book cover from
http://www.packtpub.com
Java EE 6 is the latest specification for building Enterprise Applications with Java. It is a set of APIs that will simplify your development and if you come from Java EE 5, then you will notice the difference. Fortunately, Java EE 6 is been around for almost three years now and is widely adopted, which improves the quality of the documentation and training you may find about it.

In this post, I'll be reviewing the book "Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications" written by Mick Knutson and published by PACKT. I have to say that this book is not for begginers in the Java EE field, you need some Java EE background in order to better understand the recipes. It is aimed at Java developers and programmers who want to secure, tune and, extend their Java EE applications

I liked the way chapter one updates you not only with the new features added to the specification, but also with the outgoing ones. It explains why some JSRs are pruned from this release and then it starts with the new: Context Dependency Injection (CDI), EJB 3.1, JPA 2.0, JAX-RS 1.1, Servlets 3.0, JSF 2.0, etc. Again, this is just a small review as cited on the book "... This chapter is not a tutorial or primer on the various specifications..." But is good enough to see what's new in Java EE 6. This chapter contains no recipes.

Chapter two dives into the implementation of some of the new features of the JPA 2.0 spec. The recipes are written in the form "Getting ready - How to do it - How it works - There's more..." which allows you to start using the new features very fast and avoid pitfalls. There's a tutorial in this chapter that helps you in profiling and testing JPA Operations, really useful.

Chapter three is about security using the Java EE built-in features and it also explains how to use Spring Security for a more fine-grained security implementation. This chapter is all about security, I wasn't expecting recipes about configuring Linux firewall rules or obfuscating Java byte-code... well done!

So far, so good. I haven't finished the book, but looking at the table of contents I can see really interesting chapters ahead:
  • Chapter 4: Enterprise Testing Strategies. Inlcuding Testing JPA with DBUnit,  Testing JAX-WS and JAX-RS with soupUI, among others.
  • Chapter 5: Extending Enterprise Applications. Inlcudes integrating Scala, Groovy and Jython into Enterprise Applications. I think this is an important chapter, specially because JSR 292 Invoke Dynamics is now part of the JDK 7.
  • Chapter 6: Enterprise Mobile Device Integration. Inlcudes an evaluation of Mobile-Web frameworks: jQuery Mobile, Sencha Touch and Modernizer. Really interesting and updated. There is also information about development and distribution considerations for iOS and Android.
  • Chapter 7: Deployment and Configuration. Inlcudes recipes that cover issues and solutions to application configuration with the use of Java Management Extensions (JMX).This chapter also cover tools to aid in  rapid and hot-deployment of Java EE applications such as Apache Ant and Apache Maven.
  • Chapter 8: Performance and Debugging. Inlcudes recipes that will help you in improving  the performance of your Enterprise applications with: jVisualVM, jstatd, among others.
There are so many recipes about topics I wasn't expecting that it really surprised me. After reviewing this book I realize that many of the topics will show you tools that you can leverage when tuning and extending your Java EE applications, so you can make the difference in your team. This is a must read if you are a architect/leadDevelper/consultant.

One more thing, the book is published in a lot of formats: printed book, kindle, PDF, ePub, so you have many options to read it. I have the kindle edition and it looks great on my kindle, paragraphs are well formatted and the source code is easy to read.

For more information about this book go to:
http://www.packtpub.com/java-ee6-securing-tuning-extending-enterprise-applications-cookbook/book

See ya!

Friday, October 19, 2012

Java 7: File Filtering using NIO.2 - Part 1

Hello all. NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, that's why this post is Part 1.

What you need
NetBeans 7+ or any other IDE that supports Java 7
JDK 7+

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:
  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It's like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using glob patterns, which are like regular expressions but simpler. The pattern is matched against the name of the files, directories, etc. That live inside the directory. This is important to remember, using this method you can only filter by the name of the file, directory, etc.
For more information about globbing, check this wiki. Also, there is some documentation about globbing patterns in the Java Docs.

So, let's suppose that we have a directory called Images, and we need to iterate over the files inside this directory, but we only need the .png files. We have to follow this steps in order to do so:
  1. Obtain a java.nio.file.Path instance which points to the directory Images.
  2. Open a new java.nio.file.DirectoryStream using the java.nio.file.Files class and passing the directory and the pattern (*.png) as parameters.
  3. Iterate over the content of the directory using the java.nio.file.DirectoryStream instance.
Next is the source code of a method that scans a directory using the pattern passed as parameter:

//in a class...
    
    /**
     * Scans the directory using the glob pattern passed 
     * as parameter. 
     * @param folder directory to scan
     * @param pattern glob pattern (filter)
     */
    private static void scan(String folder, String pattern) {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println("No such directory!");
        }
        //Try with resources... so nice!
        try (DirectoryStream<path> ds = 
                    Files.newDirectoryStream(dir, pattern)) {
            //iterate over the content of the directory
            int count = 0;
            for (Path path : ds) {
                System.out.println(path.getFileName());
                count++;
            }
            System.out.println();
            System.out.printf("%d Files match the pattern"
                                                     , count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


Following is the result of the execution on my windows machine:



And on my Linux virtual machine:


That's something I love from Java, Write once, run everywhere! I hope you enjoyed this post, there are more ways to filter the content of a directory and we'll explore them in future posts.

Click here to download the complete source code.

See ya!

References:

Reese Richard and Reese Jennifer (2012). Java 7 New Features Cookbook. United Kingdom: Packt Publishing Ltd.

Sunday, October 14, 2012

Java 7: Meet the Fork/Join Framework

Free clip art from
http://www.freepik.com
JSR-166(y) is the official name of this new feature which is included in Java 7. If you notice there is a "y" in the name, this is because JSR-166 (Concurrency Utilities) is being added since Java 5, but it wont stop here as there are already plans to add new classes in Java 8 under the JSR-166(e). Check this page maintained by Doug Lea, the creator of JSR-166, for more information.

According to Wikipedia, Parallelism is the "simultaneous execution of some combination of multiple instances of programmed instructions and data on multiple processors" and Java has classes and interfaces to achieve this (sort of...) since DAY 1. You may know them as: java.lang.Thread, java.lang.Runnable, etc... What Concurrency Utilities (java.util.concurrent package) does is simplify the way we code concurrent tasks, so our code is much simpler and cleaner. As developers we haven't had to do anything when running our applications in machines with higher processing resources, obviously, the performance of our applications will improve, but are we really using the processing resources to the maximum? The answer is big NO.

This post will show you how the Fork/Join framework will help us in using the processing resources to the maximum when dealing with problems that can be divided into small problems and all the solutions to each one of those small problems produce the solution of the big problem (like recursion, divide and conquer).

What you need
NetBeans 7+ or any other IDE that supports Java 7
JDK 7+
Blur on an image, example from Oracle

The Basics
The Fork/Join framework focuses on using all the processing resources available in the machine to improve the performance of the applications. It was designed to simplify parallelism in Divide and Conquer algorithms. The magic behind the Fork/Join framework is its work-stealing algorithm in which work threads that are free steal tasks from other busy threads, so all threads are working at all times. Following are the basics you should know in order to start using the framework:
  • Fork means splitting the task into subtasks and work on them.
  • Join means merging the solution of every subtask into one general solution.
  • java.lang.Runtime use this class in order to obtain the number of processors available to the Java virtual machine. Use the method +availableProcessors():int in order to do so.
  • java.util.concurrent.ForkJoinPool Main class of the framework, is the one that implements the work-stealing algorithm and is responsible for running the tasks.
  • java.util.concurrent.ForkJoinTask Abstract class for the tasks that run in a java.util.concurrent.ForkJoinPool. Understand a task as a portion of the whole work, for example, if you need to to do something on an array, one task can work on positions 0 to n/2 and another task can work on positions (n/2) +1 to n-1, where n is the length of the array.
    • java.util.concurrent.RecursiveAction Subclass of the abstract task class, use it when you don't need the task to return a result, for example, when the task works on positions of an array, it doesn't return anything because it worked on the array. The method you should implement in order to do the job is compute():void, notice the void return.
    • java.util.concurrent.RecursiveTask Subclass of the abstract task class, use it when your tasks return a result. For example, when computing Fibonacci numbers, each task must return the number it computed in order to join them and obtain the general solution. The method you should implement in order to do the job is compute():V, where V is the type of return; for the Fibonacci example, V may be java.lang.Integer.

When using the framework, you should define a flag that indicates whether it is necessary to fork/join the tasks or whether you should compute the work directly. For example, when working on an array, you may specify that if the length of the array is bigger than 500_000_000 you should fork/join the tasks, otherwise, the array is small enough to compute directly. In essence, the algorithm you should follow is shown next:

if(the job is small enough)
{
   compute directly
}
else
{
   split the work in two pieces (fork)
   invoke the pieces and join the results (join)
}

OK, too much theory for now, let's review an example.

The Example
Blurring an image requires to work on every pixel of the image. If the image is big enough we are going to have a big array of pixels to work on and so we can use fork/join to work on them and use the processing resources to the maximum. You can download the source code from the Java™ Tutorials site.

Once you download the source code, open NetBeans IDE 7.x and create a new project:



Then select Java Project with Existing Sources from the Java category in the displayed pop-up window:



Select a name and a project folder and click Next >



Now, select the folder where you downloaded the source code for the Blur on an image example:



And select the file ForkBlur.java then click finish:



The source code will be imported and a new project will be created. Notice that the new project is shown with erros, this is because Java 7 is not enable for default:



To fix this, right click on the project name and select the option Properties. On the pop-up dialog, go to Libraries and select JDK 1.7 from the Java Platform ComboBox:



Now, go to the option Sources and select JDK 7 from the Source/Binary Format ComboBox:



Last but not least, increase the memory assigned to the virtual machine when running this application as we'll be accessing a 5 million positions array (or more). Go to the option Run and insert -Xms1024m -Xmx1024m on the VM Options TextBox:


Click OK and your project should be compiling with no errors. Now, we need to find an image bigger enough so we can have a large array to work on. After a while, I found some great images (around 150 MB) from planet Mars, thanks to the curiosity robot, you can download yours from here. Once you download the image, past it on the project's folder.

Before we run the example, we need to modify the source code in order to control when to run it using the Fork/Join framework. In the ForkBlur.java file, go to line 104 in order to change the name of the image that we are going to use:

//Change for the name of the image you pasted 
//on the project's folder.
String filename = "red-tulips.jpg";

Then, replace lines 130 to 136 with the following piece of code:

ForkBlur fb = new ForkBlur(src, 0, src.length, dst);
        boolean computeDirectly = true;

        long startTime = System.currentTimeMillis();
        if (computeDirectly) {
            fb.computeDirectly();
        } else {
            ForkJoinPool pool = new ForkJoinPool();
            pool.invoke(fb);
        }
        long endTime = System.currentTimeMillis();

Notice the computeDirectly flag. When true, we'll NOT be using the fork/Join Framework, instead we will compute the task directly. When false, the fork/join framework will be used.

The compute():void method in the ForkBlur class implements the fork/join algorithm. It's based on the length of the array, when the length of the array is bigger than 10_000, the task will be forked, otherwise, the task will be computed directly.

Following you can see my 2 processors when executing the Blur on an image example without using the Fork/Join framework (computeDirectly = true), it took about 14s to finish the work:



You can see that the processors are working, but not to the maximum. When using the Fork/Join framework (computeDirectly = false) you can see them working at 100% and it took almost 50% less time to finish the work:



This video shows the complete process:




I hope you can see how useful this framework is. Of course, you cannot use it all around your code, but whenever you have a task that can be divided into small tasks then you know who to call.

For more tutorials and information about concurrency in Java, check the Java™ Tutorials site.

see ya!


References:

The Java™ Tutorials. Oracle [online].
Available on Internet: http://docs.oracle.com/javase/tutorial/essential/concurrency/
[accessed on October 10 2012].

PHOTOJOURNAL. NASA Jet Propulsion Laboratory [online].
Available on Internet: http://photojournal.jpl.nasa.gov/catalog/PIA16101
[accessed on October 10 2012].

Monday, October 8, 2012

ADF Essentials

Hello all. Today I'm going to present you a great framework for bulding Rich Web applications with Java. It is called ADF Essentials and for those of you who haven't heard about it, let me tell you its benefits:
  • 150 Ajax-enabled JSF components.
  • Support for Skinning.
  • Visual and declarative development.
  • Free to develop and deploy.
  • Glassfish server officially certified, check the ADF Essentials product certification page.
  • Includes ADF Faces, ADF Data Visualization Tools (DVT), ADF Controller, ADF Model, ADF Share, and ADF Business Components. Check this page for a complete list of components included.
  • For more information and other benefits visit the ADF Essentials product home page.

The framework is built over JSF 2 so if you have previous experience in building Web applications using JSF you are likely to start developing with ADF Essentials pretty fast. On the contrary, if you are just beggining with Web development, the visual and declarative development this framework offers will let you build the application in a fast pace. Either case, there are tons of documentation online: books, tutorials and  videos to help you.

What you will need:

I've been using ADF Faces (which is the complete version of the framework) for the last 3 years and I can tell how easy it is to develop RIA (Rich Internet Applications) with it. In future posts I will show you how to deploy a Web application developed with ADF Essentials on Glassfish server and even on Tomcat (which is not yet officially supported).

Two things before we finish:
  • ADF Faces is the complete framework (which is free to develop but not free to deploy) and ADF Essentials is a subset of ADF Faces and is free to develop and deploy.
  • Check the following video from ADF Insider Essentials's channel on youtube so you can see the framework in action:




see ya!


References:

Oracle ADF Essentials. Oracle [online].
Available on Internet: http://www.oracle.com/technetwork/developer-tools/adf/overview/adfessentials-1719844.html
[accessed on October 06 2012].