001// Copyright 2007, 2008, 2010, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.commons.util.AvailableValues;
018import org.apache.tapestry5.commons.util.UnknownValueException;
019import org.apache.tapestry5.func.F;
020import org.apache.tapestry5.func.Mapper;
021import org.apache.tapestry5.plastic.PlasticUtils;
022import org.apache.tapestry5.services.ComponentEventResultProcessor;
023
024import java.io.IOException;
025import java.util.Collection;
026import java.util.List;
027
028/**
029 * A catch-all for type Object that reports the return value as an error.
030 */
031@SuppressWarnings("unchecked")
032public class ObjectComponentEventResultProcessor implements ComponentEventResultProcessor<Object>
033{
034    private final Collection<Class> configuredClasses;
035
036    public ObjectComponentEventResultProcessor(Collection<Class> configuredClasses)
037    {
038        this.configuredClasses = configuredClasses;
039    }
040
041    public void processResultValue(Object value) throws IOException
042    {
043        List<String> names = F.flow(configuredClasses).map(new Mapper<Class, String>()
044        {
045            public String map(Class input)
046            {
047                return PlasticUtils.toTypeName(input);
048            }
049        }).toList();
050
051        String message = String.format(
052                "A component event handler method returned the value %s. Return type %s can not be handled.", value,
053                PlasticUtils.toTypeName(value.getClass()));
054
055        throw new UnknownValueException(message, new AvailableValues("Configured return types", names));
056    }
057}