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