1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.listener;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import com.eviware.soapui.SoapUI;
24 import com.eviware.soapui.config.SoapUIListenerConfig;
25 import com.eviware.soapui.config.SoapUIListenersConfig;
26 import com.eviware.soapui.config.SoapuiListenersDocumentConfig;
27
28 public class SoapUIListenerRegistry
29 {
30 private Map<Class<?>, List<Class<?>>> listeners = new HashMap<Class<?>, List<Class<?>>>();
31 private Map<Class<?>, List<Object>> singletonListeners = new HashMap<Class<?>, List<Object>>();
32 private Map<Class<?>, SoapUIListenerConfig> listenerConfigs = new HashMap<Class<?>, SoapUIListenerConfig>();
33
34 public void addListener( Class<?> listenerInterface, Class<?> listenerClass, SoapUIListenerConfig config )
35 {
36 List<Class<?>> classes = null;
37 if( listeners.containsKey( listenerInterface ) )
38 {
39 classes = listeners.get( listenerInterface );
40 }
41 if( classes == null )
42 {
43 classes = new ArrayList<Class<?>>();
44 }
45 classes.add( listenerClass );
46 listeners.put( listenerInterface, classes );
47
48 if( config != null )
49 {
50 listenerConfigs.put( listenerClass, config );
51 }
52 }
53
54 public void removeListener( Class<?> listenerInterface, Class<?> listenerClass )
55 {
56 List<Class<?>> classes = null;
57 if( listeners.containsKey( listenerInterface ) )
58 {
59 classes = listeners.get( listenerInterface );
60 }
61 if( classes != null )
62 {
63 classes.remove( listenerClass );
64 }
65 if( classes == null || classes.size() == 0 )
66 {
67 listeners.remove( listenerInterface );
68 }
69
70 listenerConfigs.remove( listenerClass );
71 }
72
73 public SoapUIListenerRegistry( InputStream config )
74 {
75 if( config != null )
76 addConfig( config, getClass().getClassLoader() );
77 }
78
79 public void addConfig( InputStream config, ClassLoader classLoader )
80 {
81 try
82 {
83 SoapuiListenersDocumentConfig configDocument = SoapuiListenersDocumentConfig.Factory.parse( config );
84 SoapUIListenersConfig soapuiListeners = configDocument.getSoapuiListeners();
85
86 for( SoapUIListenerConfig listenerConfig : soapuiListeners.getListenerList() )
87 {
88 try
89 {
90 String listenerInterfaceName = listenerConfig.getListenerInterface();
91 String listenerClassName = listenerConfig.getListenerClass();
92
93 Class<?> listenerInterface = Class.forName( listenerInterfaceName, true, classLoader );
94 Class<?> listenerClass = Class.forName( listenerClassName, true, classLoader );
95 if( !listenerInterface.isInterface() )
96 {
97 throw new RuntimeException( "Listener interface: " + listenerInterfaceName + " must be an interface" );
98 }
99 if( !listenerInterface.isAssignableFrom( listenerClass ) )
100 {
101 throw new RuntimeException( "Listener class: " + listenerClassName + " must implement interface: "
102 + listenerInterfaceName );
103 }
104
105
106 Object obj = listenerClass.newInstance();
107 if( listenerConfig.getSingleton() )
108 {
109 if( obj instanceof InitializableListener )
110 {
111 ((InitializableListener)obj).init( listenerConfig );
112 }
113
114 addSingletonListener( listenerInterface, obj );
115 }
116 else
117 {
118
119 addListener( listenerInterface, listenerClass, listenerConfig );
120 }
121 }
122 catch( Exception e )
123 {
124 System.err.println( "Error initializing Listener: " + e );
125 }
126 }
127 }
128 catch( Exception e )
129 {
130 SoapUI.logError( e );
131 }
132 finally
133 {
134 try
135 {
136 config.close();
137 }
138 catch( IOException e )
139 {
140 SoapUI.logError( e );
141 }
142 }
143 }
144
145 public void addSingletonListener( Class<?> listenerInterface, Object listener )
146 {
147 if( !singletonListeners.containsKey( listenerInterface ))
148 singletonListeners.put( listenerInterface, new ArrayList<Object>() );
149
150 singletonListeners.get( listenerInterface ).add( listener );
151 }
152
153 public void removeSingletonListener( Class<?> listenerInterface, Object listener )
154 {
155 if( singletonListeners.containsKey( listenerInterface ))
156 singletonListeners.get( listenerInterface ).remove( listener );
157 }
158
159 @SuppressWarnings("unchecked")
160 public <T extends Object> List<T> getListeners( Class<T> listenerType )
161 {
162 List<T> result = new ArrayList<T>();
163 if( listeners.containsKey( listenerType ) )
164 {
165 List<Class<?>> list = listeners.get( listenerType );
166 for( Class<?> listenerClass : list )
167 {
168 try
169 {
170 T listener = ( T ) listenerClass.newInstance();
171 if( listenerConfigs.containsKey( listenerClass ) && listener instanceof InitializableListener )
172 ((InitializableListener)listener).init( listenerConfigs.get( listenerClass ) );
173
174 result.add( listener );
175 }
176 catch( Exception e )
177 {
178 SoapUI.logError( e );
179 }
180 }
181 }
182
183 if( singletonListeners.containsKey( listenerType ))
184 result.addAll( ( Collection<? extends T> ) singletonListeners.get( listenerType) );
185
186 return result;
187 }
188 }