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.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    }