001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * Thomas Roger <troger@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.platform.content.template.service; 019 020import java.util.ArrayList; 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.runtime.model.ContributionFragmentRegistry; 029 030/** 031 * Registry of {@link PostContentCreationHandler}s. 032 * 033 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 034 * @since 5.5 035 */ 036public class PostContentCreationHandlerRegistry extends 037 ContributionFragmentRegistry<PostContentCreationHandlerDescriptor> { 038 039 private static final Log log = LogFactory.getLog(PostContentCreationHandlerRegistry.class); 040 041 protected Map<String, PostContentCreationHandlerDescriptor> postContentCreationHandlerDescriptors = new HashMap<String, PostContentCreationHandlerDescriptor>(); 042 043 @Override 044 public String getContributionId(PostContentCreationHandlerDescriptor contrib) { 045 return contrib.getName(); 046 } 047 048 @Override 049 public void contributionUpdated(String id, PostContentCreationHandlerDescriptor contrib, 050 PostContentCreationHandlerDescriptor newOrigContrib) { 051 if (contrib.isEnabled()) { 052 postContentCreationHandlerDescriptors.put(id, contrib); 053 } else { 054 postContentCreationHandlerDescriptors.remove(id); 055 } 056 } 057 058 @Override 059 public void contributionRemoved(String id, PostContentCreationHandlerDescriptor contrib) { 060 postContentCreationHandlerDescriptors.remove(id); 061 } 062 063 @Override 064 public PostContentCreationHandlerDescriptor clone(PostContentCreationHandlerDescriptor postContentCreationHandler) { 065 try { 066 return (PostContentCreationHandlerDescriptor) postContentCreationHandler.clone(); 067 } catch (CloneNotSupportedException e) { 068 // this should never occur since clone implements Cloneable 069 throw new RuntimeException(e); 070 } 071 } 072 073 @Override 074 public void merge(PostContentCreationHandlerDescriptor src, PostContentCreationHandlerDescriptor dst) { 075 if (src.getClazz() != null) { 076 dst.setClazz(src.getClazz()); 077 } 078 if (src.isEnabled() != dst.isEnabled()) { 079 dst.setEnabled(src.isEnabled()); 080 } 081 int order = src.getOrder(); 082 if (order > 0 && order != dst.getOrder()) { 083 dst.setOrder(src.getOrder()); 084 } 085 } 086 087 public List<PostContentCreationHandler> getOrderedHandlers() { 088 List<PostContentCreationHandlerDescriptor> descs = new ArrayList<PostContentCreationHandlerDescriptor>( 089 postContentCreationHandlerDescriptors.values()); 090 Collections.sort(descs); 091 092 List<PostContentCreationHandler> handlers = new ArrayList<PostContentCreationHandler>(); 093 for (PostContentCreationHandlerDescriptor desc : descs) { 094 try { 095 handlers.add(desc.getClazz().newInstance()); 096 } catch (ReflectiveOperationException e) { 097 log.error("Unable to instantiate class for handler: " + desc.getName(), e); 098 } 099 } 100 return handlers; 101 } 102 103}