my-Channels

/**
 *
 * ----------------------------------------------------------------------------------
 *
 * my-Channels License Version 1.1
 * Copyright © my-Channels. All rights reserved
 *
 * In the event that you should download or otherwise use this software
 * ( the "Software" ) you hereby acknowledge and agree that:
 *
 * 1. The Software is the property of my-Channels: Title, Copyright and all
 * other proprietary rights, interest and benefit in and to the Software is and
 * shall be owned by my-Channels;
 *
 * 2. You will not make copies of the Software whatsoever other than, if you should
 * so wish, a single copy for archival purposes only;
 *
 * 3. You will not modify, reverse assemble, decompile, reverse engineer or otherwise
 * translate the Software;
 *
 * 4. You will not redistribute, copy, forward electronically or circulate the Software
 * to any person for any purpose whatsoever without the prior written consent of
 * my-Channels;
 *
 * 5. You will not charge for, market or provide any managed service or product that
 * is based upon or includes the Software or any variant of it; and
 *
 * 6. You will not use the Software for any purpose apart from your own personal,
 * noncommercial and lawful use;
 *
 * You hereby agree that the software is used by you on an "as is" basis, without
 * warranty of any kind. my-Channels hereby expressly disclaim all warranties
 * and conditions, either expressed or implied, including but not limited to any
 * implied warranties or conditions or merchantability and fitness for a particular
 * purpose.
 *
 * You agree that you are solely responsible for determining the appropriateness of
 * using the Software and assume all risks associated with it including but not
 * limited to the risks of program errors, damage to or loss of of data, programs or
 * equipment and unavailability or interruption of operations.
 *
 * my-Channels will not be liable for any direct damages or for any, special,
 * incidental or indirect damages or for any economic consequential damages ( including
 * lost profits or savings ), or any damage howsoever arising.
 *
 *
 * ------------------------------------------------------------------------
 *
 */
package com.pcbsys.Nirvana.nSpace;

import com.pcbsys.foundation.utils.fEnvironment;

import com.pcbsys.Nirvana.client.*;
import com.pcbsys.Nirvana.client.nSessionAttributes;

import Java.util.*;

import javax.naming.*;


/**
 * <*Enter Class Description*>
 */
public class JMSAdmin {
 static public String sClassVersion = "$Name:  $ - $Revision: 1.1 $";

 //
 //---------------------------------------------------------------------------
 // JMSAdmin Class Interface
 //---------------------------------------------------------------------------
 //
 private Context ictx = null;
 private nSession mysession = null;
 private String rname = "nsp://127.0.0.1:9000";

 // static public String sLicenseInfo = "This code is protected by copyright, any reverse engineering of this code is prohibited. Please refer to the license agreement";
 private static void usage2() {
 System.out.println("Java -DRNAME [-DPRINCIPAL] [-DPASSWORD] -DCONTEXT_FACTORY -DPROVIDER_URL JMSAdmin bind|unbind|list queueFactory|topicFactory|connectionFactory|queue|topic name/alias");
 System.exit(1);
 }

 /**
 * Prints the usage message for this class
 */
 private static void usage() {
 System.out.println("Usage ...\n");
 System.out.println("jmsadmin    \n");
 System.out.println(" \n");
 System.out.println(" - The operation to be performed on the object specified. Valid operations are : bind, unbind and list");
 System.out.println(" - The object where the specified operation will be performed on. Valid objects are : queueFactory, topicFactory, connectionFactory, queue and topic");
 System.out.println(" - The logical name or alias of that object");
 System.out.println("\n\nNote: -? provides help on environment variables \n");
 }

 private static void UsageEnv() {
 System.out.println("\n\n(Environment Variables) \n");

 System.out.println("(RNAME) - One or more RNAME entries in the form protocol://host:port");
 System.out.println(" protocol - Can be one of nsp, nhp, nsps, or nhps, where:");
 System.out.println(" nsp - Specifies Nirvana Socket Protocol (nsp)");
 System.out.println(" nhp - Specifies Nirvana HTTP Protocol (nhp)");
 System.out.println(" nsps - Specifies Nirvana Socket Protocol Secure (nsps), i.e. using SSL/TLS");
 System.out.println(" nhps - Specifies Nirvana HTTP Protocol Secure (nhps), i.e. using SSL/TLS");
 System.out.println(" port - The port number of the server");
 System.out.println("\nHint: - For multiple RNAME entries, use comma separated values which will be attempted in connection weight order\n");

 System.out.println("(PRINCIPAL) - This specifies the principal to be used to authenticate with the JNDI provider (Default: anon)\n");
 System.out.println("(PASSWORD) - This specifies the password to be used to authenticate with the JNDI provider (Default: anon)\n");
 System.out.println("(CONTEXT_FACTORY) - This specifies the JNDI provider context factory (Default: com.pcbsys.Nirvana.nSpace.NirvanaContextFactory)\n");
 System.out.println("(PROVIDER_URL) - This specifies the JNDI provider URL (Default: nsp://127.0.0.1:9000)\n");

 System.out.println("(LOGLEVEL) - This determines how much information the Nirvana API will output 0 = verbose 7 = quiet\n");

 System.out.println("(CKEYSTORE) - If using SSL, the location of the keystore containing the client cert\n");
 System.out.println("(CKEYSTOREPASSWD) - If using SSL, the password for the keystore containing the client cert\n");
 System.out.println("(CAKEYSTORE) - If using SSL, the location of the ca truststore\n");
 System.out.println("(CAKEYSTOREPASSWD) - If using SSL, the password for the ca truststore\n");

 System.out.println("(HPROXY) - HTTP Proxy details in the form proxyhost:proxyport, where:");
 System.out.println(" proxyhost - The HTTP proxy host");
 System.out.println(" proxyport - The HTTP proxy port\n");
 System.out.println("(HAUTH) - HTTP Proxy authentication details in the form user:pass, where:");
 System.out.println(" user - The HTTP proxy authentication username");
 System.out.println(" pass - The HTTP proxy authentication password\n");
 System.exit(1);
 }

 private static void processArgs(String[] args) {
 switch (args.length) {
 case 1:
 if (args[0].equals("-?")) {
 UsageEnv();
 }

 break;
 }
 }

 private static void processEnvironmentVariable(String variable) {
 String laxVAR = System.getProperty("lax.nl.env." + variable);
 if (laxVAR != null) {
 System.setProperty(variable, laxVAR);
 }
 }

 public static void main(String[] args) {
 //Process command line arguments
 processArgs(args);

 //Process Environment Variables
 processEnvironmentVariable("RNAME");
 processEnvironmentVariable("PRINCIPAL");
 processEnvironmentVariable("PASSWORD");
 processEnvironmentVariable("CONTEXT_FACTORY");
 processEnvironmentVariable("PROVIDER_URL");
 processEnvironmentVariable("LOGLEVEL");
 processEnvironmentVariable("HPROXY");
 processEnvironmentVariable("HAUTH");
 processEnvironmentVariable("CKEYSTORE");
 processEnvironmentVariable("CKEYSTOREPASSWD");
 processEnvironmentVariable("CAKEYSTORE");
 processEnvironmentVariable("CAKEYSTOREPASSWD");

 // Install any proxy server settings
 fEnvironment.setProxyEnvironments();

 // Install any ssl settings
 fEnvironment.setSSLEnvironments();

 JMSAdmin jMSAdmin = new JMSAdmin();
 if (args.length <1) {
 usage();
 System.exit(1);
 }

 String ctype = args[0];
 String wtype = null;
 String uname = null;
 if (args.length == 3) {
 wtype = args[1];
 uname = args[2];
 }

 try {
 jMSAdmin.doIt(ctype, wtype, uname);
 } catch (Exception ex) {
 ex.printStackTrace();
 }
 }

 //
 //---------------------------------------------------------------------------
 // JMSAdmin Constructor
 //---------------------------------------------------------------------------
 //
 private void doUnBind(String wtype, String uname) throws Exception {
 if (wtype.equalsIgnoreCase("topicFactory")) {
 this.deleteTopicConnectionFactory(uname);
 } else if (wtype.equalsIgnoreCase("connectionFactory")) {
 this.deleteConnectionFactory(uname);
 } else if (wtype.equalsIgnoreCase("queueFactory")) {
 this.deleteQueueConnectionFactory(uname);
 } else if (wtype.equalsIgnoreCase("topic")) {
 this.deleteTopic(uname);
 } else if (wtype.equalsIgnoreCase("queue")) {
 this.deleteQueue(uname);
 } else {
 usage();
 }
 }

 private void doBind(String wtype, String uname) throws Exception {
 if (wtype.equalsIgnoreCase("topicFactory")) {
 this.createTopicConnectionFactory(uname);
 } else if (wtype.equalsIgnoreCase("queueFactory")) {
 this.createQueueConnectionFactory(uname);
 } else if (wtype.equalsIgnoreCase("connectionFactory")) {
 this.createConnectionFactory(uname);
 } else if (wtype.equalsIgnoreCase("topic")) {
 this.createTopic(uname);
 } else if (wtype.equalsIgnoreCase("queue")) {
 this.createQueue(uname);
 } else {
 usage();
 }
 }

 public void doIt(String ctype, String wtype, String uname) throws Exception {
 Hashtable env = new Hashtable();

 env.put(Context.INITIAL_CONTEXT_FACTORY, System.getProperty("CONTEXT_FACTORY", "com.pcbsys.Nirvana.nSpace.NirvanaContextFactory"));
 env.put(Context.PROVIDER_URL, System.getProperty("PROVIDER_URL", System.getProperty("RNAME", "nsp://127.0.0.1:9000")));
 env.put(Context.SECURITY_AUTHENTICATION, "simple");
 env.put(Context.SECURITY_PRINCIPAL, System.getProperty("PRINCIPAL", "anon"));
 env.put(Context.SECURITY_CREDENTIALS, System.getProperty("PASSWORD", "anon"));
 rname = System.getProperty("RNAME", "nsp://127.0.0.1:9000");
 ictx = new InitialContext(env);
 mysession = nSessionFactory.create(new nSessionAttributes(rname));
 mysession.init();
 if (ctype.equalsIgnoreCase("bind")) {
 doBind(wtype, uname);
 } else if (ctype.equalsIgnoreCase("unbind")) {
 doUnBind(wtype, uname);
 } else if (ctype.equalsIgnoreCase("list")) {
 NamingEnumeration ne = ictx.list("");
 while (ne.hasMoreElements()) {
 System.out.println(ne.nextElement());
 }
 } else {
 usage();
 }

 nSessionFactory.close(mysession);
 ictx.close();
 }

 public void createStringVal(String name, String val) {
 try {
 ictx.rebind(name, val);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void createQueueConnectionFactory(String name) {
 try {
 com.pcbsys.Nirvana.nJMS.QueueConnectionFactoryImpl qcf = new com.pcbsys.Nirvana.nJMS.QueueConnectionFactoryImpl(new com.pcbsys.Nirvana.nJMS.DestinationImpl(rname));
 ictx.rebind(name, qcf);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void createTopicConnectionFactory(String name) {
 try {
 com.pcbsys.Nirvana.nJMS.TopicConnectionFactoryImpl tcf = new com.pcbsys.Nirvana.nJMS.TopicConnectionFactoryImpl(new com.pcbsys.Nirvana.nJMS.DestinationImpl(rname));
 ictx.rebind(name, tcf);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void createConnectionFactory(String name) {
 try {
 com.pcbsys.Nirvana.nJMS.ConnectionFactoryImpl cf = new com.pcbsys.Nirvana.nJMS.ConnectionFactoryImpl(new com.pcbsys.Nirvana.nJMS.DestinationImpl(rname));
 ictx.rebind(name, cf);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void createQueue(String name) {
 nChannelAttributes nca = new nChannelAttributes();
 try {
 nca.setName(name);
 nca.setType(nChannelAttributes.MIXED_TYPE);
 mysession.createQueue(nca);
 } catch (nChannelAlreadyExistsException e) {
 } catch (Exception e) {
 e.printStackTrace();
 }

 try {
 com.pcbsys.Nirvana.nJMS.QueueImpl queue = new com.pcbsys.Nirvana.nJMS.QueueImpl(name);
 ictx.rebind(name, queue);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void createTopic(String name) {
 nChannelAttributes nca = new nChannelAttributes();
 try {
 nca.setName(name);
 nca.setType(nChannelAttributes.MIXED_TYPE);
 mysession.createChannel(nca);
 } catch (com.pcbsys.Nirvana.client.nChannelAlreadyExistsException ncaee) {
 } catch (Exception e) {
 e.printStackTrace();
 }

 try {
 com.pcbsys.Nirvana.nJMS.TopicImpl topic = new com.pcbsys.Nirvana.nJMS.TopicImpl(name);
 ictx.rebind(name, topic);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void deleteQueue(String name) {
 try {
 nChannelAttributes nca = new nChannelAttributes();
 nca.setName(name);
 mysession.deleteQueue(nca);
 } catch (nChannelNotFoundException e) {
 } catch (Exception e) {
 e.printStackTrace();
 }

 try {
 ictx.unbind(name);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void deleteTopic(String name) {
 try {
 nChannelAttributes nca = new nChannelAttributes();
 nca.setName(name);
 mysession.deleteChannel(nca);
 } catch (nChannelNotFoundException e) {
 } catch (Exception e) {
 e.printStackTrace();
 }

 try {
 ictx.unbind(name);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void deleteTopicConnectionFactory(String name) {
 try {
 ictx.unbind(name);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void deleteConnectionFactory(String name) {
 try {
 ictx.unbind(name);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public void deleteQueueConnectionFactory(String name) {
 try {
 ictx.unbind(name);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
}