View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.component.agent.impl.worldbank;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  
24  import javax.jms.JMSException;
25  import javax.jms.Message;
26  import javax.jms.ObjectMessage;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.stereotype.Service;
32  
33  import com.hack23.cia.model.internal.application.data.impl.WorldBankDataSources;
34  import com.hack23.cia.service.component.agent.impl.common.AbstractAgentWorkConsumerImpl;
35  import com.hack23.cia.service.component.agent.impl.worldbank.workgenerator.WorldBankDataSourcesWorkGenerator;
36  
37  /**
38   * The Class WorldBankApiAgentWorkConsumerImpl.
39   */
40  @Service("WorldBankApiAgentWorkConsumer")
41  final class WorldBankApiAgentWorkConsumerImpl extends AbstractAgentWorkConsumerImpl {
42  
43  	/** The Constant LOGGER. */
44  	private static final Logger LOGGER = LoggerFactory.getLogger(WorldBankApiAgentWorkConsumerImpl.class);
45  
46  	/** The beans of type. */
47  	private final Map<String, WorldBankDataSourcesWorkGenerator> beansOfType;
48  
49  	/**
50  	 * Instantiates a new world bank api agent work consumer impl.
51  	 *
52  	 * @param context
53  	 *            the context
54  	 */
55  	public WorldBankApiAgentWorkConsumerImpl(final ApplicationContext context) {
56  		super();
57  		beansOfType = context.getBeansOfType(WorldBankDataSourcesWorkGenerator.class);
58  	}
59  
60  	@Override
61  	public void onMessage(final Message message) {
62  		final ObjectMessage msg = (ObjectMessage) message;
63  
64  		try {
65  			LOGGER.info("Consumed message:{}", msg.getObject());
66  			final WorldBankDataSources dataSource = (WorldBankDataSources) msg.getObject();
67  
68  			final Collection<WorldBankDataSourcesWorkGenerator> values = beansOfType.values();
69  
70  			for (final WorldBankDataSourcesWorkGenerator worldBankDataSourcesWorkGenerator : values) {
71  				if (worldBankDataSourcesWorkGenerator.matches(dataSource)) {
72  					worldBankDataSourcesWorkGenerator.generateWorkOrders();
73  					return;
74  				}
75  			}
76  			LOGGER.warn("Missing import for :{}", dataSource);
77  
78  		} catch (final JMSException exception) {
79  			LOGGER.warn("jms", exception);
80  		}
81  	}
82  
83  }