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.portal.webdav.methods;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.webdav.methods.MethodFactory;
021    import com.liferay.portal.kernel.webdav.methods.MethodFactoryRegistry;
022    
023    import java.util.List;
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class MethodFactoryRegistryImpl implements MethodFactoryRegistry {
031    
032            @Override
033            public MethodFactory getDefaultMethodFactory() {
034                    return _defaultMethodFactory;
035            }
036    
037            @Override
038            public List<MethodFactory> getMethodFactories() {
039                    return ListUtil.fromMapValues(_methodFactories);
040            }
041    
042            @Override
043            public MethodFactory getMethodFactory(String className) {
044                    return _methodFactories.get(className);
045            }
046    
047            @Override
048            public void registerMethodFactory(MethodFactory methodFactory) {
049                    Class<?> clazz = methodFactory.getClass();
050    
051                    MethodFactory previousMethodFactory = _methodFactories.put(
052                            clazz.getName(), methodFactory);
053    
054                    if (previousMethodFactory == _defaultMethodFactory) {
055                            _defaultMethodFactory = methodFactory;
056                    }
057    
058                    if (_log.isWarnEnabled() && (previousMethodFactory != null)) {
059                            _log.warn(
060                                    "Replacing " + previousMethodFactory + " for class name " +
061                                            clazz.getName() + " with " + methodFactory);
062                    }
063            }
064    
065            public void setDefaultMethodFactory(MethodFactory defaultMethodFactory) {
066                    _defaultMethodFactory = defaultMethodFactory;
067            }
068    
069            @Override
070            public void unregisterMethodFactory(MethodFactory methodFactory) {
071                    Class<?> clazz = methodFactory.getClass();
072    
073                    _methodFactories.remove(clazz.getName());
074            }
075    
076            private static Log _log = LogFactoryUtil.getLog(
077                    MethodFactoryRegistryImpl.class);
078    
079            private MethodFactory _defaultMethodFactory;
080            private Map<String, MethodFactory> _methodFactories =
081                    new ConcurrentHashMap<String, MethodFactory>();
082    
083    }