View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.inform;
17  
18  import org.kuali.common.util.Assert;
19  import org.kuali.common.util.log.LoggerUtils;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  /**
24   * 
25   */
26  public final class StartStopInformer {
27  
28  	private static final Logger logger = LoggerFactory.getLogger(StartStopInformer.class);
29  
30  	private final Inform inform;
31  
32  	public StartStopInformer() {
33  		this(Inform.DEFAULT_INFORM);
34  	}
35  
36  	public StartStopInformer(Inform inform) {
37  		Assert.noNulls(inform);
38  		this.inform = inform;
39  	}
40  
41  	private boolean started = false;
42  
43  	/**
44  	 * Indicates if we are in the "started" state
45  	 */
46  	public boolean isStarted() {
47  		return started;
48  	}
49  
50  	/**
51  	 * Make sure we haven't already been started. Set the started indicator to true. Log the start message. Print the start token.
52  	 */
53  	public synchronized void start() {
54  		Assert.isFalse(started, "Already started");
55  		this.started = true;
56  		LoggerUtils.log(inform.getStartMessage(), logger);
57  		inform.getPrintStream().print(inform.getStartToken());
58  	}
59  
60  	/**
61  	 * Make sure we haven't already been stopped. Set the started indicator to false. Print the stop token. Log the stop message.
62  	 */
63  	public synchronized void stop() {
64  		Assert.isTrue(started, "Not started");
65  		this.started = false;
66  		inform.getPrintStream().print(inform.getCompleteToken());
67  		LoggerUtils.log(inform.getStopMessage(), logger);
68  	}
69  
70  	public Inform getInform() {
71  		return inform;
72  	}
73  
74  }