Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kohsuke.stapler.jelly.jruby;
  import  org.apache.commons.jelly.JellyTagException;
  import  org.apache.commons.jelly.Script;
 import java.util.List;
 import java.util.Map;
Facet that adds Ruby-based view technologies.

Kohsuke Kawaguchi
Hiroshi Nakamura
 public class JRubyFacet extends Facet implements JellyCompatibleFacet {
     /*package*/ final List<RubyTemplateLanguagelanguages = new CopyOnWriteArrayList<RubyTemplateLanguage>();

There are all kinds of downsides in doing this, but for the time being we just use one scripting container.
     private final ScriptingContainer container;
     private final RubyKlassNavigator navigator;

RubyTemplateContainers keyed by their extensions. (since container is a singleton per JRubyFacet, this is also just one map.
     private final Collection<Class<? extends AbstractRubyTearOff>> tearOffTypes = new CopyOnWriteArrayList<Class<? extends AbstractRubyTearOff>>();
     public JRubyFacet() {
         // TODO: is this too early? Shall we allow registrations later?
                 Thread.currentThread().getContextClassLoader(), getClass().getClassLoader()));
          = new ScriptingContainer(.); // we don't want any funny multiplexing from ScriptingContainer.
         .runScriptlet("require 'org/kohsuke/stapler/jelly/jruby/JRubyJellyScriptImpl'");
         for (RubyTemplateLanguage l : ) {
         int idx = path.lastIndexOf('.');
         if (idx >= 0) {
             RubyTemplateContainer t = .get(path.substring(idx));
             if (t!=null)    return t;
         throw new IllegalArgumentException("Unrecognized file extension: "+path);
     public Script parseScript(URL templatethrows IOException {
         return selectTemplateContainer(template.getPath()).parseScript(template);
     public Klass<RubyModulegetKlass(Object o) {
         if (o instanceof RubyObject)
             return makeKlass(((RubyObjecto).getMetaClass());
         return null;
     private Klass<RubyModulemakeKlass(RubyModule o) {
         return new Klass<RubyModule>(o,);
    public synchronized MetaClass getClassInfo(RubyClass r) {
        return WebApp.getCurrent().getMetaClass(makeKlass(r));
    private boolean isRuby(MetaClass mc) {
        return mc.klass.clazz instanceof RubyModule;
    public void buildViewDispatchers(final MetaClass ownerList<Dispatcherdispatchers) {
        for (final Class<? extends AbstractRubyTearOfft : getClassTearOffTypes()) {
            dispatchers.add(new ScriptInvokingDispatcher() {
                final AbstractRubyTearOff tearOff = owner.loadTearOff(t);
                public boolean dispatch(RequestImpl reqResponseImpl rspObject nodethrows IOExceptionServletException {
                    String next = req.tokens.peek();
                    if(next==nullreturn false;
                    // only match the end of the URL
                    if (req.tokens.countRemainingTokens()>1)    return false;
                    // and avoid serving both "foo" and "foo/" as relative URL semantics are drastically different
                    if (req.getRequestURI().endsWith("/"))      return false;
                    return invokeScript(reqrspnodenext.findScript(next));
    public void buildFallbackDispatchers(MetaClass ownerList<Dispatcherdispatchers) {
        if (isRuby(owner)) {
            dispatchers.add(new RackDispatcher());
    public Collection<Class<? extends AbstractRubyTearOff>> getClassTearOffTypes() {
        return ;
        List<Stringr = new ArrayList<String>();
        for (RubyTemplateLanguage l : )
        return r;
    public RequestDispatcher createRequestDispatcher(RequestImpl requestKlass<?> typeObject itString viewNamethrows IOException {
        TearOffSupport mc = request.stapler.getWebApp().getMetaClass(type);
        return mc.loadTearOff(ERbClassTearOff.class).createDispatcher(it,viewName);
    public boolean handleIndexRequest(RequestImpl reqResponseImpl rspObject nodeMetaClass mcthrows IOExceptionServletException {
        for (Class<? extends AbstractRubyTearOfft : getClassTearOffTypes()) {
            AbstractRubyTearOff rt = mc.loadTearOff(t);
            Script script = rt.findScript("index");
            if(script!=null) {
                try {
                        .fine("Invoking index"+rt.getDefaultScriptExtension()+" on " + node);
                    return true;
                } catch (JellyTagException e) {
                    throw new ServletException(e);
        return false;
New to GrepCode? Check out our FAQ X