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