File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\widgets\RenderOnDemandClosure.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.widgets;
25
26 import hudson.Util;
27 import hudson.model.Descriptor;
28 import hudson.util.IOException2;
29 import hudson.util.PackedMap;
30 import org.apache.commons.jelly.JellyContext;
31 import org.apache.commons.jelly.JellyTagException;
32 import org.apache.commons.jelly.Script;
33 import org.kohsuke.stapler.HttpResponse;
34 import org.kohsuke.stapler.StaplerRequest;
35 import org.kohsuke.stapler.StaplerResponse;
36 import org.kohsuke.stapler.bind.JavaScriptMethod;
37 import org.kohsuke.stapler.framework.adjunct.AdjunctsInPage;
38 import org.kohsuke.stapler.jelly.DefaultScriptInvoker;
39 import org.xml.sax.SAXException;
40
41 import javax.servlet.ServletException;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Set;
48 import java.util.logging.Level;
49 import java.util.logging.Logger;
50
51 /**
52 * Captured Jelly {@link Script} that can be rendered later on demand from JavaScript.
53 *
54 * @author Kohsuke Kawaguchi
55 */
56 public class RenderOnDemandClosure {
57 /**
58 * Captures the recursive taglib call stack.
59 */
60 private final Script[] bodyStack;
61 private final Map<String,Object> variables;
62 private final String currentDescriptorByNameUrl;
63
64 private final String[] adjuncts;
65
66 public RenderOnDemandClosure(JellyContext context, String attributesToCapture) {
67 List<Script> bodyStack = new ArrayList<Script>();
68 for (JellyContext c = context; c!=null; c=c.getParent()) {
69 Script script = (Script) c.getVariables().get("org.apache.commons.jelly.body");
70 if(script!=null) bodyStack.add(script);
71 }
72 this.bodyStack = bodyStack.toArray(new Script[bodyStack.size()]);
73 assert !bodyStack.isEmpty(); // there must be at least one, which is the direct child of <l:renderOnDemand>
74
75 Map<String,Object> variables = new HashMap<String, Object>();
76 for (String v : Util.fixNull(attributesToCapture).split(","))
77 variables.put(v.intern(),context.getVariable(v));
78
79 // capture the current base of context for descriptors
80 currentDescriptorByNameUrl = Descriptor.getCurrentDescriptorByNameUrl();
81
82 this.variables = PackedMap.of(variables);
83
84 Set<String> _adjuncts = AdjunctsInPage.get().getIncluded();
85 this.adjuncts = new String[_adjuncts.size()];
86 int i = 0;
87 for (String adjunct : _adjuncts) {
88 this.adjuncts[i++] = adjunct.intern();
89 }
90 }
91
92 /**
93 * Renders the captured fragment.
94 */
95 @JavaScriptMethod
96 public HttpResponse render() {
97 return new HttpResponse() {
98 public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
99 try {
100 new DefaultScriptInvoker() {
101 @Override
102 protected JellyContext createContext(StaplerRequest req, StaplerResponse rsp, Script script, Object it) {
103 JellyContext context = super.createContext(req, rsp, script, it);
104 for (int i=bodyStack.length-1; i>0; i--) {// exclude bodyStack[0]
105 context = new JellyContext(context);
106 context.setVariable("org.apache.commons.jelly.body",bodyStack[i]);
107 }
108 try {
109 AdjunctsInPage.get().assumeIncluded(adjuncts);
110 } catch (IOException e) {
111 LOGGER.log(Level.WARNING, "Failed to resurrect adjunct context",e);
112 } catch (SAXException e) {
113 LOGGER.log(Level.WARNING, "Failed to resurrect adjunct context",e);
114 }
115 return context;
116 }
117
118 @Override
119 protected void exportVariables(StaplerRequest req, StaplerResponse rsp, Script script, Object it, JellyContext context) {
120 super.exportVariables(req, rsp, script, it, context);
121 context.setVariables(variables);
122 req.setAttribute("currentDescriptorByNameUrl",currentDescriptorByNameUrl);
123 }
124 }.invokeScript(req,rsp,bodyStack[0],null);
125 } catch (JellyTagException e) {
126 LOGGER.log(Level.WARNING, "Failed to evaluate the template closure", e);
127 throw new IOException2("Failed to evaluate the template closure",e);
128 }
129 }
130 };
131 }
132
133 private static final Logger LOGGER = Logger.getLogger(RenderOnDemandClosure.class.getName());
134 }
135