summaryrefslogtreecommitdiff
path: root/java/newclient/src/main/java/org/apache/qpid/nclient/model/ModelPhase.java
blob: 77003b4d215f584790cd44ee28e826a0b30db85e (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
package org.apache.qpid.nclient.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.qpid.nclient.amqp.state.AMQPStateManager;
import org.apache.qpid.nclient.config.ClientConfiguration;
import org.apache.qpid.nclient.core.AMQPException;
import org.apache.qpid.nclient.core.AbstractPhase;
import org.apache.qpid.nclient.core.Phase;
import org.apache.qpid.nclient.core.PhaseContext;
import org.apache.qpid.nclient.core.QpidConstants;

/**
 * This Phase handles Layer 3 functionality of the AMQP spec.
 * This class acts as the interface between the API and the pipeline
 */
public class ModelPhase extends AbstractPhase {

	private static final Logger _logger = Logger.getLogger(ModelPhase.class);
	
	private Map <Class,List> _methodListners = new HashMap<Class,List>();
	
	/**
	 * ------------------------------------------------
	 * Phase - methods introduced by Phase
	 * ------------------------------------------------
	 */    
	public void init(PhaseContext ctx, Phase nextInFlowPhase, Phase nextOutFlowPhase) 
	{
		super.init(ctx, nextInFlowPhase, nextOutFlowPhase);
		try
		{
			loadMethodListeners();
		}
		catch(Exception e)
		{
			_logger.fatal("Error loading method listeners", e);
		}
	}

	public void messageReceived(Object msg) throws AMQPException 
	{
		notifyMethodListerners((AMQPMethodEvent)msg);
		
		// not doing super.methodReceived here, as this is the end of
		// the pipeline
		//super.messageReceived(msg);
	}

	/**
	 * This method should only except and pass messages
	 * of Type @see AMQPMethodEvent
	 */
	public void messageSent(Object msg) throws AMQPException 
	{		
		super.messageSent(msg);
	}
	
	/**
	 * ------------------------------------------------
	 *  Event Handling 
	 * ------------------------------------------------
	 */
	
	public void notifyMethodListerners(AMQPMethodEvent event) throws AMQPException
	{
		if (_methodListners.containsKey(event.getMethod().getClass()))
		{
			List<AMQPMethodListener> listeners = _methodListners.get(event.getMethod().getClass()); 
		
			if(listeners.size()>0)
			{
				throw new AMQPException("There are no registered listeners for this method");
			}
			
			for(AMQPMethodListener l : listeners)
			{
				try 
				{
					l.methodReceived(event);
				} 
				catch (Exception e) 
				{
					_logger.error("Error handling method event " +  event, e);
				}
			}
		}
	}
	
	/**
	 * ------------------------------------------------
	 *  Configuration 
	 * ------------------------------------------------
	 */    

	/**
	 * This method loads method listeners from the client.xml file
	 * For each method class there is a list of listeners
	 */
	private void loadMethodListeners() throws Exception
	{
		int count = ClientConfiguration.get().getMaxIndex(QpidConstants.METHOD_LISTENERS + "." + QpidConstants.METHOD_LISTENER);
		System.out.println(count);
				
		for(int i=0 ;i<count;i++)
		{
			String methodListener = QpidConstants.METHOD_LISTENERS + "." + QpidConstants.METHOD_LISTENER + "(" + i + ")";
			String className =  ClientConfiguration.get().getString(methodListener + "." + QpidConstants.CLASS);
			Class listenerClass = Class.forName(className);
			List<String> list = ClientConfiguration.get().getList(methodListener + "." + QpidConstants.METHOD_CLASS);
			for(String s:list)
			{
				List listeners;
				Class methodClass = Class.forName(s);
				if (_methodListners.containsKey(methodClass))
				{
					listeners = _methodListners.get(methodClass); 
				}
				else
				{
					listeners = new ArrayList();
					_methodListners.put(methodClass,listeners);
				}
				listeners.add(listenerClass);
			}
		}		
	}
	
}