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