JNDICheck.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 
022 package org.apache.qpid.tools;
023 
024 import org.apache.qpid.client.AMQDestination;
025 import org.apache.qpid.client.AMQConnectionFactory;
026 import org.apache.qpid.jms.FailoverPolicy;
027 
028 import javax.naming.Context;
029 import javax.naming.InitialContext;
030 import javax.naming.NamingException;
031 import java.util.Properties;
032 import java.util.Hashtable;
033 import java.util.Enumeration;
034 import java.util.List;
035 import java.util.LinkedList;
036 import java.io.IOException;
037 import java.io.File;
038 import java.io.FileInputStream;
039 
040 public class JNDICheck
041 {
042     private static final String QUEUE = "queue.";
043     private static final String TOPIC = "topic.";
044     private static final String DESTINATION = "destination.";
045     private static final String CONNECTION_FACTORY = "connectionfactory.";
046 
047     public static void main(String[] args)
048     {
049 
050         if (args.length != 1)
051         {
052             usage();
053         }
054 
055         String propertyFile = args[0];
056 
057         new JNDICheck(propertyFile);
058     }
059 
060     private static void usage()
061     {
062         exit("Usage: JNDICheck <JNDI Config file>"0);
063     }
064 
065     private static void exit(String message, int exitCode)
066     {
067         System.err.println(message);
068         System.exit(exitCode);
069     }
070 
071     private static String JAVA_NAMING = "java.naming.factory.initial";
072 
073     Context _context = null;
074     Hashtable _environment = null;
075 
076     public JNDICheck(String propertyFile)
077     {
078 
079         // Load JNDI properties
080         Properties properties = new Properties();
081 
082         try
083         {
084             properties.load(new FileInputStream(new File(propertyFile)));
085         }
086         catch (IOException e)
087         {
088             exit("Unable to open property file:" + propertyFile + ". Due to:" + e.getMessage()1);
089         }
090 
091         //Create the initial context
092         try
093         {
094 
095             System.setProperty(JAVA_NAMING, properties.getProperty(JAVA_NAMING));
096 
097             _context = new InitialContext(properties);
098 
099             _environment = _context.getEnvironment();
100 
101             Enumeration keys = _environment.keys();
102 
103             List<String> queues = new LinkedList<String>();
104             List<String> topics = new LinkedList<String>();
105             List<String> destinations = new LinkedList<String>();
106             List<String> connectionFactories = new LinkedList<String>();
107 
108             while (keys.hasMoreElements())
109             {
110                 String key = keys.nextElement().toString();
111 
112                 if (key.startsWith(QUEUE))
113                 {
114                     queues.add(key);
115                 }
116                 else if (key.startsWith(TOPIC))
117                 {
118                     topics.add(key);
119                 }
120                 else if (key.startsWith(DESTINATION))
121                 {
122                     destinations.add(key);
123                 }
124                 else if (key.startsWith(CONNECTION_FACTORY))
125                 {
126                     connectionFactories.add(key);
127                 }
128             }
129 
130             printHeader(propertyFile);
131             printEntries(QUEUE, queues);
132             printEntries(TOPIC, topics);
133             printEntries(DESTINATION, destinations);
134             printEntries(CONNECTION_FACTORY, connectionFactories);
135 
136         }
137         catch (NamingException e)
138         {
139             exit("Unable to load JNDI Context due to:" + e.getMessage()1);
140         }
141 
142     }
143 
144     private void printHeader(String file)
145     {
146         print("JNDI file :" + file);
147     }
148 
149     private void printEntries(String type, List<String> list)
150     {
151         if (list.size() 0)
152         {
153             String name = type.substring(01).toUpperCase() + type.substring(1, type.length() 1);
154             print(name + " elements in file:");
155             printList(list);
156             print("");
157         }
158     }
159 
160     private void printList(List<String> list)
161     {
162         for (String item : list)
163         {
164             String key = item.substring(item.indexOf('.'1);
165 
166             try
167             {
168                 print(key, _context.lookup(key));
169             }
170             catch (NamingException e)
171             {
172                 exit("Error: item " + key + " no longer in context."1);
173             }
174         }
175     }
176 
177     private void print(String key, Object object)
178     {
179         if (object instanceof AMQDestination)
180         {
181             print(key + ":" + object);
182         }
183         else if (object instanceof AMQConnectionFactory)
184         {
185             AMQConnectionFactory factory = (AMQConnectionFactoryobject;
186             print(key + ":Connection");
187             print("ConnectionURL:");
188             print(factory.getConnectionURL().toString());
189             print("FailoverPolicy");
190             print(new FailoverPolicy(factory.getConnectionURL(),null).toString());
191             print("");
192         }
193     }
194 
195     private void print(String msg)
196     {
197         System.out.println(msg);
198     }
199 
200 }