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            public final void processTemplate(Writer writer) throws TemplateException {
035                    TemplateControlContext templateControlContext =
036                            getTemplateContextHelper().getTemplateControlContext();
037    
038                    AccessControlContext accessControlContext =
039                            templateControlContext.getAccessControlContext();
040    
041                    if (accessControlContext == null) {
042                            doProcessTemplate(writer);
043    
044                            return;
045                    }
046    
047                    try {
048                            AccessController.doPrivileged(
049                                    new DoProcessTemplatePrivilegedExceptionAction(writer),
050                                    accessControlContext);
051                    }
052                    catch (PrivilegedActionException pae) {
053                            throw (TemplateException)pae.getException();
054                    }
055            }
056    
057            protected abstract void doProcessTemplate(Writer writer)
058                    throws TemplateException;
059    
060            private class DoProcessTemplatePrivilegedExceptionAction
061                    implements PrivilegedExceptionAction<Void> {
062    
063                    public DoProcessTemplatePrivilegedExceptionAction(Writer writer) {
064                            _writer = writer;
065                    }
066    
067                    public Void run() throws Exception {
068                            doProcessTemplate(_writer);
069    
070                            return null;
071                    }
072    
073                    private Writer _writer;
074    
075            }
076    
077    }