Friday, July 21, 2017

Serializing Java Objects with JAXB

Java Model

The model class Team


import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

 * As the class Team is annotated by XmlRootElement, JAXB transforms a Team 
 * instance into an XML document whose root element is <team> 
 * The @XmlAccessorType(XmlAccessType.FIELD) annotation means that every non
 * static, non transient field in the class will be automatically bound to XML
public class Team {

  private String name;
  private String description;
  @XmlElementWrapper(name = "developers")
  private List<Developer> developer;

  public Team(String name, String description) { = name;
    this.description = description;
    this.developer = new ArrayList<Developer>();

  // default constructor required by JAXB
  public Team() {
    this("team name", "team descr");

  public String getName() {
    return name;
  public void setName(String name) { = name;
  public String getDescription() {
    return description;
  public void setDescription(String description) {
    this.description = description;
  public List<Developer> getDevelopers() {
    return developer;
  public void setDevelopers(List<Developer> developers) {
    this.developer = developers;
  public void addDeveloper(Developer dev) {

Tuesday, June 6, 2017

Web Services Introduction

Web Service definition

  • A web service is a distributed software whose components can be deployed and executed on distinct devices.
  • A web service consists of a service (a.k.a. producer) and a client (a.k.a. consumer or requester).

Web service terminology

  • message: the basic unit of communication between a Web service and a requester; data to be communicated to or from a Web service as a single logical transmission.
  • operation: a description of an action supported by the service; a set of messages related to a single Web service action.
  • synchronous: an interaction is said to be synchronous when the participating agents must be available to receive and process the associated messages from the time the interaction is initiated until all messages are actually received or some failure condition is determined.
  • asynchronous: an interaction is said to be asynchronous when the associated messages are chronologically and procedurally decoupled.

Web Service over HTTP

  • A web service is an application typically running over HTTP.
  • A web service over HTTP is a web service that uses the HTTP as transport, HTTP messages are infrastructure.

Web Service architecture

  • The architecture of a simple web service is a client and a server.
  • The architecture of a complicated web service have many clients and a service composed of other services.
    • for example, an e-commerce service can be composed of multiple code components (each hosted on a separate web server) and any combination of PCs, tablets, mobile phones and other networked devices may host programs that make requests to the service.

Web services come in two flavors: SAOP-based and REST-style.

  • SOAP is an XML dialect that specifies the structure of XML documents that count as SOAP envelopes
  • A REST-style service is one that treats HTTP not only as a transport infrastructure but also as a set of guidelines for making service requests and responses.

Web service features

  • Web service major attractiveness is interoperability: clients and services can interact despite differences in programming languages, operating systems and hardware platforms.
  • XML and JSON are web service data interchange formats that provide an intermediary level and handle the differences in data types between different programming languages.

Tuesday, May 9, 2017

How to validate an xml document against a schema

STEP 1: Create the XML

To create a new XML file, start eclipse IDE and choose File->New->Other->XML file->Create XML file from XML template.
Model the data of interest in the XML file. In my case, I have created the following XML.

The teams.xml file

<?xml version="1.0" encoding="UTF-8"?>
 <name>the great team</name>
 <description>these team was recruited by google to develop a secret product</description>
  <developer role="frontend">
   <name>John Carrot</name>
  <developer role="backend">
   <name>Joshua Allock</name>
  <developer role="frontend">
   <name>Brendan Tich</name>

Tuesday, March 14, 2017

XML Schema Primer

1 Introduction

Basic Concepts: The Purchase Order (§2) covers the basic mechanisms of XML Schema. It describes how to declare the elements and attributes that appear in XML documents, the distinctions between simple and complex types, defining complex types, the use of simple types for element and attribute values, schema annotation, a simple mechanism for re-using element and attribute definitions, and nil values.

Advanced Concepts I: Namespaces, Schemas & Qualification (§3), explains the basics of how namespaces are used in XML and schema documents.

2 Basic Concepts: The Purchase Order

The purpose of XML schemas

  • a schema defines a class of XML documents (is a description of an XML document)
  • an instance document is an XML document that conforms to a particular schema

The instance document, the po.xml file, describes a purchase order that may be generated by a product ordering application.

The Purchase Order, po.xml
<?xml version="1.0"?>
<purchaseOrder orderDate="1999-10-20">
   <shipTo country="US">
      <name>Alice Smith</name>
      <street>123 Maple Street</street>
      <city>Mill Valley</city>
   <billTo country="US">
      <name>Robert Smith</name>
      <street>8 Oak Avenue</street>
      <city>Old Town</city>
   <comment>Hurry, my lawn is going wild!</comment>
      <item partNum="872-AA">
         <comment>Confirm this is electric</comment>
      <item partNum="926-AA">
         <productName>Baby Monitor</productName>

The purchase order's elements have simple types or complex types

  • the purchase order consists of a main element purchaseOrder and the subelements {shipTo, billTo, comment, items}.
  • subelements can contain other subelements or data
  • elements that contain subelements or carry attributes are said to have complex types
  • elements that contain numbers, strings, dates and no subelements are said to have simple types, attributes always have simple types

Where to find the definitions of the types in the instance document

  • the complex types in the instance document are defined in the schema for purchase orders
  • the simple types in the instance document are defined either in the schema for purchase orders or are part of the built-in simple types of XML Schema

What is the association between the instance document and the the purchase order schema?

  • an instance document does not need to refer to a schema
  • the purchase order does not reference the purchase order schema

Saturday, October 8, 2016

How to run maven

mvn command

The syntax for running Maven is as follows:

mvn [options] [<goal(s)>] [<phase(s)>]

The order of execution depends on the order in which the goals and the build phases are invoked.

Running maven phases

Example: build a Maven project with the package phase of the default life cycle

mvn package

Example: install in local repository

mvn clean install

Above you see the most common build invocation for a Maven project. The command mvn clean install creates the package and install it in the local repository for re-use from other projects. This command executes each default life cycle phase prior to install, in order, before executing install.

Example: deploy to shared repository

mvn clean deploy

In a build environment, the call mvn clean deploy clean, build and deploy artifacts into the shared repository. When the command is used in a project with one or more sub-projects, Maven traverses every subproject to clean, build and then executes deploy.

Running maven goals

A build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute. If it has one or more goals bound to it, it will execute all those goals

Example: call generate goal with archetype plugin

mvn archetype:generate

Example: call check goal with checkstyle plugin

mvn checkstyle:check

Example: call copy-dependencies goal before the package phase

mvn clean dependency:copy-dependencies package

The clean and package arguments are build phases, while the dependency:copy-dependencies is a goal of a plugin.
The clean phase is executed first (comprising of all preceding phases of the clean lifecycle), then the dependency:copy-dependencies goal, finally is executed the package phase (and all its preceding build phases).

Introduction to maven

What is Maven ?

Maven is a project management tool, based on the concept of a project object model (POM).

Maven was created to solve the lack of standards in Ant.

Maven’s Objectives

Maven’s primary areas of concern are:

  • Providing a uniform build system through a project object model and plugins
  • Providing quality project information from the POM and from the project’s sources
  • Providing guidelines for best practices in software development
    • The maven normal build cycle includes the execution of unit tests.
    • Maven suggests how to layout the structure of the directories of a project.
    • Maven assists in project workflow such as release management and issue tracking.

Introduction to the Build Lifecycle

Maven is based on the concept of a build lifecycle. A lifecycle is a well defined process for building and distributing a project.

There are three built-in build lifecycles:

  • the default lifecycle handles the project build and distribution
  • the clean lifecycle handles project cleaning
  • the site lifecycle handles the creation of documentation of the project
A Build Lifecycle is Made Up of Phases

Each build lifecycles is defined as a list of build phases, where a build phase represents a step in the lifecycle. For example, the main phases of the default lifecycle are:

validate - validate the project is correct and all necessary information is available
compile - compile the source code of the project
test - test the compiled source code using a suitable unit testing framework.
package - package the compiled code in distributable format, such as a .jar file.
verify - run any checks on results of integration tests to ensure quality criteria are met
install - install the package into the local repository, for use as a dependency in other projects locally
deploy - copy the final package to the remote repository for sharing with other developers and projects.

The lifecycle phases are executed sequentially to complete the default lifecycle.

A Build Phase is Made Up of Plugin Goals

The manner in which a build phase carries out its responsibilities may vary and depends on the plugin goals bound to that build phase. Each phase has zero or more plugin goals bound to it.

  • A goal represents a specific task which contributes to the building and managing of a project.
    • examples of goals include the compile goal in the Compiler plugin or the test goal of the Surefire plugin
  • A plugin is a collection of one or more goals.
    • example of core plugins are: the Jar plugin, the Compiler plugin, the Surefire plugin
    • example of specialized plugins are: the Hibernate3 plugin

Wednesday, September 7, 2016

How To Install Tomcat In OS X

Tomcat Installation

Download the latest binary distribution of tomcat, in my case apache-tomcat-8.0.37.tar.gz, from apache

Extract the file from the tar archive and move the tomcat folder to /opt:

cd Downloads/
tar -xvf apache-tomcat-8.0.36.tar.gz 
mv apache-tomcat-8.0.36 /opt

Set CATALIA_HOME and PATH Environment Variables

$ nano ~/.bash_profile

# Set Catalina Home
export CATALINA_HOME=/opt/apache-tomcat-8.0.36
# Export Catalina/bin to path

Common Tomcat Commands

To start tomcat, open a shell Terminal window in any directory and issue the command:


Test Tomcat installation by pointing your browser to: http://localhost:8080

To stop tomcat, type on the shell:


To read the log file:

cd /opt/apache-tomcat-8.0.36/logs
tail -f catalina.out

Manager Web Application

The Manager App is an application that comes with Tomcat and allows you to start, stop, reload and undeploy web applications installed in Tomcat

To allow the access to the Manager App add a manager user to the tomcat users configuration file.

Edit the tomcat-users.xml file:

$ nano /opt/apache-tomcat-8.0.36/conf/tomcat-users.xml

add the following two lines:

<role rolename="manager-gui"/>
<user username="manager" password="password" roles="manager-gui"/>