Tuesday, September 7, 2010

Most commonly used JavaScript method or Function


****************************************************************************************************************************************
function sepcomma(number)
{
       
        number=number+"";
        for(ln=0;ln<number.length;ln++)
        {
                number = number.replace(",","");                
        }
        if (number.length > 3)
        {
                var mod = number.length % 3;
                var output = (mod > 0 ? (number.substring(0,mod)) : '');
               
                for (i=0 ; i < Math.floor(number.length / 3); i++)
                {
                        if ((mod == 0) && (i == 0))
                        output += number.substring(mod+ 3 * i, mod + 3* i + 3);
                        else
                        output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
                }
               
                return (output);
        }
        else
        {
         
          return number;
        }
         
}
****************************************************************************************************************************************
function IntegerCheck(opt, obj1){
        flag=0;
        obj = obj1.value;
        flag = obj1.value.indexOf(".");
        if(opt > 0){
                if(flag == -1)
                {                
                        if (!((event.keyCode >47 && event.keyCode < 58) || event.keyCode == 46))
                        event.keyCode=0;
                }else{
                        if (event.keyCode <47 || event.keyCode > 57)
                        event.keyCode=0;
                }
        }else{
                if (event.keyCode <47 || event.keyCode > 57)
                        event.keyCode=0;
        }
}

****************************************************************************************************************************************

function textcounter(field,maxlen)
{        
        if(field.value.length > maxlen)
                field.value = field.value.substring(0,maxlen);
}
****************************************************************************************************************************************
function movef(osr, odest){
        var i = 0;
        for(i = osr.options.length-1; i > -1; i--)
                if(osr.options[i].selected){
                        odest.options[odest.options.length] = new Option(osr.options[i].text, osr.options[i].value);
                        osr.remove(i);
                }
}
****************************************************************************************************************************************
function opencenterwindow(url)
{
        var w=screen.width-80;
        var h=screen.height-80;
        window.open(url,'','height='+(parseInt(h)-parseInt(50))+',width='+(parseInt(w)-parseInt(50))+', resizable=1, scrollbars=1, top=0, left=0');
}
****************************************************************************************************************************************
function opendetailwindow(url)
{
        var w=screen.width;
        var h=screen.height-80;
        window.open(url,'','height='+(parseInt(h)-parseInt(50))+',width='+(parseInt(w)-parseInt(50))+', status=1, resizable=0, scrollbars=1, top=0, left=0');
 
}
****************************************************************************************************************************************
function openmidyeardetailwindow(url)
{
        var w=screen.width;
        var h=screen.height-80;
        window.open(url,'','height='+(parseInt(h)-parseInt(240))+',width='+(parseInt(w)-parseInt(50))+', status=1, resizable=0, scrollbars=1, top=0, left=0');
}

****************************************************************************************************************************************
function opencentermenuwindow(url) // with menu bar
{
        var w=screen.width-80;
        var h=screen.height-80;
        window.open(url,'','height='+(parseInt(h)-parseInt(50))+',width='+(parseInt(w)-parseInt(50))+', menubar=1,resizable=1, scrollbars=1, top=0, left=0');
}
****************************************************************************************************************************************
function opencenterswindow(url)
{
        window.open(url,'','height=300, width=650,resizable=no, scrollbars=0, top=0, left=0, status=0');
}
****************************************************************************************************************************************
function openplanhistorywindow(url) // with menu bar
{
        var w=screen.width-150;
        var h=screen.height-140;
        window.open(url,'','height='+(parseInt(h)-parseInt(50))+',width='+(parseInt(w)-parseInt(50))+', resizable=0, scrollbars=1, status=1, top=0, left=0');
}
****************************************************************************************************************************************
function dispdate(dt){
        if(dt.length < 1)
                return;
        if(dt.indexOf("-") < 0)
                return;
        if(dt.length > 10)
                dt = dt.substring(0, 10);
        var arr = dt.split("-");
                return arr[2] + "/" + arr[1] + "/" + arr[0];
}
****************************************************************************************************************************************
function setLength(){
        var objs = document.getElementsByTagName('TEXTAREA');
        for(var i = 0; i < objs.length; i++){
                if(objs[i].value.length == 0 || objs[i].id == "no")
                        continue;
                while(objs[i].scrollHeight > objs[i].clientHeight)
                        objs[i].rows = objs[i].rows + 1;
                        //if(objs[i].id == "le" && objs[i].value.length < 80)
                        //{
                                //objs[i].cols = objs[i].value.length + 5;
                        //}
        }
}
****************************************************************************************************************************************
function setLengthtarget(){
        var objs = document.getElementsByTagName('TEXTAREA');
        for(var i = 0; i < objs.length; i++){
                if(objs[i].id == "idkrakpi")
                {
                        while(objs[i].scrollHeight != objs[i].clientHeight)
                                objs[i].rows = objs[i].rows + 1;
                }
        }
}
****************************************************************************************************************************************
function f_open_window_max( aURL, aWinName )
{
   var wOpen;
   var sOptions;
   sOptions ='menubar=no,scrollbars=yes,toolbar=no';
   sOptions = sOptions + ',width=' + (screen.availWidth-10).toString();
   sOptions = sOptions + ',height=' + (screen.availHeight-50).toString();
   sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';
   sOptions = sOptions + ',resizable=no, status=1';

   win=window.open(aURL,'', sOptions );
        //alert(win);
}
****************************************************************************************************************************************
function closeall()
{        
       
        if(win !=null)
        {        
                if( ! win.closed)
                {
                        win.close();
                }
        }
       
        window.close();        
}
****************************************************************************************************************************************
function getConfirmation(msg)
{
 if(confirm(msg))
  return true;
 else
  return false;
}

function autochkselect(chkbxname,chkbxno, field)
{
        var f = eval("document.all."+chkbxname);
        if(field.value.length != 0)
        {
          if(chkbxno != -1)
           f[chkbxno].checked = true;
           else
           f.checked = true;        
    }
}
function setTextArea(){
        var _objs = document.getElementsByTagName('TEXTAREA');
        var _objstxt = document.getElementsByTagName('INPUT');
        for(var i = 0; i < _objs.length; i++){
                if(_objs[i].id == "t1")
                        _objs[i].cols = screen.width*(6.68/100);
                if(_objs[i].id == "t2")
                        _objs[i].cols = screen.width*(1.76/100);        
                if(_objs[i].id == "t3")
                        _objs[i].cols = screen.width*(4.50/100);        
                if(_objs[i].id == "t95")
                        _objs[i].cols = screen.width*(9.3/100);
                if(_objs[i].id == "t80")
                        _objs[i].cols = screen.width*(7.81/100);
                if(_objs[i].id == "t45")
                        _objs[i].cols = screen.width*(4.4/100);
                if(_objs[i].id == "t40")
                        _objs[i].cols = screen.width*(4.0/100);                        
        }
        for(var i = 0; i < _objstxt.length; i++){
        if(_objstxt[i].id == "tt105")
                        _objstxt[i].size = screen.width*(10.26/100);
        if(_objstxt[i].id == "tt50")
                        _objstxt[i].size = screen.width*(4.89/100);
        if(_objstxt[i].id == "tt5")
                        _objstxt[i].size = parseFloat(screen.width*(parseFloat(0.49/100)));
        }
}
function openconfirmwindow(url)
{
        var w=screen.width/2;
        var h=screen.height/2;
        window.open(url,'','height='+(parseInt(h)-parseInt(50))+',width='+(parseInt(w)-parseInt(50))+', resizable=1, scrollbars=1, top=0, left=0');
}
function f_open_window_max1( aURL, aWinName )
{
   var wOpen;
   var sOptions;
   sOptions ='menubar=no,scrollbars=yes,toolbar=no';
   sOptions = sOptions + ',width=' + (screen.availWidth-10).toString();
   sOptions = sOptions + ',height=' + (screen.availHeight-50).toString();
   sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';
   sOptions = sOptions + ',resizable=no, status=1';

   win=window.open(aURL,aWinName, sOptions );
        //alert(win);
}
function setWidthTextarea(){        
        var objs = document.getElementsByTagName('TEXTAREA');
        var screenwidthmadeon=1024;
 
         for(var i = 0; i < objs.length; i++){
                 var colsta=objs[i].cols;
                   objs[i].cols = screen.width*(colsta/screenwidthmadeon);
           }
}
function setLengthTextarea(){        
        var objs = document.getElementsByTagName('TEXTAREA');
        var screenwidthmadeon=768;
 
         for(var i = 0; i < objs.length; i++){
                  while(objs[i].scrollHeight > objs[i].clientHeight){
                        objs[i].rows = objs[i].rows + 1;
                  }
           }
}

function trim(strText) {
    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}


function showcomma(number2)
{
if (number2 =="NaN")
{
        number2 = "0";
}
else
{
var number1 = "" + number2;
if (number1.indexOf(".") > 0)
{
        var arr  = number1.split(".");
        if(arr.length > 1)
        {
                arr[1]= arr[1].substring(0,3);
                number2= arr[0];
                arr[0] = sepcomma(number2);
        return        arr[0]+"."+arr[1];
        }
}
//else {return sepcomma(number2)+".0";}                        
}
}

Best Coding Practices for connecting to JMS (PUBLISHER)–


Best Coding Practices for connecting to JMS (PUBLISHER)–

1.        Connect to DS, save the initial context and create a connection factory object–

The first step is to connect to DS. For this,

·        use a single comma separated list of urls to connect to primary as well as backupservers.
Eg: For Production use,
abc001.xyz.com:2566,abc002.xyz.com:2566,abc004.xyz.com:2569
·        Do not use TryBackupOnFailure since it is not supported by 7.5 jar files.
·        Do not use secondary list of urls.
·        Keep the value of connectionTimeout as 240000 (2 mins).
·        Save the context.
·        Get Topic Connection Factory using the context.

Eg:

Properties env = new Properties();
env.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,initialcontextfactory);                
env.setProperty(javax.naming.Context.PROVIDER_URL, commaseparatedurl);
env.setProperty(javax.naming.Context.SECURITY_PRINCIPAL, username);
env.setProperty(javax.naming.Context.SECURITY_CREDENTIALS, password);
env.put("com.sonicsw.jndi.mfcontext.connectTimeout", connectionTimeout);
ctx = new javax.naming.directory.InitialDirContext(env);
factory = (TopicConnectionFactory) ctx.lookup(connectionFactory);

2.        If there is an issue connecting to DS, use saved connection factory object to connect to DS –

ObjectInputStream ois=null;
        FileInputStream fis=null;
        try
        {                        
            fis = new FileInputStream("connection_factory.sjo");
                        ois = new ObjectInputStream(fis);                                                    
                factory = (progress.message.jclient.TopicConnectionFactory)ois.readObject();        
        }
        catch(Exception e)
        {
                factory = null;
        }
        finally
        {
                if(ois != null)
                {
                        try
                        {
                                ois.close();
                        } catch (IOException e)
                        {
                                e.printStackTrace();
                        }
                }
        }

3.        If no issue found in connecting to DS and connection factory object is successfully created, save it for later use –

FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        try
        {
                if (factory != null) // If ConnectionFactory is null, read it from file.
                    {
                                    fos = new FileOutputStream("connection_factory.sjo");                                    
                        oos = new ObjectOutputStream(fos);                        
                        oos.writeObject(factory);                                                
                }
        }
        catch (Exception e)
        {}
        finally
        {
                if(oos != null)
                {
                        try
                        {
                                oos.close();
                        } catch (IOException e)
                        {
                                e.printStackTrace();
                        }
                }
        }

4.        Create a connection with broker using Topic/Queue connection factory.

·        Creating a connection –

While creating a connection, first check if topic connection factory object is null or not.

If it is null, reconnect to DS, else, connect to Broker.

Eg:
if(factory == null)
{
Connect to DS;
}
else
{
        factory.setConnectID("App-Pub"); // Set connect ID
        connection = factory.createTopicConnection(username, password);
}        

Do proper exception handling while connecting to broker and if there is an exception found during connecting to Broker, retry connecting for a specified period of time (after a specified time interval) and if not able to connect, reconnect to DS.

·        Setting the connect id –

It determines whether the broker allows multiple connections to be established using a single username/ConnectID combination.

·        To allow only one connection, provide a valid connectID.
·        To allow unlimited connections, use null as the connectID.


5.        Create a session using connection.

If connection is created successfully, create a session.

Eg:

if (connection != null)
{
session = connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
}

Use the value of the parameters passed in createTopicSession as per application requirement.

6.        Create a topic (destination) -

If  session is not null, create a topic.

Eg:

if (session != null)
{                                
pubTopic = session.createTopic(topicName);
}

7.        Create a publisher -

If session is not null, create a publisher.

        Eg:

if (session != null)
        {
                Publisher = session.createProducer(pubTopic);
                System.out.println("Publisher Created.");                                
        }

8.        Create a message to be published.

This is very specific to the application. However, define all the properties of message at this time carefully.

Eg:

DeliveryMode of the message – Persistent/Non-persistent
Priority of the message
Life of the message.

9.        Start the connection.

connection.start();

10.        At the end (before exiting), close the publisher, topic, session and connection

if(publisher != null)
        {                
                publisher.close();
        }
        if(session != null)
        {
                session.close();
        }
       
if(connection != null)
        {
                connection.stop(); // stop the connection before closing it.
                connection.close();
        }

Write the close sequence in a finally block so that it executes always and close all the objects and avoid obsolete/orphan/excessive objects.

11. Do proper Exception Handling in all the codes written above.

Apart from these, following items need to be taken care of –

1)        Handle all the exceptions in a proper way and log each exception with the stacktrace.
2)        Every time you try to reconnect to broker, check for the connection factory context. Connect to DS only if the context is not available or is null.
3)        Keep connection logic separate from application logic. This will ensure that the connection logic can easily be debug in case of any issue.
4)        Take thread dumps at the time of issue so that it becomes easier to investigate and find the root cause.

PS – Please note that the code snippets listed above are not fully qualified codes. They are provided just to serve as samples. Also, the functionality of the code can be changed on the basis of personalized requirements (Eg: value of connectionTimeout, deciding the amount and number of times you want your application to try to reconnect to DS/Broker in case of an issue.)

JMSPublisher Java Code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.NamingException;

import progress.message.jclient.TopicConnectionFactory;

public class JMSPublisher
{
        /** Declare all the variables */
        private final String initialcontextfactory = "com.sonicsw.jndi.mfcontext.MFContextFactory";
        private String commaseparatedurl = "tcp://localhost:2506";
        private final String USERNAME = "Administrator";
        private final String PASSWORD = "Administrator";
        private String connectionTimeout = "240000";// As per requirement
        private final String connectionFactory = "TCF";
        private final String topicName = "Sample.T08";
        private final int MESSAGE_LIFESPAN = 86400000; // As per requirement
        private int NUMBERS_Of_RETRY_FOR_CONTEXT = 10; // As per requirement
        private long RETRY_TIME_FOR_CONNECTION = 20000;// As per requirement
        private int NUMBERS_Of_RETRY_FOR_CONNECTION = 20;// As per requirement
        private progress.message.jclient.TopicConnectionFactory topicConnectionFactory = null;
        private TopicConnection connection = null;
        private TopicSession topicSession = null;
        private Topic pubTopic = null;
        private MessageProducer publisher = null;

        /**
         *
         * @param args
         */
        public static void main(String args[])
        {
                new JMSPublisher();

        }

        public JMSPublisher()
        {
                try
                {
                        topicConnectionFactory = getConnectionFactory();
                        if (topicConnectionFactory != null)
                        {
                                connection = getConnection(topicConnectionFactory);
                                if (connection == null)
                                {
                                        connection = retryConnection(topicConnectionFactory);
                                        if (connection == null)
                                        {
                                                System.out.println("All attempts for creating Connection exhausted");
                                                System.out.println("Exiting the application");
                                                System.exit(-1);
                                        }
                                }
                                topicSession = getSession(connection);
                                if (topicSession != null)
                                {

                                        pubTopic = getTopic(topicSession);
                                        if (pubTopic != null)
                                        {
                                                publisher = getPublisher(topicSession, pubTopic);
                                                connection.start();
                                                if (publisher != null)
                                                {
                                                        long count = 0;
                                                        while (true)
                                                        {
                                                                try
                                                                {
                                                                        System.out.println("Creating message.....");
                                                                        javax.jms.TextMessage msg = topicSession.createTextMessage();
                                                                        msg.setText("Testing.........");
                                                                        msg.setJMSMessageID("TestID:XX1");
                                                                        msg.setJMSCorrelationID("TestingBCP");
                                                                        System.out.println("Message created");
                                                                        publisher.send(msg, javax.jms.DeliveryMode.PERSISTENT, // Persistent
                                                                                        javax.jms.Message.DEFAULT_PRIORITY, // Priority
                                                                                        MESSAGE_LIFESPAN);
                                                                        count++;
                                                                        System.out.println("Message send [ Count=" + count + " ]");                                                                        
                                                                }
                                                                catch (JMSException e)
                                                                {
                                                                        if (topicConnectionFactory == null)
                                                                        {                                                                                
                                                                                topicConnectionFactory = getConnectionFactory();
                                                                        }
                                                                        if (topicConnectionFactory != null)
                                                                        {                                                                                                                                                                                                                        
                                                                                connection = getConnection(topicConnectionFactory);
                                                                                if (connection == null)
                                                                                {
                                                                                        connection = retryConnection(topicConnectionFactory);
                                                                                        if (connection == null)
                                                                                        {
                                                                                                System.out.println("All attempts for creating Connection exhausted");
                                                                                                System.out.println("Exiting the application");
                                                                                                System.exit(-1);
                                                                                        }
                                                                                }
                                                                                topicSession = getSession(connection);
                                                                                if (topicSession != null)
                                                                                {
                                                                                        pubTopic = getTopic(topicSession);
                                                                                        if (pubTopic != null)
                                                                                        {
                                                                                                publisher = getPublisher(topicSession, pubTopic);
                                                                                                if (publisher != null)
                                                                                                {
                                                                                                        connection.start();
                                                                                                System.out.println("Connection Started.");                                                                                                                
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                        System.out.println("Unable to cretae MessageProducer");
                                                                                                        System.out.println("Exiting the application");
                                                                                                        System.exit(-1);
                                                                                                }
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                                System.out.println("Unable to cretae Topic");
                                                                                                System.out.println("Exiting the application");
                                                                                                System.exit(-1);
                                                                                        }
                                                                                }
                                                                                else
                                                                                {
                                                                                        System.out.println("Unable to cretae TopicSession");
                                                                                        System.out.println("Exiting the application");
                                                                                        System.exit(-1);
                                                                                }
                                                                        }                                                                                                                                        
                                                                }
                                                        }
                                                }
                                                else
                                                {
                                                        System.out.println("Unable to cretae MessageProducer");
                                                        System.out.println("Exiting the application");
                                                        System.exit(-1);
                                                }
                                        }
                                        else
                                        {
                                                System.out.println("Unable to cretae Topic");
                                                System.out.println("Exiting the application");
                                                System.exit(-1);
                                        }
                                }
                                else
                                {
                                        System.out.println("Unable to cretae TopicSession");
                                        System.out.println("Exiting the application");
                                        System.exit(-1);
                                }
                        }
                        else
                        {
                                System.out.println("All attempts to initialze context exhausted");
                                System.out.println("Exiting the application");
                                System.exit(-1);
                        }
                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                        System.out.println("Exiting the application");
                        System.exit(-1);
                }
                finally
                {
                        if(publisher != null)
                        {
                                try {
                                        publisher.close();
                                } catch (JMSException e) {
                                        e.printStackTrace();
                                }
                        }                        
                        if(topicSession != null)
                        {
                                try {
                                        topicSession.close();
                                        } catch (JMSException e) {
                                                e.printStackTrace();
                                        }
                        }
                        if(connection != null)
                        {
                                try
                                {
                                        connection.stop();
                                        connection.close();
                                } catch (JMSException e) {
                                        e.printStackTrace();
                                }
                        }
                }                
        }
        /**
         *
         * @param topicConnectionFactory
         * @return Connection
         */
        private TopicConnection retryConnection(TopicConnectionFactory topicConnectionFactory)
        {
                TopicConnection con = null;
                for (int i = 0; i < NUMBERS_Of_RETRY_FOR_CONNECTION; i++)
                {
                        if (i == 0)
                                System.out.println("Application is in retrying mode to create connection");
                        System.out.println("Will retry after " + RETRY_TIME_FOR_CONNECTION / 1000 + " seconds");
                        try
                        {
                                Thread.sleep(RETRY_TIME_FOR_CONNECTION);
                                con = getConnection(topicConnectionFactory);
                                if (con != null)
                                        break;
                        }
                        catch (InterruptedException e1)
                        {
                                e1.printStackTrace();
                        }
                }
                return con;
        }
        /**
         *
         * @return TopicConnectionFactory
         */
        private progress.message.jclient.TopicConnectionFactory getConnectionFactory()
        {                
                System.out.println("Creating Context");
                Context ctx = null;                
                topicConnectionFactory = null;
                for (int i = 0; i <= NUMBERS_Of_RETRY_FOR_CONTEXT; i++)
                {
                        try
                        {                        
                                /** Connect to DS and save the initial context */
                                Properties env = new Properties();
                                env.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,initialcontextfactory); //MFContextFactory
                                env.setProperty(javax.naming.Context.PROVIDER_URL, commaseparatedurl); //Comma separated url of primary and backup servers
                                env.setProperty(javax.naming.Context.SECURITY_PRINCIPAL, USERNAME); //Authentication
                                env.setProperty(javax.naming.Context.SECURITY_CREDENTIALS, PASSWORD); //Authentication
                                env.put("com.sonicsw.jndi.mfcontext.connectTimeout", connectionTimeout); // Appln will try to reconnect to DS for this much amount of time, else will throw exception.                                                        
                                ctx = new javax.naming.directory.InitialDirContext(env); // Save the initial context
                                System.out.println("Context Created.");
                                System.out.println("Creating Connection Factory.");
                                topicConnectionFactory = (TopicConnectionFactory) ctx.lookup(connectionFactory); // Create CONNECTIONFACTORY
                                System.out.println("Connection Factory Created.");
                                saveConnectionFactoryObj(topicConnectionFactory); // Save the factory object created successfully
                                if (topicConnectionFactory != null)
                                        break;
                        }
                        catch (Exception aa)
                        {
                                ctx = null;
                                topicConnectionFactory = null;
                                System.out.println("Exception connecting to DS, using saved connection factory object to connect");
                                topicConnectionFactory = getConnectionFactoryObjFromDisk();                
                                if(topicConnectionFactory!=null)
                                        break;
                        }                                
                }
                return topicConnectionFactory;                
        }

        /**
         *
         * @param topicConnectionFactory
         */
        private void saveConnectionFactoryObj(TopicConnectionFactory topicConnectionFactory)
        {
                FileOutputStream fos = null;
                ObjectOutputStream oos = null;
                try
                {
                        if (topicConnectionFactory != null)
                        {
                                // Write connection topicConnectionFactory object to a file for
                                // later use
                                fos = new FileOutputStream("connection_factory.sjo");
                                oos = new ObjectOutputStream(fos);
                                oos.writeObject(topicConnectionFactory);
                                System.out.println("Saved Connection Factory Object");
                        }
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                finally
                {
                        try
                        {
                                if (oos != null)
                                        oos.close();
                                if (fos != null)
                                        fos.close();
                        }
                        catch (IOException e1)
                        {
                                e1.printStackTrace();
                        }
                }
        }
       
        /**
         *
         * @return TopicConnectionFactory
         */
        private TopicConnectionFactory getConnectionFactoryObjFromDisk()
        {
                ObjectInputStream ois = null;
                FileInputStream fis = null;
                TopicConnectionFactory topicConnectionFactory = null;
                try
                {
                        System.out.println("Attempting to read JMS Connection topicConnectionFactory from file...");
                        // Try to read the object from a file
                        fis = new FileInputStream("connection_factory.sjo");
                        ois = new ObjectInputStream(fis);
                        topicConnectionFactory = (progress.message.jclient.TopicConnectionFactory) ois.readObject();
                        System.out.println("JMS Connection topicConnectionFactory read from file!");
                }
                catch (Exception e)
                {
                        topicConnectionFactory = null;
                        e.printStackTrace();
                }
                finally
                {
                        try
                        {
                                if (ois != null)
                                        ois.close();
                                if (fis != null)
                                        fis.close();
                        }
                        catch (IOException e)
                        {
                                e.printStackTrace();
                        }                        
                }
                return topicConnectionFactory;
        }
       
        /**
         *
         * @param topicConnectionFactory
         * @return TopicConnection
         */
        private TopicConnection getConnection(TopicConnectionFactory topicConnectionFactory)
        {
                TopicConnection connection = null;
                try
                {
                        System.out.println("Creating Connection");
                        topicConnectionFactory.setConnectID("ConnectId-Pub");
                        connection = topicConnectionFactory.createTopicConnection(USERNAME, PASSWORD);
                        System.out.println("Connection Created.");
                }
                catch (Exception cc)
                {
                        System.out.println("Unable to create connection");
                }
                return connection;
        }

        /**
         *
         * @param connection
         * @return TopicSession
         */
        private TopicSession getSession(TopicConnection connection)
        {
                TopicSession topicSession = null;
                try
                {
                        System.out.println("Creating Session");
                        topicSession = connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
                        System.out.println("Session Created.");
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                return topicSession;
        }
        /**
         *
         * @param topicSession
         * @return Topic
         */
        private Topic getTopic(TopicSession topicSession)
        {
                Topic pubTopic = null;
                try
                {
                        System.out.println("Creating Topic");
                        pubTopic = topicSession.createTopic(topicName);
                        System.out.println("Publisher Topic Created.");
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                return pubTopic;
        }
        /**
         *
         * @param topicSession
         * @param pubTopic
         * @return MessageProducer
         */
        private MessageProducer getPublisher(TopicSession topicSession, Topic pubTopic)
        {
                MessageProducer publisher = null;
                try
                {
                        System.out.println("Creating Publisher");
                        publisher = topicSession.createProducer(pubTopic);
                        System.out.println("Publisher Created.");
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                return publisher;
        }        
}