Automatically Creating Java Architecture for XML Binding (JAXB) Classes

In a previous post, I wrote about the basics of marshalling and unmarshalling XML documents using JAXB. I also outlined the creation of Java POJOs using JAXB annotations within a class. As XML becomes more complex, you may wish to automatically generate your POJOs. Since I am primarily an Eclipse user, I will outline how to do this using an XML specification document (.xsd file) and Eclipse.

The process for creating the POJOs via .xsd is really quite simple. For the example I have created the following .xsd file. The XML would have a person root element with a single version attribute. The person element would have three child elements: firstname, lastname, and middlename. Using the minOccurs and maxOccurs attributes you can see firstname and lastname are required, but middlename is optional.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="" targetNamespace="" xmlns:tns="" elementFormDefault="qualified">
<!-- Attributes -->
<attribute name="version" type="string" />
<!-- Elements -->
<element name="firstname" type="string"/>
<element name="lastname" type="string" />
<element name="middlename" type="string" />
<!-- Root -->
<element name="person">
<element ref="tns:firstname" minOccurs="1" maxOccurs="1" />
<element ref="tns:lastname" minOccurs="1" maxOccurs="1" />
<element ref="tns:middlename" minOccurs="0" maxOccurs="1" />
<attribute ref="tns:version" />

Within you project you simply right click the .xsd file and select Generate and then JAXB Classes…

Menu Selection for Generating JAXB Classes

After making the menu selection you will be prompted to specify the project for the new classes. As shown below, I selected my jaxb_experiment project and then selected Next.

After selecting the project, you must then select the package you want your class files to be created in. You select the package Browse button and make your selection. After this is complete, select Finish.

Once you select Finish, the class generation will begin. In the package you selected you will get the classes which can be used for representing your XML file. You will also get an ObjectFactory which can be used to programatically create JAXBElement objects. The output of the generation should look similar to the following.

parsing a schema...
compiling a schema...

After this process is complete, you should now be able to unmarshal and marshall XML files as I discussed in my article Java Architecture for XML Binding.

Java Architecture for XML Binding (JAXB)

Even though JAXB is not a new technology, it is still useful when application information is contained within XML files. Starting with Java 1.6, JAXB was included with the Java Runtime Environment (JRE), so no additional libraries are required for its use if you are using newer Java versions. The benefit of using JAXB is directly converting XML into Java objects which can be used to obtain the information from the file. Conversely, you can create Java objects and write them to disk as XML. In JAXB, this is referred to as unmarshalling (reading XML) and marshalling (writing XML).

<?xml version="1.0" encoding="UTF-8"?>
<configuration version="1.0">
	<step number="1" value="One" />
	<step number="2" value="Two" />
	<step number="3" value="Three" />

The example above is a simple XML file which could be used for holding configuration information or workflow data. JAXB can be used to read this file into Plain Old Java Objects (POJO), but it does require a small amount of work up front creating the POJOs. There are automated ways to create POJOs from XML schema files, but in this article I am going to discuss manual creation.

From the example, you can quickly see there are two POJOs needed for this XML file. The first POJO would be for the Configuration element and the second for the Step element. Essentially, each element in the XML document is represented by a POJO. The example below shows the POJO for the configuration element.

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Configuration {
	private String version;
	private List<Step> steps;
	public String getVersion() {
		return version;
	public void setVersion(String version) {
		this.version = version;
	public List<Step> getSteps() {
		return steps;
	public void setSteps(List<Step> steps) {
		this.steps = steps;

First, you must define the class which will represent the element. Next, each sub element or attribute should be added as a field. If elements are contained in multiple you define them as a list. Once your fields are created, you then create getters and setters for each field. Finally, you annotate the getters with the appropriate JAXB annotation. The annotations are the key pieces of the POJO.

On the class declaration, the @XmlRootElement annotation is used. The name parameter allows you to define the element name corresponding to your XML. This indicates the POJO shown will represent the configuration element. Attributes of an element are annotated using @XmlAttribute. Again, the name parameter is used to define the value of the attribute corresponding to the XML. Sub elements are annotated with @XmlElement and the same name parameter. For sub elements, additional POJOs are required. I previously mentioned this XML example would require two POJOs.

After creating POJOs, unmarshalling the XML into Java objects is pretty straight forward.

JAXBContext context = JAXBContext.newInstance(Configuration.class);
Unmarshaller m = context.createUnmarshaller();
Configuration c = (Configuration) m.unmarshal(new FileInputStream(new File("configuration.xml")));

From the code snippet above, you can see you first create the JAXBContext object and tell JAXB what the root POJO is expected to be. In our case it is the Configuration object. You then create an Unmarshaller from the previously created JAXBContext. Finally, using the unmarshaller, you unmarshall an InputStream casting the result to the expected object.

Marshalling POJOs to an XML file is just as easy. You once again create a JAXBContext and from that context you create a Marshaller. Then using the marshaller, you marshal the object to an OutputStream. In the example below, the object was marshalled to a FileOutputStream.

JAXBContext context = JAXBContext.newInstance(Configuration.class);
Marshaller mout = context.createMarshaller();
mout.marshal(configuration, new FileOutputStream(outputFile));

This article covered the basic example of creating annotated POJOs, reading XML into Java POJOs, and also writing a POJO to file. There are other uses of JAXB. One for example, is the validation of the XML content. You can also create POJOs automatically from XML specification documents. I will explore each of these topics in future articles.




Adding SSL Certificate to Apache Web Server (CentOS)

A SSL certificate is required to allow secure communication between the web browser and web server. A self signed certificate can be used. However, users will have to accept the security exception when informed of the unsigned certificate.

An alternate method would be to obtain a commercially signed certificate. The following steps outline how you can add a commercial SSL certificate to an Apache web server using the CentOS operating system.

Step One: Before you can proceed with installing a SSL you must ensure MOD SSL is installed. You can verify this by executing the following command:

 yum list installed | grep mod

If it is not installed run the following installation command:

yum install mod_ssl

Step Two: Now you can generate the new certificate. You must first create the directory for the new certificate.

mkdir /etc/ssl

Then create the certificate with OpenSSL:

openssl req -new -newkey rsa:2048 -nodes -sha256 -days 365 -keyout /etc/pki/tls/private/ -out
  • -nodes: instructs OpenSSL to create a certificate that does not require a passphrase. If this option is excluded, you will be required to enter the the passphrase in the console each time the application using it is restarted.
  • -days: determines the length of time in days that the certificate is being issued for. In this case 365 was used because a one year SSL certificate verification from a commercial certificate authority (CA) was purchased.
  • -rsa: allows you to specify the size of the RSA key. Here 2048 bits was chosen because it is the recommended minimum size.
  • -sha256: ensures that the certificate request is generated using 256-bit SHA (Secure Hash Algorithm).

You will need to use the generated CSR file for requesting your signed certificate.

Step Three: The third step is to request your signed certificate. To obtain the SSL you must first decide what type of SSL Certificate you need. For a single domain in which you just want a secure connection a standard SSL certificate is acceptable. You can purchase the standard SSL for as little as $7.99/yr from Starfield Technologies.

After purchasing your SSL you will need to follow their instructions for setting up the certificate. The setup will require the CSR previously generated.

Step Four: Once you have obtained your signed certificate you will need  to install your certificate on the web server. Create a ssl directory in the website root directory:

mkdir /path/to/root/ssl

Now copy the provided certificate and chain file to this directory. You will next need to modify the ssl.conf file.

vi /etc/httpd/conf.d/ssl.conf
  • Change the Virtual host to:
<VirtualHost *:443>
  • Add the server certificate path:
SSLCertificateFile /path/to/certificate/somecert.crt
  • Add the server private key:
SSLCertificateKeyFile /path/to/key/some.key
  • Update the chain file:
SSLCertificateChainFile /path/to/chainfile/some.crt
  • Modify the .conf file for your web site by adding the following:
<VirtualHost *:443>
 ServerName <your_server_name>
 ServerAdmin none
 DocumentRoot <your_server_root>
 ErrorLog <Path_to_logs>

 # SSL Engine Switch:
 # Enable/Disable SSL for this virtual host.
 SSLEngine on

 SSLCertificateFile <path_to_cert>
 SSLCertificateKeyFile <path_to_key>
 SSLCertificateChainFile <path_to_cert>
  • To force your users to https you can add a redirect to the port 80 VirtualHost tag:
Redirect / https://<web_site>/

Once these steps are completed you will need to restart Apache:

systemctl restart httpd

Now you should be able to access the web site and see the secure symbol provided by your browser.

Artifactory: A Software Artifact Repository

An issue with the software development process is providing the rapidly evolving project to the end user. I have discussed Jenkins for automatic builds, but there is still a need to provide the automatically generated artifacts to the user. Artifacts can include the executable, configuration files, and even source code. Artifactory, by JFrog, is one possible tool for the job.

JFrog provides different versions of Artifactory. There is an open source version and three different versions which require a license. The open source version should be able to accommodate most developer’s needs.


CentOS Installation

Installation on CentOS is very straight forward. The Artifactory web site actually has very good instructions for doing so. You can perform an RPM installation using yum by following the commands directly from their site shown below.

wget -O bintray-jfrog-artifactory-rpms.repo
sudo mv bintray-jfrog-artifactory-rpms.repo /etc/yum.repos.d/
yum install jfrog-artifactory

Deploying Artifacts Manually

Manual deployment is a very simple process. Simply choose the repository you wish to deploy to and click deploy in the upper right corner. This will open a deployment dialog.

Deployment Dialog

Next you will drag or select the file you wish to upload. Then you will complete the target path. Artifactory will try and assist you, but it may not get the path correct. The repository layout which is shown is the pattern you have configured for the selected repository. Complete the path per that layout. For example a file named found at and version 3.2 would have a path such as com/yahoo/foo/3.2/ The last step is clicking the deploy button.

Obtaining Ivy Dependency Elements

Obtaining the Ivy dependency element for your Ivy dependency file is very simple. Select the file you would like to have as a dependency and then select the Ivy tab in the dependency declaration section. There is a copy button you may select or you can highlight the dependency. Copy this element to your project’s ivy.xml file and the dependency will then be included.

Ivy Dependency