KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > jndi > JNDIhandler


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms.jndi;
47
48 import java.io.IOException JavaDoc;
49 import java.io.InputStreamReader JavaDoc;
50 import java.util.Hashtable JavaDoc;
51 import java.util.StringTokenizer JavaDoc;
52
53 import javax.jms.JMSException JavaDoc;
54 import javax.naming.Context JavaDoc;
55 import javax.naming.InitialContext JavaDoc;
56 import javax.naming.NamingException JavaDoc;
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 /**
67  * Rights MantaRay objects to FS JNDI
68  */

69 public class JNDIhandler {
70
71      
72     public static void main(String JavaDoc argv[]) {
73
74         
75         
76         // Is there anything to do?
77
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 JavaDoc location = argv[0];
86         Hashtable JavaDoc env = new Hashtable JavaDoc(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 JavaDoc initCtx = null;
92         try {
93             initCtx= new InitialContext JavaDoc(env);
94         }
95         catch (NamingException JavaDoc ne) {
96             ne.printStackTrace();
97             System.exit(1);
98         }
99         
100         try {
101             // Read all standard input and send it as a message.
102
java.io.BufferedReader JavaDoc stdin =
103                 new java.io.BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
104             System.out.println("\nEnter JNDI commands. h for help.");
105             
106             String JavaDoc name;
107             String JavaDoc type;
108             String JavaDoc command;
109             String JavaDoc 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 JavaDoc st = new StringTokenizer JavaDoc(s," ");
119                     command = st.nextToken();
120                     
121                     if (command.equalsIgnoreCase("b")||command.equalsIgnoreCase("r")) {
122                         name = st.nextToken();
123                         type = st.nextToken();
124                         Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ioe) {
182             ioe.printStackTrace();
183        
184         }
185         catch (NamingException JavaDoc ne) {
186             ne.printStackTrace();
187         }
188     }
189
190         
191         
192         
193         
194     /** Prints the usage. */
195     private static void printHelp() {
196
197         StringBuffer JavaDoc use = new StringBuffer JavaDoc();
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 JavaDoc e) {
220             e.printStackTrace();
221         }
222     }
223
224
225 }
226
Popular Tags