File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\ui-samples-plugin\src\main\java\jenkins\plugins\ui_samples\ProgressivelyRendered.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright 2012 Jesse Glick.
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
25 package jenkins.plugins.ui_samples;
26
27 import hudson.Extension;
28 import java.util.LinkedList;
29 import java.util.List;
30 import jenkins.util.ProgressiveRendering;
31 import net.sf.json.JSON;
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONObject;
34
35 @Extension
36 public class ProgressivelyRendered extends UISample {
37
38 @Override public String getDescription() {
39 return "Shows how to use progressively rendered content to avoid overloading the server with a slow HTTP request.";
40 }
41
42 public ProgressiveRendering factor(final String numberS) {
43 return new ProgressiveRendering() {
44 final List<Integer> newFactors = new LinkedList<Integer>();
45 @Override protected void compute() throws Exception {
46 int number = Integer.parseInt(numberS); // try entering a nonnumeric value!
47 // Deliberately inefficient:
48 for (int i = 1; i <= number; i++) {
49 if (canceled()) {
50 return;
51 }
52 if (i % 1000000 == 0) {
53 Thread.sleep(10); // take a breather
54 }
55 if (number % i == 0) {
56 synchronized (this) {
57 newFactors.add(i);
58 }
59 }
60 progress(((double) i) / number);
61 }
62 }
63 @Override protected synchronized JSON data() {
64 JSONArray r = new JSONArray();
65 for (int i : newFactors) {
66 r.add(i);
67 }
68 newFactors.clear();
69 return new JSONObject().accumulate("newfactors", r);
70 }
71 };
72 }
73
74 @Extension
75 public static final class DescriptorImpl extends UISampleDescriptor {}
76
77 }
78