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.PortalClassLoaderUtil;
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.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 = PortalClassLoaderUtil.getClassLoader();
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);
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                    if (!DLProcessorThreadLocal.isEnabled()) {
135                            return;
136                    }
137    
138                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
139                            return;
140                    }
141    
142                    FileVersion latestFileVersion = _getLatestFileVersion(fileEntry);
143    
144                    if (latestFileVersion == null) {
145                            return;
146                    }
147    
148                    for (DLProcessor dlProcessor : _dlProcessors.values()) {
149                            if (dlProcessor.isSupported(latestFileVersion)) {
150                                    dlProcessor.trigger(fileVersion, latestFileVersion);
151                            }
152                    }
153            }
154    
155            public void unregister(DLProcessor dlProcessor) {
156                    String type = _getType(dlProcessor);
157    
158                    _dlProcessors.remove(type);
159            }
160    
161            private FileVersion _getLatestFileVersion(FileEntry fileEntry) {
162                    FileVersion latestFileVersion = null;
163    
164                    try {
165                            if (fileEntry.getModel() instanceof DLFileEntry) {
166                                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
167    
168                                    latestFileVersion = new LiferayFileVersion(
169                                            dlFileEntry.getLatestFileVersion(false));
170                            }
171                            else {
172                                    latestFileVersion = fileEntry.getLatestFileVersion();
173                            }
174    
175                            return latestFileVersion;
176                    }
177                    catch (Exception e) {
178                            _log.error(e, e);
179    
180                            return null;
181                    }
182            }
183    
184            private String _getType(DLProcessor dlProcessor) {
185                    if (dlProcessor instanceof AudioProcessor) {
186                            return DLProcessorConstants.AUDIO_PROCESSOR;
187                    }
188                    else if (dlProcessor instanceof ImageProcessor) {
189                            return DLProcessorConstants.IMAGE_PROCESSOR;
190                    }
191                    else if (dlProcessor instanceof PDFProcessor) {
192                            return DLProcessorConstants.PDF_PROCESSOR;
193                    }
194                    else if (dlProcessor instanceof RawMetadataProcessor) {
195                            return DLProcessorConstants.RAW_METADATA_PROCESSOR;
196                    }
197                    else if (dlProcessor instanceof VideoProcessor) {
198                            return DLProcessorConstants.VIDEO_PROCESSOR;
199                    }
200    
201                    return null;
202            }
203    
204            private static final String[] _DL_FILE_ENTRY_PROCESSORS =
205                    PropsUtil.getArray(PropsKeys.DL_FILE_ENTRY_PROCESSORS);
206    
207            private static Log _log = LogFactoryUtil.getLog(
208                    DLProcessorRegistryImpl.class);
209    
210            private Map<String, DLProcessor> _dlProcessors =
211                    new ConcurrentHashMap<String, DLProcessor>();
212    
213    }