001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.kernel.lar.PortletDataContext;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.FileVersion;
022    import com.liferay.portal.kernel.util.InstanceFactory;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.PropsUtil;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
027    import com.liferay.portal.util.ClassLoaderUtil;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.model.DLProcessorConstants;
030    
031    import java.util.Map;
032    import java.util.concurrent.ConcurrentHashMap;
033    
034    /**
035     * @author Mika Koivisto
036     */
037    public class DLProcessorRegistryImpl implements DLProcessorRegistry {
038    
039            public void afterPropertiesSet() throws Exception {
040                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
041    
042                    for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
043                            DLProcessor dlProcessor = (DLProcessor)InstanceFactory.newInstance(
044                                    classLoader, dlProcessorClassName);
045    
046                            dlProcessor.afterPropertiesSet();
047    
048                            register(dlProcessor);
049                    }
050            }
051    
052            public void cleanUp(FileEntry fileEntry) {
053                    if (!DLProcessorThreadLocal.isEnabled()) {
054                            return;
055                    }
056    
057                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
058                            if (dlProcessor.isSupported(fileEntry.getMimeType())) {
059                                    dlProcessor.cleanUp(fileEntry);
060                            }
061                    }
062            }
063    
064            public void cleanUp(FileVersion fileVersion) {
065                    if (!DLProcessorThreadLocal.isEnabled()) {
066                            return;
067                    }
068    
069                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
070                            if (dlProcessor.isSupported(fileVersion)) {
071                                    dlProcessor.cleanUp(fileVersion);
072                            }
073                    }
074            }
075    
076            public void exportGeneratedFiles(
077                            PortletDataContext portletDataContext, FileEntry fileEntry,
078                            Element fileEntryElement)
079                    throws Exception {
080    
081                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
082                            return;
083                    }
084    
085                    FileVersion latestFileVersion = _getLatestFileVersion(fileEntry, true);
086    
087                    if (latestFileVersion == null) {
088                            return;
089                    }
090    
091                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
092                            if (dlProcessor.isSupported(latestFileVersion)) {
093                                    dlProcessor.exportGeneratedFiles(
094                                            portletDataContext, fileEntry, fileEntryElement);
095                            }
096                    }
097            }
098    
099            public DLProcessor getDLProcessor(String dlProcessorType) {
100                    return _dlProcessors.get(dlProcessorType);
101            }
102    
103            public void importGeneratedFiles(
104                            PortletDataContext portletDataContext, FileEntry fileEntry,
105                            FileEntry importedFileEntry, Element fileEntryElement)
106                    throws Exception {
107    
108                    if ((importedFileEntry == null) || (importedFileEntry.getSize() == 0)) {
109                            return;
110                    }
111    
112                    FileVersion fileVersion = importedFileEntry.getFileVersion();
113    
114                    if (fileVersion == null) {
115                            return;
116                    }
117    
118                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
119                            if (dlProcessor.isSupported(fileVersion)) {
120                                    dlProcessor.importGeneratedFiles(
121                                            portletDataContext, fileEntry, importedFileEntry,
122                                            fileEntryElement);
123                            }
124                    }
125            }
126    
127            public void register(DLProcessor dlProcessor) {
128                    String type = _getType(dlProcessor);
129    
130                    _dlProcessors.put(type, dlProcessor);
131            }
132    
133            public void trigger(FileEntry fileEntry, FileVersion fileVersion) {
134                    trigger(fileEntry, fileVersion, false);
135            }
136    
137            public void trigger(
138                    FileEntry fileEntry, FileVersion fileVersion, boolean trusted) {
139    
140                    if (!DLProcessorThreadLocal.isEnabled()) {
141                            return;
142                    }
143    
144                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
145                            return;
146                    }
147    
148                    FileVersion latestFileVersion = _getLatestFileVersion(
149                            fileEntry, trusted);
150    
151                    if (latestFileVersion == null) {
152                            return;
153                    }
154    
155                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
156                            if (dlProcessor.isSupported(latestFileVersion)) {
157                                    dlProcessor.trigger(fileVersion, latestFileVersion);
158                            }
159                    }
160            }
161    
162            public void unregister(DLProcessor dlProcessor) {
163                    String type = _getType(dlProcessor);
164    
165                    _dlProcessors.remove(type);
166            }
167    
168            private FileVersion _getLatestFileVersion(
169                    FileEntry fileEntry, boolean trusted) {
170    
171                    FileVersion latestFileVersion = null;
172    
173                    try {
174                            if (fileEntry.getModel() instanceof DLFileEntry) {
175                                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
176    
177                                    latestFileVersion = new LiferayFileVersion(
178                                            dlFileEntry.getLatestFileVersion(trusted));
179                            }
180                            else {
181                                    latestFileVersion = fileEntry.getLatestFileVersion();
182                            }
183    
184                            return latestFileVersion;
185                    }
186                    catch (Exception e) {
187                            _log.error(e, e);
188    
189                            return null;
190                    }
191            }
192    
193            private String _getType(DLProcessor dlProcessor) {
194                    if (dlProcessor instanceof AudioProcessor) {
195                            return DLProcessorConstants.AUDIO_PROCESSOR;
196                    }
197                    else if (dlProcessor instanceof ImageProcessor) {
198                            return DLProcessorConstants.IMAGE_PROCESSOR;
199                    }
200                    else if (dlProcessor instanceof PDFProcessor) {
201                            return DLProcessorConstants.PDF_PROCESSOR;
202                    }
203                    else if (dlProcessor instanceof RawMetadataProcessor) {
204                            return DLProcessorConstants.RAW_METADATA_PROCESSOR;
205                    }
206                    else if (dlProcessor instanceof VideoProcessor) {
207                            return DLProcessorConstants.VIDEO_PROCESSOR;
208                    }
209    
210                    return null;
211            }
212    
213            private static final String[] _DL_FILE_ENTRY_PROCESSORS =
214                    PropsUtil.getArray(PropsKeys.DL_FILE_ENTRY_PROCESSORS);
215    
216            private static Log _log = LogFactoryUtil.getLog(
217                    DLProcessorRegistryImpl.class);
218    
219            private Map<String, DLProcessor> _dlProcessors =
220                    new ConcurrentHashMap<String, DLProcessor>();
221    
222    }