1 46 package org.mr.api.jms.jndi; 47 48 import java.io.IOException ; 49 import java.io.InputStreamReader ; 50 import java.util.Hashtable ; 51 import java.util.StringTokenizer ; 52 53 import javax.jms.JMSException ; 54 import javax.naming.Context ; 55 import javax.naming.InitialContext ; 56 import javax.naming.NamingException ; 57 import org.mr.api.jms.MantaConnectionFactory; 58 import org.mr.api.jms.MantaDestination; 59 import org.mr.api.jms.MantaQueue; 60 import org.mr.api.jms.MantaQueueConnectionFactory; 61 import org.mr.api.jms.MantaTopic; 62 import org.mr.api.jms.MantaTopicConnectionFactory; 63 64 65 66 69 public class JNDIhandler { 70 71 72 public static void main(String argv[]) { 73 74 75 76 if (argv.length == 0) { 78 System.out.println("Please enter the path where yor JNDI resides."); 79 System.out.println("e.g. - for the path e:/filejndi/ -"); 80 System.out.println("java JNDIHandler ///e:/filejndi"); 81 waitForAnyKey(); 82 System.exit(1); 83 } 84 85 String location = argv[0]; 86 Hashtable env = new Hashtable (11); 87 env.put(Context.INITIAL_CONTEXT_FACTORY, 88 "com.sun.jndi.fscontext.RefFSContextFactory"); 89 env.put(Context.PROVIDER_URL, 90 "file:///"+location); 91 Context initCtx = null; 92 try { 93 initCtx= new InitialContext (env); 94 } 95 catch (NamingException ne) { 96 ne.printStackTrace(); 97 System.exit(1); 98 } 99 100 try { 101 java.io.BufferedReader stdin = 103 new java.io.BufferedReader (new InputStreamReader (System.in)); 104 System.out.println("\nEnter JNDI commands. h for help."); 105 106 String name; 107 String type; 108 String command; 109 String s; 110 111 while (true) { 112 s = stdin.readLine(); 113 114 if (s == null) 115 System.exit(0); 116 else if (s.length() > 0) { 117 118 StringTokenizer st = new StringTokenizer (s," "); 119 command = st.nextToken(); 120 121 if (command.equalsIgnoreCase("b")||command.equalsIgnoreCase("r")) { 122 name = st.nextToken(); 123 type = st.nextToken(); 124 Object obj=null; 125 if (type.equalsIgnoreCase("CF")) 126 type = "MantaConnectionFactory"; 127 else if (type.equalsIgnoreCase("QCF")) 128 type = "MantaQueueConnectionFactory"; 129 else if (type.equalsIgnoreCase("TCF")) 130 type = "MantaTopicConnectionFactory"; 131 else if (type.equalsIgnoreCase("D")) 132 type = "MantaDestination"; 133 else if (type.equalsIgnoreCase("Q")) 134 type = "MantaQueue"; 135 else if (type.equalsIgnoreCase("T")) 136 type = "MantaTopic"; 137 138 if (type.equalsIgnoreCase("MantaConnectionFactory")) 139 obj = new MantaConnectionFactory(); 140 else if (type.equalsIgnoreCase("MantaQueueConnectionFactory")) 141 obj = new MantaQueueConnectionFactory(); 142 else if (type.equalsIgnoreCase("MantaTopicConnectionFactory")) 143 obj = new MantaTopicConnectionFactory(); 144 else if (type.equalsIgnoreCase("MantaDestination")) 145 obj = new MantaDestination(name); 146 else if (type.equalsIgnoreCase("MantaQueue")) 147 obj = new MantaQueue(name); 148 else if (type.equalsIgnoreCase("MantaTopic")){ 149 try { 150 obj = new MantaTopic(name); 151 } catch (JMSException e) { 152 System.out.println("Bind / Rebind failed."); 153 e.printStackTrace(); 154 } 155 } 156 157 158 try { 159 initCtx.rebind(name,obj); 160 System.out.println(name+" bound as "+type); 161 } 162 catch (NamingException ne) { 163 System.out.println("Bind / Rebind failed."); 164 } 165 } 166 167 else if (command.equalsIgnoreCase("u")) { 168 name = st.nextToken(); 169 initCtx.unbind(name); 170 System.out.println(name +" unbound from JNDI."); 171 } 172 else if (command.equalsIgnoreCase("h")) { 173 printHelp(); 174 } 175 else 176 System.out.println("Use h to learn the usages."); 177 } 178 } 179 } 180 181 catch (java.io.IOException ioe) { 182 ioe.printStackTrace(); 183 184 } 185 catch (NamingException ne) { 186 ne.printStackTrace(); 187 } 188 } 189 190 191 192 193 194 195 private static void printHelp() { 196 197 StringBuffer use = new StringBuffer (); 198 use.append("commands:\n"); 199 use.append(" b object-name object-type bind/rebind an object in JNDI.\n"); 200 use.append(" r object-name object-type bind/rebind an object in JNDI.\n"); 201 use.append(" u object-name unbind an object from JNDI.\n"); 202 use.append(" h This help screen.\n\n"); 203 use.append("When binding, you can use shorthand for the object-type:\n"); 204 use.append("You can use either shorthand or fully qualified names, not case sensitive."); 205 use.append("SHORTHAND FULLY QUALIFIED FORM\n\n"); 206 use.append("TCF MantaTopicConnectionFactory\n"); 207 use.append("QCF MantaQueueConnectionFactory\n"); 208 use.append("CF MantaConnectionFactory\n"); 209 use.append("T MantaTopic\n"); 210 use.append("Q MantaQueue\n"); 211 use.append("D MantaDestination\n"); 212 System.err.println (use); 213 } 214 215 private static void waitForAnyKey(){ 216 System.err.println("press any key ..."); 217 try { 218 System.in.read(); 219 } catch (IOException e) { 220 e.printStackTrace(); 221 } 222 } 223 224 225 } 226 | Popular Tags |