summaryrefslogtreecommitdiff
path: root/java/management/client/src/example/org/apache/qpid/management/example/AbstractQManExample.java
blob: ffa96635a84dbe3a576ccc0f35820359cf2b7c5a (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
/*
 *
 * 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.example;

import java.io.IOException;
import java.net.URI;

import org.apache.muse.ws.addressing.EndpointReference;

/**
 * Common interface for all QMan related examples.
 * 
 * @author Andrea Gazzarini
 */
public abstract class AbstractQManExample
{
	final static String LINE_SEPARATOR = System.getProperty("line.separator","\n");
	protected final static String PREFIX = "qman";

	/**
	 * Prints out the expected command line of this sample and after that exits.
	 */
	static void printUsageAndExit(String reason)
	{
		StringBuilder builder = new StringBuilder();
		builder.append("WARNING! Unable to run this sample : ")
			.append(reason)
			.append(LINE_SEPARATOR)
			.append("-------------------------------------------------------------")
			.append(LINE_SEPARATOR)			
			.append("Expected command line args for this sample are :")
			.append(LINE_SEPARATOR)
			.append(LINE_SEPARATOR)
			.append("1) host : ip or host name where QMan is running.")
			.append(LINE_SEPARATOR)
			.append("2) port : port number where QMan is running.")
			.append(LINE_SEPARATOR)
			.append("------------------------------------------------------------");
		System.out.println(builder);
		System.exit(1);
	}

	/**
	 * Prints out a description of this example.
	 */
	abstract void printOutExampleDescription();

	/**
	 * Executes this example.
	 * Note that this is just a template method used to avoid code duplication 
	 * (printOutExampleDescription() line) so in order to see how the example 
	 * works you should have a look at the concrete implementation of 
	 * executeExample(String host, int port).
	 * 
	 * @param host the host where QMan is running.
	 * @param port the port where QMan is running.
	 */
	void execute(String [] arguments)
	{
		if (arguments.length != 2){
			printUsageAndExit("invalid command line was given.");
		}

		try 
		{
			// 1) Parses command line arguments...
			String host = arguments[0];
			int port = Integer.parseInt(arguments[1]);
			
			printOutExampleDescription();
			
			waitForUserInput("Type enter to proceed...");
			
			executeExample(host, port);
			
		} catch(NumberFormatException exception)
		{
			printUsageAndExit("port number must be a number.");
		} catch(Exception exception)
		{
			System.out.println("-----------------------EXAMPLE FAILURE-----------");
			System.out.println("Not well-defined exception was detected while");
			System.out.println("running the example.");
			exception.printStackTrace(System.out);
			System.out.println("--------------------------------------------------------");						
		}
	}		
	
	protected void waitForUserInput(String message) throws IOException {
		System.out.println(message);
		System.in.read();
	}
	
	/**
	 * Each concrete implementor must define here how the example works.
	 * So, on top of that, user who wants to see how to use a specific feature 
	 * should have a look at the concrete implementation of this method..
	 * 
	 * @param host the host where QMan is running.
	 * @param port the port where QMan is running.
	 * @throws Exception when the example fails (not at application level).
	 */
	void executeExample(String host, int port) throws Exception{};
	
	/**
	 * Returns the endpoint reference of the adapter service.
	 * 
	 * @param host ip or host name where the service is running.
	 * @param port the port number of the server where the service is running.
	 * @return the endpoint reference of the adapter service.
	 */
	EndpointReference getAdapterEndpointReference(String host, int port)
	{
		URI address = URI.create(
				"http://"+
				host+
				":"+
				port+
				"/qman/services/adapter");
		return new EndpointReference(address);		
	}	
}