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.riksdagen;
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.RiksdagenDataSources;
34  import com.hack23.cia.service.component.agent.impl.common.AbstractAgentWorkConsumerImpl;
35  import com.hack23.cia.service.component.agent.impl.riksdagen.workgenerator.RiksdagenDataSourcesWorkGenerator;
36  
37  /**
38   * The Class RiksdagenApiAgentWorkConsumerImpl.
39   */
40  @Service("RiksdagenApiAgentWorkConsumer")
41  final class RiksdagenApiAgentWorkConsumerImpl extends AbstractAgentWorkConsumerImpl {
42  
43  	/** The Constant LOGGER. */
44  	private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenApiAgentWorkConsumerImpl.class);
45  
46  	private final Map<String, RiksdagenDataSourcesWorkGenerator> beansOfType;
47  
48  	/**
49  	 * Instantiates a new riksdagen api agent work consumer impl.
50  	 */
51  	public RiksdagenApiAgentWorkConsumerImpl(final ApplicationContext context) {
52  		super();
53  		beansOfType = context.getBeansOfType(RiksdagenDataSourcesWorkGenerator.class);
54  	}
55  
56  	@Override
57  	public void onMessage(final Message message) {
58  		final ObjectMessage msg = (ObjectMessage) message;
59  
60  		try {
61  			LOGGER.info("Consumed message:{}", msg.getObject());
62  			final RiksdagenDataSources dataSource = (RiksdagenDataSources) msg.getObject();
63  
64  			final Collection<RiksdagenDataSourcesWorkGenerator> values = beansOfType.values();
65  
66  			for (final RiksdagenDataSourcesWorkGenerator riksdagenDataSourcesWorkGenerator : values) {
67  				if (riksdagenDataSourcesWorkGenerator.matches(dataSource)) {
68  					riksdagenDataSourcesWorkGenerator.generateWorkOrders();
69  					return;
70  				}
71  			}
72  			LOGGER.warn("Missing import for :{}", dataSource);
73  
74  		} catch (final JMSException exception) {
75  			LOGGER.warn("jms", exception);
76  		}
77  	}
78  
79  }