001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
016 *
017 * $Id:  $
018 */
019
020package org.nuxeo.ecm.directory.ui;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.LinkedHashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.directory.api.ui.DirectoryUI;
031import org.nuxeo.ecm.directory.api.ui.DirectoryUIDescriptor;
032import org.nuxeo.ecm.directory.api.ui.DirectoryUIManager;
033import org.nuxeo.runtime.model.ComponentContext;
034import org.nuxeo.runtime.model.ComponentInstance;
035import org.nuxeo.runtime.model.DefaultComponent;
036
037/**
038 * Component managing directory ui information
039 *
040 * @author Anahide Tchertchian
041 */
042public class DirectoryUIManagerImpl extends DefaultComponent implements DirectoryUIManager {
043
044    private static final long serialVersionUID = 1L;
045
046    private static final Log log = LogFactory.getLog(DirectoryUIManagerImpl.class);
047
048    protected static final String DIRECTORIES_EP_NAME = "directories";
049
050    protected Map<String, DirectoryUI> registry;
051
052    @Override
053    @SuppressWarnings("unchecked")
054    public <T> T getAdapter(Class<T> adapter) {
055        if (adapter.isAssignableFrom(DirectoryUIManager.class)) {
056            return (T) this;
057        }
058        return null;
059    }
060
061    @Override
062    public void activate(ComponentContext context) {
063        super.activate(context);
064        registry = new LinkedHashMap<String, DirectoryUI>();
065    }
066
067    @Override
068    public void deactivate(ComponentContext context) {
069        registry = null;
070        super.deactivate(context);
071    }
072
073    @Override
074    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
075        if (!extensionPoint.equals(DIRECTORIES_EP_NAME)) {
076            log.warn("Unknown extension point: " + extensionPoint);
077            return;
078        }
079
080        DirectoryUIDescriptor desc = (DirectoryUIDescriptor) contribution;
081        String name = desc.getName();
082        boolean disabled = Boolean.FALSE.equals(desc.isEnabled());
083        if (registry.containsKey(name)) {
084            log.info("Overriding " + name);
085        }
086        if (disabled) {
087            registry.remove(name);
088        } else {
089            registry.put(name, desc);
090        }
091    }
092
093    @Override
094    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
095        if (!extensionPoint.equals(DIRECTORIES_EP_NAME)) {
096            log.warn("Unknown extension point: " + extensionPoint);
097            return;
098        }
099
100        DirectoryUIDescriptor desc = (DirectoryUIDescriptor) contribution;
101        registry.remove(desc.getName());
102    }
103
104    public DirectoryUI getDirectoryInfo(String directoryName) {
105        return registry.get(directoryName);
106    }
107
108    public List<String> getDirectoryNames() {
109        List<String> dirNames = new ArrayList<String>(registry.keySet());
110        Collections.sort(dirNames, String.CASE_INSENSITIVE_ORDER);
111        return dirNames;
112    }
113
114}