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.runonce;
017
018import org.kuali.common.util.Assert;
019import org.kuali.common.util.execute.Executable;
020
021/**
022 * @deprecated Use org.kuali.common.util.runonce.smart.RunOnceExecutable instead
023 */
024@Deprecated
025public class RunOnceExecutable implements Executable {
026
027        public RunOnceExecutable(Executable executable, RunOnceStateManager stateManager, boolean skip) {
028                Assert.noNulls(executable, stateManager);
029                this.executable = executable;
030                this.stateManager = stateManager;
031                this.skip = skip;
032        }
033
034        private final Executable executable;
035        private final RunOnceStateManager stateManager;
036        private final boolean skip;
037
038        @Override
039        public void execute() {
040
041                // Skip has been explicitly configured
042                if (skip) {
043                        return;
044                }
045
046                // Give the state manager a chance to initialize itself
047                stateManager.initialize();
048
049                // Let the state manager tell us if we are in RunOnce mode
050                if (!stateManager.isRunOnce()) {
051                        return;
052                }
053
054                // Transition to INPROGRESS
055                stateManager.persistState(RunOnceState.INPROGRESS);
056
057                try {
058                        // Now that the state manager has transitioned things to INPROGRESS it is safe to fire the executable
059                        // The transition to INPROGRESS is what prevents us from executing the executable more than once
060                        executable.execute();
061
062                        // Transition to COMPLETED
063                        stateManager.persistState(RunOnceState.COMPLETED);
064                } catch (Exception e) {
065                        // Transition to FAILED
066                        stateManager.persistState(RunOnceState.FAILED);
067                        throw new IllegalStateException("Unexpected execution error", e);
068                }
069        }
070
071        public Executable getExecutable() {
072                return executable;
073        }
074
075        public RunOnceStateManager getStateManager() {
076                return stateManager;
077        }
078
079        public boolean isSkip() {
080                return skip;
081        }
082}