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