001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }