001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.util;
006:
007: import com.opensymphony.workflow.*;
008:
009: import org.springframework.beans.BeansException;
010:
011: import org.springframework.context.ApplicationContext;
012: import org.springframework.context.ApplicationContextAware;
013:
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: /**
018: * Spring-aware type resolver.
019: */
020: public class SpringTypeResolver extends TypeResolver implements
021: ApplicationContextAware {
022: //~ Static fields/initializers /////////////////////////////////////////////
023:
024: private static final String BEANNAME = "bean.name";
025: private static final String SPRING = "spring";
026:
027: //~ Instance fields ////////////////////////////////////////////////////////
028:
029: private ApplicationContext applicationContext;
030: private Map springFunctions = new HashMap();
031:
032: //~ Methods ////////////////////////////////////////////////////////////////
033:
034: public void setApplicationContext(
035: ApplicationContext applicationContext)
036: throws BeansException {
037: this .applicationContext = applicationContext;
038: }
039:
040: public Condition getCondition(String type, Map args)
041: throws WorkflowException {
042: if (SPRING.equals(type)) {
043: return (Condition) getApplicationContext().getBean(
044: (String) args.get(BEANNAME));
045: }
046:
047: return super .getCondition(type, args);
048: }
049:
050: public void setConditions(Map conditions) {
051: this .conditions = conditions;
052: }
053:
054: public FunctionProvider getFunction(String type, Map args)
055: throws WorkflowException {
056: if (SPRING.equals(type)) {
057: return (FunctionProvider) getApplicationContext().getBean(
058: (String) args.get(BEANNAME));
059: }
060:
061: String className = (String) springFunctions.get(type);
062:
063: if (className == null) {
064: className = (String) args.get(Workflow.CLASS_NAME);
065: }
066:
067: if (className != null) {
068: return (FunctionProvider) loadObject(className);
069: }
070:
071: return super .getFunction(type, args);
072: }
073:
074: public void setFunctions(Map functions) {
075: this .springFunctions = functions;
076: }
077:
078: public Register getRegister(String type, Map args)
079: throws WorkflowException {
080: if (SPRING.equals(type)) {
081: return (Register) getApplicationContext().getBean(
082: (String) args.get(BEANNAME));
083: }
084:
085: return super .getRegister(type, args);
086: }
087:
088: public void setRegisters(Map registers) {
089: this .registers = registers;
090: }
091:
092: public Validator getValidator(String type, Map args)
093: throws WorkflowException {
094: if (SPRING.equals(type)) {
095: return (Validator) getApplicationContext().getBean(
096: (String) args.get(BEANNAME));
097: }
098:
099: return super .getValidator(type, args);
100: }
101:
102: public void setValidators(Map validators) {
103: this .validators = validators;
104: }
105:
106: private ApplicationContext getApplicationContext() {
107: return applicationContext;
108: }
109: }
|