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