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
20 package org.apache.jsieve;
21
22 /**
23 * Thread singleton class CommandStateManager records the state of a Sieve
24 * evaluation.
25 */
26 public class CommandStateManager {
27
28 /**
29 * The evaluated script is processing Prolog Commands
30 */
31 private boolean fieldInProlog = true;
32
33 /**
34 * The evaluated script has rejected the mail
35 */
36 private boolean fieldRejected = false;
37
38 /**
39 * The evaluated script must keep the mail
40 */
41 private boolean fieldImplicitKeep = true;
42
43 /**
44 * The evaluation has processed Action Commands
45 */
46 private boolean fieldHasActions = false;
47
48 /**
49 * The instance of the reciever for the current thread
50 */
51 static private final ThreadLocal fieldInstance = new ThreadLocal();
52
53 /**
54 * Constructor for CommandStateManager.
55 */
56 private CommandStateManager() {
57 super();
58 initialize();
59 }
60
61 /**
62 * Initialize the receiver.
63 */
64 protected void initialize() {
65 setInProlog(true);
66 setRejected(false);
67 setHasActions(false);
68 setImplicitKeep(true);
69 }
70
71 /**
72 * Answers a new instance of the receiver.
73 *
74 * @return ConditionManager
75 */
76 static protected CommandStateManager computeInstance() {
77 return new CommandStateManager();
78 }
79
80 /**
81 * <p>
82 * Returns an instance of the receiver for the current thread, lazily
83 * intialised if required.
84 * </p>
85 *
86 * <p>
87 * Note that this must be synchronized to prevent another thread detecting
88 * the null state while this thread is initialising.
89 * </p>
90 *
91 * @return ConditionManager
92 */
93 static synchronized public CommandStateManager getInstance() {
94 CommandStateManager instance = null;
95 if (null == (instance = getInstanceBasic())) {
96 updateInstance();
97 return getInstance();
98 }
99 return instance;
100 }
101
102 /**
103 * Returns the current CommandStateManager for the current thread.
104 *
105 * @return CommandStateManager
106 */
107 static private CommandStateManager getInstanceBasic() {
108 return (CommandStateManager) fieldInstance.get();
109 }
110
111 /**
112 * Sets the CommandStateManager for the current thread.
113 *
114 * @param conditionManager
115 * The CommandStateManager to set
116 */
117 static protected void setInstance(CommandStateManager conditionManager) {
118 fieldInstance.set(conditionManager);
119 }
120
121 /**
122 * resets the current CommandStateManager.
123 */
124 static public void resetInstance() {
125 setInstance(null);
126 }
127
128 /**
129 * Updates the current CommandStateManager.
130 */
131 static protected void updateInstance() {
132 setInstance(computeInstance());
133 }
134
135 /**
136 * Returns the hasActions.
137 *
138 * @return boolean
139 */
140 public boolean isHasActions() {
141 return fieldHasActions;
142 }
143
144 /**
145 * Returns the inProlog.
146 *
147 * @return boolean
148 */
149 public boolean isInProlog() {
150 return fieldInProlog;
151 }
152
153 /**
154 * Returns the isRejected.
155 *
156 * @return boolean
157 */
158 public boolean isRejected() {
159 return fieldRejected;
160 }
161
162 /**
163 * Sets the hasActions.
164 *
165 * @param hasActions
166 * The hasActions to set
167 */
168 public void setHasActions(boolean hasActions) {
169 fieldHasActions = hasActions;
170 }
171
172 /**
173 * Sets the inProlog.
174 *
175 * @param inProlog
176 * The inProlog to set
177 */
178 public void setInProlog(boolean inProlog) {
179 fieldInProlog = inProlog;
180 }
181
182 /**
183 * Sets the isRejected.
184 *
185 * @param isRejected
186 * The isRejected to set
187 */
188 public void setRejected(boolean isRejected) {
189 fieldRejected = isRejected;
190 }
191
192 /**
193 * Returns the implicitKeep.
194 *
195 * @return boolean
196 */
197 public boolean isImplicitKeep() {
198 return fieldImplicitKeep;
199 }
200
201 /**
202 * Sets the implicitKeep.
203 *
204 * @param implicitKeep
205 * The implicitKeep to set
206 */
207 public void setImplicitKeep(boolean implicitKeep) {
208 fieldImplicitKeep = implicitKeep;
209 }
210
211 }