001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.workflow.support;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import com.opensymphony.workflow.Condition;
024import com.opensymphony.workflow.FunctionProvider;
025import com.opensymphony.workflow.Register;
026import com.opensymphony.workflow.TypeResolver;
027import com.opensymphony.workflow.Validator;
028import com.opensymphony.workflow.WorkflowException;
029
030/**
031 * Avalon-aware type resolver.
032 * <p>
033 * Used for retreiving avalon component using the {@link ServiceManager} and a
034 * role.
035 */
036public class AvalonTypeResolver extends TypeResolver
037{
038    private static final String __AVALON_TYPE = "avalon";
039    private static final String __ROLE = "role";
040    private ServiceManager _serviceManager;
041
042    /**
043     * Create an avalon type resolver.
044     * 
045     * @param serviceManager the service manager.
046     */
047    public AvalonTypeResolver(ServiceManager serviceManager)
048    {
049        _serviceManager = serviceManager;
050    }
051
052    @Override
053    public Condition getCondition(String type, Map args) throws WorkflowException
054    {
055        if (__AVALON_TYPE.equals(type))
056        {
057            return (Condition) _getComponent((String) args.get(__ROLE));
058        }
059
060        return super.getCondition(type, args);
061    }
062
063    @Override
064    public FunctionProvider getFunction(String type, Map args) throws WorkflowException
065    {
066        if (__AVALON_TYPE.equals(type))
067        {
068            return (FunctionProvider) _getComponent((String) args.get(__ROLE));
069        }
070
071        return super.getFunction(type, args);
072    }
073
074    @Override
075    public Register getRegister(String type, Map args) throws WorkflowException
076    {
077        if (__AVALON_TYPE.equals(type))
078        {
079            return (Register) _getComponent((String) args.get(__ROLE));
080        }
081
082        return super.getRegister(type, args);
083    }
084
085    @Override
086    public Validator getValidator(String type, Map args) throws WorkflowException
087    {
088        if (__AVALON_TYPE.equals(type))
089        {
090            return (Validator) _getComponent((String) args.get(__ROLE));
091        }
092
093        return super.getValidator(type, args);
094    }
095
096    private Object _getComponent(String role) throws WorkflowException
097    {
098        if (role == null)
099        {
100            throw new WorkflowException("Missing 'role' argument for avalon type");
101        }
102        
103        try
104        {
105            return _serviceManager.lookup(role);
106        }
107        catch (ServiceException e)
108        {
109            throw new WorkflowException("Unable to retrieve avalon component for role: '" + role + "'", e);
110        }
111    }
112}