001    /**
002     * Copyright 2010-2013 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     */
016    package org.kuali.common.util;
017    
018    import java.io.PrintStream;
019    
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    
023    /**
024     * Print a dot to the console each time we make progress
025     */
026    public abstract class AbstractProgressInformer {
027    
028            private static final Logger logger = LoggerFactory.getLogger(AbstractProgressInformer.class);
029    
030            protected long progress;
031    
032            PrintStream printStream = System.out;
033            String startToken = "[INFO] Progress: ";
034            String progressToken = ".";
035            String completeToken = "\n";
036            LogMsg startMessage;
037            LogMsg stopMessage;
038    
039            /**
040             * Thread safe method exposing the current progress
041             */
042            public synchronized long getProgress() {
043                    return progress;
044            }
045    
046            /**
047             * Print the start token
048             */
049            public void start() {
050                    if (startMessage != null) {
051                            LoggerUtils.log(startMessage, logger);
052                    }
053    
054                    Assert.notNull(printStream, "printStream is null");
055                    this.progress = 0;
056    
057                    printStream.print(startToken);
058            }
059    
060            /**
061             * Print the stop token
062             */
063            public void stop() {
064                    printStream.print(completeToken);
065    
066                    if (stopMessage != null) {
067                            LoggerUtils.log(stopMessage, logger);
068                    }
069            }
070    
071            public PrintStream getPrintStream() {
072                    return printStream;
073            }
074    
075            public void setPrintStream(PrintStream printStream) {
076                    this.printStream = printStream;
077            }
078    
079            public String getStartToken() {
080                    return startToken;
081            }
082    
083            public void setStartToken(String startToken) {
084                    this.startToken = startToken;
085            }
086    
087            public String getCompleteToken() {
088                    return completeToken;
089            }
090    
091            public void setCompleteToken(String completeToken) {
092                    this.completeToken = completeToken;
093            }
094    
095            public String getProgressToken() {
096                    return progressToken;
097            }
098    
099            public void setProgressToken(String progressToken) {
100                    this.progressToken = progressToken;
101            }
102    
103            public LogMsg getStartMessage() {
104                    return startMessage;
105            }
106    
107            public void setStartMessage(LogMsg startMessage) {
108                    this.startMessage = startMessage;
109            }
110    
111            public LogMsg getStopMessage() {
112                    return stopMessage;
113            }
114    
115            public void setStopMessage(LogMsg stopMessage) {
116                    this.stopMessage = stopMessage;
117            }
118    
119    }