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.template;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.template.TemplateHandler;
019    import com.liferay.portal.kernel.template.TemplateHandlerRegistry;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.util.PortalUtil;
022    
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    /**
028     * @author Juan Fernández
029     */
030    @DoPrivileged
031    public class TemplateHandlerRegistryImpl implements TemplateHandlerRegistry {
032    
033            public long[] getClassNameIds() {
034                    long[] classNameIds = new long[_templateHandlers.size()];
035    
036                    int i = 0;
037    
038                    for (Map.Entry<String, TemplateHandler> entry :
039                                    _templateHandlers.entrySet()) {
040    
041                            TemplateHandler templateHandler = entry.getValue();
042    
043                            classNameIds[i++] = PortalUtil.getClassNameId(
044                                    templateHandler.getClassName());
045                    }
046    
047                    return classNameIds;
048            }
049    
050            public TemplateHandler getTemplateHandler(long classNameId) {
051                    String className = PortalUtil.getClassName(classNameId);
052    
053                    return _templateHandlers.get(className);
054            }
055    
056            public TemplateHandler getTemplateHandler(String className) {
057                    return _templateHandlers.get(className);
058            }
059    
060            public List<TemplateHandler> getTemplateHandlers() {
061                    return ListUtil.fromMapValues(_templateHandlers);
062            }
063    
064            public void register(TemplateHandler templateHandler) {
065                    _templateHandlers.put(templateHandler.getClassName(), templateHandler);
066            }
067    
068            public void unregister(TemplateHandler templateHandler) {
069                    _templateHandlers.remove(templateHandler.getClassName());
070            }
071    
072            private Map<String, TemplateHandler> _templateHandlers =
073                    new HashMap<String, TemplateHandler>();
074    
075    }