static  EMSQueueConnection()  /* Static Constructor for creating Connection at Start-up */
                {
                    try
                    {
                        SetEnvironment();
                        Hashtable Environment = LC.Settings;
                        
                        
                        /* Username , password and targetHostname were already retrieved in SetEnv.At present , we are assuming 
                         that lookup JNDI and connectiing QCF are of same provider */

                        
                        String username = Environment[LookupContext.SECURITY_PRINCIPAL].ToString();
                        String password = Environment[LookupContext.SECURITY_CREDENTIALS].ToString();
                       
                      

                        


                        /*QCF and CLient-ID are not required for SetEnv/Lookup obviously. Hence now retriving from config. */
                        
                        string ClientID = System.Configuration.ConfigurationManager.ConnectionStrings["Client-ID"].ConnectionString;
                        string QueueConnectionFactory = System.Configuration.ConfigurationManager.ConnectionStrings["QueueConnectionFactory"].ConnectionString;
                        QueueConnectionFactory QCF = (QueueConnectionFactory)LC.Lookup(QueueConnectionFactory);
                       
                        
                        QCF.SetClientID(ClientID);

                        if (Environment[LookupContext.SECURITY_PROTOCOL].ToString().Equals("ssl"))
                        {
                            Console.WriteLine("########### QCF=" + QCF.ToString());
                            QCF.SetTargetHostName(Environment[LookupContext.SSL_TARGET_HOST_NAME].ToString());
                        }


                        connection = (QueueConnection)QCF.CreateQueueConnection(username, password);

                        connection.ExceptionHandler += new EMSExceptionHandler(_HandleException);

                        connection.Start();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine("\n###############\nERROR thrown in EMSQueueConnection Constructor  :" + E.ToString());
                        if (E is EMSException)
                        {
                            EMSException je = (EMSException)E;

                            if (je.LinkedException != null)
                            {
                                System.Console.WriteLine("##### Linked Exception:");
                                System.Console.WriteLine(je.LinkedException.StackTrace);
                            }
                        }

                    }
                }
        protected override void OnSetUp()
        {
            admin = new Admin("tcp://localhost:7222", "admin", "");
            Destination destination = null;

            try
            {
                destination = (Destination)lookupContext.Lookup(queueName);
                if (destination != null)
                {
                    admin.DestroyQueue(queueName);
                }
            } catch (NameNotFoundException)
            {}
            admin.CreateQueue(new QueueInfo(queueName));
            admin.BindQueue(queueName, queueName);
            admin.PurgeQueue(queueName);
        }
    public csUFOLookup(String[] args)
    {
        parseArgs(args);
        if (providerUrl == null)
        {
            providerUrl = defaultProviderURL;
        }

        // Print parameters
        Console.WriteLine("\n------------------------------------------------------------------------");
        Console.WriteLine("csUFOLookup SAMPLE");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + providerUrl);
        Console.WriteLine("------------------------------------------------------------------------\n");

        try
        {
            Hashtable env = new Hashtable();

            env.Add(LookupContext.PROVIDER_URL, providerUrl);
            if (userName != null)
            {
                env.Add(LookupContext.SECURITY_PRINCIPAL, userName);
                if (password != null)
                {
                    env.Add(LookupContext.SECURITY_CREDENTIALS, password);
                }
            }

            LookupContext lcxt = new LookupContext(env);

            // Lookup connection factory which must exist in the factories
            // config file.
            ConnectionFactory factory = (ConnectionFactory)lcxt.Lookup("ConnectionFactory");
            Console.WriteLine("OK - successfully did lookup ConnectionFactory");

            // Let's create a connection to the server and a session to verify
            // that the server is running so we can continue with our lookup
            // operations.
            Connection connection = null;
            Session    session    = null;

            try
            {
                connection = factory.CreateConnection(userName, password);
                session    = connection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
            }
            catch (TIBCO.EMS.EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while creating connection");
                Console.Error.WriteLine("  Most likely the server is not running, the exception trace follows:");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Lookup topic 'topic.sample' which must exist in the topics
            // configuration file; if not successfull then most likely such
            // topic does not exist in the config files.
            Topic sampleTopic = null;
            try
            {
                sampleTopic = (Topic)lcxt.Lookup("topic.sample");
                Console.WriteLine("OK - successfully did lookup topic 'topic.sample'");
            }
            catch (TIBCO.EMS.NamingException ne)
            {
                Console.Error.WriteLine(ne.StackTrace);
                Console.Error.WriteLine("**NamingException occurred while looking up the topic topic.sample");
                Console.Error.WriteLine("  Most likely such topic does not exist in your configuration.");
                Console.Error.WriteLine("  Exception message follows:");
                Console.Error.WriteLine(ne.Message);
                Environment.Exit(0);
            }

            // Lookup queue 'queue.sample' which must exist in the queues
            // configuration file; if not successfull then it does not exist.
            TIBCO.EMS.UFO.Queue sampleQueue = null;
            try
            {
                sampleQueue = (TIBCO.EMS.UFO.Queue)lcxt.Lookup("queue.sample");
                Console.WriteLine("OK - successfully did lookup queue 'queue.sample'");
            }
            catch (TIBCO.EMS.NamingException ne)
            {
                Console.Error.WriteLine("**NamingException occurred while looking up the queue queue.sample");
                Console.Error.WriteLine("  Most likely such topic does not exist in your configuration.");
                Console.Error.WriteLine("  Exception message follows:");
                Console.Error.WriteLine(ne.Message);
                Environment.Exit(0);
            }

            // Try to lookup a topic when a topic *and* a queue with the
            // same name exist in the configuration. The sample configuration
            // has both topic and queue with the name 'sample' used here. If
            // everything is OK, we will get an exception, then try to lookup
            // those objects using fully-qualified names.
            try
            {
                Object object_Renamed = lcxt.Lookup("sample");

                // If the sample config was not altered, we should not get to this
                // line because lookup() should throw an exception.
                Console.WriteLine("Object named 'sample' has been looked up as: " + object_Renamed);
            }
            catch (TIBCO.EMS.NamingException ne)
            {
                Console.Error.WriteLine("OK - NamingException occurred while looking up the object named 'sample'");
                Console.Error.WriteLine("     This is the CORRECT BEHAVIOUR if the exception message below");
                Console.Error.WriteLine("     specifies name conflict situation. Exception message follows:");
                Console.Error.WriteLine("     " + ne.Message);

                // Let's look them up using name qualifiers.
                try
                {
                    Topic t = (Topic)lcxt.Lookup("$topics.sample");
                    Console.WriteLine("OK - successfully did lookup topic '$topics.sample'");
                    TIBCO.EMS.UFO.Queue q = (TIBCO.EMS.UFO.Queue)lcxt.Lookup("$queues.sample");
                    Console.WriteLine("OK - successfully did lookup queue '$queues.sample'");
                }
                catch (TIBCO.EMS.NamingException nex)
                {
                    // These may not be configured: Print a warning and continue.
                    Console.Error.WriteLine("**NamingException occurred while looking up the topic or queue 'sample'");
                    Console.Error.WriteLine("  Exception message follows:");
                    Console.Error.WriteLine("  " + nex.Message);
                }
            }

            // Now let's try to lookup a topic which definitely does not
            // exist. A lookup() operation will throw an exception and then we
            // will try to create such topic explicitly with the createTopic()
            // method.
            String dynamicName  = "topic.does.not.exist." + (DateTime.Now.Ticks - 621355968000000000) / 10000;
            Topic  dynamicTopic = null;
            try
            {
                dynamicTopic = (Topic)lcxt.Lookup(dynamicName);

                // If the sample config was not altered, we should not get to
                // this line because lookup() should throw an exception.
                Console.Error.WriteLine("**Error: dynamic topic " + dynamicName + " found by lookup");
            }
            catch (TIBCO.EMS.NamingException)
            {
                // This is OK.
                Console.Error.WriteLine("OK - could not lookup dynamic topic " + dynamicName);
            }

            // Try to create it.
            try
            {
                dynamicTopic = session.CreateTopic(dynamicName);
                Console.WriteLine("OK - successfully created dynamic topic " + dynamicName);
            }
            catch (TIBCO.EMS.EMSException e)
            {
                // A few reasons are possible here. Maybe there is no > entry
                // in the topics configuration or the server security is
                // turned on. If the original sample configuration is used, this
                // should work.
                Console.Error.WriteLine("**EMSException occurred while creating topic " + dynamicName);
                Console.Error.WriteLine("  Please verify your topics configuration and security settings.");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0);
            }

            connection.Close();

            Console.WriteLine("OK - Done.");
        }
        catch (TIBCO.EMS.EMSException e)
        {
            Console.Error.WriteLine(e.StackTrace);
            Environment.Exit(0);
        }
    }
Example #4
0
    public csLookupSSL(String[] args)
    {
        parseArgs(args);
        if (providerUrl == null)
        {
            providerUrl = defaultProviderURL;
        }

        // Print parameters
        Console.WriteLine("\n------------------------------------------------------------------------");
        Console.WriteLine("csLookupSSL SAMPLE");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + providerUrl);
        Console.WriteLine("------------------------------------------------------------------------\n");

        try
        {
            Hashtable env = new Hashtable();

            env.Add(LookupContext.PROVIDER_URL, providerUrl);
            if (userName != null)
            {
                env.Add(LookupContext.SECURITY_PRINCIPAL, userName);
                if (password != null)
                {
                    env.Add(LookupContext.SECURITY_CREDENTIALS, password);
                }
            }

            // Creating the SSL related file store info object.
            EMSSSLFileStoreInfo info = new EMSSSLFileStoreInfo();
            if (ssl_identity != null)
            {
                info.SetSSLClientIdentity(ssl_identity);
            }

            if (ssl_password != null)
            {
                string _sslpassword = ssl_password;
                info.SetSSLPassword(_sslpassword.ToCharArray());
            }

            if (ssl_target_hostname == null)
            {
                Console.WriteLine("ssl_target_hostname is required parameter");
                usage();
            }

            // Setting up the SSL properties for the lookup context.
            env.Add(LookupContext.SSL_STORE_INFO, info);
            env.Add(LookupContext.SSL_STORE_TYPE, EMSSSLStoreType.EMSSSL_STORE_TYPE_FILE);
            env.Add(LookupContext.SECURITY_PROTOCOL, "ssl");
            env.Add(LookupContext.SSL_TARGET_HOST_NAME, ssl_target_hostname);

            LookupContext lcxt = new LookupContext(env);

            // Lookup connection factory which must exist in the factories config file.

            ConnectionFactory factory    = null;
            ConnectionFactory sslFactory = null;

            try
            {
                factory = (ConnectionFactory)lcxt.Lookup("ConnectionFactory");
                Console.WriteLine("OK - successfully did lookup ConnectionFactory");

                // Lookup SSL connection factory which must exist in the
                // factories config file.
                sslFactory = (ConnectionFactory)lcxt.Lookup("SSLConnectionFactory");
                Console.WriteLine("OK - successfully did lookup SSLConnectionFactory");

                // Setting additional parameters for this SSL factory.
                sslFactory.SetTargetHostName(ssl_target_hostname);

                // Also, if users want to set any other SSL parameters,
                // i.e EMSSSLFileStoreInfo parameters, they can call GetCertificateStore
                // on the connection factory object.
                EMSSSLFileStoreInfo fileStoreInfo = (EMSSSLFileStoreInfo)sslFactory.GetCertificateStore();
                fileStoreInfo.SetSSLClientIdentity(ssl_identity);
                string _sslpassword = ssl_password;
                fileStoreInfo.SetSSLPassword(_sslpassword.ToCharArray());
            }
            catch (EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while doing a lookup");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Let's create a connection to the server and a session to verify
            // that the server is running.
            Connection sslConnection = null;
            Session    sslSession    = null;

            try
            {
                sslConnection = sslFactory.CreateConnection(userName, password);
                sslSession    = sslConnection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
                sslConnection.Close();
            }
            catch (EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while creating SSL connection");
                Console.Error.WriteLine("  Most likely the server is not running, the exception trace follows:");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Let's create a connection to the server and a session to verify
            // that the server is running so we can continue with our lookup operations.
            Connection connection = null;
            Session    session    = null;

            try
            {
                connection = factory.CreateConnection(userName, password);
                session    = connection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
            }
            catch (EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while creating connection");
                Console.Error.WriteLine("  Most likely the server is not running, the exception trace follows:");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Lookup topic 'topic.sample' which must exist in the topics
            // configuration file; if not successfull then most likely such
            // topic does not exist in the config file.
            Topic sampleTopic = null;
            try
            {
                sampleTopic = (Topic)lcxt.Lookup("topic.sample");
                Console.WriteLine("OK - successfully did lookup topic 'topic.sample'");
            }
            catch (NamingException ne)
            {
                Console.Error.WriteLine(ne.StackTrace);
                Console.Error.WriteLine("**NamingException occurred while lookup the topic topic.sample");
                Console.Error.WriteLine("  Most likely such topic does not exist in your configuration.");
                Console.Error.WriteLine("  Exception message follows:");
                Console.Error.WriteLine(ne.Message);
                Environment.Exit(0);
            }

            // Lookup queue 'queue.sample' which must exist in the queues
            // configuration file; if not successfull then it does not exist.
            TIBCO.EMS.Queue sampleQueue = null;
            try
            {
                sampleQueue = (TIBCO.EMS.Queue)lcxt.Lookup("queue.sample");
                Console.WriteLine("OK - successfully did lookup queue 'queue.sample'");
            }
            catch (NamingException ne)
            {
                Console.Error.WriteLine("**NamingException occurred while lookup the queue queue.sample");
                Console.Error.WriteLine("  Most likely such topic does not exist in your configuration.");
                Console.Error.WriteLine("  Exception message follows:");
                Console.Error.WriteLine(ne.Message);
                Environment.Exit(0);
            }

            // Try to lookup a topic when a topic *and* a queue with the
            // same name exist in the configuration. The sample configuration
            // has both topic and queue with the name 'sample' used here. If
            // everything is OK, we will get an exception, then try to lookup
            // those objects using fully-qualified names.
            try
            {
                Object object_Renamed = lcxt.Lookup("sample");

                // If the sample config was not altered, we should not get to this
                // line because lookup() should throw an exception.
                Console.WriteLine("Object named 'sample' has been looked up as: " + object_Renamed);
            }
            catch (NamingException ne)
            {
                Console.Error.WriteLine("OK - NamingException occurred while lookup the object named 'sample'");
                Console.Error.WriteLine("     This is the CORRECT BEHAVIOUR if the exception message below");
                Console.Error.WriteLine("     specifies name conflict situation. Exception message follows:");
                Console.Error.WriteLine("     " + ne.Message);

                // Let's look them up using name qualifiers.
                try
                {
                    Topic t = (Topic)lcxt.Lookup("$topics.sample");
                    Console.WriteLine("OK - successfully did lookup topic '$topics.sample'");
                    TIBCO.EMS.Queue q = (TIBCO.EMS.Queue)lcxt.Lookup("$queues.sample");
                    Console.WriteLine("OK - successfully did lookup queue '$queues.sample'");
                }
                catch (NamingException nex)
                {
                    // These may not be configured: Print a warning and continue.
                    Console.Error.WriteLine("**NamingException occurred while lookup the topic or queue 'sample'");
                    Console.Error.WriteLine("  Exception message follows:");
                    Console.Error.WriteLine("  " + nex.Message);
                }
            }

            // Now let's try to lookup a topic which definitely does not
            // exist. A lookup() operation will throw an exception and then we
            // will try to create such a topic explicitly with the createTopic()
            // method.
            String dynamicName  = "topic.does.not.exist." + (DateTime.Now.Ticks - 621355968000000000) / 10000;
            Topic  dynamicTopic = null;
            try
            {
                dynamicTopic = (Topic)lcxt.Lookup(dynamicName);

                // If the sample config was not altered, we should not get to
                // this line because lookup() should throw an exception.
                Console.Error.WriteLine("**Error: dynamic topic " + dynamicName + " found by lookup");
            }
            catch (NamingException)
            {
                // This is OK.
                Console.Error.WriteLine("OK - could not lookup dynamic topic " + dynamicName);
            }

            // Try to create it.
            try
            {
                dynamicTopic = session.CreateTopic(dynamicName);
                Console.WriteLine("OK - successfully created dynamic topic " + dynamicName);
            }
            catch (EMSException e)
            {
                // A few reasons are possible here. Maybe there is no > entry
                // in the topics configuration or the server security is
                // turned on. If the original sample configuration is used, this
                // should work.
                Console.Error.WriteLine("**EMSException occurred while creating topic " + dynamicName);
                Console.Error.WriteLine("  Please verify your topics configuration and security settings.");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0);
            }

            connection.Close();

            Console.WriteLine("OK - Done.");
        }
        catch (EMSException e)
        {
            Console.Error.WriteLine(e.StackTrace);
            Environment.Exit(0);
        }
    }