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.template.Template;
018    import com.liferay.portal.kernel.template.TemplateException;
019    
020    import java.io.Writer;
021    
022    import java.security.AccessControlContext;
023    import java.security.AccessController;
024    import java.security.PrivilegedActionException;
025    import java.security.PrivilegedExceptionAction;
026    
027    /**
028     * @author Raymond Aug??
029     */
030    public abstract class AbstractProcessingTemplate implements Template {
031    
032            public abstract TemplateContextHelper getTemplateContextHelper();
033    
034            @Override
035            public final void processTemplate(Writer writer) throws TemplateException {
036                    TemplateControlContext templateControlContext =
037                            getTemplateContextHelper().getTemplateControlContext();
038    
039                    AccessControlContext accessControlContext =
040                            templateControlContext.getAccessControlContext();
041    
042                    if (accessControlContext == null) {
043                            doProcessTemplate(writer);
044    
045                            return;
046                    }
047    
048                    try {
049                            AccessController.doPrivileged(
050                                    new DoProcessTemplatePrivilegedExceptionAction(writer),
051                                    accessControlContext);
052                    }
053                    catch (PrivilegedActionException pae) {
054                            throw (TemplateException)pae.getException();
055                    }
056            }
057    
058            protected abstract void doProcessTemplate(Writer writer)
059                    throws TemplateException;
060    
061            private class DoProcessTemplatePrivilegedExceptionAction
062                    implements PrivilegedExceptionAction<Void> {
063    
064                    public DoProcessTemplatePrivilegedExceptionAction(Writer writer) {
065                            _writer = writer;
066                    }
067    
068                    @Override
069                    public Void run() throws Exception {
070                            doProcessTemplate(_writer);
071    
072                            return null;
073                    }
074    
075                    private Writer _writer;
076    
077            }
078    
079    }