How can Java/Groovy connect to Oracle Always Free Autonomous Database

Thanks to Oracle’s Cloud Always Free services, now we can have a free VM and a free Oracle Autonomous Database.

Isn’t it excited? And then you want to do some thing with it.

1 constraint of the Always Free Oracle Autonomous Database is that, you need to use it.

If you don’t use the database within 7 days, Oracle will shut the database down, you need to start it manually later. And if you don’t use it for 3 months after that, Oracle deletes the whole instance.

So, how to avoid that? Or, how to auto do something in the database to keep it alive?

An easy way to do this is to insert some data into the database periodically.

We got a VM, and we got the database, so why don’t use the VM to insert some data into the database?

So here I come up with the “ping” groovy script, which get triggered by cron job, and inserts a dummy record every hour to the Database.

Here’s how it works.

Firstly, we install Java and Groovy into the database

How to install Java and Groovy in Oracle Always free Ubuntu

Secondly, we download the Oracle Wallet, and then, referring to Oracle document on how to use JDBC thin to connect to Database, we can download all the required jar files from Oracle Database 18c (18.3) JDBC Driver and UCP Downloads. (We will need ojdbc8.jar, ucp.jar, oraclepki.jar, osdt_core.jar and osdt_cert.jar)

Put the downloaded Jar file to your Groovy /lib directory
And put the wallet to an folder, unzipped it, we need the unzipped path later in the Groovy script.

Create a table using following script

create table ping(creation_date date)

Thirdly, we create the Groovy Script as following:

//Please note the wallet path is the unzipped path
//Replace DBNAME, USERNAME, PASSWORD to your own ones
url = "jdbc:oracle:thin:@DBNAME_high?TNS_ADMIN=PATH_TO_THE_Wallet/Wallet_DBNAME"
username = "USERNAME"
password = "PASSWORD"

println url

driver = "oracle.jdbc.driver.OracleDriver"

import groovy.sql.*
sql = Sql.newInstance(url,username,password,driver)
try{
  sql.execute("insert into ping(creation_date) values (sysdate)")
} finally{
  sql.close()
}

Finally, we create a Cron job in the crontab, and let Linux schedule and trigger the Groovy script every hour

 0 */1 * * * /home/ubuntu/.sdkman/candidates/groovy/current/bin/groovy /home/ubuntu/groovy/PingDB.groovy 

If everything works fine, you should see the Ping table been populated every hour.

Now you can enjoy a none turned off Free database and VM.

How to install Java and Groovy in Oracle Always free Ubuntu

Refer to following URL to install Java and Groovy in Ubuntu

https://computingforgeeks.com/how-to-install-apache-groovy-on-ubuntu-18-04-ubuntu-16-04/

How to Install Java on Ubuntu

http://groovy-lang.org/install.html
Install Java to Ubuntu
You need to upgrate your Ubuntu to the latest version
sudo apt update
sudo apt -y upgrade
sudo reboot
sudo apt-get install default-jdk
The jdk will be installed at /usr/lib/jvm

After this, we need to setup the JAVA_HOME environment parameter
go to folder /etc

change the “environment” file and add a new line of the JAVA_HOME
you need to use sudo vi environment to open the file and edit, since this folder needs root access

JAVA_HOME=/usr/lib/jvm/default-java

after saving the file, use to load the new profile and double check the setting
source environment
echo $JAVA_HOME

Install Groovy
before installing Groovy, we need to install zip and unzip tool
sudo apt-get install -y zip
sudo apt-get install -y unzip

and after this, we refer to following Groovy official document to install Groovy
http://groovy-lang.org/install.html

We need to use sudo to install the sdkman
sudo curl -s get.sdkman.io | sudo bash

we need to change user to root to execute the sdkman-init.sh
sudo -s
source “$HOME/.sdkman/bin/sdkman-init.sh”

now install groovy using root account
sudo -s
sdk install groovy
groovy -version

 

The groovy was installed under following directory
/home/ubuntu/.sdkman/candidates/groovy/current/bin

 

need to add the same to environment so it will take in the setting in PATH
/etc/environment

now we can try to deploy the groovy script in the ubuntu server and try to run them

How can Java run Groovy script dynamically 2

This is a continue with the previous blog “How can Java run Groovy script dynamically”.

We try to do some complex things inside the groovy script, this time we tried to define a data structure directly inside the script, and see if the GroovyShell can identify it at run time and run correctly

Following are the sample script that passed to the GroovyShell

The code refers to “How to use a Groovy trait to output any class as csv?”

import java.io.ByteArrayInputStream
import java.io.Reader
import java.io.InputStreamReader
import static com.xlson.groovycsv.CsvParser.parseCsv

def sb = StringBuilder.newInstance()

/*
// TEST1 - simple CSV parsing
Reader inputReader = new InputStreamReader(new ByteArrayInputStream(input));
for(line in parseCsv(inputReader,separator:",")){
  // println line
  sb.append(line.attribute1+";"+line.attribute2)
  sb.append("\n")

}

output = sb.toString().getBytes()
*/

// TEST2 - Define data structure inside the dynamic script and gets output
def m = new MeetupMember()
m.name = "Sergio del Amo"
m.locality = "Guadalajara"
m.twitter = "https://twitter.com/sdelamo"
m.facebook = null
m.tumblr = null
m.imageUrl = "http://photos4.meetupstatic.com/photos/member/c/d/6/b/member_254392587.jpeg"
m.website = "http://www.meetup.com/es-ES/Warsaw-Groovy-User-Group/members/200767921/"

output = m.asCSV().getBytes()

// Following are the data structure definition
// You can see that this part is at the end of the script
// But GroovyShell handle it correctly
trait TraitAsCSV {
// manually provide the header to make sure the csv output is in the expected sequence
   List<String> propertyNames() {
        return ["name","locality","twitter","facebook","tumblr","background","imageUrl","website"]
    }

    String csvHeaders() {
        propertyNames().join(delimiter())
    }

    String asCSV() {
        def str = ""
        def arr = []
        for(def propName in propertyNames()) {
            def v = this."$propName"
            arr <<  (v ?: "")
        }
        arr.join(delimiter())
    }

    static String delimiter() {
     ";"
    }
}
class MeetupMember implements TraitAsCSV {
    String name
    String locality
    String twitter
    String facebook
    String tumblr
    String background
    String imageUrl
    String website
}

Groovy – get file from sftp

Following code shows how to use Groovy to download a file from sftp.

It uses package “com.jcraft”

Thanks to Groovy Grab, we don’t need to manually fetch the library ourselves.


@Grab(group='com.jcraft', module='jsch', version='0.1.46')
import com.jcraft.jsch.*

java.util.Properties config = new java.util.Properties()
config.put "StrictHostKeyChecking", "no"

JSch ssh = new JSch()
Session sess = ssh.getSession "USERNAME", "HOST", PORT

sess.with{
setConfig config
setPassword "PASSWORD"
connect()
Channel chan = openChannel "sftp"
chan.connect()

ChannelSftp sftp = (ChannelSftp) chan;
sftp.get("/DOWNLOADFOLDER/DOWNLOADFILENAME","c:/LOCALFOLDER/LOCALFILENAME")
sftp.rm("/DOWNLOADFOLDER/DOWNLOADFILENAME")
chan.disconnect()
disconnect()
}

Using Groovy to do xslt transformation

Following demo code shows how we can use Groovy and xslt templates to do a simple file transform.

To transform a file, we need to do following before we can get the final result:

  1. Get a source file, here we use a simple xml as a source
  2. Define a .xls file – xslt transformation file
  3. Use Groovy to link the process and create output

Following is the code of the Groovy script that calls:

import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

// Load xslt
def xslt = new File("c:/test/product2.xsl").getText()

// create transformer
def transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt)))

// Load xml
def xml = new File("c:/test/product.xml").getText()

// Set output file
def output = new FileOutputStream("c:/test/product_output.xml")

// perform transformation
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output))

Following is the .xsl file defined:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<xsl:for-each select="//products/product[not(.=preceding::*)]">
	<li>
				<xsl:value-of select="."/></li>
</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Following is the source .xml file:


<items>
  <item>
    <products>
      <product>laptop</product>
      <product>charger</product>
    </products>
  </item>
  <item>
    <products>
      <product>laptop</product>
      <product>headphones</product>
    </products>
  </item>
</items>

After running the groovy code, you got the result as following – not a correct format xml, but it’s what the transformation file requested:

<ul>
	<li>laptop</li>
	<li>charger</li>
	<li>headphones</li>
</ul>