1616
1717import java .util .ArrayList ;
1818import java .util .Collections ;
19+ import java .util .EventObject ;
1920import java .util .LinkedHashMap ;
2021import java .util .LinkedList ;
2122import java .util .List ;
2223import java .util .Map ;
2324
2425import javax .xml .namespace .QName ;
2526
27+ import org .switchyard .Exchange ;
2628import org .switchyard .admin .Application ;
29+ import org .switchyard .admin .Component ;
30+ import org .switchyard .admin .ComponentReference ;
2731import org .switchyard .admin .ComponentService ;
2832import org .switchyard .admin .Reference ;
2933import org .switchyard .admin .Service ;
3034import org .switchyard .admin .Transformer ;
3135import org .switchyard .admin .Validator ;
3236import org .switchyard .config .model .composite .ComponentModel ;
37+ import org .switchyard .config .model .composite .ComponentReferenceModel ;
3338import org .switchyard .config .model .composite .ComponentServiceModel ;
3439import org .switchyard .config .model .composite .CompositeReferenceModel ;
3540import org .switchyard .config .model .composite .CompositeServiceModel ;
3641import org .switchyard .config .model .composite .InterfaceModel ;
3742import org .switchyard .config .model .property .PropertyModel ;
38- import org .switchyard .config .model .switchyard .EsbInterfaceModel ;
3943import org .switchyard .config .model .switchyard .SwitchYardModel ;
4044import org .switchyard .config .model .transform .TransformModel ;
4145import org .switchyard .config .model .validate .ValidateModel ;
46+ import org .switchyard .deploy .ComponentNames ;
4247import org .switchyard .deploy .internal .AbstractDeployment ;
48+ import org .switchyard .event .EventObserver ;
49+ import org .switchyard .runtime .event .ExchangeCompletionEvent ;
4350
4451/**
4552 * Base implementation of Application.
4653 */
47- public class BaseApplication implements Application {
54+ public class BaseApplication implements Application , EventObserver {
4855
4956 private final QName _name ;
5057 private Map <QName , Service > _services ;
5158 private Map <QName , Reference > _references ;
59+ private Map <QName , Component > _components ;
5260 private Map <QName , ComponentService > _componentServices ;
5361 private List <Transformer > _transformers ;
5462 private List <Validator > _validators ;
@@ -69,6 +77,15 @@ public BaseApplication(AbstractDeployment deployment) {
6977 addServices ();
7078 addReferences ();
7179 addProperties ();
80+
81+
82+ // register event listener for metrics
83+ _deployment .getDomain ().addEventObserver (this , ExchangeCompletionEvent .class );
84+ }
85+
86+ void dispose () {
87+ // remove event listener for metrics
88+ _deployment .getDomain ().removeObserver (this );
7289 }
7390
7491 @ Override
@@ -129,6 +146,17 @@ public ComponentService getComponentService(QName componentServiceName) {
129146 return _componentServices .get (componentServiceName );
130147 }
131148
149+ @ Override
150+ public Component getComponent (QName name ) {
151+ return _components .get (name );
152+ }
153+
154+ @ Override
155+ public List <Component > getComponents () {
156+ return new ArrayList <Component >(_components .values ());
157+ }
158+
159+
132160 @ Override
133161 public List <Transformer > getTransformers () {
134162 return Collections .unmodifiableList (_transformers );
@@ -145,6 +173,13 @@ public Map<String, String> getProperties() {
145173 return Collections .unmodifiableMap (_properties );
146174 }
147175
176+ @ Override
177+ public void notify (EventObject event ) {
178+ if (event instanceof ExchangeCompletionEvent ) {
179+ exchangeCompleted ((ExchangeCompletionEvent )event );
180+ }
181+ }
182+
148183 /**
149184 * @return the deployment associated with this application.
150185 */
@@ -195,35 +230,121 @@ private void addValidators() {
195230 }
196231
197232 private void addComponents () {
233+ _components = new LinkedHashMap <QName , Component >();
198234 _componentServices = new LinkedHashMap <QName , ComponentService >();
199235 if (getConfig ().getComposite ().getComponents () == null ) {
200236 return ;
201237 }
202- for (ComponentModel component : getConfig ().getComposite ().getComponents ()) {
203- // TODO: we need a separate node for components, to support cases
204- // where the component implements no services. Should also consider
205- // multiple services per component.
206- if (component .getServices ().size () > 0 ) {
207- ComponentServiceModel service = component .getServices ().get (0 );
208- if (service .getInterface () == null || EsbInterfaceModel .ESB .equals (service .getInterface ().getType ())) {
209- _componentServices .put (service .getQName (), new BaseNoopComponentService (service , component , this ));
210- } else if (InterfaceModel .JAVA .equals (service .getInterface ().getType ())) {
211- _componentServices .put (service .getQName (), new BaseJavaComponentService (service , component , this ));
212- } else if (InterfaceModel .WSDL .equals (service .getInterface ().getType ())) {
213- _componentServices .put (service .getQName (), new BaseWsdlComponentService (service , component , this ));
238+
239+ for (ComponentModel componentConfig : getConfig ().getComposite ().getComponents ()) {
240+ // TODO: Should consider multiple services per component.
241+ final ComponentService service ;
242+ if (componentConfig .getServices ().size () > 0 ) {
243+ ComponentServiceModel serviceConfig = componentConfig .getServices ().get (0 );
244+ if (serviceConfig .getInterface () == null ) {
245+ service = new BaseNoopComponentService (serviceConfig , componentConfig , this );
246+ } else if (InterfaceModel .JAVA .equals (serviceConfig .getInterface ().getType ())) {
247+ service = new BaseJavaComponentService (serviceConfig , componentConfig , this );
248+ } else if (InterfaceModel .WSDL .equals (serviceConfig .getInterface ().getType ())) {
249+ service = new BaseWsdlComponentService (serviceConfig , componentConfig , this );
250+ } else {
251+ // ESB or unknown
252+ service = new BaseNoopComponentService (serviceConfig , componentConfig , this );
214253 }
254+ _componentServices .put (serviceConfig .getQName (), service );
255+ } else {
256+ service = null ;
215257 }
258+ final Map <QName , ComponentReference > references = new LinkedHashMap <QName , ComponentReference >();
259+ if (service == null ) {
260+ for (ComponentReferenceModel referenceModel : componentConfig .getReferences ()) {
261+ references .put (referenceModel .getQName (), new BaseComponentReference (referenceModel .getQName (),
262+ getInterfaceName (referenceModel .getInterface ())));
263+ }
264+ } else {
265+ for (ComponentReference reference : service .getReferences ()) {
266+ references .put (reference .getName (), reference );
267+ }
268+ }
269+ final BaseComponent component = new BaseComponent (componentConfig .getQName (),
270+ componentConfig .getImplementation () == null ? "null" : componentConfig .getImplementation ()
271+ .getType (), service , references , convertProperties (componentConfig .getProperties ()));
272+ _components .put (component .getName (), component );
216273 }
217274 }
218275
219276 private void addProperties () {
220- _properties = new LinkedHashMap <String , String >();
221277 if (getConfig ().getComposite () == null ) {
222- return ;
278+ _properties = convertProperties (null );
279+ } else {
280+ _properties = convertProperties (getConfig ().getComposite ().getProperties ());
281+ }
282+ }
283+
284+ private Map <String ,String > convertProperties (final Map <String , PropertyModel > properties ) {
285+ final Map <String ,String > retVal = new LinkedHashMap <String , String >();
286+ if (properties == null ) {
287+ return retVal ;
223288 }
224- for (PropertyModel property : getConfig (). getComposite (). getProperties () .values ()) {
225- _properties .put (property .getName (), property .getValue ());
289+ for (PropertyModel property : properties .values ()) {
290+ retVal .put (property .getName (), property .getValue ());
226291 }
292+ return retVal ;
293+ }
294+
295+ private String getInterfaceName (InterfaceModel interfaceModel ) {
296+ if (interfaceModel == null ) {
297+ return null ;
298+ }
299+ return interfaceModel .getInterface ();
227300 }
228301
302+ void exchangeCompleted (final ExchangeCompletionEvent event ) {
303+ // Recording metrics at multiple levels at this point instead of
304+ // aggregating them.
305+ final Exchange exchange = event .getExchange ();
306+ final QName qualifiedReferenceName = exchange .getConsumer ().getName ();
307+ final QName referenceName = ComponentNames .unqualify (qualifiedReferenceName );
308+ final QName componentName = ComponentNames .componentName (qualifiedReferenceName );
309+ if (componentName == null ) {
310+ // service gateway initiated exchange
311+ final Service service = _services .get (referenceName );
312+ if (service != null ) {
313+ /*
314+ * service also records promoted component service metrics too
315+ * (i.e. producer metrics)
316+ */
317+ service .recordMetrics (exchange );
318+ }
319+ } else {
320+ // component reference initiated exchange
321+ // 1 - recored service metrics (producer)
322+ final QName serviceName = exchange .getProvider ().getName ();
323+ final ComponentService service = _componentServices .get (serviceName );
324+ if (service == null ) {
325+ // must be routed to composite reference
326+ final Reference reference = _references .get (serviceName );
327+ if (reference != null ) {
328+ reference .recordMetrics (exchange );
329+ }
330+ } else {
331+ /*
332+ * XXX: this could throw off metrics for composite services
333+ * since they simply return the metrics for the component
334+ * service they promote. That said, the metrics for the gateways
335+ * will correlate with the global metrics.
336+ */
337+ service .recordMetrics (exchange );
338+ }
339+ // 2 - record reference metrics (consumer)
340+ final Component component = _components .get (componentName );
341+ if (component != null ) {
342+ final ComponentReference reference = component .getReference (referenceName );
343+ if (reference != null ) {
344+ reference .recordMetrics (exchange );
345+ }
346+ // else may have been an internal invocation, e.g. orders demo
347+ }
348+ }
349+ }
229350}
0 commit comments