001/*
002 * (C) Copyright 2006-2011 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api.adapter;
023
024import java.util.Collection;
025import java.util.Map;
026import java.util.concurrent.ConcurrentHashMap;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.runtime.model.ComponentContext;
031import org.nuxeo.runtime.model.ComponentInstance;
032import org.nuxeo.runtime.model.ComponentName;
033import org.nuxeo.runtime.model.DefaultComponent;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038public class DocumentAdapterService extends DefaultComponent {
039
040    public static final ComponentName NAME = new ComponentName(ComponentName.DEFAULT_TYPE,
041            "org.nuxeo.ecm.core.api.DocumentAdapterService");
042
043    private static final Log log = LogFactory.getLog(DocumentAdapterService.class);
044
045    /**
046     * Document adapters
047     */
048    protected Map<Class<?>, DocumentAdapterDescriptor> adapters;
049
050    public DocumentAdapterDescriptor getAdapterDescriptor(Class<?> itf) {
051        return adapters.get(itf);
052    }
053
054    /**
055     * @since 5.7
056     */
057    public DocumentAdapterDescriptor[] getAdapterDescriptors() {
058        Collection<DocumentAdapterDescriptor> values = adapters.values();
059        return values.toArray(new DocumentAdapterDescriptor[values.size()]);
060    }
061
062    public void registerAdapterFactory(DocumentAdapterDescriptor dae) {
063        adapters.put(dae.getInterface(), dae);
064        log.info("Registered document adapter factory " + dae);
065    }
066
067    public void unregisterAdapterFactory(Class<?> itf) {
068        DocumentAdapterDescriptor dae = adapters.remove(itf);
069        if (dae != null) {
070            log.info("Unregistered document adapter factory: " + dae);
071        }
072    }
073
074    @Override
075    public void activate(ComponentContext context) {
076        adapters = new ConcurrentHashMap<>();
077    }
078
079    @Override
080    public void deactivate(ComponentContext context) {
081        adapters.clear();
082        adapters = null;
083    }
084
085    @Override
086    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
087        if (extensionPoint.equals("adapters")) {
088            DocumentAdapterDescriptor dae = (DocumentAdapterDescriptor) contribution;
089            registerAdapterFactory(dae);
090        }
091    }
092
093    @Override
094    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
095        if (extensionPoint.equals("adapters")) {
096            DocumentAdapterDescriptor dae = (DocumentAdapterDescriptor) contribution;
097            unregisterAdapterFactory(dae.getInterface());
098        }
099    }
100
101}