summaryrefslogtreecommitdiff
path: root/qpid/java/management/client/src/main/java/org/apache/qpid/management/domain/services/QMan.java
blob: c4c0ce5e74b54eb6168c54fe3f7e29dc5f69163a (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 *
 * 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.management.domain.services;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.DynamicMBean;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.ReflectionException;

import org.apache.log4j.xml.DOMConfigurator;
import org.apache.qpid.management.Messages;
import org.apache.qpid.management.Names;
import org.apache.qpid.management.configuration.BrokerAlreadyConnectedException;
import org.apache.qpid.management.configuration.BrokerConnectionData;
import org.apache.qpid.management.configuration.BrokerConnectionException;
import org.apache.qpid.management.configuration.Configuration;
import org.apache.qpid.management.configuration.Configurator;
import org.apache.qpid.management.domain.model.JmxService;
import org.apache.qpid.transport.util.Logger;

/**
 * Main entry point for starting Q-Man application.
 */
public class QMan extends NotificationBroadcasterSupport implements DynamicMBean, NotificationListener
{
    private final static Logger LOGGER = Logger.get(QMan.class);
    private final List<ManagementClient> managementClients = new ArrayList<ManagementClient>();
    
    private Configurator _configurator = new Configurator();
    private ThreadPoolExecutor _workManager;
    
    /**
     * Starts QMan.
     * @throws StartupFailureException when it's not possible to proceed with startup.
     */
    public void start() throws StartupFailureException
    {
        LOGGER.info(Messages.QMAN_000001_STARTING_QMAN);
        LOGGER.info(Messages.QMAN_000002_READING_CONFIGURATION);

        try
        {
        	 registerQManService();
        	 
            _configurator.configure();            
            
            configureWorkManager();
            
            LOGGER.info(Messages.QMAN_000019_QMAN_STARTED);
       } catch(Exception exception) {
            LOGGER.error(exception,Messages.QMAN_100018_UNABLE_TO_STARTUP_CORRECTLY );
            throw new StartupFailureException(exception);
        } 
    }
    
	/**
	 * Connects Q-Man with a broker defined by the given parameter.
	 * 
	 * @param host the hostname where the broker is running.
	 * @param port the port where the broker is running.
	 * @param username the username for connecting with the broker.
	 * @param password the password for connecting with the broker.
	 * @param virtualHost the virtual host.
	 * @param initialPoolCapacity the number of the connection that must  be immediately opened.
	 * @param maxPoolCapacity the maximum number of opened connection.
	 * @param maxWaitTimeout the maximum amount of time that a client will wait for obtaining a connection.
	 * @throws MBeanException when it's not possible to connect with the broker.
	 */
	public void addBroker(
			String host, 
			int port, 
			String username,
			String password, 
			String virtualHost, 
			int initialPoolCapacity,
			int maxPoolCapacity, 
			long maxWaitTimeout) throws BrokerAlreadyConnectedException, BrokerConnectionException
	{
		Configurator configurator = new Configurator();
		try {
			UUID brokerId = UUID.randomUUID();
			BrokerConnectionData data = configurator.createAndReturnBrokerConnectionData(
					brokerId,
					host,
					port, 
					username,
					password, 
					virtualHost, 
					initialPoolCapacity,
					maxPoolCapacity, 
					maxWaitTimeout);
			createManagementClient(brokerId, data);
		} catch (BrokerAlreadyConnectedException exception) 
		{
			LOGGER.warn(Messages.QMAN_300003_BROKER_ALREADY_CONNECTED, exception.getBrokerConnectionData());
			throw exception;
		}
	}
	
    /**
     * Stop Qman
     */
    public void stop() 
    {
        LOGGER.info(Messages.QMAN_000020_SHUTTING_DOWN_QMAN);
        try 
        {
            for (ManagementClient client : managementClients)
            {   
                client.shutdown();  
            }
        } catch(Exception exception)
        {
        }
        LOGGER.info(Messages.QMAN_000021_SHUT_DOWN);                    	
    }
    
	/**
     * Creates a management client using the given data.
     * 
     * @param brokerId the broker identifier.
     * @param data the broker connection data.
     */
    public void createManagementClient(UUID brokerId, BrokerConnectionData data)
    {
        try 
        {
            ManagementClient client = new ManagementClient(brokerId,data);
            client.estabilishFirstConnectionWithBroker();
            managementClients.add(client);
            
            LOGGER.info(Messages.QMAN_000004_MANAGEMENT_CLIENT_CONNECTED,brokerId);
        } catch(StartupFailureException exception) {
            LOGGER.error(exception, Messages.QMAN_100017_UNABLE_TO_CONNECT,brokerId,data);
        }
    }    
    
    /**
     * Returns the list of management clients currently handled by QMan.
     * 
     * @return the list of management clients currently handled by QMan.
     */
    public List<ManagementClient> getManagementClients()
    {
    	return managementClients;
    }
    
    /**
     * Injects the configurator on this QMan instance.
     * That configutator later will be responsible to manage the configuration.
     * 
     * @param configurator the configurator to be injected.
     */
    public void setConfigurator(Configurator configurator){
    	this._configurator = configurator;
    }
    
    /**
     * Main method used for starting Q-Man.
     * 
     * @param args the command line arguments.
     */
    public static void main (String[] args)
    {  
    	if (args.length == 1) 
    	{
    		String logFileName = args[0];
    		DOMConfigurator.configureAndWatch(logFileName,5000);
    	}
    	
		final QMan qman = new QMan();
		
		Thread hook = new Thread()
        {
            @Override
            public void run ()
            {
            	qman.stop();
            }
        };    	
		
		Runtime.getRuntime().addShutdownHook(hook);
		try 
		{
			qman.start();
		    
			System.out.println("Type \"q\" to quit.");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            while ( !"q".equals(reader.readLine()) )
            {
            	
            }
            Runtime.getRuntime().removeShutdownHook(hook);
            qman.stop();
			System.exit(-1);
		} catch (StartupFailureException exception) 
		{
			qman.stop();
			System.exit(-1);
		} catch (IOException exception)
		{
			System.exit(-1);					
		}
    }
    
    /**
     * Not implemented for this MBean.
     */
	public Object getAttribute(String attribute)
	{
		return null;
	}

    /**
     * Not implemented for this MBean.
     */
	public AttributeList getAttributes(String[] attributes) 
	{
		return null;
	}

    /**
     * Returns the metadata for this MBean
     * 
     * @return the metadata for this MBean
     */
	public MBeanInfo getMBeanInfo() 
	{
		MBeanParameterInfo parameters [] = new MBeanParameterInfo[8];
		
		parameters[0] = new MBeanParameterInfo(
				"host",
				String.class.getName(),
				"The IP address or DNS name that Qpid Broker uses to listen for incoming connections.");
		parameters[1] = new MBeanParameterInfo(
				"port",
				int.class.getName(),
				"The port number that Qpid Broker uses to listen for incoming connections.");
		parameters[2] = new MBeanParameterInfo(
				"username",
				String.class.getName(),
				"The Qpid account name used in the physical connection.");
		parameters[3] = new MBeanParameterInfo(
				"password",
				String.class.getName(),
				"The Qpid account password used in the physical connection.");
		parameters[4]= new MBeanParameterInfo(
				"virtualHost",
				String.class.getName(),
				"The virtualHost name.");
		parameters[5]= new MBeanParameterInfo(
				"initialPoolCapacity",
				int.class.getName(),
				"The number of physical connections (between 0 and a positive 32-bit integer) to create when creating the (Qpid) connection pool.");
		parameters[6]= new MBeanParameterInfo(
				"maxPoolCapacity",
				int.class.getName(),
				"The maximum number of physical database connections (between 0 and a positive 32-bit integer) that the (Qpid) connection pool can contain. ");
		parameters[7]= new MBeanParameterInfo(
				"maxWaitTimeout",
				long.class.getName(),
				"The maximum amount of time to wait for an idle connection.A value of -1 indicates an illimted amount of time (i.e. forever)");
				
		MBeanOperationInfo operation = new MBeanOperationInfo(
				"addBroker",
				"Connects QMan with a broker.",
				parameters,
				void.class.getName(),
				MBeanOperationInfo.ACTION);
		
		MBeanInfo mbean = new MBeanInfo(
				QMan.class.getName(),
				"QMan Management & Administration interface.",
				null,
				null,
				new MBeanOperationInfo[]{operation},
				null);
		
		return mbean;
	}

	/**
	 * Invokes an operation on QMan (MBean).
	 * 
	 * @param actionName the operation name.
	 * @param params the operation parameters.
	 * @param signature the operation signature.
	 * @return the result of the invocation (if the operation is not void);
	 * @exception MBeanException  Wraps a <CODE>java.lang.Exception</CODE> thrown by the MBean's invoked method.
     * @exception ReflectionException  Wraps a <CODE>java.lang.Exception</CODE> thrown while trying to invoke the method
	 */
	public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException 
	{
		if (Names.ADD_BROKER_OPERATION_NAME.equals(actionName))
		{
			try 
			{
				addBroker(
						(String)params[0], 
						(Integer)params[1], 
						(String)params[2], 
						(String)params[3], 
						(String)params[4], 
						(Integer)params[5], 
						(Integer)params[6], 
						(Long)params[7]); 
			} catch(Exception exception)
			{
				throw new MBeanException(exception);
			}
		} else 
		{
			throw new ReflectionException(new NoSuchMethodException(actionName));
		}
		return null;
	}

    /**
     * Not implemented for this MBean.
     */
	public void setAttribute(Attribute attribute) 
	{		
	}

    /**
     * Not implemented for this MBean.
     */
	public AttributeList setAttributes(AttributeList attributes) 
	{
		return null;
	}    

	/**
	 * Simply dispatches the incoming notification to registered listeners.
	 * Consider that the notification is sent asynchronously so the QMan current thread is not 
	 * waiting for completion of receiver task.
	 * 
	 * @param notification the incoming notification.
	 * @param handback the context associated to this notification.
	 */
	public void handleNotification(final Notification notification, Object handback) 
	{
		_workManager.execute(new Runnable(){
			public void run()
			{
				sendNotification(notification);
			}
		});
	}	
	
    /**
     * Registers QMan as an MBean on MBeanServer.
     * 
     * @throws MBeanException when it's not possible to proceeed with registration.
     */
    private void registerQManService() throws MBeanException 
    {    	
    	JmxService service = new JmxService(); 
    	service.registerQManService(this);
    	
    	LOGGER.info(Messages.QMAN_000023_QMAN_REGISTERED_AS_MBEAN);
	}
    
    /**
     * Configures work manager component. 
     */
	private void configureWorkManager()
	{
		Configuration configuration = Configuration.getInstance();
		_workManager = new ThreadPoolExecutor(
				configuration.getWorkerManagerPoolSize(),
				configuration.getWorkerManagerMaxPoolSize(),
				configuration.getWorkerManagerKeepAliveTime(),
				TimeUnit.MILLISECONDS,
				new ArrayBlockingQueue<Runnable>(30));
	}
}