001/* 002 * (C) Copyright 2012 Nuxeo SA (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 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.directory.multi; 018 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.directory.Directory; 027import org.nuxeo.ecm.directory.DirectoryException; 028import org.nuxeo.runtime.model.ContributionFragmentRegistry; 029 030/** 031 * @since 5.6 032 */ 033public class MultiDirectoryRegistry extends ContributionFragmentRegistry<MultiDirectoryDescriptor> { 034 035 private static final Log log = LogFactory.getLog(MultiDirectoryRegistry.class); 036 037 protected Map<String, MultiDirectoryDescriptor> descriptors = new HashMap<String, MultiDirectoryDescriptor>(); 038 039 // cache of directories 040 protected Map<String, MultiDirectory> directories = new HashMap<String, MultiDirectory>(); 041 042 @Override 043 public String getContributionId(MultiDirectoryDescriptor contrib) { 044 return contrib.name; 045 } 046 047 @Override 048 public void contributionUpdated(String id, MultiDirectoryDescriptor descriptor, 049 MultiDirectoryDescriptor newOrigContrib) { 050 String name = descriptor.name; 051 if (descriptor.remove) { 052 contributionRemoved(id, descriptor); 053 } else { 054 if (descriptors.containsKey(name)) { 055 log.info("Directory registration updated: " + name); 056 } else { 057 log.info("Directory registered: " + name); 058 } 059 descriptors.put(id, descriptor); 060 MultiDirectory directory = new MultiDirectory(descriptor); 061 directories.put(name, directory); 062 } 063 } 064 065 @Override 066 public void contributionRemoved(String id, MultiDirectoryDescriptor origContrib) { 067 descriptors.remove(id); 068 Directory dir = directories.remove(id); 069 if (dir != null) { 070 try { 071 dir.shutdown(); 072 } catch (DirectoryException e) { 073 log.error(String.format("Error while shutting down directory '%s'", id), e); 074 } 075 } 076 log.info("Directory removed: " + id); 077 } 078 079 @Override 080 public MultiDirectoryDescriptor clone(MultiDirectoryDescriptor orig) { 081 return orig.clone(); 082 } 083 084 @Override 085 public void merge(MultiDirectoryDescriptor src, MultiDirectoryDescriptor dst) { 086 boolean remove = src.remove; 087 // keep old remove info: if old contribution was removed, new one 088 // should replace the old one completely 089 boolean wasRemoved = dst.remove; 090 if (remove) { 091 dst.remove = remove; 092 // don't bother merging 093 return; 094 } 095 096 dst.merge(src, wasRemoved); 097 } 098 099 // API 100 101 public MultiDirectory getDirectory(String name) { 102 return directories.get(name); 103 } 104 105 public List<Directory> getDirectories() { 106 List<Directory> res = new ArrayList<Directory>(); 107 res.addAll(directories.values()); 108 return res; 109 } 110 111}