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