1 package org.kohsuke.stapler.jelly; 2 3 import org.apache.commons.jelly.JellyContext; 4 import org.apache.commons.jelly.JellyTagException; 5 import org.apache.commons.jelly.MissingAttributeException; 6 import org.apache.commons.jelly.Script; 7 import org.apache.commons.jelly.TagSupport; 8 import org.apache.commons.jelly.XMLOutput; 9 import org.kohsuke.stapler.MetaClass; 10 11 16 public class IncludeTag extends TagSupport { 17 private Object it; 18 19 private String page; 20 21 private Object from; 22 23 26 public void setPage(String page) { 27 this.page = page; 28 } 29 30 33 public void setIt(Object it) { 34 this.it = it; 35 } 36 37 41 public void setFrom(Object from) { 42 this.from = from; 43 } 44 45 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { 46 Object it = this.it; 47 if(it==null) 48 it = getContext().getVariable("it"); 49 50 MetaClass c = MetaClass.get((from!=null?from:it).getClass()); 51 Script script; 52 try { 53 script = c.loadTearOff(JellyClassTearOff.class).findScript(page); 54 } catch (RuntimeException e) { 55 throw e; 56 } catch (Exception e) { 57 throw new JellyTagException("Error loading '"+page+"' for "+it.getClass(),e); 58 } 59 60 if(script==null) { 61 throw new JellyTagException("No page found '"+page+"' for "+it.getClass()); 62 } 63 64 JellyContext context = getContext(); 65 if(this.it!=null) { 66 context = new JellyContext(context); 67 context.setVariable("it",this.it); 68 } 69 70 ClassLoader old = Thread.currentThread().getContextClassLoader(); 71 Thread.currentThread().setContextClassLoader(c.classLoader.loader); 72 try { 73 script.run(context,output); 74 } finally { 75 Thread.currentThread().setContextClassLoader(old); 76 } 77 } 78 } 79 | Popular Tags |