1 /** 2 * This file Copyright (c) 2008-2011 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.module.rssaggregator.generator; 35 36 import info.magnolia.module.ModuleRegistry; 37 import info.magnolia.module.rssaggregator.RSSAggregator; 38 import info.magnolia.module.rssaggregator.util.Assert; 39 40 import java.lang.reflect.InvocationTargetException; 41 import java.util.Map; 42 43 import javax.inject.Inject; 44 45 import org.apache.commons.beanutils.BeanUtils; 46 import org.apache.commons.collections.FactoryUtils; 47 import org.slf4j.Logger; 48 import org.slf4j.LoggerFactory; 49 50 /** 51 * Responsible for resolving {@link FeedGenerator}s based on given parameters. In order to resolve a {@link 52 * FeedGenerator}, a {@link FeedGeneratorFactory} capable of building it must first be {@link 53 * #registerFeedGeneratorFactory(String, FeedGeneratorFactory) registered} with a given name. 54 * 55 * @author Rob van der Linden Vooren 56 * @see FeedGeneratorFactory 57 * @see FeedGenerator 58 */ 59 public class FeedGeneratorResolver { 60 61 public static final String REQUIRED_PARAMETER = "generatorName"; 62 private static final Logger log = LoggerFactory.getLogger(AbstractSyndFeedGenerator.class); 63 private final ModuleRegistry moduleRegistry; 64 private final FeedGenerator feederGenerator; 65 66 @Inject 67 public FeedGeneratorResolver(ModuleRegistry moduleRegistry,FeedGenerator feederGenerator) { 68 this.moduleRegistry = moduleRegistry; 69 this.feederGenerator = feederGenerator; 70 } 71 /** 72 * Return a FeedGenerator instance based on the given {@code parameters}. As a minimal requirement, the incoming 73 * parameter map is expected to hold a parameter named "{@value #REQUIRED_PARAMETER}". Based on the value of 74 * this parameter the associated {@link FeedGenerator} is constructed and returned. 75 * 76 * @param parameterMap parameter map (must not be null) (key = name / values = 0..N values for parameter name) 77 * @return the SyndFeedGenerator appropriate for the given parameters 78 * @throws IllegalArgumentException when {@code parameters} is null 79 */ 80 public FeedGenerator resolve(Map<String, String[]> parameterMap) { 81 Assert.notNull(parameterMap, "'parameters' cannot be null"); 82 FeedGenerator generator; 83 if (!parameterMap.containsKey(REQUIRED_PARAMETER)) { 84 // use default implementation set by module descriptor 85 generator = feederGenerator; 86 } else { 87 // use named generator from module config as prototype for actual generator 88 String generatorName = parameterMap.get(REQUIRED_PARAMETER)[0]; 89 RSSAggregator module = (RSSAggregator) moduleRegistry.getModuleInstance("rssaggregator"); 90 generator = (FeedGenerator) FactoryUtils.prototypeFactory(module.getFeedGenerators().get(generatorName)).create(); 91 } 92 if (generator == null) { 93 throw new FeedGeneratorConstructionException("Failed to resolve feed generator. Set it up in config:/modules/rssaggregator/config/feedGenerators or in module descriptor as a value for " + FeedGenerator.class.getCanonicalName() + " property."); 94 } 95 try { 96 BeanUtils.populate(generator, parameterMap); 97 } catch (IllegalAccessException e) { 98 // some properties can't be set. just silently ignore 99 log.error(e.getMessage(), e); 100 } catch (InvocationTargetException e) { 101 log.error(e.getMessage(), e); 102 throw new FeedGeneratorConstructionException("Failed to setup " + generator.getClass().getCanonicalName(), e); 103 } 104 return generator; 105 } 106 }