All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.bean.proxy.ClientProxyProvider Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.bean.proxy;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.jboss.weld.Container;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.Proxies.TypeInfo;
import org.slf4j.cal10n.LocLogger;

import javax.enterprise.inject.spi.Bean;

import static org.jboss.weld.logging.Category.BEAN;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanMessage.BEAN_ID_CREATION_FAILED;
import static org.jboss.weld.logging.messages.BeanMessage.CREATED_NEW_CLIENT_PROXY_TYPE;
import static org.jboss.weld.logging.messages.BeanMessage.LOOKED_UP_CLIENT_PROXY;
import static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;

/**
 * A proxy pool for holding scope adaptors (client proxies)
 *
 * @author Nicklas Karlsson
 * @see org.jboss.weld.bean.proxy.ProxyMethodHandler
 */
public class ClientProxyProvider {
    private static final LocLogger log = loggerFactory().getLogger(BEAN);

    private static final CacheLoader, Object> CREATE_CLIENT_PROXY = new CacheLoader, Object>() {

        public Object load(Bean from) {
            String id = Container.instance().services().get(ContextualStore.class).putIfAbsent(from);
            if (id == null) {
                throw new DefinitionException(BEAN_ID_CREATION_FAILED, from);
            }
            return createClientProxy(from, id);
        }
    };

    /**
     * A container/cache for previously created proxies
     *
     * @author Nicklas Karlsson
     */
    private final LoadingCache, Object> pool;

    /**
     * Constructor
     */
    public ClientProxyProvider() {
        this.pool = CacheBuilder.newBuilder().build(CREATE_CLIENT_PROXY);
    }

    /**
     * Creates a Javassist scope adaptor (client proxy) for a bean
     * 

* Creates a Javassist proxy factory. Gets the type info. Sets the interfaces * and superclass to the factory. Hooks in the MethodHandler and creates the * proxy. * * @param bean The bean to proxy * @param beanIndex The index to the bean in the manager bean list * @return A Javassist proxy * @throws InstantiationException When the proxy couldn't be created * @throws IllegalAccessException When the proxy couldn't be created */ private static T createClientProxy(Bean bean, String id) throws RuntimeException { ContextBeanInstance beanInstance = new ContextBeanInstance(bean, id); TypeInfo typeInfo = TypeInfo.of(bean.getTypes()); T proxy = new ClientProxyFactory(typeInfo.getSuperClass(), bean.getTypes(), bean).create(beanInstance); log.trace(CREATED_NEW_CLIENT_PROXY_TYPE, proxy.getClass(), bean, id); return proxy; } /** * Gets a client proxy for a bean *

* Looks for a proxy in the pool. If not found, one is created and added to * the pool if the create argument is true. * * @param bean The bean to get a proxy to * @return the client proxy for the bean */ public T getClientProxy(final Bean bean) { T proxy = getCastCacheValue(pool, bean); log.trace(LOOKED_UP_CLIENT_PROXY, proxy.getClass(), bean); return proxy; } /** * Gets a string representation * * @return The string representation */ @Override public String toString() { return "Proxy pool with " + pool.size() + " proxies"; } public void clear() { this.pool.invalidateAll(); } }