All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.grails.web.servlet.view.SitemeshLayoutViewResolver Maven / Gradle / Ivy

There is a newer version: 2023.1.0-RC1
Show newest version
/*
 * Copyright 2014-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.grails.web.servlet.view;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletContext;

import com.opensymphony.module.sitemesh.Config;
import com.opensymphony.module.sitemesh.Factory;
import com.opensymphony.module.sitemesh.factory.DefaultFactory;
import com.opensymphony.sitemesh.ContentProcessor;
import com.opensymphony.sitemesh.compatability.PageParser2ContentProcessor;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import grails.core.GrailsApplication;
import grails.core.support.GrailsApplicationAware;

import org.grails.web.sitemesh.FactoryHolder;
import org.grails.web.sitemesh.GroovyPageLayoutFinder;
import org.grails.web.sitemesh.SitemeshLayoutView;

public class SitemeshLayoutViewResolver extends GrailsLayoutViewResolver
        implements GrailsApplicationAware, DisposableBean, Ordered, ApplicationListener {

    private static final String FACTORY_SERVLET_CONTEXT_ATTRIBUTE = "sitemesh.factory";

    private ContentProcessor contentProcessor;

    protected GrailsApplication grailsApplication;

    private boolean sitemeshConfigLoaded = false;

    private int order = Ordered.LOWEST_PRECEDENCE - 50;

    public SitemeshLayoutViewResolver() {
        super();
    }

    public SitemeshLayoutViewResolver(ViewResolver innerViewResolver, GroovyPageLayoutFinder groovyPageLayoutFinder) {
        super(innerViewResolver, groovyPageLayoutFinder);
    }

    @Override
    protected View createLayoutView(View innerView) {
        return new SitemeshLayoutView(groovyPageLayoutFinder, innerView, this.contentProcessor);
    }

    public void init() {
        if (servletContext == null) {
            return;
        }

        Factory sitemeshFactory = (Factory) servletContext.getAttribute(FACTORY_SERVLET_CONTEXT_ATTRIBUTE);
        if (sitemeshFactory == null) {
            sitemeshFactory = loadSitemeshConfig();
        }
        this.contentProcessor = new PageParser2ContentProcessor(sitemeshFactory);
    }

    protected Factory loadSitemeshConfig() {
        FilterConfig filterConfig = new FilterConfig() {
            private final Map customConfig =
                    Collections.singletonMap("configFile",
                            "classpath:org/grails/web/sitemesh/sitemesh-default.xml");

            @Override
            public ServletContext getServletContext() {
                return servletContext;
            }

            @Override
            public Enumeration getInitParameterNames() {
                return Collections.enumeration(this.customConfig.keySet());
            }

            @Override
            public String getInitParameter(String name) {
                return this.customConfig.get(name);
            }

            @Override
            public String getFilterName() {
                return null;
            }
        };

        Config config = new Config(filterConfig);
        DefaultFactory sitemeshFactory = new DefaultFactory(config);
        if (servletContext != null) {
            servletContext.setAttribute(FACTORY_SERVLET_CONTEXT_ATTRIBUTE, sitemeshFactory);
        }
        sitemeshFactory.refresh();
        FactoryHolder.setFactory(sitemeshFactory);
        this.sitemeshConfigLoaded = true;
        return sitemeshFactory;
    }

    @Override
    public void setGrailsApplication(GrailsApplication grailsApplication) {
        this.grailsApplication = grailsApplication;
    }

    @Override
    public void destroy() throws Exception {
        clearSitemeshConfig();
    }

    protected void clearSitemeshConfig() {
        if (servletContext == null) {
            return;
        }
        if (this.sitemeshConfigLoaded) {
            FactoryHolder.setFactory(null);
            if (servletContext != null) {
                servletContext.removeAttribute(FACTORY_SERVLET_CONTEXT_ATTRIBUTE);
            }
            this.sitemeshConfigLoaded = false;
        }
    }

    public int getOrder() {
        return this.order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        init();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy