001    /**
002     * Copyright (c) 2000-2013 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.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.FileVersion;
023    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
024    import com.liferay.portal.kernel.util.InstanceFactory;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.PropsUtil;
027    import com.liferay.portal.kernel.util.ProxyUtil;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
030    import com.liferay.portal.util.ClassLoaderUtil;
031    import com.liferay.portal.util.PrefsPropsUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034    import com.liferay.portlet.documentlibrary.model.DLProcessorConstants;
035    
036    import java.lang.reflect.InvocationHandler;
037    
038    import java.util.Map;
039    import java.util.concurrent.ConcurrentHashMap;
040    
041    /**
042     * @author Mika Koivisto
043     */
044    @DoPrivileged
045    public class DLProcessorRegistryImpl implements DLProcessorRegistry {
046    
047            public void afterPropertiesSet() throws Exception {
048                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
049    
050                    for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
051                            DLProcessor dlProcessor = (DLProcessor)InstanceFactory.newInstance(
052                                    classLoader, dlProcessorClassName);
053    
054                            dlProcessor.afterPropertiesSet();
055    
056                            register(dlProcessor);
057                    }
058            }
059    
060            @Override
061            public void cleanUp(FileEntry fileEntry) {
062                    if (!DLProcessorThreadLocal.isEnabled()) {
063                            return;
064                    }
065    
066                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
067                            if (dlProcessor.isSupported(fileEntry.getMimeType())) {
068                                    dlProcessor.cleanUp(fileEntry);
069                            }
070                    }
071            }
072    
073            @Override
074            public void cleanUp(FileVersion fileVersion) {
075                    if (!DLProcessorThreadLocal.isEnabled()) {
076                            return;
077                    }
078    
079                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
080                            if (dlProcessor.isSupported(fileVersion)) {
081                                    dlProcessor.cleanUp(fileVersion);
082                            }
083                    }
084            }
085    
086            @Override
087            public void exportGeneratedFiles(
088                            PortletDataContext portletDataContext, FileEntry fileEntry,
089                            Element fileEntryElement)
090                    throws Exception {
091    
092                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
093                            return;
094                    }
095    
096                    FileVersion latestFileVersion = _getLatestFileVersion(fileEntry, true);
097    
098                    if (latestFileVersion == null) {
099                            return;
100                    }
101    
102                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
103                            if (dlProcessor.isSupported(latestFileVersion)) {
104                                    dlProcessor.exportGeneratedFiles(
105                                            portletDataContext, fileEntry, fileEntryElement);
106                            }
107                    }
108            }
109    
110            @Override
111            public DLProcessor getDLProcessor(String dlProcessorType) {
112                    return _dlProcessors.get(dlProcessorType);
113            }
114    
115            @Override
116            public void importGeneratedFiles(
117                            PortletDataContext portletDataContext, FileEntry fileEntry,
118                            FileEntry importedFileEntry, Element fileEntryElement)
119                    throws Exception {
120    
121                    if ((importedFileEntry == null) || (importedFileEntry.getSize() == 0)) {
122                            return;
123                    }
124    
125                    FileVersion fileVersion = importedFileEntry.getFileVersion();
126    
127                    if (fileVersion == null) {
128                            return;
129                    }
130    
131                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
132                            if (dlProcessor.isSupported(fileVersion)) {
133                                    dlProcessor.importGeneratedFiles(
134                                            portletDataContext, fileEntry, importedFileEntry,
135                                            fileEntryElement);
136                            }
137                    }
138            }
139    
140            @Override
141            public boolean isPreviewableSize(FileVersion fileVersion) {
142                    long fileEntryPreviewableProcessorMaxSize =
143                            PropsValues.DL_FILE_ENTRY_PREVIEWABLE_PROCESSOR_MAX_SIZE;
144    
145                    try {
146                            fileEntryPreviewableProcessorMaxSize = PrefsPropsUtil.getLong(
147                                    PropsKeys.DL_FILE_ENTRY_PREVIEWABLE_PROCESSOR_MAX_SIZE);
148                    }
149                    catch (Exception e) {
150                            _log.error(e, e);
151                    }
152    
153                    if (fileEntryPreviewableProcessorMaxSize == 0) {
154                            return false;
155                    }
156    
157                    if ((fileEntryPreviewableProcessorMaxSize > 0) &&
158                            (fileVersion.getSize() > fileEntryPreviewableProcessorMaxSize)) {
159    
160                            return false;
161                    }
162    
163                    return true;
164            }
165    
166            @Override
167            public void register(DLProcessor dlProcessor) {
168                    String type = _getType(dlProcessor);
169    
170                    if (type != null) {
171                            _dlProcessors.put(type, dlProcessor);
172                    }
173            }
174    
175            @Override
176            public void trigger(FileEntry fileEntry, FileVersion fileVersion) {
177                    trigger(fileEntry, fileVersion, false);
178            }
179    
180            @Override
181            public void trigger(
182                    FileEntry fileEntry, FileVersion fileVersion, boolean trusted) {
183    
184                    if (!DLProcessorThreadLocal.isEnabled()) {
185                            return;
186                    }
187    
188                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
189                            return;
190                    }
191    
192                    FileVersion latestFileVersion = _getLatestFileVersion(
193                            fileEntry, trusted);
194    
195                    if (latestFileVersion == null) {
196                            return;
197                    }
198    
199                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
200                            if (dlProcessor.isSupported(latestFileVersion)) {
201                                    dlProcessor.trigger(fileVersion, latestFileVersion);
202                            }
203                    }
204            }
205    
206            @Override
207            public void unregister(DLProcessor dlProcessor) {
208                    String type = _getType(dlProcessor);
209    
210                    _dlProcessors.remove(type);
211            }
212    
213            private FileVersion _getLatestFileVersion(
214                    FileEntry fileEntry, boolean trusted) {
215    
216                    FileVersion latestFileVersion = null;
217    
218                    try {
219                            if (fileEntry.getModel() instanceof DLFileEntry) {
220                                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
221    
222                                    latestFileVersion = new LiferayFileVersion(
223                                            dlFileEntry.getLatestFileVersion(trusted));
224                            }
225                            else {
226                                    latestFileVersion = DLImpl.getLatestFileVersion(
227                                            fileEntry, trusted);
228                            }
229    
230                            return latestFileVersion;
231                    }
232                    catch (Exception e) {
233                            _log.error(e, e);
234    
235                            return null;
236                    }
237            }
238    
239            private String _getType(DLProcessor dlProcessor) {
240                    if (ProxyUtil.isProxyClass(dlProcessor.getClass())) {
241                            InvocationHandler invocationHandler =
242                                    ProxyUtil.getInvocationHandler(dlProcessor);
243    
244                            if (invocationHandler instanceof ClassLoaderBeanHandler) {
245                                    ClassLoaderBeanHandler classLoaderBeanHandler =
246                                            (ClassLoaderBeanHandler)invocationHandler;
247    
248                                    Object bean = classLoaderBeanHandler.getBean();
249    
250                                    if (bean instanceof DLProcessor) {
251                                            dlProcessor = (DLProcessor)bean;
252                                    }
253                            }
254                    }
255    
256                    if (dlProcessor instanceof AudioProcessor) {
257                            return DLProcessorConstants.AUDIO_PROCESSOR;
258                    }
259                    else if (dlProcessor instanceof ImageProcessor) {
260                            return DLProcessorConstants.IMAGE_PROCESSOR;
261                    }
262                    else if (dlProcessor instanceof PDFProcessor) {
263                            return DLProcessorConstants.PDF_PROCESSOR;
264                    }
265                    else if (dlProcessor instanceof RawMetadataProcessor) {
266                            return DLProcessorConstants.RAW_METADATA_PROCESSOR;
267                    }
268                    else if (dlProcessor instanceof VideoProcessor) {
269                            return DLProcessorConstants.VIDEO_PROCESSOR;
270                    }
271    
272                    return null;
273            }
274    
275            private static final String[] _DL_FILE_ENTRY_PROCESSORS =
276                    PropsUtil.getArray(PropsKeys.DL_FILE_ENTRY_PROCESSORS);
277    
278            private static Log _log = LogFactoryUtil.getLog(
279                    DLProcessorRegistryImpl.class);
280    
281            private Map<String, DLProcessor> _dlProcessors =
282                    new ConcurrentHashMap<String, DLProcessor>();
283    
284    }