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            @Override
034            public long[] getClassNameIds() {
035                    long[] classNameIds = new long[_templateHandlers.size()];
036    
037                    int i = 0;
038    
039                    for (Map.Entry<String, TemplateHandler> entry :
040                                    _templateHandlers.entrySet()) {
041    
042                            TemplateHandler templateHandler = entry.getValue();
043    
044                            classNameIds[i++] = PortalUtil.getClassNameId(
045                                    templateHandler.getClassName());
046                    }
047    
048                    return classNameIds;
049            }
050    
051            @Override
052            public TemplateHandler getTemplateHandler(long classNameId) {
053                    String className = PortalUtil.getClassName(classNameId);
054    
055                    return _templateHandlers.get(className);
056            }
057    
058            @Override
059            public TemplateHandler getTemplateHandler(String className) {
060                    return _templateHandlers.get(className);
061            }
062    
063            @Override
064            public List<TemplateHandler> getTemplateHandlers() {
065                    return ListUtil.fromMapValues(_templateHandlers);
066            }
067    
068            @Override
069            public void register(TemplateHandler templateHandler) {
070                    _templateHandlers.put(templateHandler.getClassName(), templateHandler);
071            }
072    
073            @Override
074            public void unregister(TemplateHandler templateHandler) {
075                    _templateHandlers.remove(templateHandler.getClassName());
076            }
077    
078            private Map<String, TemplateHandler> _templateHandlers =
079                    new HashMap<String, TemplateHandler>();
080    
081    }