Select Page

Handy Database Design for Records to Track Created Date Time and Updated Date Time

A common requirement for any project is to track the created and updated date/time stamp information. Thankfully with MySQL this is a relatively straight forward task… well, at least at the pure database level.  

It is always recommended to track when your records were created and updated within all tables in your database as you can guarantee that at the point when you need it most, if you don’t have it you’ll regret not having it. Particularly when debugging problems and investigating issues.

Databases rarely live in isolation these days and there are many ways of logging the created and updated times. For example, you could within your web application actually generate the timestamp for all of your Insert and Update statements throughout your codebase and that is one perfectly good way of doing it. But let’s be honest, who has time for that. This approach comes with a  lot of overhead for basic requirements although it is often essential to go down this approach for more complex requirements. But for now, let’s keep things simple.

MySQL is extremely powerful when it comes to managing your data. The two commands we’re going to take a look at for this are;

  • DEFAULT
  • ON UPDATE

 

DETAULT Command

As you can probably imagine, this defines the default value for a column in your table. This allows you to assign a default value of your choosing based on the data type. In this case, a TIMESTAMP field which tracks the date and time. For this you can set the default to be the CURRENT_TIMESTAMP which will automatically populate your column created_date_time with the current timestamp whenever a new row is inserted.

 

ON UPDATE Command

And you can probably guess what this command does too…. It does something whenever a row of data is updated. You can only have one column per table that uses the ON UPDATE command so use it wisely. Again, you simply say what you want to do ON UPDATE, in this case you again want to store the CURRENT_TIMESTAMP against the field updated_date_time.

 

Example Alteration to a Table

Below you can see an example of how you can alter one of your database tables to have the created_date_time and updated_date_time columns with the handy MySQL features turned on so that you never have to worry about when the record was created or updated.

ALTER TABLE your_table_name ADD COLUMN created_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE your_table_name ADD COLUMN updated_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

How to Deploy a Java WAR File without Tomcat Manager using cPanel

For those core Java developers you will be very comfortable utilising the power of the Tomcat Manager to deploy your applications to your servers. But what happens when you’re working on a server that doesn’t have the usual Tomcat Manager available for you to use, just how do you go about deploying your application easily?

Thankfully, it is fairly straight forward to do, it just requires a couple more steps to get this working.

Ultimately your .war file for your Java web application is just a fancy .zip file and as such we can treat it as so. So follow the simple steps outlined below and you’ll have your Java WAR file deployed on cPanel in no time, all without using the Tomcat Manager.

  • Within your IDE, Clean and Build your project to compile and package the war file so you’ll end up with a file here: /{IDE_FOLDER}/ExampleProject/dist/ExampleProject.war
  • Rename this file to .zip
  • Login to your cPanel account and go to the File Manager
  • Navigate to /public_html
  • Create a folder called: Backup_yyyy_mm_dd (you know…. Just in case)
  • Move all of your current files into the backup folder you just created (excluding any files that aren’t part of your project i.e. .htaccess file, cgi-bin, .well-known (SSL certificates with Let’s Encrypt) and any application live data files such as images or document uploads that only sit within your live system due to user usage etc.
  • Upload your ExampleProject.zip file
  • Unzip the file
  • Done

Note, you shouldn’t need to restart Tomcat as all your new files should get automatically loaded into memory by Tomcat, but if you do notice that some files aren’t showing the latest version then you may need to restart Tomcat to ensure the files are all uploaded. To do this the handy command on the latest version of cPanel (at the time of writing!) is as follows. Simple SSH into your server or use the Terminal functionality that exists within cPanel and run the command;

                ubic restart ea-tomcat85

This will restart the running Tomcat instance for your user only. Simple!

And if all has failed and you need to restore the backup files you backed up at the start, jump into your FTP Client software such as FileZilla where you can easily move all files within your backup folder back to the parent level. Unfortunately this part isn’t out of the box functionality with cPanel, so I’d make sure you can access your server using FileZilla before you attempt this process as you may need to quickly jump back on there and fix it.

How to Install Multiple Versions of Java on Linux

You’re probably looking for information on how to install multiple versions of Java on Linux as part of a migration piece of work and/or a new deployment activity after spinning up a new Linux server which has a different version of Java running than what your Java web application was compiled in. Whatever Java version your application is compiled in needs to be the same Java version that is running on your web application server to ensure it works without issues (or even just works!).

So, let’s assume you’ve got the handy yum tool installed on your Linux box which allows you to easily run handy commands to install software. Within yum you can find lots of handy versions of java just sitting around waiting to install them. Take a look at all the Java packages within yum.

What you’ll notice is that the majority of them are utilising the OpenJDK setup. This is a handy open source version of the Java platform. So all you need to do is get it installed then get it configured. Simple.

 

Check Current Java Version Installed

Firstly you need to check the current version of Java that you have installed on your Linux box. To do this simply run the following command;

 

java -version

 

 

Install Java 1.8 Version on Linux Using Yum

Simply run the following command to install the latest version of Java (whatever the latest version is on your system);

 

sudo yum instal java-1.8.0-openjdk

 

Configure Linux to Use the New Version of Java via Alternatives

On Linux machines you can configure multiple versions of Java to run via the ‘alternatives’ feature. This essentially allows you to install multiple Java versions alongside each other without having to worry about the specifics. This allows you to have multiple versions of Java installed, and still configure a default.

Which basically means that when you run any default java command that it will run against whatever alternative has been configured to point at the different versions of Java installed on your Linux box.

To check this, simply run the command;

 

sudo alternatives --config java

 

Which will allow you to configure which is the default Java version.

And it’s as simple as that. Install as many versions of Java as you need, but you can only use one of them at once.

If you have multiple applications running which all require different versions of Java then you have a couple of options. Either upgrade everything so they are all running the latest version of Java so you can keep everything on a single server. Or move applications to their own servers to give yourself the maximum control.

How to Configure Apache as a Reverse Proxy for Apache Tomcat

So you’re running an Apache Tomcat environment on Linux but since this is a public facing web application, you want to get rid of the port 80 business so that your users don’t have to go remembering what port the application is running on. After all, port 8080 or 8084 isn’t exactly easy to remember for the average non-technical user. And god forbid you’ve decided to change this to something non-default, trying to remember that is going to be even more fun!

Thankfully it’s fairly straight forward to use Apache as a Reverse Proxy that sits in front of Apache Tomcat so that you can serve requests over port 80. You could configure Apache Tomcat to run on port 80, but if you’re running the Apache Tomcat Manager GUI then this restricts you to running your web applications only in sub-directories – and let’s be honest – no-one wants that. Everyone wants to be running their web application at the root of their domain, i.e. www.example.com rather than www.example.com/my-web-application/.

 

Configure an Apache Virtual Host File

Simply SSH into your server and run the command below;

 

sudo nano /etc/httpd/conf.d/httpd.conf

 

It’s worth nothing to double check the other files that currently exist in that directory before configuring this. Sometimes you will have slightly differently named files and/or this file may already exist with current configurations. Don’t just go adding configurations to this fil unless you’re sure what you are doing. At least take a backup of the current file(s) so you can revert your changes if you mess things up.

Thankfully on many vanilla installations of Apache this file will not exist, so the nano command above will create the file so you can edit it straight away.

To keep things simple all you need to do is add the following lines of code to that file;

 

<VirtualHost www.example.com:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

 

Now run the command to restart Apache to ensure the changes take effect.

 

sudo service httpd restart

 

Now you’ll notice that the above doesn’t actually change anything. It just means the Apache Tomcat Manager GUI now loads over port 80 rather than port 8080. That isn’t quite what we want, so we need to configure the Virtual Hosts file to suit your application needs.

Let’s say your application is called MyWebApplication, which sits at www.example.com:8080/MyWebApplicaion on Apache Tomcat. You need to ensure your Virtual Hosts file looks as follows so that the application routes successfully;

 

<VirtualHost www.example.com:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/MyWebApplication
    ProxyPassReverse / http://127.0.0.1:8080/ MyWebApplication
</VirtualHost>

 

Simple. Now you’re done.

This is how you configure Apache as a Reverse Proxy for Apache Tomcat. This approach also allows you to run multiple Java web applications from a single Apache Tomcat instance if that suits your needs from a practical, management and security perspective. Alternatively, run everything from separate setups if you need something more segmented.  

How to Convert an SVG to a PNG in Java

Ok, this one is a little tricky as there isn’t a lot of authoritative and clear information online about how to do this, so I thought it would help to clarify how to do this as it’s a problem I just came across myself and spent a fair amount of time to get this working. Thankfully, this blog post should allow you to just read through it and be able to start converting SVG image to a PNG image in no time.

 

SVG to PNG Libraries

Firstly let’s look at the core libraries you need to do this. Thankfully there is just the one which is the Apache Batik library which is part of the Apache XML Graphics Project. In summary, Apache Batik is a Java-based toolkit for applications or applets that want to use images in the Scalable Vector Graphics (SVG) format for various purposes, such as display, generation or manipulation.

Now, this is where things start to get a little more challenging. I would recommend downloading the Bin ZIP file from the download link just mentioned. If you take a look around the Maven Repository you’ll notice that there are a lot of different versions which look all very similar, and all of them are generally last updated around 10 years ago. Sure, these things of tools rarely change once built, but this always worries me.

Once you’ve extracted the ZIP, you’re looking for the file titled batik-all-1.12.jar (Note, the version number may differ when you are reading this…) which will be in the folder, /batik-bin-1.12/batik-1.12/lib/batik-all-1.12.jar.

Add that JAR file to your project.

Now you would think that the project developers would have included all of the dependencies already bundled up. But no. And the documentation isn’t clear about all the dependencies which makes life even more difficult.

 

Dependencies

So, you need a few dependencies to get this up and running.

 

XML Commons from the Apache Xerces project

Download XML Commons and add the two JAR files to your project called, xml-apis.jar and xml-apis-ext.jar.

 

XML Graphics Commons from the Apache XML Graphics Project

Yes, this is the same project as where Batik lives, but hey. Download XML Graphics Commons and add the xmlgraphics-commons-2.4.jar file to your project.   

 

Java Code to Convert an SVG into a PNG

Ok, so now we’ve got all the libraries and dependencies added to your project, it’s now time to actually write the code to convert an SVG to a PNG. Use this code as a starting point to get you going;

 

try {

            // Use this to Read a Local Path
            // String svgUriImputLocation = Paths.get("https://www.contradodigital.com/logo.svg").toUri().toURL().toString();
            // Read Remote Location for SVG
            String svgUriImputLocation = "https:// www.contradodigital.com/logo.svg";
            TranscoderInput transcoderInput = new TranscoderInput(svgUriImputLocation);

            // Define OutputStream Location
            OutputStream outputStream = new FileOutputStream("C:\\Users\\{{Your User Name on Windows}}\\Documents\\logoAsPngFile.png");
            TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream);

            // Convert SVG to PNG and Save to File System
            PNGTranscoder pngTranscoder = new PNGTranscoder();
            pngTranscoder.transcode(transcoderInput, transcoderOutput);

            // Clean Up
            outputStream.flush();
            outputStream.close();

        } catch (IOException | TranscoderException ex) {
            System.out.println("Exception Thrown: " + ex);
        }

 

Hope that helps to get you going in the right direction if you’re working on a similar problem to this.