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.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.security.pacl.DoPrivileged;
023    import com.liferay.portal.kernel.util.InstanceFactory;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.PropsUtil;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
028    import com.liferay.portal.util.ClassLoaderUtil;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030    import com.liferay.portlet.documentlibrary.model.DLProcessorConstants;
031    
032    import java.util.Map;
033    import java.util.concurrent.ConcurrentHashMap;
034    
035    /**
036     * @author Mika Koivisto
037     */
038    @DoPrivileged
039    public class DLProcessorRegistryImpl implements DLProcessorRegistry {
040    
041            public void afterPropertiesSet() throws Exception {
042                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
043    
044                    for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
045                            DLProcessor dlProcessor = (DLProcessor)InstanceFactory.newInstance(
046                                    classLoader, dlProcessorClassName);
047    
048                            dlProcessor.afterPropertiesSet();
049    
050                            register(dlProcessor);
051                    }
052            }
053    
054            public void cleanUp(FileEntry fileEntry) {
055                    if (!DLProcessorThreadLocal.isEnabled()) {
056                            return;
057                    }
058    
059                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
060                            if (dlProcessor.isSupported(fileEntry.getMimeType())) {
061                                    dlProcessor.cleanUp(fileEntry);
062                            }
063                    }
064            }
065    
066            public void cleanUp(FileVersion fileVersion) {
067                    if (!DLProcessorThreadLocal.isEnabled()) {
068                            return;
069                    }
070    
071                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
072                            if (dlProcessor.isSupported(fileVersion)) {
073                                    dlProcessor.cleanUp(fileVersion);
074                            }
075                    }
076            }
077    
078            public void exportGeneratedFiles(
079                            PortletDataContext portletDataContext, FileEntry fileEntry,
080                            Element fileEntryElement)
081                    throws Exception {
082    
083                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
084                            return;
085                    }
086    
087                    FileVersion latestFileVersion = _getLatestFileVersion(fileEntry, true);
088    
089                    if (latestFileVersion == null) {
090                            return;
091                    }
092    
093                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
094                            if (dlProcessor.isSupported(latestFileVersion)) {
095                                    dlProcessor.exportGeneratedFiles(
096                                            portletDataContext, fileEntry, fileEntryElement);
097                            }
098                    }
099            }
100    
101            public DLProcessor getDLProcessor(String dlProcessorType) {
102                    return _dlProcessors.get(dlProcessorType);
103            }
104    
105            public void importGeneratedFiles(
106                            PortletDataContext portletDataContext, FileEntry fileEntry,
107                            FileEntry importedFileEntry, Element fileEntryElement)
108                    throws Exception {
109    
110                    if ((importedFileEntry == null) || (importedFileEntry.getSize() == 0)) {
111                            return;
112                    }
113    
114                    FileVersion fileVersion = importedFileEntry.getFileVersion();
115    
116                    if (fileVersion == null) {
117                            return;
118                    }
119    
120                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
121                            if (dlProcessor.isSupported(fileVersion)) {
122                                    dlProcessor.importGeneratedFiles(
123                                            portletDataContext, fileEntry, importedFileEntry,
124                                            fileEntryElement);
125                            }
126                    }
127            }
128    
129            public void register(DLProcessor dlProcessor) {
130                    String type = _getType(dlProcessor);
131    
132                    _dlProcessors.put(type, dlProcessor);
133            }
134    
135            public void trigger(FileEntry fileEntry, FileVersion fileVersion) {
136                    trigger(fileEntry, fileVersion, false);
137            }
138    
139            public void trigger(
140                    FileEntry fileEntry, FileVersion fileVersion, boolean trusted) {
141    
142                    if (!DLProcessorThreadLocal.isEnabled()) {
143                            return;
144                    }
145    
146                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
147                            return;
148                    }
149    
150                    FileVersion latestFileVersion = _getLatestFileVersion(
151                            fileEntry, trusted);
152    
153                    if (latestFileVersion == null) {
154                            return;
155                    }
156    
157                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
158                            if (dlProcessor.isSupported(latestFileVersion)) {
159                                    dlProcessor.trigger(fileVersion, latestFileVersion);
160                            }
161                    }
162            }
163    
164            public void unregister(DLProcessor dlProcessor) {
165                    String type = _getType(dlProcessor);
166    
167                    _dlProcessors.remove(type);
168            }
169    
170            private FileVersion _getLatestFileVersion(
171                    FileEntry fileEntry, boolean trusted) {
172    
173                    FileVersion latestFileVersion = null;
174    
175                    try {
176                            if (fileEntry.getModel() instanceof DLFileEntry) {
177                                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
178    
179                                    latestFileVersion = new LiferayFileVersion(
180                                            dlFileEntry.getLatestFileVersion(trusted));
181                            }
182                            else {
183                                    latestFileVersion = fileEntry.getLatestFileVersion();
184                            }
185    
186                            return latestFileVersion;
187                    }
188                    catch (Exception e) {
189                            _log.error(e, e);
190    
191                            return null;
192                    }
193            }
194    
195            private String _getType(DLProcessor dlProcessor) {
196                    if (dlProcessor instanceof AudioProcessor) {
197                            return DLProcessorConstants.AUDIO_PROCESSOR;
198                    }
199                    else if (dlProcessor instanceof ImageProcessor) {
200                            return DLProcessorConstants.IMAGE_PROCESSOR;
201                    }
202                    else if (dlProcessor instanceof PDFProcessor) {
203                            return DLProcessorConstants.PDF_PROCESSOR;
204                    }
205                    else if (dlProcessor instanceof RawMetadataProcessor) {
206                            return DLProcessorConstants.RAW_METADATA_PROCESSOR;
207                    }
208                    else if (dlProcessor instanceof VideoProcessor) {
209                            return DLProcessorConstants.VIDEO_PROCESSOR;
210                    }
211    
212                    return null;
213            }
214    
215            private static final String[] _DL_FILE_ENTRY_PROCESSORS =
216                    PropsUtil.getArray(PropsKeys.DL_FILE_ENTRY_PROCESSORS);
217    
218            private static Log _log = LogFactoryUtil.getLog(
219                    DLProcessorRegistryImpl.class);
220    
221            private Map<String, DLProcessor> _dlProcessors =
222                    new ConcurrentHashMap<String, DLProcessor>();
223    
224    }