001/*
002 * (C) Copyright 2013 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.HashSet;
027import java.util.Set;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.runtime.model.ContributionFragmentRegistry;
033
034/**
035 * Registry for the {@code activeFileSystemItemFactories} contributions.
036 *
037 * @author Antoine Taillefer
038 * @see FileSystemItemAdapterServiceImpl
039 */
040public class ActiveFileSystemItemFactoryRegistry extends
041        ContributionFragmentRegistry<ActiveFileSystemItemFactoriesDescriptor> {
042
043    private static final Log log = LogFactory.getLog(ActiveFileSystemItemFactoryRegistry.class);
044
045    protected static final String CONTRIBUTION_ID = "activeFileSystemItemFactoriesContrib";
046
047    protected Set<String> activeFactories = new HashSet<String>();
048
049    @Override
050    public String getContributionId(ActiveFileSystemItemFactoriesDescriptor contrib) {
051        return CONTRIBUTION_ID;
052    }
053
054    @Override
055    public void contributionUpdated(String id, ActiveFileSystemItemFactoriesDescriptor contrib,
056            ActiveFileSystemItemFactoriesDescriptor newOrigContrib) {
057        if (log.isTraceEnabled()) {
058            log.trace(String.format("Updating activeFileSystemItemFactories contribution %s.", contrib));
059        }
060        if (contrib.isMerge()) {
061            // Merge active factories
062            for (ActiveFileSystemItemFactoryDescriptor factory : contrib.getFactories()) {
063                if (activeFactories.contains(factory.getName()) && !factory.isEnabled()) {
064                    if (log.isTraceEnabled()) {
065                        log.trace(String.format("Removing factory %s from active factories.", factory.getName()));
066                    }
067                    activeFactories.remove(factory.getName());
068                }
069                if (!activeFactories.contains(factory.getName()) && factory.isEnabled()) {
070                    if (log.isTraceEnabled()) {
071                        log.trace(String.format("Adding factory %s to active factories.", factory.getName()));
072                    }
073                    activeFactories.add(factory.getName());
074                }
075            }
076        } else {
077            // No merge, reset active factories
078            if (log.isTraceEnabled()) {
079                log.trace(String.format("Clearing active factories as contribution %s doesn't merge.", contrib));
080            }
081            activeFactories.clear();
082            for (ActiveFileSystemItemFactoryDescriptor factory : contrib.getFactories()) {
083                if (factory.isEnabled()) {
084                    if (log.isTraceEnabled()) {
085                        log.trace(String.format("Adding factory %s to active factories.", factory.getName()));
086                    }
087                    activeFactories.add(factory.getName());
088                }
089            }
090        }
091    }
092
093    @Override
094    public void contributionRemoved(String id, ActiveFileSystemItemFactoriesDescriptor origContrib) {
095        log.trace("Clearing active factories.");
096        activeFactories.clear();
097    }
098
099    @Override
100    public ActiveFileSystemItemFactoriesDescriptor clone(ActiveFileSystemItemFactoriesDescriptor orig) {
101        if (log.isTraceEnabled()) {
102            log.trace(String.format("Cloning contribution %s.", orig));
103        }
104        try {
105            ByteArrayOutputStream bos = new ByteArrayOutputStream();
106            ObjectOutputStream oos = new ObjectOutputStream(bos);
107            oos.writeObject(orig);
108            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
109            ObjectInputStream ois = new ObjectInputStream(bis);
110            return (ActiveFileSystemItemFactoriesDescriptor) ois.readObject();
111        } catch (IOException | ClassNotFoundException e) {
112            throw new NuxeoException(String.format("Cannot clone contribution %s.", orig), e);
113        }
114    }
115
116    @Override
117    public void merge(ActiveFileSystemItemFactoriesDescriptor src, ActiveFileSystemItemFactoriesDescriptor dst) {
118        if (log.isTraceEnabled()) {
119            log.trace(String.format("Merging contribution %s to contribution %s.", src, dst));
120        }
121        // Merge
122        if (src.isMerge() != dst.isMerge()) {
123            dst.setMerge(src.isMerge());
124        }
125        // Factories
126        for (ActiveFileSystemItemFactoryDescriptor factory : src.getFactories()) {
127            int indexOfFactory = dst.getFactories().indexOf(factory);
128            if (indexOfFactory > -1) {
129                dst.getFactories().get(indexOfFactory).setEnabled(factory.isEnabled());
130            } else {
131                dst.getFactories().add(factory);
132            }
133        }
134    }
135}