    001// Copyright 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.internal.plastic.PlasticInternalUtils;
    018import org.apache.tapestry5.ioc.annotations.PostInjection;
    019import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    020import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
    021import org.apache.tapestry5.ioc.services.TypeCoercer;
    022import org.apache.tapestry5.services.ComponentClasses;
    023import org.apache.tapestry5.services.ComponentLayer;
    024import org.apache.tapestry5.services.InvalidationEventHub;
    025import org.apache.tapestry5.services.InvalidationListener;
    026
    027import java.util.Map;
    028
    029public class ComponentClassCacheImpl implements ComponentClassCache
    030{
    031    private final Map<String, Class> cache = CollectionFactory.newConcurrentMap();
    032
    033    private final PlasticProxyFactory plasticFactory;
    034
    035    private final TypeCoercer typeCoercer;
    036
    037    public ComponentClassCacheImpl(@ComponentLayer
    038    PlasticProxyFactory plasticFactory, TypeCoercer typeCoercer)
    039    {
    040        this.plasticFactory = plasticFactory;
    041        this.typeCoercer = typeCoercer;
    042    }
    043
    044    @PostInjection
    045    public void setupInvalidation(@ComponentClasses InvalidationEventHub hub) {
    046        hub.clearOnInvalidation(cache);
    047    }
    048
    049    @SuppressWarnings("unchecked")
    050    public Object defaultValueForType(String className)
    051    {
    052        Class clazz = forName(className);
    053
    054        if (!clazz.isPrimitive())
    055            return null;
    056
    057        // Remembering that 0 coerces to boolean false, this covers all the primitive
    058        // types (boolean, int, short, etc.)
    059        return typeCoercer.coerce(0, clazz);
    060    }
    061
    062    public Class forName(String className)
    063    {
    064        Class result = cache.get(className);
    065
    066        if (result == null)
    067        {
    068            result = lookupClassForType(className);
    069
    070            cache.put(className, result);
    071        }
    072
    073        return result;
    074    }
    075
    076    private Class lookupClassForType(String className)
    077    {
    078        ClassLoader componentLoader = plasticFactory.getClassLoader();
    079        try
    080        {
    081            return PlasticInternalUtils.toClass(componentLoader, className);
    082        }
    083        catch (ClassNotFoundException ex)
    084        {
    085            throw new RuntimeException(ex);
    086        }
    087    }
    088}