001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (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 */ 015package org.nuxeo.ecm.platform.preview.adapter; 016 017import java.util.ArrayList; 018import java.util.Collections; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 027import org.nuxeo.ecm.platform.preview.adapter.factories.BlobHolderPreviewAdapterFactory; 028import org.nuxeo.ecm.platform.preview.adapter.factories.FileBasedPreviewAdapterFactory; 029import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter; 030import org.nuxeo.runtime.model.ComponentInstance; 031import org.nuxeo.runtime.model.DefaultComponent; 032 033/** 034 * Runtime component that handles the extension points and the service interface for Preview Adapter management. 035 * 036 * @author tiry 037 */ 038public class PreviewAdapterManagerComponent extends DefaultComponent implements PreviewAdapterManager { 039 040 public static final String ADAPTER_FACTORY_EP = "AdapterFactory"; 041 042 public static final String PREVIEWED_MIME_TYPE = "MimeTypePreviewer"; 043 044 public static final String BLOB_POST_PROCESSOR_EP = "blobPostProcessor"; 045 046 private static final Log log = LogFactory.getLog(PreviewAdapterManagerComponent.class); 047 048 protected Map<String, PreviewAdapterFactory> factoryRegistry = new HashMap<String, PreviewAdapterFactory>(); 049 050 protected Map<String, MimeTypePreviewer> previewerFactory = new HashMap<String, MimeTypePreviewer>(); 051 052 protected List<BlobPostProcessor> blobPostProcessors = new ArrayList<BlobPostProcessor>(); 053 054 // Component and EP management 055 056 @Override 057 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 058 059 if (ADAPTER_FACTORY_EP.equals(extensionPoint)) { 060 AdapterFactoryDescriptor desc = (AdapterFactoryDescriptor) contribution; 061 if (desc.isEnabled()) { 062 PreviewAdapterFactory factory = desc.getNewInstance(); 063 if (factory != null) { 064 factoryRegistry.put(desc.getTypeName(), factory); 065 } 066 } else { 067 factoryRegistry.remove(desc.getTypeName()); 068 } 069 } else if (PREVIEWED_MIME_TYPE.equals(extensionPoint)) { 070 MimeTypePreviewerDescriptor desc = (MimeTypePreviewerDescriptor) contribution; 071 previewerFactory.put(desc.getPattern(), newInstance(desc.getKlass())); 072 } else if (BLOB_POST_PROCESSOR_EP.equals(extensionPoint)) { 073 BlobPostProcessorDescriptor desc = (BlobPostProcessorDescriptor) contribution; 074 blobPostProcessors.add(newInstance(desc.getKlass())); 075 } 076 } 077 078 protected <T> T newInstance(Class<T> klass) { 079 try { 080 return klass.newInstance(); 081 } catch (ReflectiveOperationException e) { 082 throw new RuntimeException(e); 083 } 084 } 085 086 @Override 087 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 088 } 089 090 // service interface impl 091 092 public boolean hasAdapter(DocumentModel doc) { 093 if (doc == null) { 094 return false; 095 } 096 097 String docType = doc.getType(); 098 if (factoryRegistry.containsKey(docType)) { 099 return true; 100 } 101 102 return doc.hasSchema("file") || doc.hasSchema("files"); 103 } 104 105 public HtmlPreviewAdapter getAdapter(DocumentModel doc) { 106 if (doc == null) { 107 return null; 108 } 109 110 String docType = doc.getType(); 111 112 log.debug("Looking for HTMLPreviewAdapter for type " + docType); 113 114 if (factoryRegistry.containsKey(docType)) { 115 log.debug("dedicated HTMLPreviewAdapter factory found"); 116 return factoryRegistry.get(docType).getAdapter(doc); 117 } 118 119 if (doc.isFolder()) { 120 return null; 121 } 122 123 BlobHolder bh = doc.getAdapter(BlobHolder.class); 124 if (bh != null) { 125 log.debug("using Blob Holder based HtmlPreviewAdapter factory"); 126 PreviewAdapterFactory factory = new BlobHolderPreviewAdapterFactory(); 127 return factory.getAdapter(doc); 128 129 } 130 131 if (doc.hasSchema("file") || doc.hasSchema("files")) { 132 log.debug("using default file based HtmlPreviewAdapter factory"); 133 PreviewAdapterFactory factory = new FileBasedPreviewAdapterFactory(); 134 return factory.getAdapter(doc); 135 } else { 136 return null; 137 } 138 } 139 140 public MimeTypePreviewer getPreviewer(String mimeType) { 141 for (Map.Entry<String, MimeTypePreviewer> entry : previewerFactory.entrySet()) { 142 if (mimeType.matches(entry.getKey())) { 143 return entry.getValue(); 144 } 145 } 146 return null; 147 } 148 149 public List<BlobPostProcessor> getBlobPostProcessors() { 150 return Collections.unmodifiableList(blobPostProcessors); 151 } 152 153}