/*
 * Copyright 2004-2005 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
 *
 *      http://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.codehaus.groovy.grails.web.servlet.filter;

import groovy.lang.GroovyClassLoader;
import groovy.lang.Writable;
import groovy.text.Template;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator;
import org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext;
import org.codehaus.groovy.grails.plugins.GrailsPluginManager;
import org.codehaus.groovy.grails.plugins.PluginManagerHolder;
import org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException;
import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine;
import org.codehaus.groovy.grails.web.pages.GSPResponseWriter;
import org.codehaus.groovy.grails.web.servlet.DefaultGrailsApplicationAttributes;
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes;
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.UrlPathHelper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import grails.util.GrailsUtil;

/**
 * A servlet filter that copies resources from the source on content change and manages reloading if necessary
 *
 * @author Graeme Rocher
 * @since Jan 10, 2006
 */
public class GrailsReloadServletFilter extends OncePerRequestFilter {
    public static final Log LOG = LogFactory.getLog(GrailsReloadServletFilter.class);
    private static final int BUFFER_SIZE = 8024;

    ResourceCopier copyScript;
    GrailsWebApplicationContext context;
    GrailsApplication application;

    private GrailsRuntimeConfigurator config;

	private GrailsPluginManager manager;
    private UrlPathHelper urlHelper = new UrlPathHelper();
    private WebApplicationContext parent;

    public GrailsReloadServletFilter() {
    }


    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {


      GrailsApplicationAttributes attrs = new DefaultGrailsApplicationAttributes(getServletContext());
      context = (GrailsWebApplicationContext)attrs.getApplicationContext();

      if(LOG.isDebugEnabled()) {
	      LOG.debug("Executing Grails reload filter...");
      }
      if(context == null) {
          filterChain.doFilter(httpServletRequest,httpServletResponse);
          return;
      }
      application = (GrailsApplication)context.getBean(GrailsApplication.APPLICATION_ID);
      if(application == null) {
          filterChain.doFilter(httpServletRequest,httpServletResponse);
          return;
      }      
      if(config == null) {
    	  this.parent = (WebApplicationContext)getServletContext().getAttribute(GrailsApplicationAttributes.PARENT_APPLICATION_CONTEXT);
    	  config = new GrailsRuntimeConfigurator(application,parent);  
      }


      String uri = urlHelper.getPathWithinApplication(httpServletRequest);
      String lastPart = uri.substring(uri.lastIndexOf("/"));
      if(lastPart.indexOf('.') > -1) {
          filterChain.doFilter(httpServletRequest, httpServletResponse);
          return;
      }

      if(copyScript == null) {
          GroovyClassLoader gcl = new GroovyClassLoader(getClass().getClassLoader());

          Class groovyClass;
          try {
              groovyClass = gcl.parseClass(gcl.getResource("org/codehaus/groovy/grails/web/servlet/filter/GrailsResourceCopier.groovy").openStream());
              copyScript = (ResourceCopier)groovyClass.newInstance();
          } catch (IllegalAccessException e) {
              LOG.error("Illegal access creating resource copier. Save/reload disabled: " + e.getMessage(), e);
          } catch (InstantiationException e) {
              LOG.error("Error instantiating resource copier. Save/reload disabled: " + e.getMessage(), e);
          } catch (CompilationFailedException e) {
               LOG.error("Error compiling resource copier. Save/reload disabled: " + e.getMessage(), e);
          } catch(Exception e) {
             LOG.error("Error loading resource copier. Save/reload disabled: " + e.getMessage(), e);
          }
        }
	if(LOG.isDebugEnabled()) {
	      LOG.debug("Running copy script...");
	}	
        if(copyScript != null) {
            copyScript.copyResourceBundles();
        } 

        
        if(manager == null) {
        	manager = PluginManagerHolder.getPluginManager();
        }
        try {
            if(manager != null) {
                if(LOG.isDebugEnabled())
                    LOG.debug("Checking Plugin manager for changes..");
                manager.checkForChanges();
                if(!application.isInitialised()) {
                    application.rebuild();
                    config = new GrailsRuntimeConfigurator(application,parent);
                    config.reconfigure(context, getServletContext(), true);
                }
                
            }
            else if(LOG.isDebugEnabled()) {
                LOG.debug("Plugin manager not found, skipping change check");
            }
        } catch (Exception e) {
            GrailsUtil.deepSanitize(e);
            LOG.error("Error occured reloading application: " + e.getMessage(),e);

            httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

            GroovyPagesTemplateEngine engine = attrs.getPagesTemplateEngine();

            Template t = engine.createTemplate(GrailsApplicationAttributes.PATH_TO_VIEWS + "/error.gsp");

            GrailsWrappedRuntimeException wrapped = new GrailsWrappedRuntimeException(getServletContext(), e);
            Map model = new HashMap();
            model.put("exception", wrapped);

            Writable w = t.make(model);
            Writer out = createResponseWriter(httpServletResponse);
            w.writeTo(out);

            return;
        }
        filterChain.doFilter(httpServletRequest,httpServletResponse);
    }

    protected Writer createResponseWriter(HttpServletResponse response) {
        Writer out = GSPResponseWriter.getInstance(response, BUFFER_SIZE);
        GrailsWebRequest webRequest =  (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
        webRequest.setOut(out);
        return out;
    }

}

@@ -4321,1006 +4321,100 @@
     
-GroovyClassLoader gcl = new GroovyClassLoader(getClass().getClassLoader());%0A%0A          Class groovyClass;%0A          try %7B%0A              groovyClass = gcl.parseClass(gcl.getResource(%22org/codehaus/groovy/grails/web/servlet/filter/GrailsResourceCopier.groovy%22).openStream());%0A              copyScript = (ResourceCopier)groovyClass.newInstance();%0A          %7D catch (IllegalAccessException e) %7B%0A              LOG.error(%22Illegal access creating r
+copyScript = new GrailsR
 esource
- c
+C
 opier
-. Save/reload disabled: %22 + e.getMessage(), e);%0A          %7D catch (InstantiationException e) %7B%0A              LOG.error(%22Error instantiating resource copier. Save/reload disabled: %22 + e.getMessage(), e);%0A          %7D catch (CompilationFailedException e) %7B%0A               LOG.error(%22Error compiling resource copier. Save/reload disabled: %22 + e.getMessage(), e);%0A          %7D catch(Exception e) %7B%0A             LOG.error(%22Error loading resource copier. Save/reload disabled: %22 + e.getMessage(), e);%0A          %7D%0A        %7D%0A%09if(LOG.isDebugEnabled()) %7B%0A%09
+();%0A      %7D%0A        if(LOG.isDebugEnabled()) %7B%0A        
     
@@ -4456,11 +4456,17 @@
 %22);%0A
-%09%7D%09
+        %7D
 %0A   

fix for class loading problem in reload filter