Monday, 26 January 2015

POM is not a Derogatory Term

The Maven Project Object Model (POM) file is a powerful way to view a project.  It supports dependency management, build management and adds hierarchy to a project.  You can even specify how to auto-deploy a project.  There is far too much on the POM to cover here so we will just use what we need.

If you have not yet setup a simple project, please see The Wonderful World of JSF.  We will now add the dependencies we require.  The reason I chose to use Java JDK 7 or later is because Java 7 supports non-blocking IO.  The version of Jetty we are going to use requires this feature.  The reason we need to use a JDK rather than a JRE is that Maven and JSF require a full blown compiler, not just a runtime environment.

Dependencies of Dependencies

For a long time dependency management has been an important part of software engineering.  It allows us to track dependencies and dependencies of dependencies.  Without, an upgrade of an application would be nearly impossible.  For Java Maven is a dependency management tool (actually it is a lifecycle tool, but dependemcy management is part of this).  Maven keeps track of our required dependencies and fetches them if required.  It also keeps track of the versions we use.  It saves a great deal of time.

First we will look at the Jetty dependencies.  To view the central Maven repository for Jetty we can simply Google 'Maven Jetty' which should take you here.  Now we can pick and choose what we require.

Firstly, we will do some cleanup.  Open the pom.xml file in Eclipse.  Select the Dependencies tab.  Delete the dependencies (JUnit 3) by selecting them and clicking Remove.  Select the pom.xml tab.  I prefer to add dependencies in test mode as we can simply paste them in from the central repoistory.  It is only when I am doing some of the more complicated tasks that I will use the forms.  Format the document by pressing Crtl + Shift + F.  Between the <dependencies></dependencies> tags add the dependencies below.  I have included the entire POM for completeness.  Again format the document (Ctrl + Shift + F).


       
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.sf.pbu.ontrack</groupId>
    <artifactId>ontrack</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <name>ontrack</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.3.0.M1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.3.0.M1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.3.0.M1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-jsp</artifactId>
            <version>9.3.0.M1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>2.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>2.2.7</version>
        </dependency>

    </dependencies>
</project>

       

The dependencies above are the minimum set of dependencies.  To give a breakdown, jetty-server provides a lightweight HTTP server, jetty-servlet provides us with Java servlet support required by JSP and JSF, jetty-webapp provides us with a handler that will process our web.xml file, jetty-jsp provides us with the glassfish implementation of JSP and Expression Language (EL) that we will be making extensive use of, myfaces-api provides the JSF API glue and finally myfaces-impl provides the apache implementation of JSF.  With these dependencies we can compile an run our first JSF application.

There is still a considerable amount of clean-up left.  We will revise these dependencies later.  We will also introduce some features of the POM.


Creating a Page

Now we have the framework that will run our code.  Now let's create some stuff to run.

Delete the pre-created classes App.java and AppTest.java.  They are not required.  We will create a class net.sf.pbu.ontrack.main.Main.  To create a new class under the selected project press Ctrl + N and select Java > Class.  Change the Package to net.sf.pbu.ontrack.main and the Class Name to Main.  Click Finish.  What you have just witnessed is just part of the powerful Eclipse engine.  It allows many forms of refactoring as well as optimisation shortcuts.  We will see more of this soon.

Repeat the step above creating a new class called Welcome with a package net.sf.pbu.ontrack.ui.webapp.general.

The code we will put in the Main class will allow us to run our server.  The code below creates our Web Application and assigns it to our server.  The Context Path is the path URL path that will be used to access our application.  Keep in mind that multiple applications can run under one Web Server instance.  For example, if we wanted a 'foo' application and a 'bar' application we would specify '/foo/' and '/bar/' respectively.  This would allow us to access foo via www.example.com/foo/ and bar via www.example.com/bar/.  As our application will be the only one on the server, we shall use the root context '/'.  This will allow us to access our application with something like www.example.com.

The resource base is the location of our web application files relative to the working directory of our running web server.  This will change from development to production.  Finally we have our descriptor.  This is a special file that tell our web server how to setup the rest of our web server.

Please note that none of these configuration items should be hard coded, this is just an example.  Later we will use the spring framework to allow us to define these attributes in configuration.

The final four lines creates and runs our webserver.  We are using TCP port 8080.  This is an unprivileged port so we can avoid security issues.  When deploying this on a production environment you may want to use the standard HTTP port (TCP Port 80).

       
package net.sf.pbu.ontrack.main;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {
    public static void main(String[] args) throws Exception {
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath("/");
        webAppContext.setResourceBase("src/main/webapp/");
        webAppContext.setDescriptor("src/main/webapp/WEB-INF/web.xml");
        
        Server server = new Server(8080);
        server.setHandler(webAppContext);
        server.start();
        server.join();
    }
}

Next, create the following file and directory structure.
  1. Expand the src folder.
  2. Right-click the main folder and select New > Folder
  3. Name the folder webapp
  4. Right-click the webapp folder and select New > Folder
  5. Name the folder WEB-INF
  6. Right-click the webapp folder and select New > File
  7. Name the file welcome.xhtml
  8. Right-click the WEB-INF folder and select New > File
  9. Name the file faces-config.xml
  10. Right-click the WEB-INF folder and select New > File
  11.  Name the file web.xml
After following the step above you should have:


web.xml

Let's focus on web.xml.  This tells our web application server how to run our application.  In the configuration below we set the refresh period to 10 seconds.  This allows us to modify dynamic resources without having to reload the server.  This should be turned off for production environments.  The listener class is the MyFaces entry point into our web application.  One servlet has been defined called faces.  This will process our JSF pages.  A servlet-mapping is used to specify how files are selected for processing by a servlet.  More than one mapping can be specified per servlet.  Finally, if we do not identify a resource or path when loading our page, the specified welcome file will be used.  This file can get large.  There are hundreds of possible MyFaces and JSF options to choose from.  I have only presented the basics here.

       
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>facelets.REFRESH_PERIOD</param-name>
        <param-value>10</param-value>
    </context-param>

    <listener>
        <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>welcome.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>welcome.xhtml</welcome-file>
    </welcome-file-list>
</web-app>


On a side note.  Anything stored in the WEB-INF folder is private.  The web server can access any files in this folder but the clients cannot.  It is great for storing templates and configuration.  Use it with some caution, no super secret stuff (like passwords) should be stored here.  Just in case.

faces-config.xml

This file specifies all the JSF specific configuration.  In this case we use it to map a Java POJO to a managed bean.  A managed bean is controlled by the JSF engine.  They are a powerful way to interface standard code or Plain Old Java Objects (POJOs) with the view logic of a web application.  Here we specify one managed bean.  You may also use annotations inside the POJO of the managed bean instead of the faces-config.xml file.  We will use annotations a little later.

Too annotate or not to annotate?  Annotations are a powerful construct but they may not be suitable for all applications.  I am more than happy to use them in tests and in other tools like hibernate.  Like many of the tools on offer, I have preferences (opinion only) as to where and when I use them.  This should not blow-up into a religious war.  Your opinion is allowed to be different to mine.  I would just say, if you have joined a project that has been using annotations or not, keep with the project standard!  Do not mix them and do not break the agreed project standard.

       
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <managed-bean>
        <managed-bean-name>welcome</managed-bean-name>
        <managed-bean-class>net.sf.pbu.ontrack.ui.webapp.general.Welcome</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>



welcome.xhtml

This is the JSF file that will get processed.  By default the xhtml extension will get mapped to a jsf extension.  As of JSF 2.0, HTML tags and JSF tags may be used together.  What we are left with is a powerful mechanism of defining widgets coupled with the ability to customise the HTML style and tools however you like them.  In the files below we have specified and output label with some EL code.  This code will look-up the message property in our welcome managed bean and display the contents.  As you can see, it is a powerful mix of simple java and simple mark-up.

       
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">

<h:body>
    <h:outputLabel value="#{welcome.message}" />
</h:body>
</html>


Run!

As you can see there is a fair amount of setup required.  This is one downside.  The upside is that once the setup is completed, you can use it over and over again.  To make things easier, there are several Maven archetypes dedicated towards JSF that come with sane presets.

  1. Select Main.java
  2. Select Run > Run As > Java Application (or Alt + Shift + X, then J)
  3. Open your favourite browser
  4. Browse to http://127.0.0.1:8080
  5. Happy Days (I Hope)
Just for clarity, 127.0.0.1 is the loopback address.  It is your machine.  You may also use http://localhost:8080 instead.  The 8080 is required as we are using a non-standard port.

What's next?

As always, the code can be found here.  In the mean time, I will clean up the project in the next post.  We need to sort out issues such as logging moving hard carded attributes to a configuration file.


Saturday, 24 January 2015

The Wonderful World of JSF

Recently a good friend asked me where to begin with Java Server Faces (JSF).  In particular, he was interested in using embedded Jetty to run his JSF application on a Raspberry Pi.  He had downloaded a Hello World JSF applications from the Internet but could not get it working.


This is not an uncommon story.  I remember, back in the day, the confusion I faced when creating my first JSF application.  There are many considerations.  From what applications server to choose (Jetty, Tomcat, Glassfish, etc) to what framework (Vanilla, Ice Faces, Trinidad, Prime Faces) and even implementation (Glassfish, My Faces).  The waters can get even more murky considering some implementations offer features that are not application server independent.

The Moving Parts

A well designed application (web or otherwise) should be implemented in a way that it can be easily moved from one platform to another (an easy way of saying modular, encapsulation/data hiding, blah blah).  I am not talking about a port here, I am more referring to migrations from web application to FAT client to android app, etc.  A well written application should only require a change to the front end, not a change to the back-end, or if the storage mechanism is changing, then just the storage mechanism.


Our sample application will use embedded Jetty to run the application.  This means we do not require any further setup on our Raspberry Pi other than installing Java.  We will use My Faces and some other packages for our implementation.  This provides us with the core JSF classes.  Prime Faces will be used for our framework.  Prime Faces comes with some pretty fancy widgets.  We will event throw in some HTML 5 based web sockets for some fun.  The Spring Framework will be used for configuration.  Finally, I will use MapDB for the database, again because this is embedded and I haven't used it before.  Keep in mind, we will design this so it will be easy to change.

Eclipse with Maven will be used for our IDE.  JUnit 4 will be used for testing.  This blog will not cover the ins and outs of the Raspberry Pi.  My preferred development method is behaviour driven development (BDD) however I will not be showing you the BDD process here as it may be counter intuitive for some.  I will be doing testing but I will not cover it in the blog.  If you want to see the tests in action you can download the project off source forge.  Selenium will be used to test the web page.  Again, not covered.

Specifications

This friend of mine is in to fitness.  Let make a web app to suit his purpose.  We will start small and grow it out.  The intended order of implementation is to assist my friend in his work.  Some readers may notice that the block based implementation order kind of goes against BDD but that is fine, the main goal here is learning.  The features will be implemented in the following order:
  1. Basic Page
  2. Encryption
  3. Administrator Login
  4. User Login
  5. Data Entry
  6. Data Display
  7. Analytics
  8. TLS

Getting Started

Download Eclipse for Java Developers.  You will also need to download the Java JDK.  I will be using Java 7, you may also use Java 8.  If you do not like the oracle version, you can also download Open JDK.  I have heard that there may be some compatibility issues between the two.  While both of them handle some things in different ways, I have never experienced a compatibility issue yet.

Install the Java JDK.  Extract Eclipse to where ever you want and run it.  In most cases, Eclipse will detect the JDK installation, however, you can set it explicitly.  For my work spaces, I usually just set it.
  1. Go to Window > Preferences > Java > Installed JREs
  2. Click Add
  3. Select Standard VM and click  Next
  4. For Java Home, Browse to your JDK directory (C:\Program Files\Java\jdk1.7.0_60)
  5. The JRE Name field should be automatically populated.  We will change it.
  6. Set the name to jdk7 (or jdk 8 is you are using 8)
  7. Click Finish
  8. Check the check box beside our newly added VM and click OK
  We will also install some basic packages.

  1. Go to Help > Install New Software
  2. Set the Work With field to the default update site.  For Luna this is http://download.eclipse.org/eclipse/updates/4.4.
  3. Most of these will be installed.  However, under Web, XML, Java EE... select:
    • Eclipse Java EE Developer Tools
    • Eclipse Java Web Developer Tools
    • Eclipse Web Developer Tools
    • JSF Tools
    • JSF Tools - Web Page Editor
  4. Select Next, Next, Agree then Finish.
You will be prompted to restart.  I also like to install EclEmma, Find Bugs, EGit, Subclipse (choose native java implementation) and Checkstyle.  You can find them in the Eclipse Market place under the help menu.  I would also like to put in a plug for Saros.  It is an awesome way to share projects and edit them shared document live.

Now to bring some sanity to checkstyle.  The default line width is 80 characters.  This is there for hysterical (historical) reasons.  Now we have wide screens and IDEs.  I recommend you change it to 240.  This can be change under Windows > Preferences > Checkstyle.  I usually set a method of no more than 15 lines.  No more than 4 parameters to a method.  A class of no more than 150 lines.  You choose what you want.  I recommend copying another to create your own.

As a note.  Eclipse has some very powerful shortcut keys.  I will draw attention to them when I remember.

Creating the Project

In Eclipse:
  1. Select File > New > Project (Just Project, not Java Project)
  2. Select Maven > Maven Project
  3. For the basic users, the default location is okay.  The project will appear under the <user>/workspace directory.  Mine is different as I will be checking this into source forge.
  4. Click Next
  5. Maven is a tool that manages dependencies and even allows us to choose from project templates.  It is very powerful.  It can also be used outside Eclipse.  We will select the maven-archetype-quickstart option and select Next
  6. We can use PBU for present but unavailable.  SF is short for sourceforge.  We will name the project after a pun for getting fit 'ontrack'.  The source forge project name is already taken so the project will be called 'pbuontrack'.  For simplicity, we will only use one project for this.  Please note it is common to split the project up into sub-projects to keep thing separate.  Maven handles this very well.  Typically you would have a project for the web page, one for the core logic, one for storage, etc.  Set the following attributes:
    • Group Id: net.sf.pbu.ontrack
    • Artefact Id: ontrack
    • Version: 0.0.1
    • Package: net.sf.pbu.ontrack
  7. Click Finish
Give the project a minute to finish creating.  You should see the following.


We will change the java instance to the jdk7 profile we created earlier.
  1. Select the line that reads JRE System Library (J2SE-1.5).
  2. Press Alt + Enter (Same as Right-Click > Properties)
  3. Change the Execution Environment to JavaSE-1.7 (jdk7)
  4. Select OK

Looking Ahead

Now we have our basic project setup.  A zip file of the project is available at Source Forge.  Next we will look at adding dependencies to our POM so Maven can grab what we need.  After that, the fun will begin as we create an run a basic web page.

Thursday, 22 January 2015

The Lab

Every evil genius, mad scientist and all round good guy knows that if you want to get something done get a lab to do it.  The intention for this lab is to simulate a complete network on a single laptop.

Goal

Build a lab network that consists of multiple data centres, security zones, VRFs, users and services on a single laptop.

Specifications

Nothing special.  It is a laptop that has some massive overheating issues.
  • Intel i7
  • 3 GB RAM
  • WiFi Networking
  • Xubuntu 14.04.1
  • Slow Internet Connection
  • Nothing else really matters
Although it is an i7, the laptop has some MASSIVE overheating issues.  To solve this I use thermald.  It is a tool that throttles the CPU in order to reduce the heat.  By the time that has kicked in, I would be lucky if the laptop runs like a 286.  The main thing that the laptop has going for it is the modest 8GB of RAM.

You do not need much these days to build a decent lab.  Sure you won't have the same throughput as the big boys, but all the configuration can be the same.  Just to give you an idea of scale, I have successfully deployed 32 routing instance amongst two Raspberry Pis.

Overview

My goal is to create two data centres and an external network.  As is customary, the high level system will be named after a theme.  The environment shall be known as 'Mythical'.  Two Data Centres (DC) will be deployed.  The first DC is Valhalla, abbreviated Val.  The second DC is Elysium, abbreviated Ely.  Otherworld will be used as the glue for inter DC components.  Aaru is an external environment.


Services

The following services will be deployed in the Lab.  Probably in this order.  I reserve the right to use this lab for blogs posts that are not explicitly related to the Lab.
  • DNS
  • LDAP
  • RADIUS
  • SYSLOG
  • ZenOSS
  • Asterisk
  • User PCs
  • Various other fun services.  I am open to requests.

One glaring omission is NTP.  The virtualisation we will be using is very lightweight.  A single host kernel will be used by the guests.  For this reason, NTP will be pointless as time is controlled by the kernel.  If this sounds like another language to you, do not fear, it does not matter.

Network

 The key reason I am doing this is to simulate non-trivial networks.  As the world is poised on the move from conventional routing protocols to Software Defined Networks (SDN) I wish to have a conventional network that I can migrate to SDN.

The network will be configured as a hierarchy of bridges.  It may look complicated, but remember, everything is in its own little sandbox.  We only need to deal with a couple of bridges at a time.  The bridges will be named after their respective uses.  According to the diagram above, we shall have one bridge for each of Valhalla, Elysium, Otherworld and Aaru.

Final Thoughts

I will possibly update this blog post as thing progress.  This should make things easier to search.  As I mentioned, this is for play.  However, I develop a fair amount of software.  It would also be nice if this environment could eventually be used for system testing.


 


Friday, 2 January 2015

In a World of My Own

Since I can remember, people have often told me that I am in a world of my own.  It is true.  I am often so far lost in my own mind that I can walk past a good friend of twenty years and not even know it.  Fortunately, many people who know me have come to expect this of me.  They chalk it up to one of my many quirks.  Many people have also enquired as to where I go when I am not on this planet.  The answer, unfortunately, is not as exciting as it may seem.

The truth of the matter is I go to a place that most would find boring.  A place where you might find a hint of mathematics, a dash of science and blend of technology.  This is a place where many of my random interests come together and demand some spare clock cycles.  With so many things demanding my clock cycles there is not enough capacity for the real world.  Eventually priority takes over and the real world gets drowned out.

This is a blog that looks at many of the random technologies I have used.  Some I have used professionally and others I have used just for the sake of it.  If anyone finds anything useful in these posts, all the better.

I hope you enjoy.
Graeme