Saturday, October 29, 2011

Signing files in windows

Gpg4win helps users to securely encrypt  and sign  files with the help of encryption and digital signatures in windows platform. Gpg4win is the official GnuPG distribution for Windows .You can download it here.

After installing it you can use GUI to create a key or else use the command prompt to genertae the key.

#gpg --gen-key

This will ask number of options to be filled(name,mail,city code, etc..)After the successful generation, you can export your public key;
#gpg --armor --export  > KEYS
eg:  
gpg --armor --export ratha > KEYS

This will save your public key at your current directory. Save it safely. You might need to provide your public key when you sign the files.

Now create a checksum file for your file;(You may need to install OpenSSL utility for windows)
(eg: lets use a zip file to be signed)
Change directory path your 'test.zip' folder location..

#openssl md5  <test.zip> test.zip.md5

This will create a checksum named " test.zip.md5".

Then sign the test.zip file;
#gpg --armor --output test.zip.asc --detach-sig test.zip

Finally verify whether, you have signed the packs without any issue;

#gpg --verify test.zip.asc test.zip

Output indicates it is a bad signature or good one;
gpg: Signature made 10/29/11 13:08:56 Sri Lanka Standard Time using RSA key ID 125C5F48
gpg: Good signature from "vijayaratha


If you check via the GUI, it will list out available certificates;



Monday, October 10, 2011

Java JNDI based client for OJMS

To work with Java Naming and Directory Interface (JNDI) in OJMS , which is the JMS interface for AQ, we need to register the oracle database with LDAP server. JMS administrator can register ConnectionFactory objects in a LDAP server.
Lets check following sample code, which is used to register the connection factory objects @ LDAP server.

void register_Factory_in_LDAP() throws Exception {
        Hashtable env = new Hashtable();
        // ldap settings
        env.put(Context.INITIAL_CONTEXT_FACTORY, AQjmsConstants.INIT_CTX_FACTORY);
        env.put(Context.PROVIDER_URL, "ldap://localhost:10389/");
        env.put(AQjmsConstants.SERVER_DN, "cn=ORCL,cn=OracleContext,ou=Services, o=sgi,c=us");
        env.put(Context.SECURITY_PRINCIPAL, "uid=ratha,ou=Services, o=sgi,c=us");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");

        String url = "jdbc:oracle:thin:@localhost:1521/orcl";
        Properties properties = new Properties();
        properties.setProperty("user", "ratha");
        properties.setProperty("password", "ratha");
        try {
            AQjmsFactory.registerConnectionFactory(env, "test_queue_factory", url, properties,
                                                   "queue");
            System.out.println("Connection factory craeted ");

        } catch (Exception e) {
            e.printStackTrace();
          }
    }

// DO lookup

 void get_Factory_from_LDAP() throws Exception {

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, AQjmsConstants.INIT_CTX_FACTORY);
        // ldapserver is your LDAP host and 389 is your port
        env.put(Context.INITIAL_CONTEXT_FACTORY, AQjmsConstants.INIT_CTX_FACTORY);
        env.put(Context.PROVIDER_URL, "ldap://localhost:10388/");
        env.put(Context.SECURITY_PRINCIPAL, "uid=ratha,ou=Services, o=sgi,c=us");
        env.put(Context.SECURITY_CREDENTIALS, "secret");

        DirContext inictx = new InitialDirContext(env);
        inictx = (DirContext) inictx.lookup("cn=ORCL,cn=OracleContext,ou=Services, o=sgi,c=us");
        // go to the connection factory holder cn=OraclDBConnections
        DirContext connctx = (DirContext) inictx.lookup("cn=oracledbconnections");

        // get connection factory "test_queue_factory"
        QueueConnectionFactory qc_fact = (QueueConnectionFactory) connctx.lookup("cn=ratha");

        System.out.println("Factory look up success " + qc_fact.toString());
        QueueConnection QCon = qc_fact.createQueueConnection();
        System.out.println("Connection created " + QCon.toString());
       DirContext destctxQF = (DirContext) inictx.lookup("cn=OracleDBQueues");
        System.out.println("OracleDBQueues look up success " + destctxQF.toString());
        Queue queue = (Queue) destctxQF.lookup("cn=ratha.test");
        System.out.println("Queue look up success :" + queue.toString());

        Session session = QCon.createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE);
        QCon.start();
        QueueSender sender = ((QueueSession) session).createSender(queue);
        System.out.println("Sender creation success :" + sender.toString());
    
       String msg = "test";
       TextMessage message = session.createTextMessage(msg);

       sender.send(message);
       // MessageConsumer consumer = session.createConsumer(queue);
       // TextMessage msg2 = (TextMessage) consumer.receive();
       // System.out.println("MESSAGE RECEIVED " + msg2.getText());

}

LDAP: error code 34 - Invalid root DN given : cn=oracledbconnections,null

This issue occurs when we try to connect OracleAQ via  Oracle JMS. To overcome above issue, check whether you have set following property,
    env.put(AQjmsConstants.SERVER_DN, "cn=ORCL,cn=OracleContext,ou=Services, o=sgi,c=us");

The second parameter provides the DN value of "ORCL", which is the third level, labeled cn=orcl, is the database.

 

Thursday, October 6, 2011

Creating partitions in ApacheDS

With ApacheDS as you may know, we can create our  customized partitions in order to keep our data according to  our requirements.
Lets have a look on creating partitions..

Steps
  • As I mentioned in my previous post , install and create a connection for the LDAP server.
  •  Double click on server link. You will see the server.xml's graphical view. From that select 'Partitions' tab

  • Under partitions section you will see default two partitions are listed out
    • System
    • example
  • Click on the 'Add' button. You will see a new partition will be created. Provide ID and suffix. Click on 'Save' button @ toolbar.

  • You can do this @ server.xml by adding new .Go to the ApcheDS installation directory find the server.xml and copy one of the existing "example" partition.
  • Now you  created a new partition called "acme.com". But you can not view the partition at LDAP browser. For that you have to create a new 'context entry' for the newly created partition.
  • Go to LDAP browser,'Root DSE '-->Right click->'New Context entry' .Select 'Create entry from scratch' option.
  • Select 'ObjectClass' as 'Domain' click  'Add' so you will see added object classes at your right pane.

  • Now enter the DN as you provided when you create the partition.
    • eg: dc=acme,dc=com
  • Click 'next' and Finish.You will see the newly created entry at LDAP browser .

  • When you try to make connections to newly created partition , You have to create a user/uid for this partition .

Creating a queuetable @ OracleAQ gives "ORA-01017: invalid username/password" error

If you face such issue[1], when you try to create a 'queuetable' at OracleAQ (Even though you provided all authorizations and authentications correctly), check the following global parameter is set to 'false"


connect / as sysdba
# sqlplus sys/admin@orcl as sysdba;
# ALTER SYSTEM SET GLOBAL_TOPIC_ENABLED = FALSE;


[1]Error

oracle.jms.AQjmsException: ORA-01017: invalid username/password;
logon denied ORA-06512: at "SYS.DBMS_AQADM", line 81
at oracle.jms.AQjmsSession.createQueueTable(AQjmsSession.java:4803)
at oracle.jms.AQjmsSession.createQueueTable(AQjmsSession.java:4778)

Installing and configuring ApacheDS

ApacheDS is an open source project, which provides directory server , which is LDAP v3 compliant. You can download ApcheDS here, which comes with eclipse based LDAP browser + LDAP server. So, you don't need to use extrernal LDAP browsers (eg: JXplorer)  to configure your directory server.

Steps
  • Download , install and start the Apache Directory Studio™ (v1.5.3)
  •  Go to 'File' menu and click 'New'. Select 'ApcheDS' server. Go to 'next' page.
  •  Provide a unique name to identify your LDAP server instance.(eg: ServerA) Click 'Finish'
  • You will see your newly created server @ servers panel.
  •  Double click on ServerA's link.You will see a window which lists all options to configure the server.

  •  At the 'General' tab, you can provide the port numbers for different protocols. Here leave the default port numbers as it is.
    •  LDAP =10389
    • LDAPS=10636
  •  Go to 'servers' panel and click the 'start' button.

  • Now, server instance created. We have to create a connection for that server.(ie:ServerA)
  • Go to 'LDAP' menu and select 'New Connection'
  • Provide 
    • Connection name - Connection A
    • Hostname - localhost
    • Port no -10389 (This is the port number ServerA instance running)

  • Click 'Next'. 
    • Authentication Method - "Simple Authentication"
    • Bind DN - "ou=system,uid=admin" (this is the one by default available partition and the user)
    • Bind password - 'secret' (default  passoword)
  • If you provide all the above parameters correctly , your connection will start successfully. You can see the available partitions at LDAP browser.
That is it for installation and starting the server.


Monday, October 3, 2011

Creating a queue in OracleAQ

Oracle provides database integrated messaging functionality,which is called OracleAQ. 
Oracle JMS (OJMS) is the JMS interface to the Oracle Database Streams Advanced Queuing (AQ) feature.
To work with OracleAQ, we need to install Oracle database server.(V11g)


In this post lets have a look on how we can create a queue programmatically using OJMS libraries and AQ libraries.
For this, specific user should have required authorization to create queues..
  • Connect as 'sys/admin' ;
  • create user ratha identified by ratha;
  • grant create session to ratha;
  • grant connect, resource to ratha;
  • grant aq_administrator_role to ratha  identified by  ratha;
  • grant execute on dbms_aq to  ratha;
  • grant execute on dbms_aqadm to  ratha;
  • exec dbms_aqadm.grant_system_privilege('ENQUEUE_ANY','ratha');
  • exec dbms_aqadm.grant_system_privilege('DEQUEUE_ANY','ratha'); 
  • grant execute on sys.aq$_jms_text_message to ratha; 
  • Connect as 'ratha/ratha'
After providing required authorization for the new user 'ratha' (password : ratha) you could be able to login as user 'ratha' and able to create table/queue etc..

With the particular user information, now lets try to create a queue in oracle database.

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import oracle.AQ.AQQueueTable;
import oracle.AQ.AQQueueTableProperty;
import oracle.jms.AQjmsDestination;
import oracle.jms.AQjmsDestinationProperty;
import oracle.jms.AQjmsFactory;
import oracle.jms.AQjmsSession;

public class OracleAQClient {

public static QueueConnection getConnection() {

  String hostname = "localhost";
  String oracle_sid = "orcl";
  int portno = 1521;
  String userName = "ratha";
  String password = "ratha";
  String driver = "thin";
  QueueConnectionFactory QFac = null;
  QueueConnection QCon = null;
  try {
   // get connection factory , not going through JNDI here
   QFac = AQjmsFactory.getQueueConnectionFactory(hostname, oracle_sid, 
portno,driver);
   // create connection
   QCon = QFac.createQueueConnection(userName, password);
   } catch (Exception e) {
   e.printStackTrace();
  }
  return QCon;
 }

 public static void createQueue(Session session, String user,
String qTable, String queueName) {
  try {
   /* Create Queue Tables */
   System.out.println("Creating Queue Table...");

   AQQueueTableProperty qt_prop;
   AQQueueTable q_table = null;
   AQjmsDestinationProperty dest_prop;
   Queue queue = null;
   qt_prop = new AQQueueTableProperty("SYS.AQ$_JMS_TEXT_MESSAGE");

   /* create a queue table *///
   // /* Drop the queue table if already exists */
   // try{
   // q_table = ((AQjmsSession) session).getQueueTable(user, qTable);
   // q_table.drop(true);
   // System.out.println("Droped older queuetable...");
   // }
   // catch(Exception e){
   // e.printStackTrace();
   // return;
   // }

   q_table = ((AQjmsSession) session).createQueueTable(user, qTable,
qt_prop);
   System.out.println("Qtable created");
   dest_prop = new AQjmsDestinationProperty();
   /* create a queue */
   queue = ((AQjmsSession) session).createQueue(q_table, queueName, 
dest_prop);
   System.out.println("Queue created");
   /* start the queue */
   ((AQjmsDestination) queue).start(session, true, true);
  } catch (Exception e) {
   e.printStackTrace();
   return;
  }
 }

 public static void sendMessage(String user, String queueName) {

  try {
   QueueConnection QCon = getConnection();  
   Session session = QCon.createQueueSession(false,
Session.CLIENT_ACKNOWLEDGE);
   QCon.start();
   Queue queue = ((AQjmsSession) session).getQueue(user, queueName);
   MessageProducer producer = session.createProducer(queue);
   TextMessage tMsg = session.createTextMessage("test");
   producer.send(tMsg);
   System.out.println("Sent message = " + tMsg.getText());

   session.close();
   producer.close();
   QCon.close();

  } catch (JMSException e) {
   e.printStackTrace();
   return;
  }
 }

 public static void browseMessage(String user, String queueName) {
  Queue queue;
  try {
   QueueConnection QCon = getConnection();  
   Session session = QCon.createQueueSession(false,
Session.CLIENT_ACKNOWLEDGE);
  
   QCon.start();
   queue = ((AQjmsSession) session).getQueue(user, queueName);
   QueueBrowser browser = session.createBrowser(queue);
   Enumeration enu = browser.getEnumeration();
   List list = new ArrayList();  
   while (enu.hasMoreElements()) {
    TextMessage message = (TextMessage) enu.nextElement();   
    list.add(message.getText());
   }
   for (int i = 0; i < list.size(); i++) {
    System.out.println("Browsed msg " + list.get(i));
   }
   browser.close();
   session.close();
   QCon.close();

  } catch (JMSException e) {
   e.printStackTrace();
  }

 }

 public static void consumeMessage(String user, String queueName) {  
  Queue queue;
  try {
   QueueConnection QCon = getConnection();  
   Session session = QCon.createQueueSession(false,
Session.CLIENT_ACKNOWLEDGE);
   QCon.start();
   queue = ((AQjmsSession) session).getQueue(user, queueName);
   MessageConsumer consumer = session.createConsumer(queue);
   TextMessage msg = (TextMessage) consumer.receive();
   System.out.println("MESSAGE RECEIVED " + msg.getText());

   consumer.close();
   session.close();
   QCon.close();
  } catch (JMSException e) {  
   e.printStackTrace();
  }
 }

 public static void main(String args[]) {
  String userName = "ratha";
  String queue = "test";
  // createQueue( userName, qTable, queue);
  sendMessage(userName, queue);
  browseMessage(userName, queue);
  // consumeMessage(userName, queue);
 }
}

You might need following jars in your class-path in order to run above java client
  • ojdbc6.jar (can be found at db_home\jdbc\lib)
  • jta.jar (can be found at db_home\jdbc\jlib)
  • jmscommon.jar (can be found at db_home\RDBMS\jlib folder)
  • aqapi.jar(can be found at db_home\RDBMS\jlib folder)