View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.policy;
20  
21  import org.apache.neethi.Assertion;
22  
23  public class AssertionState {
24  
25      public enum State {
26          INIT,
27          HARD_FAILURE,
28      }
29  
30      private State state = State.INIT;
31      private boolean asserted;
32      private boolean logged;
33      private Assertion assertion;
34      private StringBuilder errorMessage = new StringBuilder();   //NOPMD
35  
36      public AssertionState(Assertion assertion, boolean initialAssertionState) {
37          this.assertion = assertion;
38          this.asserted = initialAssertionState;
39      }
40  
41      public Assertion getAssertion() {
42          return assertion;
43      }
44  
45      public boolean isHardFailure() {
46          return this.state == State.HARD_FAILURE;
47      }
48  
49      public synchronized void setAsserted(boolean asserted) {
50          //don't allow to toggle back once the assertion is explicitly marked as failed;
51          if (this.state == State.HARD_FAILURE) {
52              return;
53          }
54          if (!asserted) {
55              this.state = State.HARD_FAILURE;
56          }
57          this.asserted = asserted;
58      }
59  
60      public boolean isAsserted() {
61          return asserted;
62      }
63  
64      public void setErrorMessage(String errorMessage) {
65          if (this.errorMessage.length() > 0) {
66              this.errorMessage.append("\n");
67          }
68          this.errorMessage.append(errorMessage);
69      }
70  
71      public String getErrorMessage() {
72          if (errorMessage.length() == 0) {
73              return "Assertion " + assertion.getName() + " not satisfied";
74          } else {
75              return errorMessage.toString();
76          }
77      }
78  
79      protected void clearErrorMessage() {
80          this.errorMessage.delete(0, this.errorMessage.length());
81      }
82  
83      public boolean isLogged() {
84          return logged;
85      }
86  
87      public void setLogged(boolean logged) {
88          this.logged = logged;
89      }
90  }