001/* 002 * (C) Copyright 2015 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-2.1.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.web.resources.core.service; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Collections; 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.web.resources.api.Processor; 029import org.nuxeo.ecm.web.resources.core.ProcessorDescriptor; 030import org.nuxeo.runtime.model.ContributionFragmentRegistry; 031 032/** 033 * Registry for processors registry. 034 * 035 * @since 7.3 036 */ 037public class ProcessorRegistry extends ContributionFragmentRegistry<ProcessorDescriptor> { 038 039 private static final Log log = LogFactory.getLog(ProcessorRegistry.class); 040 041 protected final Map<String, ProcessorDescriptor> processors = new HashMap<String, ProcessorDescriptor>(); 042 043 @Override 044 public String getContributionId(ProcessorDescriptor contrib) { 045 return contrib.getName(); 046 } 047 048 @Override 049 public void contributionUpdated(String id, ProcessorDescriptor contrib, ProcessorDescriptor newOrigContrib) { 050 if (processors.containsKey(id)) { 051 processors.remove(id); 052 } 053 if (contrib.isEnabled()) { 054 processors.put(id, contrib); 055 log.info("Registering processor with name " + id); 056 } 057 } 058 059 @Override 060 public void contributionRemoved(String id, ProcessorDescriptor origContrib) { 061 processors.remove(id); 062 log.info("Unregistering processor with name " + id); 063 } 064 065 @Override 066 public ProcessorDescriptor clone(ProcessorDescriptor orig) { 067 return orig.clone(); 068 } 069 070 @Override 071 public void merge(ProcessorDescriptor src, ProcessorDescriptor dst) { 072 dst.merge(src); 073 } 074 075 // custom API 076 077 public Processor getProcessor(String id) { 078 return processors.get(id); 079 } 080 081 public List<Processor> getProcessors() { 082 return getProcessors(null); 083 } 084 085 public List<Processor> getProcessors(String type) { 086 List<Processor> res = new ArrayList<>(); 087 Collection<ProcessorDescriptor> all = processors.values(); 088 for (Processor proc : all) { 089 if (type == null || proc.getTypes().contains(type)) { 090 res.add(proc); 091 } 092 } 093 Collections.sort(res); 094 return res; 095 } 096 097}