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
019package org.apache.oozie.service;
020
021import org.apache.oozie.WorkflowActionBean;
022import org.apache.oozie.store.StoreException;
023import org.apache.oozie.store.WorkflowStore;
024import org.apache.oozie.workflow.WorkflowInstance;
025import org.apache.oozie.workflow.WorkflowLib;
026import org.apache.oozie.service.Service;
027import org.apache.oozie.store.Store;
028
029import java.util.Collections;
030import java.util.List;
031
032/**
033 * Base service for persistency of jobs and actions.
034 */
035public abstract class WorkflowStoreService implements Service {
036
037    public final static String TRANSIENT_VAR_PREFIX = "oozie.workflow.";
038    public static final String WORKFLOW_BEAN = TRANSIENT_VAR_PREFIX + "workflow.bean";
039    final static String ACTION_ID = "action.id";
040    final static String ACTIONS_TO_KILL = TRANSIENT_VAR_PREFIX + "actions.to.kill";
041    final static String ACTIONS_TO_FAIL = TRANSIENT_VAR_PREFIX + "actions.to.fail";
042    final static String ACTIONS_TO_START = TRANSIENT_VAR_PREFIX + "actions.to.start";
043
044    /**
045     * Return the public interface of the service.
046     *
047     * @return {@link WorkflowStoreService}.
048     */
049    public Class<? extends Service> getInterface() {
050        return WorkflowStoreService.class;
051    }
052
053    /**
054     * Return a workkflow lib, giving access to the parser functionality.
055     *
056     * @return a workflow lib to use the parser.
057     */
058    public abstract WorkflowLib getWorkflowLibWithNoDB();
059
060    /**
061     * Return a workflow store instance with a fresh transaction. <p> The workflow store has to be committed and then
062     * closed to commit changes, if only close it rolls back.
063     *
064     * @return a workflow store.
065     * @throws StoreException thrown if the workflow store could not be created.
066     */
067    public abstract WorkflowStore create() throws StoreException;
068
069    /**
070     * Return a workflow store instance with an existing transaction. <p> The workflow store has to be committed and
071     * then closed to commit changes, if only close it rolls back.
072     *
073     * @return a workflow store.
074     * @throws StoreException thrown if the workflow store could not be created.
075     */
076    //to do this method can be abstract or should be overridden
077    public <S extends Store> WorkflowStore create(S store) throws StoreException {
078        return null;
079    }
080
081    /**
082     * Return the list of actions started by a signal in an instance.
083     *
084     * @param instance workflow instance that has been signaled.
085     * @return the list of actions started by the signaling.
086     */
087    @SuppressWarnings("unchecked")
088    public static List<WorkflowActionBean> getActionsToStart(WorkflowInstance instance) {
089        List<WorkflowActionBean> list = (List<WorkflowActionBean>) instance.getTransientVar(ACTIONS_TO_START);
090        instance.setTransientVar(ACTIONS_TO_START, null);
091        return (list != null) ? list : Collections.EMPTY_LIST;
092    }
093
094    /**
095     * Return the list of action IDs to kill.
096     *
097     * @param instance workflow instance
098     * @return the list of action IDs to kill.
099     */
100    @SuppressWarnings("unchecked")
101    public static List<String> getActionsToKill(WorkflowInstance instance) {
102        List<String> list = (List<String>) instance.getTransientVar(ACTIONS_TO_KILL);
103        instance.setTransientVar(ACTIONS_TO_KILL, null);
104        return (list != null) ? list : Collections.EMPTY_LIST;
105    }
106
107    /**
108     * Return the list of action IDs to fail.
109     *
110     * @param instance workflow instance
111     * @return the list of action IDs to fail.
112     */
113    @SuppressWarnings("unchecked")
114    public static List<String> getActionsToFail(WorkflowInstance instance) {
115        List<String> list = (List<String>) instance.getTransientVar(ACTIONS_TO_FAIL);
116        instance.setTransientVar(ACTIONS_TO_FAIL, null);
117        return (list != null) ? list : Collections.EMPTY_LIST;
118    }
119}