Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;
 
 import java.util.Set;
 
 
 
A proxy pool for holding scope adaptors (client proxies)

Author(s):
Nicklas Karlsson
See also:
ProxyMethodHandler
 
 public class ClientProxyProvider {
 
     private static final Object BEAN_NOT_PROXYABLE_MARKER = new Object();
 
 
     private class CreateClientProxy extends CacheLoader<Bean<Object>, Object> {
         @Override
         public Object load(Bean<Objectfrom) {
             if (Proxies.isTypesProxyable(fromservices())) {
                 return createClientProxy(from);
             } else {
                 return ;
             }
         }
     };
 
     private class CreateClientProxyForType extends CacheLoader<RequestedTypeHolderObject> {
         @Override
         public Object load(RequestedTypeHolder input) {
             // first, collect all interfaces
             ImmutableSet.Builder<Typetypes = ImmutableSet.builder();
             for (Type type : input.bean.getTypes()) {
                 if (Reflections.getRawType(type).isInterface()) {
                     types.add(type);
                 }
             }
             // if the requested type if proxyable use requested type + bean interfaces
             if (Proxies.isTypeProxyable(input.requestedTypeservices())) {
                 return createClientProxy(input.beantypes.add(input.requestedType).build());
             }
             /*
              * Requested type is not proxyable. Check whether a proxyable subtype exists within the set of
              * bean types that we could use instead.
              */
             Class<?> requestedRawType = Reflections.getRawType(input.requestedType);
             for (Type type : input.bean.getTypes()) {
                 if (requestedRawType.isAssignableFrom(Reflections.getRawType(type)) && Proxies.isTypeProxyable(typeservices())) {
                     return createClientProxy(input.beantypes.add(type).build());
                 }
             }
             return ;
         }
     }
 
     private static class RequestedTypeHolder {
         private final Type requestedType;
         private final Bean<?> bean;
 
         private RequestedTypeHolder(Type requestedTypeBean<?> bean) {
             this. = requestedType;
             this. = bean;
         }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (( == null) ? 0 : .hashCode());
            result = prime * result + (( == null) ? 0 : .hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            RequestedTypeHolder other = (RequestedTypeHolderobj;
            if ( == null) {
                if (other.bean != null) {
                    return false;
                }
            } else if (!.equals(other.bean)) {
                return false;
            }
            if ( == null) {
                if (other.requestedType != null) {
                    return false;
                }
            } else if (!.equals(other.requestedType)) {
                return false;
            }
            return true;
        }
    }

    
A container/cache for previously created proxies

Author(s):
Nicklas Karlsson
    private final String contextId;
    private volatile ServiceRegistry services;

    
Constructor
    public ClientProxyProvider(String contextId) {
        CacheBuilder<ObjectObjectcacheBuilder = CacheBuilder.newBuilder();
        this. = contextId;
    }
    private ServiceRegistry services() {
        if ( == null) {
            synchronized (this) {
                if ( == null) {
                    this. = Container.instance().services();
                }
            }
        }
        return this.;
    }

    
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.

Parameters:
bean The bean to proxy
beanIndex The index to the bean in the manager bean list
Returns:
A Javassist proxy
Throws:
java.lang.InstantiationException When the proxy couldn't be created
java.lang.IllegalAccessException When the proxy couldn't be created
    private <T> T createClientProxy(Bean<T> beanthrows RuntimeException {
        return createClientProxy(beanbean.getTypes());
    }
    private <T> T createClientProxy(Bean<T> beanSet<Typetypes) {
        BeanIdentifier id = Container.instance().services().get(ContextualStore.class).putIfAbsent(bean);
        if (id == null) {
            throw ..beanIdCreationFailed(bean);
        }
        BeanInstance beanInstance = new ContextBeanInstance<T>(beanid);
        beanInstance = CachedBeanInstance.wrapIfCacheable(beanbeanInstance);
        TypeInfo typeInfo = TypeInfo.of(types);
        T proxy = new ClientProxyFactory<T>(typeInfo.getSuperClass(), typesbean).create(beanInstance);
        ..createdNewClientProxyType(proxy.getClass(), beanid);
        return proxy;
    }
    public <T> T getClientProxy(final Bean<T> bean) {
        T proxy = getCastCacheValue(bean);
        if (proxy == ) {
            throw Proxies.getUnproxyableTypesException(beanservices());
        }
        ..lookedUpClientProxy(proxy.getClass(), bean);
        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.

Parameters:
bean The bean to get a proxy to
Returns:
the client proxy for the bean
    public <T> T getClientProxy(final Bean<T> beanType requestedType) {
        // let's first try to use the proxy that implements all the bean types
        T proxy = getCastCacheValue(bean);
        if (proxy == ) {
            /*
             *  the bean may have a type that is not proxyable - this is not a problem as long as the unproxyable
             *  type is not in the type closure of the requested type
             *  https://issues.jboss.org/browse/WELD-1052
             */
            proxy = getCastCacheValue(new RequestedTypeHolder(requestedTypebean));
            if (proxy == ) {
                throw Proxies.getUnproxyableTypeException(requestedTypeservices());
            }
        }
        ..lookedUpClientProxy(proxy.getClass(), bean);
        return proxy;
    }


    
Gets a string representation

Returns:
The string representation
    @Override
    public String toString() {
        return "Proxy pool with " + .size() + " bean type proxies and " + .size() + "injection point type proxies.";
    }
    public void clear() {
    }
New to GrepCode? Check out our FAQ X