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 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.automation.core.impl; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.nuxeo.ecm.automation.OperationException; 023import org.nuxeo.ecm.automation.OperationType; 024import org.nuxeo.ecm.automation.core.annotations.Operation; 025import org.nuxeo.runtime.model.ContributionFragmentRegistry; 026 027/** 028 * @since 5.6 029 */ 030public class OperationTypeRegistry extends ContributionFragmentRegistry<OperationType> { 031 032 /** 033 * Modifiable operation registry. Modifying the registry is using a lock and it's thread safe. Modifications are 034 * removing the cache. 035 */ 036 protected final Map<String, OperationType> operations = new HashMap<String, OperationType>(); 037 038 /** 039 * Read only cache for operation lookup. Thread safe. Not using synchronization if cache already created. 040 */ 041 protected volatile Map<String, OperationType> lookup; 042 043 @Override 044 public String getContributionId(OperationType contrib) { 045 return contrib.getId(); 046 } 047 048 public synchronized void addContribution(OperationType op, boolean replace) throws OperationException { 049 if (!replace && operations.containsKey(op.getId())) { 050 throw new OperationException("An operation is already bound to: " + op.getId() 051 + ". Use 'replace=true' to replace an existing operation"); 052 } 053 super.addContribution(op); 054 } 055 056 @Override 057 public void contributionUpdated(String id, OperationType contrib, OperationType newOrigContrib) { 058 operations.put(id, contrib); 059 for (String alias : contrib.getAliases()) { 060 operations.put(alias, contrib); 061 } 062 lookup = null; 063 } 064 065 @Override 066 public void contributionRemoved(String id, OperationType origContrib) { 067 operations.remove(id); 068 lookup = null; 069 } 070 071 @Override 072 public boolean isSupportingMerge() { 073 return false; 074 } 075 076 @Override 077 public OperationType clone(OperationType orig) { 078 throw new UnsupportedOperationException(); 079 } 080 081 @Override 082 public void merge(OperationType src, OperationType dst) { 083 throw new UnsupportedOperationException(); 084 } 085 086 // API 087 088 public OperationType getOperationType(Class<?> key) { 089 return operations.get(key.getAnnotation(Operation.class).id()); 090 } 091 092 public Map<String, OperationType> lookup() { 093 Map<String, OperationType> _lookup = lookup; 094 if (_lookup == null) { 095 synchronized (this) { 096 lookup = new HashMap<String, OperationType>(operations); 097 _lookup = lookup; 098 } 099 } 100 return _lookup; 101 } 102 103}