ConciseXMLParser Java Library

I was happy to have my first opportunity at delivering an open-source project for the use of everyone. This project is available here and has been uploaded into Sonatype’s Central Maven Repository.

Isn’t there already XML Parsers for Java?

It may seem weird as there are already many XML parser out there for Java. Actually I was using XML in an odd scenario and could not find a solution suitable to my needs after a few hours of searching.
I noticed that many libraries that were available were verbose and I wanted to minimize the number of lines required to load the XML file content. I also needed it to be able to accommodate XML that is not intended to represent structured data (objects with the same attributes) but rather I needed it to be able to handle the unstructured data that can be found in an application configuration file.
Oddly, after completing the library I did read about a Java Library called XPath and also read that the same solution was not geared toward performance consideration.

Why not write a Java properties file?

One of the considerations I had is if I needed to move the config in the future I would rather have it as an XML as parsers would be available for many programming languages.
Another is that the properties file can become really difficult to read and maintain if your intention is to logically nest objects within others. Imagine having to read keys like:

cows.cow.bettie.length

Now try nesting a few more times to see the point.

What is it about?

The project basically is geared towards reading an entire XML file and providing it as key->value pairs in a HashMap. In order to accomplish this, a few assumptions had to be made in order for the library to work effectively.
The readme in GitHub explains what these assumptions are as well as the rules used for generating the keys for each value.

Certbot Quickguide

Creating Certificate

certbot certonly  -w /var/www/example/ -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net

Renewing Certificates

Certificates generated using certbot last 90 days. Following command attempts to renew all certificates managed by certbot.

certbot renew

Following command does a test renewal (ie. doesn’t keep new certificates and old one still works).

certbot renew –dry-run

Revoking Certificate

certbot revoke -d abc.example.com –cert-path /etc/letsencrypt/live/example.com/cert.pem

References

https://certbot.eff.org/#ubuntuxenial-nginx

NGINX fails to ask for PEM password on start

Build

  • Ubuntu 16
  • nginx version: nginx/1.10.0 (Ubuntu)

Problem
Amongst the common commands you will find online to start Nginx are:

Using a service
sudo systemctl restart nginx

Call the binary directly (assuming it located in /etc/init.d/) with start/stop/restart
/etc/init.d/nginx start

Even if you specify the configuration file using the -c option, it always throws an error.

However, it appears (for me) that neither of these options prompt a user for their PEM password if they have configured SSL or TLS certificates.

Solution
After a lot of digging you will find that calling the script directly without (start/stop/restart) prompts the user for the PEM password.
/etc/init.d/nginx

References
https://forum.nginx.org/read.php?2,262900,262900

Autosize Images in Android

Problem
Images were displaying in a way that was not desired, by default. Many times we want images to display with the same width and height while maintaining the aspect ratio of the image. Typically we also want to be able to see our images have consistent widths and heights, especially when working with tiles or lists.

Solution
I paste the basic layout of one of my adapters to illustrate.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/row_parent"
    android:orientation="horizontal"
    android:padding="10dp"
    android:weightSum="4">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:id="@+id/img_profile"
            android:adjustViewBounds="true"/>
    </FrameLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:id="@+id/container_profile_summary">

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lbl_name" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lbl_occupation" />
    </LinearLayout>

</LinearLayout>
android:scaleType="fitCenter"

This will apply ScaleType.FIT_CENTER which as the documentation states

Scale the image using CENTER.

CENTER as the documentation states will

Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.

android:adjustViewBounds="true"

This attribute is important and required. Android says about this attribute

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

I had issues making the ImageView work when I’m applying the scaling and using weights, so I put it as a child of a FrameLayout and found it to work successfully.

References

Using Eclipse EGit Plugin to Push Changes to Remote

Introduction

This guide was intended towards my ILP group working on adding features to an old java game made previously.

Assumptions

This guide assumes you have already cloned and made changes to indexed files in the project.

I am also assuming that there is only one branch, namely the master branch.

Step 1

Right Click on your project in the package explorer > Team > Commit…

Step 1

Step 2

Add the files you would like to apply to the remote repository to the staged changes section, shown below.

Right Click on the File > Add to Index

egit-2

Step 3

Enter a commit message.

Enter your author (if necessary).

Click “Commit”

egit-3

Step 4

At this point, if you have other members working on the same branch, you will want to retrieve updates that happened between the time you started making changes in the project and the time you committed those changes.

egit-4

Step 5

You may or may not have to deal with conflicts from other commits. I have not come across this situation yet so I can’t show an example here at this time.

Step 6

After handling conflicts. You want to push your changes to the remote Git repository.

Right Cick on Project > Team > Remote > Push…

NOTE: This is NOT the same as Team > Push Branch ‘master’ or Push to Upstream

egit-5

 

Glassfish Error – java.lang.ClassCastException: org.glassfish.grizzly.config.ContextRootInfo cannot be cast to org.apache.catalina.Context

This error appeared for me because I set a default web module previously to a different enterprise application archive (ear) project while trying to run a new one in the development environment.

The solution for me was to empty the Default Web Module field shown below and restart the server. After you do that you can deploy your application successfully.

Glassfish Configuration

Create New MySQL User and set Privileges

Introduction

This guide will show you how to make a new user and assign them all privileges to a specified database.

This is not a unique tutorial but is mainly intended as a quick reference for the author. Hope you find it useful though!

Steps

Create new database user along with their password. This will apply to all databases they access.

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

Give user all privileges to a particular database

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

Necessary to apply the changes.

FLUSH PRIVILEGES;

References

https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql

Redirect from HTTP to HTTPS on Glassfish

Introduction
This tutorial is intended to show you how you can force various url patterns to use HTTPS if a user tries to use HTTP.

Build

  • Glassfish 4.1
  • Netbeans 8.1 (Not necessary, will have instruction for both)
  • JavaEE 7 (Don’t think its necessary)

Instructions
Navigate to your Web Application Archive’s (WAR’s) web.xml file.

Method 1: Netbeans

NB Step 1

NB Step 2

Method 2: Good ol’ Copy n’ Paste

The above steps would have created the following code which you would add into your file. I apologize it would not save the spaces.

<web-app>

<security-constraint>
<display-name>Constraint1</display-name>
<web-resource-collection>
<web-resource-name>Anything</web-resource-name>
<description>This will make the constraint affect all pages of the application</description>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<description/>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>

</web-app>

References

Set context-root of Application running on Glassfish as the Domain Base

Introduction

Whether you want to set your ear, war or whatever type of application up on Glassfish, we typically want to get rid of the default landing page and replace it with one of the apps. This guide will show you how, with one image. This method is the easiest but has a caveat which I will mention later. It should work in Glassfish 3 and beyond, at the least.

Build

  • Glassfish 4.1
  • Using an Enterprise Application Archive (EAR)

Assumptions

  • Name of configuration and virtual server are default

Instructions

  1. Deploy your application
  2. Navigate to Configurations > server-config > Virtual Servers > server
  3. Select desired application on Default Web Module
  4. Save

Instruction Image

Caveat
If you specified in the configurations of your file a different context root (file depends on what type of application) then you will be able to access the application from the Domain Base as well as the context root defined in the application’s configuration file.

Setup Glassfish JDBC Resource using Admin Console for Oracle Database

Introduction
This is meant to be a reference for me anytime I use oracle with a JavaEE project. I hope it benefits you as well. This is pretty much remade from the reference at the end, so its easy for me to find at a later time.

Warning
In the default Glassfish 4.1.1 shipped with Netbeans 8.1 there is a problem with the admin console when trying to do certain things. Amongst them is adding JDBC Resource. I decided I would use Glassfish 4.1 to solve that problem but you are welcome to find a guide on how to setup a JDBC Resource in command line instead as an alternative. It’s a problem with that Glassfish version, not Netbeans.

My Build

  • Glassfish 4.1
  • Oracle Database 11g XE

Instructions

First off, we need to make a connection pool. I already made mine, its called Oracle11g so don’t be startled if it doesn’t appear.

Navigate to Resources > JDBC > JDBC Connection Pools

Step 1

 

Set the values as below, to use oracle.jdbc.pool.OracleDataSource ensure you have ojdbc_6.jar (or whatever version is appropriate for you).

Step 2

 

Then set the additional properties values. I cut out my password and username in the diagram, fill it with your own values. Click save after completing. 1521 is the default port for oracle db 11g, if yours differs change it accordingly.

Step 3

 

Next we need to create a JDBC Resource.

Navigate to Resources > JDBC > JDBC Resources

Step 4

 

Fill in the values as below. The pool name should be that which you set when you made a new Connection Pool a few steps ago.ora-jdbc-con-pool-5

The following step ONLY applies if you are using JavaEE. This is to ensure that your jdbc driver behaves in a way which is compliant with JavaEE.

IF YOU DO NOT NEED THIS STEP then you need to restart the domain on glassfish to apply. With the default domain this can be done as

/path_to_glassfish/bin/asadmin restart-domain domain1

Navigate to Configurations > server-config > JVM Settings

Step 6

In the JVM Options tab, add a new option then save.
-Doracle.jdbc.J2EE13Compliant=true

Step 7

Don’t forget to restart your domain.

/path_to_glassfish/bin/asadmin restart-domain domain1

References

  • https://blogs.oracle.com/dana/entry/jdbc_connection_pools_in_glassfish