001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.web.resources.wro.factory;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Properties;
022
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.web.resources.api.Processor;
027import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
028import org.nuxeo.ecm.web.resources.wro.provider.NuxeoConfigurableProvider;
029
030import ro.isdc.wro.cache.factory.CacheKeyFactory;
031import ro.isdc.wro.manager.factory.ConfigurableWroManagerFactory;
032import ro.isdc.wro.model.factory.WroModelFactory;
033import ro.isdc.wro.model.resource.processor.factory.ConfigurableProcessorsFactory;
034
035/**
036 * Manager generating processors configuration from contributions to {@link WebResourceManager}, and hooking up other
037 * specific factories.
038 *
039 * @since 7.3
040 */
041public class NuxeoWroManagerFactory extends ConfigurableWroManagerFactory {
042
043    private static final Log log = LogFactory.getLog(NuxeoWroManagerFactory.class);
044
045    @Override
046    protected Properties newConfigProperties() {
047        final Properties props = new Properties();
048        // automatically setup runtime service processors
049        addAliases(props, ConfigurableProcessorsFactory.PARAM_PRE_PROCESSORS, NuxeoConfigurableProvider.PRE_TYPE);
050        addAliases(props, ConfigurableProcessorsFactory.PARAM_POST_PROCESSORS, NuxeoConfigurableProvider.POST_TYPE);
051        if (log.isDebugEnabled()) {
052            log.debug("Built new conf, properties=" + props);
053        }
054        return props;
055    }
056
057    protected List<String> resolveProcessorNames(String type) {
058        List<String> res = new ArrayList<String>();
059        List<Processor> procs = NuxeoConfigurableProvider.resolveProcessors(type);
060        for (Processor proc : procs) {
061            res.add(proc.getName());
062        }
063        return res;
064    }
065
066    protected void addAliases(Properties props, String propName, String type) {
067        final String SEP = ",";
068        List<String> procs = resolveProcessorNames(type);
069        String propValue = props.getProperty(propName);
070        if (!StringUtils.isBlank(propValue)) {
071            String[] existing = StringUtils.split(propValue, SEP);
072            if (existing != null) {
073                for (String s : existing) {
074                    if (s != null) {
075                        procs.add(s.trim());
076                    }
077                }
078            }
079        }
080        props.put(propName, StringUtils.join(procs, SEP));
081    }
082
083    @Override
084    protected WroModelFactory newModelFactory() {
085        return new NuxeoWroModelFactory();
086    }
087
088    @Override
089    protected CacheKeyFactory newCacheKeyFactory() {
090        return new NuxeoWroCacheKeyFactory();
091    }
092
093}