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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.directory.ldap.registry;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.directory.Directory;
029import org.nuxeo.ecm.directory.DirectoryException;
030import org.nuxeo.ecm.directory.ldap.LDAPDirectory;
031import org.nuxeo.ecm.directory.ldap.LDAPDirectoryDescriptor;
032import org.nuxeo.runtime.model.ContributionFragmentRegistry;
033
034/**
035 * @since 5.6
036 */
037public class LDAPDirectoryRegistry extends ContributionFragmentRegistry<LDAPDirectoryDescriptor> {
038
039    private static final Log log = LogFactory.getLog(LDAPDirectoryRegistry.class);
040
041    private final Map<String, Directory> proxies = new HashMap<String, Directory>();
042
043    @Override
044    public String getContributionId(LDAPDirectoryDescriptor contrib) {
045        return contrib.getName();
046    }
047
048    @Override
049    public void contributionUpdated(String id, LDAPDirectoryDescriptor descriptor,
050            LDAPDirectoryDescriptor newOrigContrib) {
051        String descriptorName = descriptor.getName();
052        proxies.put(descriptorName, new LDAPDirectory(descriptor));
053        log.info("directory registered: " + descriptorName);
054    }
055
056    @Override
057    public void contributionRemoved(String id, LDAPDirectoryDescriptor descriptor) {
058        String directoryName = descriptor.getName();
059        Directory dir = proxies.remove(directoryName);
060        if (dir != null) {
061            try {
062                dir.shutdown();
063            } catch (DirectoryException e) {
064                log.error(String.format("Error while shutting down directory '%s'", id), e);
065            }
066        }
067        log.info("directory unregistered: " + directoryName);
068    }
069
070    @Override
071    public boolean isSupportingMerge() {
072        return false;
073    }
074
075    @Override
076    public LDAPDirectoryDescriptor clone(LDAPDirectoryDescriptor orig) {
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public void merge(LDAPDirectoryDescriptor src, LDAPDirectoryDescriptor dst) {
082        throw new UnsupportedOperationException();
083    }
084
085    // API
086
087    public Directory getDirectory(String name) {
088        return proxies.get(name);
089    }
090
091    public List<Directory> getDirectories() {
092        List<Directory> res = new ArrayList<Directory>();
093        res.addAll(proxies.values());
094        return res;
095    }
096
097}