Sunday, January 23, 2011

Reading a properties file in wlst (jython) script.

I had to write scripts to create domain and configure datasource and jms on weblogic server using wlst (weblogic script tool). Datasource credentials were in a properties file, which was to be loaded in script. Here is a piece of script to load the properties file and read the values from file.

myProps.jy
#Script to load properties file.

from java.io import File
from java.io import FileInputStream
from java.util import Properties


#Load properties file in java.util.Properties
def loadPropsFil(propsFil):

 inStream = FileInputStream(propsFil)
 propFil = Properties()
 propFil.load(inStream) 
 
 return propFil


#Displays all keys and values of properties file.
def dispayProps():

 myPorpFil = loadPropsFil('D:/myProps.properties')
 keys = myPorpFil.keySet()
 for key in keys:
  print key+': '+myPorpFil.getProperty(key)
 
 return

if(1):
 dispayProps()


The properties file to be read.
myProps.properties
dataSourceName=myds
host=localhost
port=1234
userName=usr
password=psswrd



Output after running script.
>>> execfile('D:\myProps.jy')
port: 1234
password: psswrd
host: localhost
userName: usr
dataSourceName: myds
 

Friday, January 14, 2011

Java code to execute batch file.


package com.ca.common;

import java.io.IOException;

public class RunBatchFile {

    public static void main(String[] args) {
        System.out.println("processing Main...");
        
        String batchFile = "D:/copy.bat";
        Runtime runtime = Runtime.getRuntime();
        
        try {
            runtime.exec(batchFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Done!");
    }
}

Java code to connect to MQ

package com.ca.mq;

import java.io.IOException;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

/**
 * Java class to connect to MQ. Post and Retreive messages.
 *
 */
public class MQClientTest {

    String qMngrStr = "";
    String user = "";
    String password = "";
    String queueName = "";
    String hostName = "";
    int port = 0;
    String channel = "";
    //message to put on MQ.
    String msg = "Hello World, WelCome to MQ.";
    //Create a default local queue.
    MQQueue defaultLocalQueue;
    MQQueueManager qManager;
    
    /**
     * Initialize the MQ
     *
     */
    public void init(){
        
        //Set MQ connection credentials to MQ Envorinment.
         MQEnvironment.hostname = hostName;
         MQEnvironment.channel = channel;
         MQEnvironment.port = port;
         MQEnvironment.userID = user;
         MQEnvironment.password = password;
         //set transport properties.
         MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
         
         try {
             //initialize MQ manager.
            qManager = new MQQueueManager(qMngrStr);
        } catch (MQException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Method to put message to MQ.
     *
     */
    public void putAndGetMessage(){
        
        int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; 
        try {
            defaultLocalQueue = qManager.accessQueue(queueName, openOptions);
            
            MQMessage putMessage = new MQMessage();
            putMessage.writeUTF(msg);
            
            //specify the message options...
            MQPutMessageOptions pmo = new MQPutMessageOptions(); 
            // accept 
            // put the message on the queue
            defaultLocalQueue.put(putMessage, pmo);
            
            System.out.println("Message is put on MQ.");
            
            //get message from MQ.
            MQMessage getMessages = new MQMessage();
            //assign message id to get message.
            getMessages.messageId = putMessage.messageId;
            
            //get message options.
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            defaultLocalQueue.get(getMessages, gmo);
            
            String retreivedMsg = getMessages.readUTF();
            System.out.println("Message got from MQ: "+retreivedMsg);
            
        } catch (MQException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        
        System.out.println("Processing Main...");
        
        MQClientTest clientTest = new MQClientTest();
        
        //initialize MQ.
        clientTest.init();
        
        //put and retreive message from MQ.
        clientTest.putAndGetMessage();
        
        System.out.println("Done!");
    }
    
}

Thursday, January 6, 2011

Servlet & RequestDispatcher

This a basic post on Servlet and RequestDispatcher API of Servlet.

Servlet is provided with an api “RequestDispatcher” which can be used to forward request to another resource on web container.

RequestDispatcher can be obtained in two ways: either from Request or from ServletContext.

The RequestDispather obtained from request can be used to point or forward to resources on same web application.

Eg:
public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

  RequestDispatcher rd = request.getRequestDispatcher("servletB");
  rd.forward(request, response);

In Contrast, RequestDispather Obtained from ServletContext can point to or forward to resources on the web container.

Eg:
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

   RequestDispatcher rd = getServletContext().getContext("WebAppB").getRequestDispatcher("servletB");
   rd.forward(request, response);