summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java
blob: 2bfdd930308764ab9ae68259a3ecd32047244cb3 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 *
 * 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.server;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Logger;
import org.apache.qpid.server.Broker.InitException;
import org.apache.qpid.server.registry.ApplicationRegistry;


/**
 * Main entry point for AMQPD.
 *
 */
public class Main
{

    private static final Option OPTION_HELP = new Option("h", "help", false, "print this message");

    private static final Option OPTION_VERSION = new Option("v", "version", false, "print the version information and exit");

    private static final Option OPTION_CONFIG_FILE =
            OptionBuilder.withArgName("file").hasArg().withDescription("use given configuration file").withLongOpt("config")
                    .create("c");

    private static final Option OPTION_PORT =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("listen on the specified port. Overrides any value in the config file")
                    .withLongOpt("port").create("p");

    private static final Option OPTION_SSLPORT =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("SSL port. Overrides any value in the config file")
                    .withLongOpt("sslport").create("s");

    private static final Option OPTION_EXCLUDE_0_10 =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("when listening on the specified port do not accept AMQP0-10 connections. The specified port must be one specified on the command line")
                    .withLongOpt("exclude-0-10").create();

    private static final Option OPTION_EXCLUDE_0_9_1 =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("when listening on the specified port do not accept AMQP0-9-1 connections. The specified port must be one specified on the command line")
                    .withLongOpt("exclude-0-9-1").create();

    private static final Option OPTION_EXCLUDE_0_9 =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("when listening on the specified port do not accept AMQP0-9 connections. The specified port must be one specified on the command line")
                    .withLongOpt("exclude-0-9").create();

    private static final Option OPTION_EXCLUDE_0_8 =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("when listening on the specified port do not accept AMQP0-8 connections. The specified port must be one specified on the command line")
                    .withLongOpt("exclude-0-8").create();

    private static final Option OPTION_JMX_PORT_REGISTRY_SERVER =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("listen on the specified management (registry server) port. Overrides any value in the config file")
                    .withLongOpt("jmxregistryport").create("m");

    private static final Option OPTION_JMX_PORT_CONNECTOR_SERVER =
            OptionBuilder.withArgName("port").hasArg()
                    .withDescription("listen on the specified management (connector server) port. Overrides any value in the config file")
                    .withLongOpt("jmxconnectorport").create();

    private static final Option OPTION_BIND =
            OptionBuilder.withArgName("address").hasArg()
                    .withDescription("bind to the specified address. Overrides any value in the config file")
                    .withLongOpt("bind").create("b");

    private static final Option OPTION_LOG_CONFIG_FILE =
            OptionBuilder.withArgName("file").hasArg()
                    .withDescription("use the specified log4j xml configuration file. By "
                                     + "default looks for a file named " + BrokerOptions.DEFAULT_LOG_CONFIG_FILE
                                     + " in the same directory as the configuration file").withLongOpt("logconfig").create("l");

    private static final Option OPTION_LOG_WATCH =
            OptionBuilder.withArgName("period").hasArg()
                    .withDescription("monitor the log file configuration file for changes. Units are seconds. "
                                     + "Zero means do not check for changes.").withLongOpt("logwatch").create("w");

    private static final Options OPTIONS = new Options();

    static
    {
        OPTIONS.addOption(OPTION_HELP);
        OPTIONS.addOption(OPTION_VERSION);
        OPTIONS.addOption(OPTION_CONFIG_FILE);
        OPTIONS.addOption(OPTION_LOG_CONFIG_FILE);
        OPTIONS.addOption(OPTION_LOG_WATCH);
        OPTIONS.addOption(OPTION_PORT);
        OPTIONS.addOption(OPTION_SSLPORT);
        OPTIONS.addOption(OPTION_EXCLUDE_0_10);
        OPTIONS.addOption(OPTION_EXCLUDE_0_9_1);
        OPTIONS.addOption(OPTION_EXCLUDE_0_9);
        OPTIONS.addOption(OPTION_EXCLUDE_0_8);
        OPTIONS.addOption(OPTION_BIND);

        OPTIONS.addOption(OPTION_JMX_PORT_REGISTRY_SERVER);
        OPTIONS.addOption(OPTION_JMX_PORT_CONNECTOR_SERVER);
    }

    private CommandLine commandLine;

    public static void main(String[] args)
    {
        //if the -Dlog4j.configuration property has not been set, enable the init override
        //to stop Log4J wondering off and picking up the first log4j.xml/properties file it
        //finds from the classpath when we get the first Loggers
        if(System.getProperty("log4j.configuration") == null)
        {
            System.setProperty("log4j.defaultInitOverride", "true");
        }

        new Main(args);
    }

    public Main(final String[] args)
    {
        if (parseCommandline(args))
        {
            try
            {
                execute();
            }
            catch(Throwable e)
            {
                System.err.println("Exception during startup: " + e);
                e.printStackTrace();
                shutdown(1);
            }
        }
    }

    protected boolean parseCommandline(final String[] args)
    {
        try
        {
            commandLine = new PosixParser().parse(OPTIONS, args);

            return true;
        }
        catch (ParseException e)
        {
            System.err.println("Error: " + e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("Qpid", OPTIONS, true);

            return false;
        }
    }

    protected void execute() throws Exception
    {
        BrokerOptions options = new BrokerOptions();
        String configFile = commandLine.getOptionValue(OPTION_CONFIG_FILE.getOpt());
        if(configFile != null)
        {
            options.setConfigFile(configFile);
        }

        String logWatchConfig = commandLine.getOptionValue(OPTION_LOG_WATCH.getOpt());
        if(logWatchConfig != null)
        {
            options.setLogWatchFrequency(Integer.parseInt(logWatchConfig));
        }

        String logConfig = commandLine.getOptionValue(OPTION_LOG_CONFIG_FILE.getOpt());
        if(logConfig != null)
        {
            options.setLogConfigFile(logConfig);
        }

        String jmxPortRegistryServer = commandLine.getOptionValue(OPTION_JMX_PORT_REGISTRY_SERVER.getOpt());
        if(jmxPortRegistryServer != null)
        {
            options.setJmxPortRegistryServer(Integer.parseInt(jmxPortRegistryServer));
        }

        String jmxPortConnectorServer = commandLine.getOptionValue(OPTION_JMX_PORT_CONNECTOR_SERVER.getLongOpt());
        if(jmxPortConnectorServer != null)
        {
            options.setJmxPortConnectorServer(Integer.parseInt(jmxPortConnectorServer));
        }

        String bindAddr = commandLine.getOptionValue(OPTION_BIND.getOpt());
        if (bindAddr != null)
        {
            options.setBind(bindAddr);
        }

        String[] portStr = commandLine.getOptionValues(OPTION_PORT.getOpt());
        if(portStr != null)
        {
            parsePortArray(options, portStr, false);
            for(ProtocolExclusion pe : ProtocolExclusion.values())
            {
                parsePortArray(options, commandLine.getOptionValues(pe.getExcludeName()), pe);
            }
        }

        String[] sslPortStr = commandLine.getOptionValues(OPTION_SSLPORT.getOpt());
        if(sslPortStr != null)
        {
            parsePortArray(options, sslPortStr, true);
            for(ProtocolExclusion pe : ProtocolExclusion.values())
            {
                parsePortArray(options, commandLine.getOptionValues(pe.getExcludeName()), pe);
            }
        }                
        
        setExceptionHandler();
        
        startBroker(options);
    }

    protected void setExceptionHandler()
    {
        Thread.UncaughtExceptionHandler handler = null;
        String handlerClass = System.getProperty("qpid.broker.exceptionHandler");
        if(handlerClass != null)
        {
            try
            {
                handler = (Thread.UncaughtExceptionHandler) Class.forName(handlerClass).newInstance();
            }
            catch (ClassNotFoundException e)
            {
                
            }
            catch (InstantiationException e)
            {
                
            }
            catch (IllegalAccessException e)
            {
                
            }
            catch (ClassCastException e)
            {

            }
        }
        
        if(handler == null)
        {
            handler =
                new Thread.UncaughtExceptionHandler()
                {
                    public void uncaughtException(final Thread t, final Throwable e)
                    {
                        try
                        {
                            System.err.println("########################################################################");
                            System.err.println("#");
                            System.err.print("# Unhandled Exception ");
                            System.err.print(e.toString());
                            System.err.print(" in Thread ");
                            System.err.println(t.getName());
                            System.err.println("#");
                            System.err.println("# Exiting");
                            System.err.println("#");
                            System.err.println("########################################################################");
                            e.printStackTrace(System.err);

                            Logger logger = Logger.getLogger("org.apache.qpid.server.Main");
                            logger.error("Uncaught exception, shutting down.", e);
                        }
                        finally
                        {
                            Runtime.getRuntime().halt(1);
                        }

                    }
                };

            Thread.setDefaultUncaughtExceptionHandler(handler);
        } 
    }

    protected void startBroker(final BrokerOptions options) throws Exception
    {
        Broker broker = new Broker();
        broker.startup(options);
    }

    protected void shutdown(final int status)
    {
        ApplicationRegistry.remove();
        System.exit(status);
    }

    private static void parsePortArray(final BrokerOptions options,final Object[] ports,
                                       final boolean ssl) throws InitException
    {
        if(ports != null)
        {
            for(int i = 0; i < ports.length; i++)
            {
                try
                {
                    if(ssl)
                    {
                        options.addSSLPort(Integer.parseInt(String.valueOf(ports[i])));
                    }
                    else
                    {
                        options.addPort(Integer.parseInt(String.valueOf(ports[i])));
                    }
                }
                catch (NumberFormatException e)
                {
                    throw new InitException("Invalid port: " + ports[i], e);
                }
            }
        }
    }

    private static void parsePortArray(final BrokerOptions options, final Object[] ports,
                                       final ProtocolExclusion excludedProtocol) throws InitException
    {
        if(ports != null)
        {
            for(int i = 0; i < ports.length; i++)
            {
                try
                {
                    options.addExcludedPort(excludedProtocol, 
                            Integer.parseInt(String.valueOf(ports[i])));
                }
                catch (NumberFormatException e)
                {
                    throw new InitException("Invalid port for exclusion: " + ports[i], e);
                }
            }
        }
    }
}