001/*
002 * (C) Copyright 2012 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.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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.service.impl;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectOutputStream;
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029import java.util.Set;
030
031import org.apache.commons.collections.MapUtils;
032import org.apache.commons.lang.StringUtils;
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.ecm.core.api.NuxeoException;
036import org.nuxeo.runtime.model.ContributionFragmentRegistry;
037
038/**
039 * Registry for {@code fileSystemItemFactory} contributions.
040 *
041 * @author Antoine Taillefer
042 * @see FileSystemItemAdapterServiceImpl
043 */
044public class FileSystemItemFactoryRegistry extends ContributionFragmentRegistry<FileSystemItemFactoryDescriptor> {
045
046    private static final Log log = LogFactory.getLog(FileSystemItemFactoryRegistry.class);
047
048    protected final Map<String, FileSystemItemFactoryDescriptor> factoryDescriptors = new HashMap<String, FileSystemItemFactoryDescriptor>();
049
050    @Override
051    public String getContributionId(FileSystemItemFactoryDescriptor contrib) {
052        String name = contrib.getName();
053        if (StringUtils.isEmpty(name)) {
054            throw new NuxeoException("Cannot register fileSystemItemFactory without a name.");
055        }
056        return name;
057    }
058
059    @Override
060    public void contributionUpdated(String id, FileSystemItemFactoryDescriptor contrib,
061            FileSystemItemFactoryDescriptor newOrigContrib) {
062        if (log.isTraceEnabled()) {
063            log.trace(String.format("Putting contribution %s with id %s in factory registry", contrib, id));
064        }
065        factoryDescriptors.put(id, contrib);
066    }
067
068    @Override
069    public void contributionRemoved(String id, FileSystemItemFactoryDescriptor origContrib) {
070        if (log.isTraceEnabled()) {
071            log.trace(String.format("Removing contribution with id %s from factory registry", id));
072        }
073        factoryDescriptors.remove(id);
074    }
075
076    @Override
077    public FileSystemItemFactoryDescriptor clone(FileSystemItemFactoryDescriptor orig) {
078        if (log.isTraceEnabled()) {
079            log.trace(String.format("Cloning contribution with id %s", orig.getName()));
080        }
081        try {
082            ByteArrayOutputStream bos = new ByteArrayOutputStream();
083            ObjectOutputStream oos = new ObjectOutputStream(bos);
084            oos.writeObject(orig);
085            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
086            ObjectInputStream ois = new ObjectInputStream(bis);
087            return (FileSystemItemFactoryDescriptor) ois.readObject();
088        } catch (IOException | ClassNotFoundException e) {
089            throw new NuxeoException("Cannot clone contribution " + orig, e);
090        }
091    }
092
093    @Override
094    public void merge(FileSystemItemFactoryDescriptor src, FileSystemItemFactoryDescriptor dst) {
095        if (log.isTraceEnabled()) {
096            log.trace(String.format("Merging contribution with id %s to contribution with id %s", src.getName(),
097                    dst.getName()));
098        }
099        // Order
100        int srcOrder = src.getOrder();
101        if (srcOrder > 0 && srcOrder != dst.getOrder()) {
102            dst.setOrder(srcOrder);
103        }
104        // Doc type
105        if (!StringUtils.isEmpty(src.getDocType()) && !src.getDocType().equals(dst.getDocType())) {
106            dst.setDocType(src.getDocType());
107        }
108        // Facet
109        if (!StringUtils.isEmpty(src.getFacet()) && !src.getFacet().equals(dst.getFacet())) {
110            dst.setFacet(src.getFacet());
111        }
112        // Class
113        if (src.getFactoryClass() != null && !src.getFactoryClass().equals(dst.getFactoryClass())) {
114            dst.setFactoryClass(src.getFactoryClass());
115        }
116        // Parameters
117        if (!MapUtils.isEmpty(src.getParameters())) {
118            for (String name : src.getParameters().keySet()) {
119                dst.setParameter(name, src.getParameter(name));
120            }
121        }
122    }
123
124    protected List<FileSystemItemFactoryWrapper> getOrderedActiveFactories(Set<String> activeFactories) {
125        List<FileSystemItemFactoryWrapper> factories = new ArrayList<FileSystemItemFactoryWrapper>();
126        List<FileSystemItemFactoryDescriptor> orderedFactoryDescriptors = new ArrayList<FileSystemItemFactoryDescriptor>(
127                factoryDescriptors.values());
128        Collections.sort(orderedFactoryDescriptors);
129        for (FileSystemItemFactoryDescriptor factoryDesc : orderedFactoryDescriptors) {
130            // Only include active factories
131            if (activeFactories.contains(factoryDesc.getName())) {
132                FileSystemItemFactoryWrapper factoryWrapper = new FileSystemItemFactoryWrapper(
133                        factoryDesc.getDocType(), factoryDesc.getFacet(), factoryDesc.getFactory());
134                factories.add(factoryWrapper);
135            }
136        }
137        return factories;
138    }
139
140}