View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.nio.integration;
29  
30  import java.util.Random;
31  
32  @Deprecated
33  public class Job {
34  
35      private static final Random RND = new Random();
36      private static final String TEST_CHARS = "0123456789ABCDEF";
37  
38      private final int count;
39      private final String pattern;
40  
41      private volatile boolean completed;
42      private volatile int statusCode;
43      private volatile String result;
44      private volatile String failureMessage;
45      private volatile Exception ex;
46  
47      public Job(int maxCount) {
48          super();
49          this.count = RND.nextInt(maxCount - 1) + 1;
50          StringBuilder buffer = new StringBuilder();
51          for (int i = 0; i < 5; i++) {
52              char rndchar = TEST_CHARS.charAt(RND.nextInt(TEST_CHARS.length() - 1));
53              buffer.append(rndchar);
54          }
55          this.pattern = buffer.toString();
56      }
57  
58      public Job() {
59          this(1000);
60      }
61  
62      public Job(final String pattern, int count) {
63          super();
64          this.count = count;
65          this.pattern = pattern;
66      }
67  
68      public int getCount() {
69          return this.count;
70      }
71  
72      public String getPattern() {
73          return this.pattern;
74      }
75  
76      public String getExpected() {
77          StringBuilder buffer = new StringBuilder();
78          for (int i = 0; i < this.count; i++) {
79              buffer.append(this.pattern);
80          }
81          return buffer.toString();
82      }
83  
84      public int getStatusCode() {
85          return this.statusCode;
86      }
87  
88      public String getResult() {
89          return this.result;
90      }
91  
92      public boolean isSuccessful() {
93          return this.result != null;
94      }
95  
96      public String getFailureMessage() {
97          return this.failureMessage;
98      }
99  
100     public Exception getException() {
101         return this.ex;
102     }
103 
104     public boolean isCompleted() {
105         return this.completed;
106     }
107 
108     public synchronized void setResult(int statusCode, final String result) {
109         if (this.completed) {
110             return;
111         }
112         this.completed = true;
113         this.statusCode = statusCode;
114         this.result = result;
115         notifyAll();
116     }
117 
118     public synchronized void fail(final String message, final Exception ex) {
119         if (this.completed) {
120             return;
121         }
122         this.completed = true;
123         this.result = null;
124         this.failureMessage = message;
125         this.ex = ex;
126         notifyAll();
127     }
128 
129     public void fail(final String message) {
130         fail(message, null);
131     }
132 
133     public synchronized void waitFor() throws InterruptedException {
134         while (!this.completed) {
135             wait();
136         }
137     }
138 
139 }