001    /**
002     * Copyright (c) 2000-2011 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.FileVersion;
021    import com.liferay.portal.kernel.util.InstancePool;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    
027    import java.util.List;
028    import java.util.concurrent.CopyOnWriteArrayList;
029    
030    /**
031     * @author Mika Koivisto
032     */
033    public class DLProcessorRegistryImpl implements DLProcessorRegistry {
034    
035            public void cleanUp(FileEntry fileEntry) {
036                    if (!DLProcessorThreadLocal.isEnabled()) {
037                            return;
038                    }
039    
040                    for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
041                            DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
042                                    dlProcessorClassName);
043    
044                            dlProcessor.cleanUp(fileEntry);
045                    }
046    
047                    for (DLProcessor dlProcessor : _dlProcessors) {
048                            dlProcessor.cleanUp(fileEntry);
049                    }
050            }
051    
052            public void cleanUp(FileVersion fileVersion) {
053                    if (!DLProcessorThreadLocal.isEnabled()) {
054                            return;
055                    }
056    
057                    for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
058                            DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
059                                    dlProcessorClassName);
060    
061                            dlProcessor.cleanUp(fileVersion);
062                    }
063    
064                    for (DLProcessor dlProcessor : _dlProcessors) {
065                            dlProcessor.cleanUp(fileVersion);
066                    }
067            }
068    
069            public void register(DLProcessor dlProcessor) {
070                    _dlProcessors.add(dlProcessor);
071            }
072    
073            public void trigger(FileEntry fileEntry) {
074                    if (!DLProcessorThreadLocal.isEnabled()) {
075                            return;
076                    }
077    
078                    if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
079                            return;
080                    }
081    
082                    FileVersion latestFileVersion = null;
083    
084                    try {
085                            if (fileEntry.getModel() instanceof DLFileEntry) {
086                                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
087    
088                                    latestFileVersion = new LiferayFileVersion(
089                                            dlFileEntry.getLatestFileVersion(false));
090                            }
091                            else {
092                                    latestFileVersion = fileEntry.getLatestFileVersion();
093                            }
094                    }
095                    catch (Exception e) {
096                            _log.error(e, e);
097    
098                            return;
099                    }
100    
101                    for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
102                            DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
103                                    dlProcessorClassName);
104    
105                            if (dlProcessor.isSupported(latestFileVersion)) {
106                                    dlProcessor.trigger(latestFileVersion);
107                            }
108                    }
109    
110                    for (DLProcessor dlProcessor : _dlProcessors) {
111                            if (dlProcessor.isSupported(latestFileVersion)) {
112                                    dlProcessor.trigger(latestFileVersion);
113                            }
114                    }
115            }
116    
117            public void unregister(DLProcessor dlProcessor) {
118                    _dlProcessors.remove(dlProcessor);
119            }
120    
121            private static Log _log = LogFactoryUtil.getLog(
122                    DLProcessorRegistryImpl.class);
123    
124            private static final String[] _DL_FILE_ENTRY_PROCESSORS =
125                    PropsUtil.getArray(PropsKeys.DL_FILE_ENTRY_PROCESSORS);
126    
127            private List<DLProcessor> _dlProcessors =
128                    new CopyOnWriteArrayList<DLProcessor>();
129    
130    }