Shaaf's blog

A technical blog about Java, Kubernetes and things that matter

Calling wsadmin scripts from ant

You can simply add the following to a target. For the following wsadmin should be in your PATH env.

< exec dir="." executable="wsadmin.bat" logError="true" failonerror="true" output="wsconfig.out" >
	< arg line="-lang jython -f ../../createQFactory.py"/ >
< /exec >

All output will be logged to wsconfig.out


Generate XML - DBMS_XMLGEN

On my way to my solution store just found this nice to use, old and easy feature. Possibilities endless, usage typically very easy.

I used the following to generate XML from sqlplus:

select dbms_xmlgen.getxml('select * from user') from dual;

Output: < ROWSET > < ROW > < TNAME >Employee< / TNAME > < TABTYPE > TABLE < / TABTYPE > < / ROW > < / ROWSET >


Command, Singleton, JMenuItem, JButton, AbstractButton - One Listener for the app

Here I would like to demonstrate a simple use of JMenuItems being used with Single Listener for the entire system. A simple sample of use would probably be SingleInstance Desktop Application.

Lets see how that is done here.

  1. First lets create a OneListener class that should be able to listen to ActionEvents and also be able to add Commands to itself. Please refer to my previous post on Command,Singleton if you would like to see more about this patterns and there usage.
	package com.shaafshah.jmenus;

	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.util.ArrayList;
	import javax.swing.AbstractButton;

	// Implements the ActionListener and is a Singleton also.

	public class OneListener implements ActionListener{

		private static OneListener oneListener = null;

		// Holds the list of all commands registered to this listener
		private ArrayList<Command> commandList = null;

		// A private constructor
		private OneListener(){
			commandList = new ArrayList<Command>();
		}

		// Ensuring one instance.
		public static OneListener getInstance(){
			if(oneListener != null)
				return oneListener;
			else return oneListener = new OneListener();
		}

		// Add the command and add myself as the listener
		public void addCommand(Command command){
			commandList.add(command);
		    ((AbstractButton)command).addActionListener(this);
		}


		// All Events hit here.
		@Override
		public void actionPerformed(ActionEvent e) {
			((Command)e.getSource()).execute();
		}

	}

In the above code, the addCommand method adds the command Object and adds a listener to it. Now how is that possible. Basically because I am categorizing my UI objects as Commands with in the system having some UI. And I am also assuming that these commands are Currently AbstractButton i.e. JMenuItem, JButton. Lets have a look at the Command Interface and its Implementation.


Doing the Locale - Danmark

The following illustrates how to get the Number format working with a danish locale.

	import java.text.NumberFormat;
	import java.util.Currency;
	import java.util.Locale;


	public class TestLocale {

	public static void main(String args[]){
		// Create a Locale for Danmark
		Locale DANMARK = new Locale("da","DK");

 		// get the currency instance for this locale.
 		Currency krone = Currency.getInstance(DANMARK);

 		// Get a Number format for the locale.
 		NumberFormat krFormat = NumberFormat.getCurrencyInstance(DANMARK);
 		// A symbol for the currency
 		String symbol = krFormat.getCurrency().getSymbol();
 		// A double amount
 		double amount = 10000.25;
		// print it out after formatting.
 		System.out.println(krFormat.format(amount));
 		}
	}

How to read a file from the JAR?

Someone just asked me this question today. And I thought might as well put it down for info.

 	public TestReadFileFromJar() throws FileNotFoundException, IOException {
        	InputStream is = getClass().getResource("txtData/states.properties");
        	read(is);
	}

In the case above txtData is placed in the jar on the root. Remmember to add the path with the “/”