001package org.kohsuke.stapler.jelly.groovy;
002
003import org.kohsuke.stapler.AbstractTearOff;
004import org.kohsuke.stapler.MetaClass;
005import org.kohsuke.stapler.jelly.JellyRequestDispatcher;
006
007import javax.servlet.RequestDispatcher;
008import java.io.IOException;
009import java.net.URL;
010
011/**
012 * @author Kohsuke Kawaguchi
013 */
014public class GroovyServerPageTearOff extends AbstractTearOff<GroovyClassLoaderTearOff,GroovierJellyScript,IOException> {
015    public GroovyServerPageTearOff(MetaClass owner) {
016        super(owner,GroovyClassLoaderTearOff.class);
017    }
018
019    @Override
020    protected String getDefaultScriptExtension() {
021        return ".gsp";
022    }
023
024    public GroovierJellyScript parseScript(URL res) throws IOException {
025        try {
026            return classLoader.parseGSP(res);
027        } catch (ClassNotFoundException e) {
028            throw (IOException)new IOException("Failed to compile "+res).initCause(e);
029        }
030    }
031
032    /**
033     * Creates a {@link RequestDispatcher} that forwards to the jelly view, if available.
034     */
035    @Deprecated
036    public RequestDispatcher createDispatcher(Object it, String viewName) throws IOException {
037        GroovierJellyScript s = findScript(viewName);
038        if (s!=null)    return new JellyRequestDispatcher(it,s);
039        return null;
040    }
041}