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