summaryrefslogtreecommitdiff
path: root/qpid/java/tools/src/main/java/org/apache/qpid/tools/JNDICheck.java
blob: 2390516ef05f8aaa5c55007ff94ae33d3197d0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package org.apache.qpid.tools;

import org.apache.qpid.client.AMQDestination;
import org.apache.qpid.client.AMQConnectionFactory;
import org.apache.qpid.jms.FailoverPolicy;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.List;
import java.util.LinkedList;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;

public class JNDICheck
{
    private static final String QUEUE = "queue.";
    private static final String TOPIC = "topic.";
    private static final String DESTINATION = "destination.";
    private static final String CONNECTION_FACTORY = "connectionfactory.";

    public static void main(String[] args)
    {

        if (args.length != 1)
        {
            usage();
        }

        String propertyFile = args[0];

        new JNDICheck(propertyFile);
    }

    private static void usage()
    {
        exit("Usage: JNDICheck <JNDI Config file>", 0);
    }

    private static void exit(String message, int exitCode)
    {
        System.err.println(message);
        System.exit(exitCode);
    }

    private static String JAVA_NAMING = "java.naming.factory.initial";

    Context _context = null;
    Hashtable _environment = null;

    public JNDICheck(String propertyFile)
    {

        // Load JNDI properties
        Properties properties = new Properties();

        try
        {
            properties.load(new FileInputStream(new File(propertyFile)));
        }
        catch (IOException e)
        {
            exit("Unable to open property file:" + propertyFile + ". Due to:" + e.getMessage(), 1);
        }

        //Create the initial context
        try
        {

            System.setProperty(JAVA_NAMING, properties.getProperty(JAVA_NAMING));

            _context = new InitialContext(properties);

            _environment = _context.getEnvironment();

            Enumeration keys = _environment.keys();

            List<String> queues = new LinkedList<String>();
            List<String> topics = new LinkedList<String>();
            List<String> destinations = new LinkedList<String>();
            List<String> connectionFactories = new LinkedList<String>();

            while (keys.hasMoreElements())
            {
                String key = keys.nextElement().toString();

                if (key.startsWith(QUEUE))
                {
                    queues.add(key);
                }
                else if (key.startsWith(TOPIC))
                {
                    topics.add(key);
                }
                else if (key.startsWith(DESTINATION))
                {
                    destinations.add(key);
                }
                else if (key.startsWith(CONNECTION_FACTORY))
                {
                    connectionFactories.add(key);
                }
            }

            printHeader(propertyFile);
            printEntries(QUEUE, queues);
            printEntries(TOPIC, topics);
            printEntries(DESTINATION, destinations);
            printEntries(CONNECTION_FACTORY, connectionFactories);

        }
        catch (NamingException e)
        {
            exit("Unable to load JNDI Context due to:" + e.getMessage(), 1);
        }

    }

    private void printHeader(String file)
    {
        print("JNDI file :" + file);
    }

    private void printEntries(String type, List<String> list)
    {
        if (list.size() > 0)
        {
            String name = type.substring(0, 1).toUpperCase() + type.substring(1, type.length() - 1);
            print(name + " elements in file:");
            printList(list);
            print("");
        }
    }

    private void printList(List<String> list)
    {
        for (String item : list)
        {
            String key = item.substring(item.indexOf('.') + 1);

            try
            {
                print(key, _context.lookup(key));
            }
            catch (NamingException e)
            {
                exit("Error: item " + key + " no longer in context.", 1);
            }
        }
    }

    private void print(String key, Object object)
    {
        if (object instanceof AMQDestination)
        {
            print(key + ":" + object);
        }
        else if (object instanceof AMQConnectionFactory)
        {
            AMQConnectionFactory factory = (AMQConnectionFactory) object;
            print(key + ":Connection");
            print("ConnectionURL:");
            print(factory.getConnectionURL().toString());
            print("FailoverPolicy");
            print(new FailoverPolicy(factory.getConnectionURL(),null).toString());
            print("");
        }
    }

    private void print(String msg)
    {
        System.out.println(msg);
    }

}