001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.wro.factory;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Properties;
024
025import org.apache.commons.lang.StringUtils;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.web.resources.api.Processor;
029import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
030import org.nuxeo.ecm.web.resources.wro.provider.NuxeoConfigurableProvider;
031
032import ro.isdc.wro.cache.factory.CacheKeyFactory;
033import ro.isdc.wro.manager.factory.ConfigurableWroManagerFactory;
034import ro.isdc.wro.model.factory.WroModelFactory;
035import ro.isdc.wro.model.resource.processor.factory.ConfigurableProcessorsFactory;
036
037/**
038 * Manager generating processors configuration from contributions to {@link WebResourceManager}, and hooking up other
039 * specific factories.
040 *
041 * @since 7.3
042 */
043public class NuxeoWroManagerFactory extends ConfigurableWroManagerFactory {
044
045    private static final Log log = LogFactory.getLog(NuxeoWroManagerFactory.class);
046
047    @Override
048    protected Properties newConfigProperties() {
049        final Properties props = new Properties();
050        // automatically setup runtime service processors
051        addAliases(props, ConfigurableProcessorsFactory.PARAM_PRE_PROCESSORS, NuxeoConfigurableProvider.PRE_TYPE);
052        addAliases(props, ConfigurableProcessorsFactory.PARAM_POST_PROCESSORS, NuxeoConfigurableProvider.POST_TYPE);
053        if (log.isDebugEnabled()) {
054            log.debug("Built new conf, properties=" + props);
055        }
056        return props;
057    }
058
059    protected List<String> resolveProcessorNames(String type) {
060        List<String> res = new ArrayList<String>();
061        List<Processor> procs = NuxeoConfigurableProvider.resolveProcessors(type);
062        for (Processor proc : procs) {
063            res.add(proc.getName());
064        }
065        return res;
066    }
067
068    protected void addAliases(Properties props, String propName, String type) {
069        final String SEP = ",";
070        List<String> procs = resolveProcessorNames(type);
071        String propValue = props.getProperty(propName);
072        if (!StringUtils.isBlank(propValue)) {
073            String[] existing = StringUtils.split(propValue, SEP);
074            if (existing != null) {
075                for (String s : existing) {
076                    if (s != null) {
077                        procs.add(s.trim());
078                    }
079                }
080            }
081        }
082        props.put(propName, StringUtils.join(procs, SEP));
083    }
084
085    @Override
086    protected WroModelFactory newModelFactory() {
087        return new NuxeoWroModelFactory();
088    }
089
090    @Override
091    protected CacheKeyFactory newCacheKeyFactory() {
092        return new NuxeoWroCacheKeyFactory();
093    }
094
095}