001    // Copyright 2007, 2008, 2010 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    
015    package org.apache.tapestry5.internal.services;
016    
017    import java.io.IOException;
018    import java.util.Collection;
019    import java.util.List;
020    
021    import org.apache.tapestry5.func.F;
022    import org.apache.tapestry5.func.Mapper;
023    import org.apache.tapestry5.ioc.services.ClassFabUtils;
024    import org.apache.tapestry5.ioc.util.AvailableValues;
025    import org.apache.tapestry5.ioc.util.UnknownValueException;
026    import org.apache.tapestry5.services.ComponentEventResultProcessor;
027    
028    /**
029     * A catch-all for type Object that reports the return value as an error.
030     */
031    @SuppressWarnings("unchecked")
032    public 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 ClassFabUtils.toJavaClassName(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                    ClassFabUtils.toJavaClassName(value.getClass()));
054    
055            throw new UnknownValueException(message, new AvailableValues("Configured return types", names));
056        }
057    }