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 * Florent Guillaume 016 */ 017package org.nuxeo.ecm.core.work; 018 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.Map.Entry; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.work.api.WorkQueueDescriptor; 028import org.nuxeo.runtime.model.ContributionFragmentRegistry; 029 030/** 031 * Registry for the {@link WorkQueueDescriptor}s. 032 * 033 * @since 5.6 034 */ 035public class WorkQueueDescriptorRegistry extends ContributionFragmentRegistry<WorkQueueDescriptor> { 036 037 private static final Log log = LogFactory.getLog(WorkQueueDescriptorRegistry.class); 038 039 protected final WorkManagerImpl works; 040 041 protected Map<String, WorkQueueDescriptor> registry = new HashMap<String, WorkQueueDescriptor>(); 042 043 protected volatile boolean refresh; 044 045 protected Map<String, String> categoryToQueueId = new HashMap<String, String>(); 046 047 public WorkQueueDescriptorRegistry(WorkManagerImpl works) { 048 this.works = works; 049 } 050 051 /** 052 * Gets the descriptor for a given queue id. 053 * 054 * @param queueId the queue id 055 * @return the queue descriptor, or {@code null} 056 */ 057 public synchronized WorkQueueDescriptor get(String queueId) { 058 return registry.get(queueId); 059 } 060 061 /** 062 * Gets the list of queue ids. 063 * 064 * @return the list of queue ids 065 */ 066 public synchronized List<String> getQueueIds() { 067 return new ArrayList<String>(registry.keySet()); 068 } 069 070 @Override 071 public String getContributionId(WorkQueueDescriptor contrib) { 072 return contrib.id; 073 } 074 075 @Override 076 public void contributionUpdated(String id, WorkQueueDescriptor contrib, WorkQueueDescriptor newOrigContrib) { 077 registry.put(id, contrib); 078 refresh = true; 079 if (works.started) { 080 works.activateQueue(contrib); 081 } 082 } 083 084 @Override 085 public void contributionRemoved(String id, WorkQueueDescriptor origContrib) { 086 if (works.started) { 087 works.deactivateQueue(origContrib); 088 } 089 registry.remove(id); 090 refresh = true; 091 } 092 093 protected synchronized void refresh() { 094 for (Entry<String, WorkQueueDescriptor> es : registry.entrySet()) { 095 String queueId = es.getKey(); 096 for (String category : es.getValue().categories) { 097 String old = categoryToQueueId.get("category"); 098 if (old != null) { 099 log.error("Work category '" + category + "' cannot be assigned to work queue '" + queueId 100 + "' because it is already assigned to work queue '" + old + "'"); 101 } else { 102 categoryToQueueId.put(category, queueId); 103 } 104 } 105 } 106 } 107 108 public String getQueueId(String category) { 109 if (refresh) { 110 refresh(); 111 } 112 return categoryToQueueId.get(category); 113 } 114 115 @Override 116 public WorkQueueDescriptor clone(WorkQueueDescriptor orig) { 117 return orig.clone(); 118 } 119 120 @Override 121 public void merge(WorkQueueDescriptor src, WorkQueueDescriptor dst) { 122 dst.merge(src); 123 } 124 125}