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            previewerFactory.put(desc.getPattern(), newInstance(desc.getKlass()));
074        } else if (BLOB_POST_PROCESSOR_EP.equals(extensionPoint)) {
075            BlobPostProcessorDescriptor desc = (BlobPostProcessorDescriptor) contribution;
076            blobPostProcessors.add(newInstance(desc.getKlass()));
077        }
078    }
079
080    protected <T> T newInstance(Class<T> klass) {
081        try {
082            return klass.newInstance();
083        } catch (ReflectiveOperationException e) {
084            throw new RuntimeException(e);
085        }
086    }
087
088    @Override
089    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
090    }
091
092    // service interface impl
093
094    public boolean hasAdapter(DocumentModel doc) {
095        if (doc == null) {
096            return false;
097        }
098
099        String docType = doc.getType();
100        if (factoryRegistry.containsKey(docType)) {
101            return true;
102        }
103
104        return doc.hasSchema("file") || doc.hasSchema("files");
105    }
106
107    public HtmlPreviewAdapter getAdapter(DocumentModel doc) {
108        if (doc == null) {
109            return null;
110        }
111
112        String docType = doc.getType();
113
114        log.debug("Looking for HTMLPreviewAdapter for type " + docType);
115
116        if (factoryRegistry.containsKey(docType)) {
117            log.debug("dedicated HTMLPreviewAdapter factory found");
118            return factoryRegistry.get(docType).getAdapter(doc);
119        }
120
121        if (doc.isFolder()) {
122            return null;
123        }
124
125        BlobHolder bh = doc.getAdapter(BlobHolder.class);
126        if (bh != null) {
127            log.debug("using Blob Holder based HtmlPreviewAdapter factory");
128            PreviewAdapterFactory factory = new BlobHolderPreviewAdapterFactory();
129            return factory.getAdapter(doc);
130
131        }
132
133        if (doc.hasSchema("file") || doc.hasSchema("files")) {
134            log.debug("using default file based HtmlPreviewAdapter factory");
135            PreviewAdapterFactory factory = new FileBasedPreviewAdapterFactory();
136            return factory.getAdapter(doc);
137        } else {
138            return null;
139        }
140    }
141
142    public MimeTypePreviewer getPreviewer(String mimeType) {
143        for (Map.Entry<String, MimeTypePreviewer> entry : previewerFactory.entrySet()) {
144            if (mimeType.matches(entry.getKey())) {
145                return entry.getValue();
146            }
147        }
148        return null;
149    }
150
151    public List<BlobPostProcessor> getBlobPostProcessors() {
152        return Collections.unmodifiableList(blobPostProcessors);
153    }
154
155}