Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2014 Vecna Technologies, Inc. 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 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 com.vecna.proxy;
Generates a lazy proxy around an object. Given a way to instantiate the target object (encapsulated in a InstanceFactory factory) and a set of interfaces that the object would implement, getInstance() will generate a java.lang.reflect.Proxy object that implements the interfaces, instantiates the target object (through InstanceFactory.getInstance() upon the first call to any of the interface methods, and delegates that and all subsequent calls to the target object. A typical use case for LazyProxyFactory is proxying a service whose initialization is expensive (such as a JAX-WS webservice with complex data bindings) to cut down the application initialization time. Note that the generated proxy will incur an additional reflection call cost for every invocation of an interface method.

<T> specifies one of the proxied interfaces for convenience
 public class LazyProxyFactory<T> implements InstanceFactory<T> {
   private static final Logger s_log = LoggerFactory.getLogger(LazyProxyFactory.class);
   private final Class<T> m_mainInterface;
   private final Class<?>[] m_interfaces;
   private final InstanceFactory<?> m_factory;
   private Object m_instance;

Create a new LazyProxyFactory.

factory the strategy for instantiating the target object.
mainInterface main interface that the target object implements and the proxy intercepts.
additionalInterfaces additional interfaces that the target object implements and the proxy intercepts.
   public LazyProxyFactory(InstanceFactory<T> factoryClass<T> mainInterfaceClass<?> ... additionalInterfaces) {
     if (factory == null) {
       throw new IllegalArgumentException("target object factory cannot be null");
     if (mainInterface == null) {
       throw new IllegalArgumentException("must specify at least one interface to implement");
      = mainInterface;
      = new Class<?>[additionalInterfaces.length + 1];
     [0] = mainInterface;
     for (int i = 0; i < additionalInterfaces.lengthi++) {
       [i + 1] = additionalInterfaces[i];
      = factory;


create the lazy proxy instance.
   public T getInstance() {
     .debug("creating a lazy proxy for {}", Arrays.asList());
       public Object invoke(Object proxyMethod methodObject[] argsthrows Throwable {
         if ( == null) {
           synchronized () {
             if ( == null) {
               .debug("initializing the proxy for {}", Arrays.asList());
                = .getInstance();
         return method.invoke(args);
New to GrepCode? Check out our FAQ X