📄 abstractworkflow.java
字号:
/* * Copyright (c) 2002-2003 by OpenSymphony * All rights reserved. */package com.opensymphony.workflow;import com.opensymphony.module.propertyset.PropertySet;import com.opensymphony.module.propertyset.PropertySetManager;import com.opensymphony.workflow.config.Configuration;import com.opensymphony.workflow.config.DefaultConfiguration;import com.opensymphony.workflow.loader.*;import com.opensymphony.workflow.query.WorkflowExpressionQuery;import com.opensymphony.workflow.query.WorkflowQuery;import com.opensymphony.workflow.spi.*;import com.opensymphony.workflow.util.VariableResolver;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.util.*;/** * Abstract workflow instance that serves as the base for specific implementations, such as EJB or SOAP. * * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a> * @author Hani Suleiman */public class AbstractWorkflow implements Workflow { //~ Static fields/initializers ///////////////////////////////////////////// private static final Log log = LogFactory.getLog(AbstractWorkflow.class); //~ Instance fields //////////////////////////////////////////////////////// protected WorkflowContext context; private Configuration configuration; private ThreadLocal stateCache = new ThreadLocal(); private TypeResolver typeResolver; //~ Constructors /////////////////////////////////////////////////////////// public AbstractWorkflow() { stateCache.set(new HashMap()); } //~ Methods //////////////////////////////////////////////////////////////// /** * @ejb.interface-method * @deprecated use {@link #getAvailableActions(long, Map)} with an empty Map instead. */ public int[] getAvailableActions(long id) { return getAvailableActions(id, new HashMap()); } /** * Get the available actions for the specified workflow instance. * @ejb.interface-method * @param id The workflow instance id. * @param inputs The inputs map to pass on to conditions * @return An array of action id's that can be performed on the specified entry. * @throws IllegalArgumentException if the specified id does not exist, or if its workflow * descriptor is no longer available or has become invalid. */ public int[] getAvailableActions(long id, Map inputs) { try { WorkflowStore store = getPersistence(); WorkflowEntry entry = store.findEntry(id); if (entry == null) { throw new IllegalArgumentException("No such workflow id " + id); } if (entry.getState() != WorkflowEntry.ACTIVATED) { return new int[0]; } WorkflowDescriptor wf = getConfiguration().getWorkflow(entry.getWorkflowName()); if (wf == null) { throw new IllegalArgumentException("No such workflow " + entry.getWorkflowName()); } List l = new ArrayList(); PropertySet ps = store.getPropertySet(id); Map transientVars = (inputs == null) ? new HashMap() : new HashMap(inputs); Collection currentSteps = store.findCurrentSteps(id); populateTransientMap(entry, transientVars, wf.getRegisters(), new Integer(0), currentSteps, ps); // get global actions List globalActions = wf.getGlobalActions(); for (Iterator iterator = globalActions.iterator(); iterator.hasNext();) { ActionDescriptor action = (ActionDescriptor) iterator.next(); RestrictionDescriptor restriction = action.getRestriction(); ConditionsDescriptor conditions = null; transientVars.put("actionId", new Integer(action.getId())); if (restriction != null) { conditions = restriction.getConditionsDescriptor(); } //todo verify that 0 is the right currentStepId if (passesConditions(wf.getGlobalConditions(), transientVars, ps, 0) && passesConditions(conditions, transientVars, ps, 0)) { l.add(new Integer(action.getId())); } } // get normal actions for (Iterator iterator = currentSteps.iterator(); iterator.hasNext();) { Step step = (Step) iterator.next(); l.addAll(getAvailableActionsForStep(wf, step, transientVars, ps)); } int[] actions = new int[l.size()]; for (int i = 0; i < actions.length; i++) { actions[i] = ((Integer) l.get(i)).intValue(); } return actions; } catch (Exception e) { log.error("Error checking available actions", e); return new int[0]; } } /** * @ejb.interface-method */ public void setConfiguration(Configuration configuration) { this.configuration = configuration; } /** * Get the configuration for this workflow. * This method also checks if the configuration has been initialized, and if not, initializes it. * @return The configuration that was set. * If no configuration was set, then the default (static) configuration is returned. * */ public Configuration getConfiguration() { Configuration config = (configuration != null) ? configuration : DefaultConfiguration.INSTANCE; if (!config.isInitialized()) { try { config.load(null); } catch (FactoryException e) { log.fatal("Error initialising configuration", e); //fail fast, better to blow up with an NPE that hide the error return null; } } return config; } /** * @ejb.interface-method */ public List getCurrentSteps(long id) { try { WorkflowStore store = getPersistence(); return store.findCurrentSteps(id); } catch (StoreException e) { log.error("Error checking current steps for instance #" + id, e); return Collections.EMPTY_LIST; } } /** * @ejb.interface-method */ public int getEntryState(long id) { try { WorkflowStore store = getPersistence(); return store.findEntry(id).getState(); } catch (StoreException e) { log.error("Error checking instance state for instance #" + id, e); } return WorkflowEntry.UNKNOWN; } /** * @ejb.interface-method */ public List getHistorySteps(long id) { try { WorkflowStore store = getPersistence(); return store.findHistorySteps(id); } catch (StoreException e) { log.error("Error getting history steps for instance #" + id, e); } return Collections.EMPTY_LIST; } /** * @ejb.interface-method */ public Properties getPersistenceProperties() { Properties p = new Properties(); Iterator iter = getConfiguration().getPersistenceArgs().entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); p.setProperty((String) entry.getKey(), (String) entry.getValue()); } return p; } /** * Get the PropertySet for the specified workflow ID * @ejb.interface-method * @param id The workflow ID */ public PropertySet getPropertySet(long id) { PropertySet ps = null; try { ps = getPersistence().getPropertySet(id); } catch (StoreException e) { log.error("Error getting propertyset for instance #" + id, e); } return ps; } public void setResolver(TypeResolver resolver) { this.typeResolver = resolver; } public TypeResolver getResolver() { if (typeResolver == null) { typeResolver = TypeResolver.getResolver(); } return typeResolver; } /** * @ejb.interface-method */ public List getSecurityPermissions(long id) { return getSecurityPermissions(id, null); } /** * @ejb.interface-method */ public List getSecurityPermissions(long id, Map inputs) { try { WorkflowStore store = getPersistence(); WorkflowEntry entry = store.findEntry(id); WorkflowDescriptor wf = getConfiguration().getWorkflow(entry.getWorkflowName()); PropertySet ps = store.getPropertySet(id); Map transientVars = (inputs == null) ? new HashMap() : new HashMap(inputs); Collection currentSteps = store.findCurrentSteps(id); populateTransientMap(entry, transientVars, wf.getRegisters(), null, currentSteps, ps); List s = new ArrayList(); for (Iterator interator = currentSteps.iterator(); interator.hasNext();) { Step step = (Step) interator.next(); int stepId = step.getStepId(); StepDescriptor xmlStep = wf.getStep(stepId); List securities = xmlStep.getPermissions(); for (Iterator iterator2 = securities.iterator(); iterator2.hasNext();) { PermissionDescriptor security = (PermissionDescriptor) iterator2.next(); // to have the permission, the condition must be met or not specified // securities can't have restrictions based on inputs, so it's null if (security.getRestriction() != null) { if (passesConditions(security.getRestriction().getConditionsDescriptor(), transientVars, ps, xmlStep.getId())) { s.add(security.getName()); } } } } return s; } catch (Exception e) { log.error("Error getting security permissions for instance #" + id, e); } return Collections.EMPTY_LIST; } /** * Returns a workflow definition object associated with the given name. * * @param workflowName the name of the workflow * @return the object graph that represents a workflow definition * @ejb.interface-method */ public WorkflowDescriptor getWorkflowDescriptor(String workflowName) { try { return getConfiguration().getWorkflow(workflowName); } catch (FactoryException e) { log.error("Error loading workflow " + workflowName, e); } return null; } /** * @ejb.interface-method */ public String getWorkflowName(long id) { try { WorkflowStore store = getPersistence(); WorkflowEntry entry = store.findEntry(id); if (entry != null) { return entry.getWorkflowName(); } } catch (StoreException e) { log.error("Error getting instance name for instance #" + id, e); } return null; } /** * Get a list of workflow names available * @return String[] an array of workflow names. * @ejb.interface-method */ public String[] getWorkflowNames() { try { return getConfiguration().getWorkflowNames(); } catch (FactoryException e) { log.error("Error getting workflow names", e); } return new String[0]; } /** * @ejb.interface-method */ public boolean canInitialize(String workflowName, int initialAction) { return canInitialize(workflowName, initialAction, null); } /** * @ejb.interface-method * @param workflowName the name of the workflow to check * @param initialAction The initial action to check * @param inputs the inputs map * @return true if the workflow can be initialized */ public boolean canInitialize(String workflowName, int initialAction, Map inputs) { final String mockWorkflowName = workflowName; WorkflowEntry mockEntry = new WorkflowEntry() { public long getId() { return 0; } public String getWorkflowName() { return mockWorkflowName; } public boolean isInitialized() { return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -