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.sql; 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 SQLDirectoryRegistry extends ContributionFragmentRegistry<SQLDirectoryDescriptor> { 036 037 private static final Log log = LogFactory.getLog(SQLDirectoryRegistry.class); 038 039 protected Map<String, SQLDirectoryDescriptor> descriptors = new HashMap<String, SQLDirectoryDescriptor>(); 040 041 // cache map of directories 042 protected Map<String, Directory> directories = new HashMap<String, Directory>(); 043 044 @Override 045 public String getContributionId(SQLDirectoryDescriptor contrib) { 046 return contrib.getName(); 047 } 048 049 @Override 050 public void contributionUpdated(String id, SQLDirectoryDescriptor descriptor, SQLDirectoryDescriptor newOrigContrib) { 051 String directoryName = descriptor.getName(); 052 if (descriptor.getRemove()) { 053 log.info("Removing directory: " + directoryName); 054 contributionRemoved(id, descriptor); 055 } else { 056 if (directories.containsKey(directoryName)) { 057 log.info("Re-registered directory: " + directoryName); 058 } else { 059 log.info("Registered directory: " + directoryName); 060 } 061 descriptors.put(id, descriptor); 062 directories.put(id, new SQLDirectory(descriptor)); 063 } 064 } 065 066 @Override 067 public void contributionRemoved(String id, SQLDirectoryDescriptor descriptor) { 068 String descriptorName = descriptor.getName(); 069 log.info("Unregistered directory: " + descriptorName); 070 descriptors.remove(id); 071 Directory dir = directories.remove(id); 072 if (dir != null) { 073 try { 074 dir.shutdown(); 075 } catch (DirectoryException e) { 076 log.error(String.format("Error while shutting down directory '%s'", id), e); 077 } 078 } 079 } 080 081 @Override 082 public SQLDirectoryDescriptor clone(SQLDirectoryDescriptor orig) { 083 return orig.clone(); 084 } 085 086 @Override 087 public void merge(SQLDirectoryDescriptor src, SQLDirectoryDescriptor dst) { 088 boolean remove = src.getRemove(); 089 // keep old remove info: if old contribution was removed, new one 090 // should replace the old one completely 091 boolean wasRemoved = dst.getRemove(); 092 if (remove) { 093 dst.setRemove(remove); 094 // don't bother merging 095 return; 096 } 097 098 dst.merge(src, wasRemoved); 099 } 100 101 // API 102 103 public Directory 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}