001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.inform;
017
018import org.kuali.common.util.Assert;
019import org.kuali.common.util.log.LoggerUtils;
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023/**
024 * 
025 */
026public final class StartStopInformer {
027
028        private static final Logger logger = LoggerFactory.getLogger(StartStopInformer.class);
029
030        private final Inform inform;
031
032        public StartStopInformer() {
033                this(Inform.DEFAULT_INFORM);
034        }
035
036        public StartStopInformer(Inform inform) {
037                Assert.noNulls(inform);
038                this.inform = inform;
039        }
040
041        private boolean started = false;
042
043        /**
044         * Indicates if we are in the "started" state
045         */
046        public boolean isStarted() {
047                return started;
048        }
049
050        /**
051         * Make sure we haven't already been started. Set the started indicator to true. Log the start message. Print the start token.
052         */
053        public synchronized void start() {
054                Assert.isFalse(started, "Already started");
055                this.started = true;
056                LoggerUtils.log(inform.getStartMessage(), logger);
057                inform.getPrintStream().print(inform.getStartToken());
058        }
059
060        /**
061         * Make sure we haven't already been stopped. Set the started indicator to false. Print the stop token. Log the stop message.
062         */
063        public synchronized void stop() {
064                Assert.isTrue(started, "Not started");
065                this.started = false;
066                inform.getPrintStream().print(inform.getCompleteToken());
067                LoggerUtils.log(inform.getStopMessage(), logger);
068        }
069
070        public Inform getInform() {
071                return inform;
072        }
073
074}