Update of /cvsroot/metamorphosis/krysalis-sandbox/workflow/src/java/org/krysalis/workflow/manager
In directory sc8-pr-cvs1:/tmp/cvs-serv29172/src/java/org/krysalis/workflow/manager
Added Files:
DefaultWorkFlow.java DefaultWorkFlowManager.java
DefaultWorkFlowMapper.java workflow.roles
Log Message:
Finished concept of the workflow manager.
--- NEW FILE: DefaultWorkFlow.java ---
/*****************************************************************************
* Copyright (C) The Krysalis project. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Krysalis Patchy *
* Software License version 1.1_01, a copy of which has been included *
* at the bottom of this file. *
*****************************************************************************/
package org.krysalis.workflow.manager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.krysalis.workflow.*;
/**
* Implementation of a determinitic automate.
*/
public class DefaultWorkFlow extends AbstractLogEnabled implements
WorkFlow, Configurable/*, Disposable*/ {
/** Component manager */
private ComponentManager manager = null;
private String[] stateNames;
// 1.Index:state 2.Index:event 3.Index:Index of component
private int[][] transitions;
private String[][] transitionEvents;
private WorkFlowCondition[][] transitionConditions;
private Parameters[][] transitionConditionParameters;
private WorkFlowAction[][][] transitionActions;
private Parameters[][][] transitionActionParameters;
// Intial state
private int initalState;
/**
* Pass the <code>ComponentManager</code> to the <code>composer</code>.
* The <code>Composable</code> implementation should use the specified
* <code>ComponentManager</code> to acquire the components it needs for
* execution.
*
* @param componentManager The <code>ComponentManager</code> which this
* <code>Composable</code> uses.
* @throws ComponentException if an error occurs
*/
public void compose(ComponentManager componentManager)
throws ComponentException {
this.manager = componentManager;
}
/**
* Pass the <code>Configuration</code> to the <code>Configurable</code>
* class. This method must always be called after the constructor
* and before any other method.
*
* @param configuration the class configurations.
* @throws ConfigurationException If an error occurs.
*/
public void configure(Configuration configuration)
throws ConfigurationException {
Configuration[] states = configuration.getChildren("state");
this.stateNames = new String[states.length];
for(int i=0; i<states.length; i++)
this.stateNames[i] = states[i].getAttribute("id");
this.initalState = indexOfState(configuration.getAttribute("inital"));
this.transitions = new int[states.length][];
Configuration[] transitions, actions;
Configuration condition;
for(int state=0; state<states.length; state++) {
transitions = states[state].getChildren("transition");
this.transitions[state] = new int[transitions.length];
for(int transition=0; transition<transitions.length; transition++) {
this.transitions[state][transition] =
indexOfState(transitions[transition].getAttribute("ref", stateNames[state]));
if (transitions[transition].getAttribute("event")!=null)
this.transitionEvents[state][transition] =
transitions[transition].getAttribute("event");
else
throw new ConfigurationException("Transition of state '"+this.stateNames[state]+"' don't own an event");
condition = transitions[state].getChild("condition", false);
if (condition!=null) {
this.transitionConditions[state][transition] =
(WorkFlowCondition)this.manager.lookup(condition.getAttribute("name"));
this.transitionConditionParameters[state][transition] =
Parameters.fromConfiguration(condition);
} else {
this.transitionConditions[state][transition] = null;
this.transitionConditionParameters[state][transition] = null;
}
actions = transitions[state].getChildren("action");
for(int action=0; action<actions.length; action++) {
this.transitionActions[state][transition][action] =
(WorkFlowAction)this.manager.lookup(actions[action].getAttribute("name"));
this.transitionActionParameters[state][transition][action] =
Parameters.fromConfiguration(actions[action]);
}
}
}
}
private int indexOfState(String name) {
for(int i=0; i<stateNames.length; i++)
if(this.stateNames.equals(name))
return i;
}
private int indexOfTransition(int state, String event) {
for(int i=0; i<this.transitionEvents.length; i++)
if(this.transitionEvents[state].equals(event))
return i;
}
public WorkFlowState process(WorkFlowState currentState, WorkFlowEvent event) {
if (currentState==null) {
// Add the initial state to the state
currentState = new DefaultWorkFlowState(this.stateNames[this.initalState]);
}
int state = indexOfState(currentState.getState());
int transition;
if(event!=null) {
transition = indexOfTransition(state, event.getName());
if (transition<0)
// If doesn't exist a transition ignore the event
return null;
// Execute transition
if (transitionConditions[state][transition]!=null) {
if (transitionConditions[state][transition].evaluate(
currentState, event,
transitionConditionParameters[state][transition])) {
state = transitions[state][transition];
currentState.setState(stateNames[state]);
try {
// Executes all actions
for (int action=0; action<transitionActions[state][transition].length; action++)
transitionActions[state][transition][action].perform(currentState, event,
transitionActionParameters[state][transition][action]);
} catch (Exception e) {
getLogger().error("Could not perform actions", e);
}
}
} else {
state = transitions[state][transition];
currentState.setState(stateNames[state]);
try {
// Executes all actions
for (int action=0; action<transitionActions[state][transition].length; action++)
transitionActions[state][transition][action].perform(currentState, event,
transitionActionParameters[state][transition][action]);
} catch (Exception e) {
getLogger().error("Could not perform actions", e);
}
}
}
boolean changed;
do {
changed = false;
for(transition=0; transition<transitionConditions.length; transition++)
if ((transitionEvents[state][transition]==null) &&
transitionConditions[state][transition].evaluate(
currentState, null,
transitionConditionParameters[state][transition])) {
state = transitions[state][transition];
currentState.setState(stateNames[state]);
try {
// Executes all actions
for (int action=0; action<transitionActions[state][transition].length; action++)
transitionActions[state][transition][action].perform(currentState, null,
transitionActionParameters[state][transition][action]);
} catch (Exception e) {
getLogger().error("Could not perform actions", e);
}
break;
}
} while (changed);
}
}
/*
The Krysalis Patchy Software License, Version 1.1_01
Copyright (c) 2002 Nicola Ken Barozzi. All rights reserved.
This Licence is compatible with the BSD licence as described and
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:
"This product includes software developed for project
Krysalis (http://www.krysalis.org/)."
Alternately, this acknowledgment may appear in the software itself,
if and wherever such third-party acknowledgments normally appear.
4. The names "Krysalis" and "Nicola Ken Barozzi" and
"Krysalis Metamorphosis" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact [email protected].
5. Products derived from this software may not be called "Krysalis",
"Krysalis Metamorphosis", nor may "Krysalis" appear in their name,
without prior written permission of Nicola Ken Barozzi.
6. This software may contain voluntary contributions made by many
individuals, who decided to donate the code to this project in
respect of this licence.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================*/
--- NEW FILE: DefaultWorkFlowManager.java ---
/*****************************************************************************
* Copyright (C) The Krysalis project. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Krysalis Patchy *
* Software License version 1.1_01, a copy of which has been included *
* at the bottom of this file. *
*****************************************************************************/
package org.krysalis.workflow.manager;
import org.apache.avalon.excalibur.component.DefaultRoleManager;
import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
import org.apache.avalon.excalibur.component.RoleManageable;
import org.apache.avalon.excalibur.logger.LogKitManageable;
import org.apache.avalon.excalibur.logger.LogKitManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.xml.sax.SAXParser;
import org.krysalis.workflow.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class DefaultWorkFlowManager extends AbstractLogEnabled
implements WorkFlowManager, Configurable, LogKitManageable
{
private ComponentManager pcm;
private ComponentManager cm;
private LogKitManager lkm;
private String file;
/**
* Pass the <code>ComponentManager</code> to the <code>composer</code>.
* The <code>Composable</code> implementation should use the specified
* <code>ComponentManager</code> to acquire the components it needs for
* execution.
*
* @param componentManager The <code>ComponentManager</code> which this
* <code>Composable</code> uses.
* @throws ComponentException if an error occurs
*/
public void compose(ComponentManager componentManager)
throws ComponentException
{
this.pcm = componentManager;
}
/**
* Pass the <code>Configuration</code> to the <code>Configurable</code>
* class. This method must always be called after the constructor
* and before any other method.
*
* @param configuration the class configurations.
* @throws ConfigurationException If an error occurs.
*/
public void configure(Configuration configuration)
throws ConfigurationException
{
this.file = configuration.getAttribute("file", "WEB-INF/workflow.xconf");
}
/**
* Configure the LogKitManager
*/
public void setLogKitManager(LogKitManager logkit)
{
lkm = logkit;
}
/**
* Initialialize the component. Initialization includes
* allocating any resources required throughout the
* components lifecycle.
*/
public void initialize() throws Exception
{
SourceResolver resolver = null;
SAXParser parser = null;
Source source = null;
Configuration roleConfig = null;
Configuration configuration = null;
try
{
resolver = (SourceResolver) pcm.lookup(SourceResolver.ROLE);
parser = (SAXParser) pcm.lookup(SAXParser.ROLE);
SAXConfigurationHandler confighandler = new SAXConfigurationHandler();
source = resolver.resolveURI("resource://org/krysalis/workflow/workflow.roles");
parser.parse(new InputSource(source.getInputStream()),
confighandler);
roleConfig = confighandler.getConfiguration();
source = resolver.resolveURI(this.file);
parser.parse(new InputSource(source.getInputStream()),
confighandler);
configuration = confighandler.getConfiguration();
}
catch (Exception e)
{
getLogger().error("Could not load workflow configuration file", e);
return;
}
finally
{
if (source!=null)
resolver.release(source);
if (parser!=null)
pcm.release((Component) parser);
if (resolver!=null)
pcm.release(resolver);
}
DefaultRoleManager rm = new DefaultRoleManager();
rm.enableLogging(getLogger());
rm.configure(roleConfig);
cm = new ExcaliburComponentManager();
if (cm instanceof LogEnabled)
((LogEnabled)cm).enableLogging(getLogger());
if (cm instanceof RoleManageable)
((RoleManageable)cm).setRoleManager(rm);
if (cm instanceof LogKitManageable)
((LogKitManageable)cm).setLogKitManager(lkm);
if (cm instanceof Configurable)
((Configurable)cm).configure(configuration);
}
public void process(String resource, WorkFlowEvent event)
{
WorkFlowMapper mapper = null;
ComponentSelector workflowselector = null;
WorkFlow workflow = null;
WorkFlowState state = lookup(resource);
try
{
mapper = (WorkFlowMapper)cm.lookup(WorkFlowMapper.ROLE);
workflowselector = (ComponentSelector)cm.lookup(WorkFlow.ROLE+"Selector");
workflow = (WorkFlow) workflowselector.select(mapper.getWorkFlow(resource));
state = workflow.process(state, event);
}
catch (Exception e)
{
getLogger().error("Could not process workflow", e);
return;
}
finally
{
if (state!=null)
release(state);
if (workflow!=null)
workflowselector.release(workflow);
if (workflowselector!=null)
cm.release((Component) workflowselector);
if (mapper!=null)
cm.release(mapper);
}
}
public WorkFlowState lookup(String resource)
{
WorkFlowMapper mapper = null;
ComponentSelector storeselector = null;
WorkFlowStore store = null;
WorkFlowState state = null;
try
{
mapper = (WorkFlowMapper)cm.lookup(WorkFlowMapper.ROLE);
storeselector = (ComponentSelector)cm.lookup(WorkFlowStore.ROLE+"Selector");
store = (WorkFlowStore) storeselector.select(mapper.getWorkFlowStore(resource));
state = store.lookup(resource);
}
catch (Exception e)
{
getLogger().error("Could not retrieve workflow state", e);
return null;
}
finally
{
if (store!=null)
storeselector.release(store);
if (storeselector!=null)
cm.release((Component) storeselector);
if (mapper!=null)
cm.release(mapper);
}
return state;
}
public void release(WorkFlowState state)
{
WorkFlowMapper mapper = null;
ComponentSelector storeselector = null;
WorkFlowStore store = null;
try
{
mapper = (WorkFlowMapper)cm.lookup(WorkFlowMapper.ROLE);
storeselector = (ComponentSelector)cm.lookup(WorkFlowStore.ROLE+"Selector");
store = (WorkFlowStore) storeselector.select(mapper.getWorkFlowStore(state.getResource()));
store.release(state);
}
catch (Exception e)
{
getLogger().error("Could not retrieve workflow state", e);
return;
}
finally
{
if (store!=null)
storeselector.release(store);
if (storeselector!=null)
cm.release((Component) storeselector);
if (mapper!=null)
cm.release(mapper);
}
}
}
--- NEW FILE: DefaultWorkFlowMapper.java ---
/*****************************************************************************
* Copyright (C) The Krysalis project. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Krysalis Patchy *
* Software License version 1.1_01, a copy of which has been included *
* at the bottom of this file. *
*****************************************************************************/
package org.krysalis.workflow.manager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.krysalis.workflow.*;
public class DefaultWorkFlowMapper implements WorkFlowMapper
{
public String getWorkFlow(String resource)
{
return null;
}
public String getWorkFlowStore(String resource)
{
return null;
}
}
--- NEW FILE: workflow.roles ---
<?xml version="1.0"?>
<!DOCTYPE role-list [
<!ELEMENT role-list (role+)>
<!ELEMENT role (hint*)>
<!ELEMENT hint EMPTY>
<!ATTLIST role name CDATA #REQUIRED
shorthand CDATA #REQUIRED
default-class CDATA #IMPLIED
>
<!ATTLIST hint shorthand CDATA #REQUIRED
class CDATA #REQUIRED
>
]>
<role-list>
<role name="org.krysalis.workflow.WorkFlowStoreSelector"
shorthand="stores"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"/>
<role name="org.krysalis.workflow.WorkFlowStoreSelector"
shorthand="stores"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"/>
<role name="org.krysalis.workflow.WorkFlowStoreSelector"
shorthand="stores"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"/>
<role name="org.krysalis.workflow.WorkFlowStoreSelector"
shorthand="stores"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"/>
<role name="org.krysalis.workflow.WorkFlowConditionSelector"
shorthand="conditions"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"/>
<role name="org.krysalis.workflow.WorkFlowActionSelector"
shorthand="actions"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"/>
<role name="org.krysalis.workflow.WorkFlowStoreSelector"
shorthand="stores"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector">
<hint shorthand="store" class="org.krysalis.workflow.store.MemoryStore"/>
</role>
<role name="org.krysalis.workflow.WorkFlowSelector"
shorthand="workflows"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector">
<hint shorthand="workflow" class="org.krysalis.workflow.impl.SimpleWorkFlow"/>
</role>
<role name="org.krysalis.workflow.WorkFlowMapper"
shorthand="map"
default-class="org.krysalis.workflow.impl.WorkFlowMapper"/>
</role-list>
-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open!
Get cracking and register here for some mind boggling fun and
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.