001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.service;
016    
017    import org.apache.oozie.WorkflowActionBean;
018    import org.apache.oozie.store.StoreException;
019    import org.apache.oozie.store.WorkflowStore;
020    import org.apache.oozie.workflow.WorkflowInstance;
021    import org.apache.oozie.workflow.WorkflowLib;
022    import org.apache.oozie.service.Service;
023    import org.apache.oozie.store.Store;
024    
025    import java.util.Collections;
026    import java.util.List;
027    
028    /**
029     * Base service for persistency of jobs and actions.
030     */
031    public abstract class WorkflowStoreService implements Service {
032    
033        public final static String TRANSIENT_VAR_PREFIX = "oozie.workflow.";
034        public static final String WORKFLOW_BEAN = TRANSIENT_VAR_PREFIX + "workflow.bean";
035        final static String ACTION_ID = "action.id";
036        final static String ACTIONS_TO_KILL = TRANSIENT_VAR_PREFIX + "actions.to.kill";
037        final static String ACTIONS_TO_FAIL = TRANSIENT_VAR_PREFIX + "actions.to.fail";
038        final static String ACTIONS_TO_START = TRANSIENT_VAR_PREFIX + "actions.to.start";
039    
040        /**
041         * Return the public interface of the service.
042         *
043         * @return {@link WorkflowStoreService}.
044         */
045        public Class<? extends Service> getInterface() {
046            return WorkflowStoreService.class;
047        }
048    
049        /**
050         * Return a workkflow lib, giving access to the parser functionality.
051         *
052         * @return a workflow lib to use the parser.
053         */
054        public abstract WorkflowLib getWorkflowLibWithNoDB();
055    
056        /**
057         * Return a workflow store instance with a fresh transaction. <p/> The workflow store has to be committed and then
058         * closed to commit changes, if only close it rolls back.
059         *
060         * @return a workflow store.
061         * @throws StoreException thrown if the workflow store could not be created.
062         */
063        public abstract WorkflowStore create() throws StoreException;
064    
065        /**
066         * Return a workflow store instance with an existing transaction. <p/> The workflow store has to be committed and
067         * then closed to commit changes, if only close it rolls back.
068         *
069         * @return a workflow store.
070         * @throws StoreException thrown if the workflow store could not be created.
071         */
072        //to do this method can be abstract or should be overridden 
073        public <S extends Store> WorkflowStore create(S store) throws StoreException {
074            return null;
075        }
076    
077        /**
078         * Return the list of actions started by a signal in an instance.
079         *
080         * @param instance workflow instance that has been signaled.
081         * @return the list of actions started by the signaling.
082         */
083        @SuppressWarnings("unchecked")
084        public static List<WorkflowActionBean> getStartedActions(WorkflowInstance instance) {
085            List<WorkflowActionBean> list = (List<WorkflowActionBean>) instance.getTransientVar(ACTIONS_TO_START);
086            instance.setTransientVar(ACTIONS_TO_START, null);
087            return (list != null) ? list : Collections.EMPTY_LIST;
088        }
089    
090        /**
091         * Return the list of action IDs to kill.
092         *
093         * @param instance workflow instance
094         * @return the list of action IDs to kill.
095         */
096        @SuppressWarnings("unchecked")
097        public static List<String> getActionsToKill(WorkflowInstance instance) {
098            List<String> list = (List<String>) instance.getTransientVar(ACTIONS_TO_KILL);
099            instance.setTransientVar(ACTIONS_TO_KILL, null);
100            return (list != null) ? list : Collections.EMPTY_LIST;
101        }
102    
103        /**
104         * Return the list of action IDs to fail.
105         *
106         * @param instance workflow instance
107         * @return the list of action IDs to fail.
108         */
109        @SuppressWarnings("unchecked")
110        public static List<String> getActionsToFail(WorkflowInstance instance) {
111            List<String> list = (List<String>) instance.getTransientVar(ACTIONS_TO_FAIL);
112            instance.setTransientVar(ACTIONS_TO_FAIL, null);
113            return (list != null) ? list : Collections.EMPTY_LIST;
114        }
115    }